]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/LightDaoInterceptor.java
Improve JCR
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.hibernate / src / main / java / org / argeo / server / hibernate / LightDaoInterceptor.java
1 package org.argeo.server.hibernate;
2
3 import java.beans.PropertyDescriptor;
4 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.server.dao.LightDaoSupport;
13 import org.hibernate.EmptyInterceptor;
14 import org.hibernate.type.Type;
15 import org.springframework.beans.BeanWrapper;
16 import org.springframework.beans.BeanWrapperImpl;
17
18 public class LightDaoInterceptor extends EmptyInterceptor {
19 private final static Log log = LogFactory.getLog(LightDaoInterceptor.class);
20
21 private static final long serialVersionUID = 1L;
22
23 public final static String DEFAULT_EXTERNAL_SUFFIX = "_external";
24
25 private String externalSuffix = DEFAULT_EXTERNAL_SUFFIX;
26
27 private LightDaoSupport lightDaoSupport;
28
29 private List<Class<?>> classes = new ArrayList<Class<?>>();
30
31 private Map<Class<?>, String> businessIdFields = new HashMap<Class<?>, String>();
32
33 /** internal */
34 private final Map<Class<?>, Map<Serializable, Object>> bidMappings = new HashMap<Class<?>, Map<Serializable, Object>>();
35
36 @Override
37 public Object getEntity(String entityName, Serializable id) {
38 Class<?> clss = findSupportingClass(entityName);
39 Object res = null;
40 if (clss != null) {
41 if (businessIdFields.containsKey(clss)) {
42 String field = businessIdFields.get(clss);
43 Object value = bidMappings.get(clss).get(id);
44 res = lightDaoSupport.getByField(clss, field, value);
45 if (log.isTraceEnabled())
46 log.debug("Got entity " + clss + " (" + field + "=" + value
47 + ")");
48 } else {
49 res = lightDaoSupport.getByKey(clss, id);
50 if (log.isTraceEnabled())
51 log.debug("Got entity " + clss + " (id=" + id + ")");
52 }
53 } else {
54 res = super.getEntity(entityName, id);
55 }
56 return res;
57 }
58
59 @Override
60 public String getEntityName(Object object) {
61 if (supports(object)) {
62 return toExternalName(object.getClass());
63 } else {
64 return super.getEntityName(object);
65 }
66 }
67
68 @Override
69 public boolean onSave(Object entity, Serializable id, Object[] state,
70 String[] propertyNames, Type[] types) {
71 if (supports(entity)) {
72 Class<?> clss = entity.getClass();
73 if (businessIdFields.containsKey(clss)) {
74 if (!bidMappings.containsKey(clss))
75 bidMappings.put(clss, new HashMap<Serializable, Object>());
76 BeanWrapper bw = new BeanWrapperImpl(entity);
77 Object bid = bw.getPropertyValue(businessIdFields.get(clss));
78 bidMappings.get(clss).put(id, bid);
79 if (log.isTraceEnabled())
80 log.debug("Mapped tid " + id + " with bid " + bid + " for "
81 + clss);
82 }
83 }
84 return super.onSave(entity, id, state, propertyNames, types);
85 }
86
87 @Override
88 public boolean onLoad(Object entity, Serializable id, Object[] state,
89 String[] propertyNames, Type[] types) {
90 Class<?> clss = entity.getClass();
91 Object source = null;
92 if (lightDaoSupport.getSupportedClasses().contains(clss)) {
93 if (businessIdFields.containsKey(clss)) {
94 String field = businessIdFields.get(clss);
95 Object value = bidMappings.get(clss).get(id);
96 source = lightDaoSupport.getByField(clss, field, value);
97 if (log.isTraceEnabled())
98 log.debug("Loading entity " + clss + " (" + field + "="
99 + value + ")");
100 } else {
101 source = lightDaoSupport.getByKey(clss, id);
102 if (log.isTraceEnabled())
103 log.debug("Loading entity " + clss + " (id=" + id + ")");
104 }
105 }
106
107 if (source != null) {
108 BeanWrapper bwTarget = new BeanWrapperImpl(entity);
109 BeanWrapper bwSource = new BeanWrapperImpl(source);
110 for (PropertyDescriptor pd : bwTarget.getPropertyDescriptors()) {
111 String propName = pd.getName();
112 if (bwSource.isReadableProperty(propName)
113 && bwTarget.isWritableProperty(propName)) {
114 bwTarget.setPropertyValue(propName, bwSource
115 .getPropertyValue(propName));
116 if (log.isTraceEnabled())
117 log.debug("Loaded property " + propName + " for class "
118 + clss + " (id=" + id + ")");
119 }
120 }
121
122 return true;
123 } else {
124 // res = super.getEntity(entityName, id);
125 return super.onLoad(entity, id, state, propertyNames, types);
126 }
127 }
128
129 protected Boolean supports(Object object) {
130 if (classes.contains(object.getClass()))
131 return lightDaoSupport.getSupportedClasses().contains(
132 object.getClass());
133 else
134 return false;
135 }
136
137 /** @return null if not found */
138 protected Class<?> findSupportingClass(String entityName) {
139 for (Class<?> clss : lightDaoSupport.getSupportedClasses()) {
140 if (toExternalName(clss).equals(entityName)) {
141 if (classes.contains(clss))
142 return clss;
143 }
144 }
145 return null;
146 }
147
148 protected final String toExternalName(Class<?> clss) {
149 return clss.getSimpleName() + externalSuffix;
150 }
151
152 public void setExternalSuffix(String externalSuffix) {
153 this.externalSuffix = externalSuffix;
154 }
155
156 public void setLightDaoSupport(LightDaoSupport lightDaoSupport) {
157 this.lightDaoSupport = lightDaoSupport;
158 }
159
160 public void setClasses(List<Class<?>> classes) {
161 this.classes = classes;
162 }
163
164 public void setBusinessIdFields(Map<Class<?>, String> businessIdFields) {
165 this.businessIdFields = businessIdFields;
166 }
167
168 }