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
find(Class
<?
extends CdmBase
> clazz
, int id
){
63 return genericDao
.find(clazz
, id
);
67 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
, List
<String
> propertyPaths
){
68 return genericDao
.find(clazz
, id
, propertyPaths
);
73 public Map
<String
, ?
extends ISourceable
> getSourcedObjectsByIdInSource(Class clazz
, Set
<String
> idInSourceSet
, String idNamespace
) {
74 Map
<String
, ?
extends ISourceable
> list
= originalSourceDao
.findOriginalSourcesByIdInSource(clazz
, idInSourceSet
, idNamespace
);
79 public ISourceable
getSourcedObjectByIdInSource(Class clazz
, String idInSource
, String idNamespace
) {
80 ISourceable result
= null;
81 List
<IdentifiableEntity
> list
= originalSourceDao
.findOriginalSourceByIdInSource(clazz
, idInSource
, idNamespace
);
82 if (! list
.isEmpty()){
89 public Set
<CdmBase
> getReferencingObjects(CdmBase referencedCdmBase
){
90 return this.genericDao
.getReferencingObjects(referencedCdmBase
);
95 public Set
<CdmBase
> getReferencingObjectsForDeletion(CdmBase referencedCdmBase
){
96 return this.genericDao
.getReferencingObjectsForDeletion(referencedCdmBase
);
99 // Set<Class<? extends CdmBase>> allCdmClasses = genericDao.getAllCdmClasses(false); //findAllCdmClasses();
101 // referencedCdmBase = (CdmBase)HibernateProxyHelper.deproxy(referencedCdmBase);
102 // Class referencedClass = referencedCdmBase.getClass();
103 // Set<CdmBase> result = new HashSet<CdmBase>();
104 // logger.debug("Referenced Class: " + referencedClass.getName());
106 // for (Class<? extends CdmBase> cdmClass : allCdmClasses){
107 // Set<Field> fields = getFields(cdmClass);
108 // for (Field field: fields){
109 // Class<?> type = field.getType();
111 // if (! type.isInterface()){
112 // if (referencedClass.isAssignableFrom(type)||
113 // type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
114 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
117 // }else if (type.isAssignableFrom(referencedClass)){
118 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
119 // }else if (Collection.class.isAssignableFrom(type)){
121 // if (checkIsSetOfType(field, referencedClass, type) == true){
122 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, true);
125 //// Class[] interfaces = referencedClass.getInterfaces();
126 //// for (Class interfaze: interfaces){
127 //// if (interfaze == type){
128 ////// if(interfaze.isAssignableFrom(returnType)){
129 //// handleSingleClass(interfaze, type, field, cdmClass, result, referencedCdmBase);
135 // } catch (Exception e) {
136 // e.printStackTrace();
137 // throw new RuntimeException(e);
142 // private boolean checkIsSetOfType(Field field, Class referencedClass, Class<?> type){
143 // Type genericType = (ParameterizedTypeImpl)field.getGenericType();
144 // if (genericType instanceof ParameterizedTypeImpl){
145 // ParameterizedTypeImpl paraType = (ParameterizedTypeImpl)genericType;
146 // paraType.getRawType();
147 // Type[] arguments = paraType.getActualTypeArguments();
148 // //logger.debug(arguments.length);
149 // if (arguments.length == 1){
150 // Class collectionClass;
152 // if (arguments[0] instanceof Class){
153 // collectionClass = (Class)arguments[0];
154 // }else if(arguments[0] instanceof TypeVariableImpl){
155 // TypeVariableImpl typeVariable = (TypeVariableImpl)arguments[0];
156 // GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
157 // collectionClass = (Class)genericDeclaration;
159 // logger.warn("Unknown Type");
162 // if (CdmBase.class.isAssignableFrom(collectionClass) && collectionClass.isAssignableFrom(referencedClass) ){
165 // } catch (Exception e) {
166 // logger.warn(e.getMessage());
169 // logger.warn("Length of arguments <> 1");
172 // logger.warn("Not a generic type of type ParameterizedTypeImpl");
180 // private boolean handleSingleClass(Class itemClass, Class type, Field field, Class cdmClass, Set<CdmBase> result,CdmBase value, boolean isCollection){
181 // if (! Modifier.isStatic(field.getModifiers())){
182 // String methodName = StringUtils.rightPad(field.getName(), 30);
183 // String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
184 // String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
186 // logger.debug(methodName + "\t\t" + className + "\t\t" + returnTypeName);
187 //// result_old.add(method);
188 // result.addAll(getCdmBasesByFieldAndClass(field, itemClass, cdmClass, value, isCollection));
193 // private Set<Field> getFields(Class clazz){
194 // Set<Field> result = new HashSet<Field>();
195 // for (Field field: clazz.getDeclaredFields()){
196 // if (!Modifier.isStatic(field.getModifiers())){
197 // result.add(field);
200 // Class superclass = clazz.getSuperclass();
201 // if (CdmBase.class.isAssignableFrom(superclass)){
202 // result.addAll(getFields(superclass));
207 // private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class itemClass, Class otherClazz, CdmBase item, boolean isCollection){
208 // Set<CdmBase> result = new HashSet<CdmBase>();
209 // if (isCollection){
210 // result.addAll(genericDao.getCdmBasesWithItemInCollection(itemClass, otherClazz, field.getName(), item));
212 // result.addAll(genericDao.getCdmBasesByFieldAndClass(otherClazz, field.getName(), item));
218 public List
getHqlResult(String hqlQuery
){
219 return genericDao
.getHqlResult(hqlQuery
);
223 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, IMergeStrategy mergeStrategy
) throws MergeException
{
224 if (mergeStrategy
== null){
225 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
227 genericDao
.merge((CdmBase
)mergeFirst
, (CdmBase
)mergeSecond
, mergeStrategy
);
231 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, Class
<?
extends CdmBase
> clazz
) throws MergeException
{
232 IMergeStrategy mergeStrategy
;
234 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
236 mergeStrategy
= DefaultMergeStrategy
.NewInstance(clazz
);
238 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
242 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
) throws MergeException
{
243 IMergeStrategy mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
244 merge(mergeFirst
, mergeSecond
, mergeStrategy
);
249 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, IMatchStrategy matchStrategy
) throws MatchException
{
250 if (matchStrategy
== null){
251 matchStrategy
= DefaultMatchStrategy
.NewInstance(((objectToMatch
).getClass()));
253 return genericDao
.findMatching(objectToMatch
, matchStrategy
);
259 * @see eu.etaxonomy.cdm.api.service.ICommonService#findMatching(eu.etaxonomy.cdm.strategy.match.IMatchable, eu.etaxonomy.cdm.strategy.match.MatchStrategyConfigurator.MatchStrategy)
262 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, MatchStrategy strategy
) throws MatchException
{
263 return findMatching(objectToMatch
, MatchStrategyConfigurator
.getMatchStrategy(strategy
));
267 // * @see eu.etaxonomy.cdm.api.service.IService#list(java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
270 // public <TYPE extends OriginalSourceBase> Pager<TYPE> list(Class<TYPE> type,
271 // Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
272 // List<String> propertyPaths) {
273 // logger.warn("Not yet implemented");
278 @Transactional(readOnly
= false)
280 public void saveAllMetaData(Collection
<CdmMetaData
> metaData
) {
281 Iterator
<CdmMetaData
> iterator
= metaData
.iterator();
282 while(iterator
.hasNext()){
283 CdmMetaData cdmMetaData
= iterator
.next();
284 genericDao
.saveMetaData(cdmMetaData
);
289 public Map
<MetaDataPropertyName
, CdmMetaData
> getCdmMetaData() {
290 Map
<MetaDataPropertyName
, CdmMetaData
> result
= new HashMap
<MetaDataPropertyName
, CdmMetaData
>();
291 List
<CdmMetaData
> metaDataList
= genericDao
.getMetaData();
292 for (CdmMetaData metaData
: metaDataList
){
293 MetaDataPropertyName propertyName
= metaData
.getPropertyName();
294 result
.put(propertyName
, metaData
);
300 public Object
initializeCollection(UUID ownerUuid
, String fieldName
) {
301 return genericDao
.initializeCollection(ownerUuid
, fieldName
);
306 public Object
initializeCollection(UUID ownerUuid
, String fieldName
, List
<String
> propertyPaths
) {
307 return genericDao
.initializeCollection(ownerUuid
, fieldName
, propertyPaths
);
312 public boolean isEmpty(UUID ownerUuid
, String fieldName
) {
313 return genericDao
.isEmpty(ownerUuid
, fieldName
);
318 public int size(UUID ownerUuid
, String fieldName
) {
319 return genericDao
.size(ownerUuid
, fieldName
);
324 public Object
get(UUID ownerUuid
, String fieldName
, int index
) {
325 return genericDao
.get(ownerUuid
, fieldName
, index
);
329 public boolean contains(UUID ownerUuid
, String fieldName
, Object element
) {
330 return genericDao
.contains(ownerUuid
, fieldName
, element
);
334 public boolean containsKey(UUID ownerUuid
, String fieldName
, Object key
) {
335 return genericDao
.containsKey(ownerUuid
, fieldName
, key
);
339 public boolean containsValue(UUID ownerUuid
, String fieldName
, Object value
) {
340 return genericDao
.containsValue(ownerUuid
, fieldName
, value
);
344 @Transactional(readOnly
= false)
345 public void createFullSampleData() {
346 genericDao
.createFullSampleData();
352 public <S
extends CdmBase
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
353 return genericDao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
357 public <S
extends CdmBase
> int count(Class
<S
> type
) {
358 return genericDao
.count(type
);
362 @Transactional(readOnly
= false)
363 public CdmBase
save(CdmBase newInstance
) {
364 return genericDao
.save(newInstance
);
369 @Transactional(readOnly
= false)
370 public <T
extends CdmBase
> Map
<UUID
,T
> save(Collection
<T
> newInstances
) {
371 //this is very ugly, I know, but for now I do not want to copy the saveAll method from CdmEntityDaoBase to genericDao
372 //and generally the saveAll method should work for other CdmBase types with generics removed
373 return (Map
)originalSourceDao
.saveAll((Collection
)newInstances
);
378 public <T
extends CdmBase
> boolean isMergeable(T cdmBase1
, T cdmBase2
, IMergeStrategy mergeStrategy
) throws MergeException
{
379 return genericDao
.isMergeable(cdmBase1
, cdmBase2
, mergeStrategy
);