1 package org
.argeo
.server
.hibernate
;
3 import java
.beans
.PropertyDescriptor
;
4 import java
.io
.Serializable
;
5 import java
.util
.ArrayList
;
6 import java
.util
.HashMap
;
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
;
18 public class LightDaoInterceptor
extends EmptyInterceptor
{
19 private final static Log log
= LogFactory
.getLog(LightDaoInterceptor
.class);
21 private static final long serialVersionUID
= 1L;
23 public final static String DEFAULT_EXTERNAL_SUFFIX
= "_external";
25 private String externalSuffix
= DEFAULT_EXTERNAL_SUFFIX
;
27 private LightDaoSupport lightDaoSupport
;
29 private List
<Class
<?
>> classes
= new ArrayList
<Class
<?
>>();
31 private Map
<Class
<?
>, String
> businessIdFields
= new HashMap
<Class
<?
>, String
>();
34 private final Map
<Class
<?
>, Map
<Serializable
, Object
>> bidMappings
= new HashMap
<Class
<?
>, Map
<Serializable
, Object
>>();
37 public Object
getEntity(String entityName
, Serializable id
) {
38 Class
<?
> clss
= findSupportingClass(entityName
);
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
49 res
= lightDaoSupport
.getByKey(clss
, id
);
50 if (log
.isTraceEnabled())
51 log
.debug("Got entity " + clss
+ " (id=" + id
+ ")");
54 res
= super.getEntity(entityName
, id
);
60 public String
getEntityName(Object object
) {
61 if (supports(object
)) {
62 return toExternalName(object
.getClass());
64 return super.getEntityName(object
);
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 "
84 return super.onSave(entity
, id
, state
, propertyNames
, types
);
88 public boolean onLoad(Object entity
, Serializable id
, Object
[] state
,
89 String
[] propertyNames
, Type
[] types
) {
90 Class
<?
> clss
= entity
.getClass();
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
+ "="
101 source
= lightDaoSupport
.getByKey(clss
, id
);
102 if (log
.isTraceEnabled())
103 log
.debug("Loading entity " + clss
+ " (id=" + id
+ ")");
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
+ ")");
124 // res = super.getEntity(entityName, id);
125 return super.onLoad(entity
, id
, state
, propertyNames
, types
);
129 protected Boolean
supports(Object object
) {
130 if (classes
.contains(object
.getClass()))
131 return lightDaoSupport
.getSupportedClasses().contains(
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
))
148 protected final String
toExternalName(Class
<?
> clss
) {
149 return clss
.getSimpleName() + externalSuffix
;
152 public void setExternalSuffix(String externalSuffix
) {
153 this.externalSuffix
= externalSuffix
;
156 public void setLightDaoSupport(LightDaoSupport lightDaoSupport
) {
157 this.lightDaoSupport
= lightDaoSupport
;
160 public void setClasses(List
<Class
<?
>> classes
) {
161 this.classes
= classes
;
164 public void setBusinessIdFields(Map
<Class
<?
>, String
> businessIdFields
) {
165 this.businessIdFields
= businessIdFields
;