3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.util
.Collection
;
14 import java
.util
.HashMap
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
23 import org
.springframework
.stereotype
.Service
;
24 import org
.springframework
.transaction
.annotation
.Transactional
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
28 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
29 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
30 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
.MetaDataPropertyName
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IOriginalSourceDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
34 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
35 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
36 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchable
;
37 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
38 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchStrategyConfigurator
;
39 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchStrategyConfigurator
.MatchStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.merge
.DefaultMergeStrategy
;
41 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
42 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergeStrategy
;
43 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
47 @Transactional(readOnly
= true)
48 public class CommonServiceImpl
/*extends ServiceBase<OriginalSourceBase,IOriginalSourceDao>*/ implements ICommonService
{
49 @SuppressWarnings("unused")
50 private static final Logger logger
= Logger
.getLogger(CommonServiceImpl
.class);
54 private IOriginalSourceDao originalSourceDao
;
58 private ICdmGenericDao genericDao
;
62 public CdmBase
findWithUpdate(Class
<?
extends CdmBase
> clazz
, int id
){
63 return genericDao
.find(clazz
, id
);
67 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
){
68 return genericDao
.find(clazz
, id
);
72 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
, List
<String
> propertyPaths
){
73 return genericDao
.find(clazz
, id
, propertyPaths
);
78 public Map
<String
, ?
extends ISourceable
> getSourcedObjectsByIdInSource(Class clazz
, Set
<String
> idInSourceSet
, String idNamespace
) {
79 Map
<String
, ?
extends ISourceable
> list
= originalSourceDao
.findOriginalSourcesByIdInSource(clazz
, idInSourceSet
, idNamespace
);
84 public ISourceable
getSourcedObjectByIdInSource(Class clazz
, String idInSource
, String idNamespace
) {
85 ISourceable result
= null;
86 List
<IdentifiableEntity
> list
= originalSourceDao
.findOriginalSourceByIdInSource(clazz
, idInSource
, idNamespace
);
87 if (! list
.isEmpty()){
94 public Set
<CdmBase
> getReferencingObjects(CdmBase referencedCdmBase
){
95 return this.genericDao
.getReferencingObjects(referencedCdmBase
);
100 public Set
<CdmBase
> getReferencingObjectsForDeletion(CdmBase referencedCdmBase
){
101 return this.genericDao
.getReferencingObjectsForDeletion(referencedCdmBase
);
104 // Set<Class<? extends CdmBase>> allCdmClasses = genericDao.getAllCdmClasses(false); //findAllCdmClasses();
106 // referencedCdmBase = (CdmBase)HibernateProxyHelper.deproxy(referencedCdmBase);
107 // Class referencedClass = referencedCdmBase.getClass();
108 // Set<CdmBase> result = new HashSet<CdmBase>();
109 // logger.debug("Referenced Class: " + referencedClass.getName());
111 // for (Class<? extends CdmBase> cdmClass : allCdmClasses){
112 // Set<Field> fields = getFields(cdmClass);
113 // for (Field field: fields){
114 // Class<?> type = field.getType();
116 // if (! type.isInterface()){
117 // if (referencedClass.isAssignableFrom(type)||
118 // type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
119 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
122 // }else if (type.isAssignableFrom(referencedClass)){
123 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
124 // }else if (Collection.class.isAssignableFrom(type)){
126 // if (checkIsSetOfType(field, referencedClass, type) == true){
127 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, true);
130 //// Class[] interfaces = referencedClass.getInterfaces();
131 //// for (Class interfaze: interfaces){
132 //// if (interfaze == type){
133 ////// if(interfaze.isAssignableFrom(returnType)){
134 //// handleSingleClass(interfaze, type, field, cdmClass, result, referencedCdmBase);
140 // } catch (Exception e) {
141 // e.printStackTrace();
142 // throw new RuntimeException(e);
147 // private boolean checkIsSetOfType(Field field, Class referencedClass, Class<?> type){
148 // Type genericType = (ParameterizedTypeImpl)field.getGenericType();
149 // if (genericType instanceof ParameterizedTypeImpl){
150 // ParameterizedTypeImpl paraType = (ParameterizedTypeImpl)genericType;
151 // paraType.getRawType();
152 // Type[] arguments = paraType.getActualTypeArguments();
153 // //logger.debug(arguments.length);
154 // if (arguments.length == 1){
155 // Class collectionClass;
157 // if (arguments[0] instanceof Class){
158 // collectionClass = (Class)arguments[0];
159 // }else if(arguments[0] instanceof TypeVariableImpl){
160 // TypeVariableImpl typeVariable = (TypeVariableImpl)arguments[0];
161 // GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
162 // collectionClass = (Class)genericDeclaration;
164 // logger.warn("Unknown Type");
167 // if (CdmBase.class.isAssignableFrom(collectionClass) && collectionClass.isAssignableFrom(referencedClass) ){
170 // } catch (Exception e) {
171 // logger.warn(e.getMessage());
174 // logger.warn("Length of arguments <> 1");
177 // logger.warn("Not a generic type of type ParameterizedTypeImpl");
185 // private boolean handleSingleClass(Class itemClass, Class type, Field field, Class cdmClass, Set<CdmBase> result,CdmBase value, boolean isCollection){
186 // if (! Modifier.isStatic(field.getModifiers())){
187 // String methodName = StringUtils.rightPad(field.getName(), 30);
188 // String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
189 // String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
191 // logger.debug(methodName + "\t\t" + className + "\t\t" + returnTypeName);
192 //// result_old.add(method);
193 // result.addAll(getCdmBasesByFieldAndClass(field, itemClass, cdmClass, value, isCollection));
198 // private Set<Field> getFields(Class clazz){
199 // Set<Field> result = new HashSet<Field>();
200 // for (Field field: clazz.getDeclaredFields()){
201 // if (!Modifier.isStatic(field.getModifiers())){
202 // result.add(field);
205 // Class superclass = clazz.getSuperclass();
206 // if (CdmBase.class.isAssignableFrom(superclass)){
207 // result.addAll(getFields(superclass));
212 // private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class itemClass, Class otherClazz, CdmBase item, boolean isCollection){
213 // Set<CdmBase> result = new HashSet<CdmBase>();
214 // if (isCollection){
215 // result.addAll(genericDao.getCdmBasesWithItemInCollection(itemClass, otherClazz, field.getName(), item));
217 // result.addAll(genericDao.getCdmBasesByFieldAndClass(otherClazz, field.getName(), item));
223 public List
getHqlResult(String hqlQuery
){
224 return genericDao
.getHqlResult(hqlQuery
);
228 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, IMergeStrategy mergeStrategy
) throws MergeException
{
229 if (mergeStrategy
== null){
230 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
232 genericDao
.merge((CdmBase
)mergeFirst
, (CdmBase
)mergeSecond
, mergeStrategy
);
236 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
237 IMergeStrategy mergeStrategy
;
239 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
241 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
243 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
247 @Transactional(readOnly
= false)
249 public <T
extends IMergable
> void merge(int mergeFirstId
, int mergeSecondId
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
250 IMergeStrategy mergeStrategy
;
251 T mergeFirst
= (T
) genericDao
.find(clazz
, mergeFirstId
);
252 T mergeSecond
= (T
) genericDao
.find(clazz
, mergeSecondId
);
253 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
254 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
258 @Transactional(readOnly
= false)
259 public <T
extends IMergable
> void merge(UUID mergeFirstUuid
, UUID mergeSecondUuid
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
260 IMergeStrategy mergeStrategy
;
261 T mergeFirst
= (T
) genericDao
.find(clazz
, mergeFirstUuid
);
262 T mergeSecond
= (T
) genericDao
.find(clazz
, mergeSecondUuid
);
263 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
264 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
268 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
) throws MergeException
{
269 IMergeStrategy mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
270 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
275 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, IMatchStrategy matchStrategy
) throws MatchException
{
276 if (matchStrategy
== null){
277 matchStrategy
= DefaultMatchStrategy
.NewInstance(((objectToMatch
).getClass()));
279 return genericDao
.findMatching(objectToMatch
, matchStrategy
);
285 * @see eu.etaxonomy.cdm.api.service.ICommonService#findMatching(eu.etaxonomy.cdm.strategy.match.IMatchable, eu.etaxonomy.cdm.strategy.match.MatchStrategyConfigurator.MatchStrategy)
288 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, MatchStrategy strategy
) throws MatchException
{
289 return findMatching(objectToMatch
, MatchStrategyConfigurator
.getMatchStrategy(strategy
));
293 // * @see eu.etaxonomy.cdm.api.service.IService#list(java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
296 // public <TYPE extends OriginalSourceBase> Pager<TYPE> list(Class<TYPE> type,
297 // Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
298 // List<String> propertyPaths) {
299 // logger.warn("Not yet implemented");
304 @Transactional(readOnly
= false)
306 public void saveAllMetaData(Collection
<CdmMetaData
> metaData
) {
307 Iterator
<CdmMetaData
> iterator
= metaData
.iterator();
308 while(iterator
.hasNext()){
309 CdmMetaData cdmMetaData
= iterator
.next();
310 genericDao
.saveMetaData(cdmMetaData
);
315 public Map
<MetaDataPropertyName
, CdmMetaData
> getCdmMetaData() {
316 Map
<MetaDataPropertyName
, CdmMetaData
> result
= new HashMap
<MetaDataPropertyName
, CdmMetaData
>();
317 List
<CdmMetaData
> metaDataList
= genericDao
.getMetaData();
318 for (CdmMetaData metaData
: metaDataList
){
319 MetaDataPropertyName propertyName
= metaData
.getPropertyName();
320 result
.put(propertyName
, metaData
);
326 public Object
initializeCollection(UUID ownerUuid
, String fieldName
) {
327 return genericDao
.initializeCollection(ownerUuid
, fieldName
);
332 public Object
initializeCollection(UUID ownerUuid
, String fieldName
, List
<String
> propertyPaths
) {
333 return genericDao
.initializeCollection(ownerUuid
, fieldName
, propertyPaths
);
338 public boolean isEmpty(UUID ownerUuid
, String fieldName
) {
339 return genericDao
.isEmpty(ownerUuid
, fieldName
);
344 public int size(UUID ownerUuid
, String fieldName
) {
345 return genericDao
.size(ownerUuid
, fieldName
);
350 public Object
get(UUID ownerUuid
, String fieldName
, int index
) {
351 return genericDao
.get(ownerUuid
, fieldName
, index
);
355 public boolean contains(UUID ownerUuid
, String fieldName
, Object element
) {
356 return genericDao
.contains(ownerUuid
, fieldName
, element
);
360 public boolean containsKey(UUID ownerUuid
, String fieldName
, Object key
) {
361 return genericDao
.containsKey(ownerUuid
, fieldName
, key
);
365 public boolean containsValue(UUID ownerUuid
, String fieldName
, Object value
) {
366 return genericDao
.containsValue(ownerUuid
, fieldName
, value
);
370 @Transactional(readOnly
= false)
371 public void createFullSampleData() {
372 genericDao
.createFullSampleData();
378 public <S
extends CdmBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
379 return genericDao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
383 public <S
extends CdmBase
> int count(Class
<S
> type
) {
384 return genericDao
.count(type
);
388 @Transactional(readOnly
= false)
389 public CdmBase
save(CdmBase newInstance
) {
390 return genericDao
.save(newInstance
);
395 @Transactional(readOnly
= false)
396 public <T
extends CdmBase
> Map
<UUID
,T
> save(Collection
<T
> newInstances
) {
397 //this is very ugly, I know, but for now I do not want to copy the saveAll method from CdmEntityDaoBase to genericDao
398 //and generally the saveAll method should work for other CdmBase types with generics removed
399 return (Map
)originalSourceDao
.saveAll((Collection
)newInstances
);
404 public <T
extends CdmBase
> boolean isMergeable(T cdmBase1
, T cdmBase2
, IMergeStrategy mergeStrategy
) throws MergeException
{
405 return genericDao
.isMergeable(cdmBase1
, cdmBase2
, mergeStrategy
);