2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
22 import org
.springframework
.stereotype
.Service
;
23 import org
.springframework
.transaction
.annotation
.Transactional
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
27 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
28 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
29 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaDataPropertyName
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IOriginalSourceDao
;
32 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
33 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
34 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
35 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchable
;
36 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
37 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchStrategyConfigurator
;
38 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchStrategyConfigurator
.MatchStrategy
;
39 import eu
.etaxonomy
.cdm
.strategy
.merge
.DefaultMergeStrategy
;
40 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
41 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergeStrategy
;
42 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
46 @Transactional(readOnly
= true)
47 public class CommonServiceImpl
/*extends ServiceBase<OriginalSourceBase,IOriginalSourceDao>*/ implements ICommonService
{
48 @SuppressWarnings("unused")
49 private static final Logger logger
= Logger
.getLogger(CommonServiceImpl
.class);
53 private IOriginalSourceDao originalSourceDao
;
57 private ICdmGenericDao genericDao
;
61 public <T
extends CdmBase
> T
findWithUpdate(Class
<T
> clazz
, int id
){
62 return genericDao
.find(clazz
, id
);
66 public <T
extends CdmBase
> T
find(Class
<T
> clazz
, int id
){
67 return genericDao
.find(clazz
, id
);
71 public <T
extends CdmBase
> T
find(Class
<T
> clazz
, int id
, List
<String
> propertyPaths
){
72 return genericDao
.find(clazz
, id
, propertyPaths
);
76 public <T
extends CdmBase
> T
find(Class
<T
> clazz
, UUID uuid
) {
77 return uuid
== null ?
null : genericDao
.find(clazz
, uuid
);
81 public <T
extends CdmBase
> T
find(Class
<T
> clazz
, UUID uuid
, List
<String
> propertyPaths
) {
82 return uuid
== null ?
null : genericDao
.find(clazz
, uuid
, propertyPaths
);
86 public Map
<String
, ?
extends ISourceable
> getSourcedObjectsByIdInSource(Class clazz
, Set
<String
> idInSourceSet
, String idNamespace
) {
87 Map
<String
, ?
extends ISourceable
> list
= originalSourceDao
.findOriginalSourcesByIdInSource(clazz
, idInSourceSet
, idNamespace
);
92 public ISourceable
getSourcedObjectByIdInSource(Class clazz
, String idInSource
, String idNamespace
) {
93 ISourceable
<?
> result
= null;
94 List
<IdentifiableEntity
> list
= originalSourceDao
.findOriginalSourceByIdInSource(clazz
, idInSource
, idNamespace
);
95 if (! list
.isEmpty()){
102 public Set
<CdmBase
> getReferencingObjects(CdmBase referencedCdmBase
){
103 return this.genericDao
.getReferencingObjects(referencedCdmBase
);
107 public long getReferencingObjectsCount(CdmBase referencedCdmBase
){
108 return this.genericDao
.getReferencingObjectsCount(referencedCdmBase
);
112 public Set
<CdmBase
> getReferencingObjectsForDeletion(CdmBase referencedCdmBase
){
113 return this.genericDao
.getReferencingObjectsForDeletion(referencedCdmBase
);
116 // Set<Class<? extends CdmBase>> allCdmClasses = genericDao.getAllCdmClasses(false); //findAllCdmClasses();
118 // referencedCdmBase = (CdmBase)HibernateProxyHelper.deproxy(referencedCdmBase);
119 // Class referencedClass = referencedCdmBase.getClass();
120 // Set<CdmBase> result = new HashSet<>();
121 // logger.debug("Referenced Class: " + referencedClass.getName());
123 // for (Class<? extends CdmBase> cdmClass : allCdmClasses){
124 // Set<Field> fields = getFields(cdmClass);
125 // for (Field field: fields){
126 // Class<?> type = field.getType();
128 // if (! type.isInterface()){
129 // if (referencedClass.isAssignableFrom(type)||
130 // type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
131 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
134 // }else if (type.isAssignableFrom(referencedClass)){
135 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
136 // }else if (Collection.class.isAssignableFrom(type)){
138 // if (checkIsSetOfType(field, referencedClass, type) == true){
139 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, true);
142 //// Class[] interfaces = referencedClass.getInterfaces();
143 //// for (Class interfaze: interfaces){
144 //// if (interfaze == type){
145 ////// if(interfaze.isAssignableFrom(returnType)){
146 //// handleSingleClass(interfaze, type, field, cdmClass, result, referencedCdmBase);
152 // } catch (Exception e) {
153 // e.printStackTrace();
154 // throw new RuntimeException(e);
159 // private boolean checkIsSetOfType(Field field, Class referencedClass, Class<?> type){
160 // Type genericType = (ParameterizedTypeImpl)field.getGenericType();
161 // if (genericType instanceof ParameterizedTypeImpl){
162 // ParameterizedTypeImpl paraType = (ParameterizedTypeImpl)genericType;
163 // paraType.getRawType();
164 // Type[] arguments = paraType.getActualTypeArguments();
165 // //logger.debug(arguments.length);
166 // if (arguments.length == 1){
167 // Class collectionClass;
169 // if (arguments[0] instanceof Class){
170 // collectionClass = (Class)arguments[0];
171 // }else if(arguments[0] instanceof TypeVariableImpl){
172 // TypeVariableImpl typeVariable = (TypeVariableImpl)arguments[0];
173 // GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
174 // collectionClass = (Class)genericDeclaration;
176 // logger.warn("Unknown Type");
179 // if (CdmBase.class.isAssignableFrom(collectionClass) && collectionClass.isAssignableFrom(referencedClass) ){
182 // } catch (Exception e) {
183 // logger.warn(e.getMessage());
186 // logger.warn("Length of arguments <> 1");
189 // logger.warn("Not a generic type of type ParameterizedTypeImpl");
197 // private boolean handleSingleClass(Class itemClass, Class type, Field field, Class cdmClass, Set<CdmBase> result,CdmBase value, boolean isCollection){
198 // if (! Modifier.isStatic(field.getModifiers())){
199 // String methodName = StringUtils.rightPad(field.getName(), 30);
200 // String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
201 // String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
203 // logger.debug(methodName + "\t\t" + className + "\t\t" + returnTypeName);
204 //// result_old.add(method);
205 // result.addAll(getCdmBasesByFieldAndClass(field, itemClass, cdmClass, value, isCollection));
210 // private Set<Field> getFields(Class clazz){
211 // Set<Field> result = new HashSet<>();
212 // for (Field field: clazz.getDeclaredFields()){
213 // if (!Modifier.isStatic(field.getModifiers())){
214 // result.add(field);
217 // Class superclass = clazz.getSuperclass();
218 // if (CdmBase.class.isAssignableFrom(superclass)){
219 // result.addAll(getFields(superclass));
224 // private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class itemClass, Class otherClazz, CdmBase item, boolean isCollection){
225 // Set<CdmBase> result = new HashSet<>();
226 // if (isCollection){
227 // result.addAll(genericDao.getCdmBasesWithItemInCollection(itemClass, otherClazz, field.getName(), item));
229 // result.addAll(genericDao.getCdmBasesByFieldAndClass(otherClazz, field.getName(), item));
235 public List
getHqlResult(String hqlQuery
){
236 return genericDao
.getHqlResult(hqlQuery
);
240 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, IMergeStrategy mergeStrategy
) throws MergeException
{
241 if (mergeStrategy
== null){
242 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
244 genericDao
.merge((CdmBase
)mergeFirst
, (CdmBase
)mergeSecond
, mergeStrategy
);
248 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
249 IMergeStrategy mergeStrategy
;
251 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
253 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
255 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
259 @Transactional(readOnly
= false)
261 public <T
extends IMergable
> void merge(int mergeFirstId
, int mergeSecondId
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
262 IMergeStrategy mergeStrategy
;
263 T mergeFirst
= (T
) genericDao
.find(clazz
, mergeFirstId
);
264 T mergeSecond
= (T
) genericDao
.find(clazz
, mergeSecondId
);
265 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
266 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
270 @Transactional(readOnly
= false)
271 public <T
extends IMergable
> void merge(UUID mergeFirstUuid
, UUID mergeSecondUuid
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
272 IMergeStrategy mergeStrategy
;
273 T mergeFirst
= (T
) genericDao
.find(clazz
, mergeFirstUuid
);
274 T mergeSecond
= (T
) genericDao
.find(clazz
, mergeSecondUuid
);
275 if (mergeFirst
== null){
276 throw new MergeException("The merge target is not available anymore.");
278 if (mergeSecond
== null){
279 throw new MergeException("The merge candidate is not available anymore.");
281 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
282 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
286 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
) throws MergeException
{
287 IMergeStrategy mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
288 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
293 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, IMatchStrategy matchStrategy
) throws MatchException
{
294 if (matchStrategy
== null){
295 matchStrategy
= DefaultMatchStrategy
.NewInstance(((objectToMatch
).getClass()));
297 return genericDao
.findMatching(objectToMatch
, matchStrategy
);
303 * @see eu.etaxonomy.cdm.api.service.ICommonService#findMatching(eu.etaxonomy.cdm.strategy.match.IMatchable, eu.etaxonomy.cdm.strategy.match.MatchStrategyConfigurator.MatchStrategy)
306 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, MatchStrategy strategy
) throws MatchException
{
307 return findMatching(objectToMatch
, MatchStrategyConfigurator
.getMatchStrategy(strategy
));
311 // * @see eu.etaxonomy.cdm.api.service.IService#list(java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
314 // public <TYPE extends OriginalSourceBase> Pager<TYPE> list(Class<TYPE> type,
315 // Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
316 // List<String> propertyPaths) {
317 // logger.warn("Not yet implemented");
322 @Transactional(readOnly
= false)
324 public void saveAllMetaData(Collection
<CdmMetaData
> metaData
) {
325 Iterator
<CdmMetaData
> iterator
= metaData
.iterator();
326 while(iterator
.hasNext()){
327 CdmMetaData cdmMetaData
= iterator
.next();
328 genericDao
.saveMetaData(cdmMetaData
);
333 public Map
<CdmMetaDataPropertyName
, CdmMetaData
> getCdmMetaData() {
334 Map
<CdmMetaDataPropertyName
, CdmMetaData
> result
= new HashMap
<>();
335 List
<CdmMetaData
> metaDataList
= genericDao
.getMetaData();
336 for (CdmMetaData metaData
: metaDataList
){
337 CdmMetaDataPropertyName propertyName
= metaData
.getPropertyName();
338 result
.put(propertyName
, metaData
);
344 public Object
initializeCollection(UUID ownerUuid
, String fieldName
) {
345 return genericDao
.initializeCollection(ownerUuid
, fieldName
);
350 public Object
initializeCollection(UUID ownerUuid
, String fieldName
, List
<String
> propertyPaths
) {
351 return genericDao
.initializeCollection(ownerUuid
, fieldName
, propertyPaths
);
356 public boolean isEmpty(UUID ownerUuid
, String fieldName
) {
357 return genericDao
.isEmpty(ownerUuid
, fieldName
);
362 public int size(UUID ownerUuid
, String fieldName
) {
363 return genericDao
.size(ownerUuid
, fieldName
);
368 public Object
get(UUID ownerUuid
, String fieldName
, int index
) {
369 return genericDao
.get(ownerUuid
, fieldName
, index
);
373 public boolean contains(UUID ownerUuid
, String fieldName
, Object element
) {
374 return genericDao
.contains(ownerUuid
, fieldName
, element
);
378 public boolean containsKey(UUID ownerUuid
, String fieldName
, Object key
) {
379 return genericDao
.containsKey(ownerUuid
, fieldName
, key
);
383 public boolean containsValue(UUID ownerUuid
, String fieldName
, Object value
) {
384 return genericDao
.containsValue(ownerUuid
, fieldName
, value
);
388 @Transactional(readOnly
= false)
389 public void createFullSampleData() {
390 genericDao
.createFullSampleData();
396 public <S
extends CdmBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
397 return genericDao
.list(type
, limit
, start
, orderHints
, propertyPaths
);
401 public <S
extends CdmBase
> long count(Class
<S
> type
) {
402 return genericDao
.count(type
);
406 @Transactional(readOnly
= false)
407 public CdmBase
save(CdmBase newInstance
) {
408 return genericDao
.save(newInstance
);
412 @Transactional(readOnly
= false)
413 public UUID
delete(CdmBase instance
) {
414 return genericDao
.delete(instance
);
418 @Transactional(readOnly
= false)
419 public UUID
saveOrUpdate(CdmBase newInstance
) {
420 return genericDao
.saveOrUpdate(newInstance
);
425 @Transactional(readOnly
= false)
426 public <T
extends CdmBase
> Map
<UUID
,T
> save(Collection
<T
> newInstances
) {
427 //this is very ugly, I know, but for now I do not want to copy the saveAll method from CdmEntityDaoBase to genericDao
428 //and generally the saveAll method should work for other CdmBase types with generics removed
429 return (Map
<UUID
, T
>) originalSourceDao
.saveAll((Collection
)newInstances
);
433 @Transactional(readOnly
= false)
434 public <T
extends CdmBase
> Map
<UUID
,T
> saveOrUpdate(Collection
<T
> newInstances
) {
435 //this is very ugly, I know, but for now I do not want to copy the saveAll method from CdmEntityDaoBase to genericDao
436 //and generally the saveAll method should work for other CdmBase types with generics removed
437 return (Map
<UUID
, T
>) originalSourceDao
.saveOrUpdateAll((Collection
)newInstances
);
442 public <T
extends CdmBase
> boolean isMergeable(T cdmBase1
, T cdmBase2
, IMergeStrategy mergeStrategy
) throws MergeException
{
443 return genericDao
.isMergeable(cdmBase1
, cdmBase2
, mergeStrategy
);