2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.server
.hibernate
;
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
;
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
;
33 public class LightDaoInterceptor
extends EmptyInterceptor
{
34 private final static Log log
= LogFactory
.getLog(LightDaoInterceptor
.class);
36 private static final long serialVersionUID
= 1L;
38 public final static String DEFAULT_EXTERNAL_SUFFIX
= "_external";
40 private String externalSuffix
= DEFAULT_EXTERNAL_SUFFIX
;
42 private LightDaoSupport lightDaoSupport
;
44 private List
<Class
<?
>> classes
= new ArrayList
<Class
<?
>>();
46 private Map
<Class
<?
>, String
> businessIdFields
= new HashMap
<Class
<?
>, String
>();
49 private final Map
<Class
<?
>, Map
<Serializable
, Object
>> bidMappings
= new HashMap
<Class
<?
>, Map
<Serializable
, Object
>>();
52 public Object
getEntity(String entityName
, Serializable id
) {
53 Class
<?
> clss
= findSupportingClass(entityName
);
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
64 res
= lightDaoSupport
.getByKey(clss
, id
);
65 if (log
.isTraceEnabled())
66 log
.debug("Got entity " + clss
+ " (id=" + id
+ ")");
69 res
= super.getEntity(entityName
, id
);
75 public String
getEntityName(Object object
) {
76 if (supports(object
)) {
77 return toExternalName(object
.getClass());
79 return super.getEntityName(object
);
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 "
99 return super.onSave(entity
, id
, state
, propertyNames
, types
);
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
+ "="
116 source
= lightDaoSupport
.getByKey(clss
, id
);
117 if (log
.isTraceEnabled())
118 log
.debug("Loading entity " + clss
+ " (id=" + id
+ ")");
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
+ ")");
139 // res = super.getEntity(entityName, id);
140 return super.onLoad(entity
, id
, state
, propertyNames
, types
);
144 protected Boolean
supports(Object object
) {
145 if (classes
.contains(object
.getClass()))
146 return lightDaoSupport
.getSupportedClasses().contains(
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
))
163 protected final String
toExternalName(Class
<?
> clss
) {
164 return clss
.getSimpleName() + externalSuffix
;
167 public void setExternalSuffix(String externalSuffix
) {
168 this.externalSuffix
= externalSuffix
;
171 public void setLightDaoSupport(LightDaoSupport lightDaoSupport
) {
172 this.lightDaoSupport
= lightDaoSupport
;
175 public void setClasses(List
<Class
<?
>> classes
) {
176 this.classes
= classes
;
179 public void setBusinessIdFields(Map
<Class
<?
>, String
> businessIdFields
) {
180 this.businessIdFields
= businessIdFields
;