]> git.argeo.org Git - lgpl/argeo-commons.git/blob - LightDaoInterceptor.java
868ae397ebe8bdd4c66917a73ee96d8e8e1f5171
[lgpl/argeo-commons.git] / LightDaoInterceptor.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.server.hibernate;
17
18 import java.beans.PropertyDescriptor;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.server.dao.LightDaoSupport;
28 import org.hibernate.EmptyInterceptor;
29 import org.hibernate.type.Type;
30 import org.springframework.beans.BeanWrapper;
31 import org.springframework.beans.BeanWrapperImpl;
32
33 public class LightDaoInterceptor extends EmptyInterceptor {
34 private final static Log log = LogFactory.getLog(LightDaoInterceptor.class);
35
36 private static final long serialVersionUID = 1L;
37
38 public final static String DEFAULT_EXTERNAL_SUFFIX = "_external";
39
40 private String externalSuffix = DEFAULT_EXTERNAL_SUFFIX;
41
42 private LightDaoSupport lightDaoSupport;
43
44 private List<Class<?>> classes = new ArrayList<Class<?>>();
45
46 private Map<Class<?>, String> businessIdFields = new HashMap<Class<?>, String>();
47
48 /** internal */
49 private final Map<Class<?>, Map<Serializable, Object>> bidMappings = new HashMap<Class<?>, Map<Serializable, Object>>();
50
51 @Override
52 public Object getEntity(String entityName, Serializable id) {
53 Class<?> clss = findSupportingClass(entityName);
54 Object res = null;
55 if (clss != null) {
56 if (businessIdFields.containsKey(clss)) {
57 String field = businessIdFields.get(clss);
58 Object value = bidMappings.get(clss).get(id);
59 res = lightDaoSupport.getByField(clss, field, value);
60 if (log.isTraceEnabled())
61 log.debug("Got entity " + clss + " (" + field + "=" + value
62 + ")");
63 } else {
64 res = lightDaoSupport.getByKey(clss, id);
65 if (log.isTraceEnabled())
66 log.debug("Got entity " + clss + " (id=" + id + ")");
67 }
68 } else {
69 res = super.getEntity(entityName, id);
70 }
71 return res;
72 }
73
74 @Override
75 public String getEntityName(Object object) {
76 if (supports(object)) {
77 return toExternalName(object.getClass());
78 } else {
79 return super.getEntityName(object);
80 }
81 }
82
83 @Override
84 public boolean onSave(Object entity, Serializable id, Object[] state,
85 String[] propertyNames, Type[] types) {
86 if (supports(entity)) {
87 Class<?> clss = entity.getClass();
88 if (businessIdFields.containsKey(clss)) {
89 if (!bidMappings.containsKey(clss))
90 bidMappings.put(clss, new HashMap<Serializable, Object>());
91 BeanWrapper bw = new BeanWrapperImpl(entity);
92 Object bid = bw.getPropertyValue(businessIdFields.get(clss));
93 bidMappings.get(clss).put(id, bid);
94 if (log.isTraceEnabled())
95 log.debug("Mapped tid " + id + " with bid " + bid + " for "
96 + clss);
97 }
98 }
99 return super.onSave(entity, id, state, propertyNames, types);
100 }
101
102 @Override
103 public boolean onLoad(Object entity, Serializable id, Object[] state,
104 String[] propertyNames, Type[] types) {
105 Class<?> clss = entity.getClass();
106 Object source = null;
107 if (lightDaoSupport.getSupportedClasses().contains(clss)) {
108 if (businessIdFields.containsKey(clss)) {
109 String field = businessIdFields.get(clss);
110 Object value = bidMappings.get(clss).get(id);
111 source = lightDaoSupport.getByField(clss, field, value);
112 if (log.isTraceEnabled())
113 log.debug("Loading entity " + clss + " (" + field + "="
114 + value + ")");
115 } else {
116 source = lightDaoSupport.getByKey(clss, id);
117 if (log.isTraceEnabled())
118 log.debug("Loading entity " + clss + " (id=" + id + ")");
119 }
120 }
121
122 if (source != null) {
123 BeanWrapper bwTarget = new BeanWrapperImpl(entity);
124 BeanWrapper bwSource = new BeanWrapperImpl(source);
125 for (PropertyDescriptor pd : bwTarget.getPropertyDescriptors()) {
126 String propName = pd.getName();
127 if (bwSource.isReadableProperty(propName)
128 && bwTarget.isWritableProperty(propName)) {
129 bwTarget.setPropertyValue(propName, bwSource
130 .getPropertyValue(propName));
131 if (log.isTraceEnabled())
132 log.debug("Loaded property " + propName + " for class "
133 + clss + " (id=" + id + ")");
134 }
135 }
136
137 return true;
138 } else {
139 // res = super.getEntity(entityName, id);
140 return super.onLoad(entity, id, state, propertyNames, types);
141 }
142 }
143
144 protected Boolean supports(Object object) {
145 if (classes.contains(object.getClass()))
146 return lightDaoSupport.getSupportedClasses().contains(
147 object.getClass());
148 else
149 return false;
150 }
151
152 /** @return null if not found */
153 protected Class<?> findSupportingClass(String entityName) {
154 for (Class<?> clss : lightDaoSupport.getSupportedClasses()) {
155 if (toExternalName(clss).equals(entityName)) {
156 if (classes.contains(clss))
157 return clss;
158 }
159 }
160 return null;
161 }
162
163 protected final String toExternalName(Class<?> clss) {
164 return clss.getSimpleName() + externalSuffix;
165 }
166
167 public void setExternalSuffix(String externalSuffix) {
168 this.externalSuffix = externalSuffix;
169 }
170
171 public void setLightDaoSupport(LightDaoSupport lightDaoSupport) {
172 this.lightDaoSupport = lightDaoSupport;
173 }
174
175 public void setClasses(List<Class<?>> classes) {
176 this.classes = classes;
177 }
178
179 public void setBusinessIdFields(Map<Class<?>, String> businessIdFields) {
180 this.businessIdFields = businessIdFields;
181 }
182
183 }