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
;
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.Propagation;
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
.common
.OriginalSourceBase
;
30 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
;
31 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmMetaData
.MetaDataPropertyName
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IOriginalSourceDao
;
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
.merge
.DefaultMergeStrategy
;
39 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
40 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergeStrategy
;
41 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
45 @Transactional(readOnly
= true)
46 public class CommonServiceImpl
extends ServiceBase
<OriginalSourceBase
,IOriginalSourceDao
> implements ICommonService
{
47 @SuppressWarnings("unused")
48 private static final Logger logger
= Logger
.getLogger(CommonServiceImpl
.class);
51 IOriginalSourceDao originalSourceDao
;
54 ICdmGenericDao genericDao
;
58 protected void setDao(IOriginalSourceDao dao
) {
63 public CdmBase
find(Class
<?
extends CdmBase
> clazz
, int id
){
64 return genericDao
.find(clazz
, id
);
69 public Map
<String
, ?
extends ISourceable
> getSourcedObjectsByIdInSource(Class clazz
, Set
<String
> idInSourceSet
, String idNamespace
) {
70 Map
<String
, ?
extends ISourceable
> list
= originalSourceDao
.findOriginalSourcesByIdInSource(clazz
, idInSourceSet
, idNamespace
);
75 public ISourceable
getSourcedObjectByIdInSource(Class clazz
, String idInSource
, String idNamespace
) {
76 ISourceable result
= null;
77 List
<IdentifiableEntity
> list
= originalSourceDao
.findOriginalSourceByIdInSource(clazz
, idInSource
, idNamespace
);
78 if (! list
.isEmpty()){
85 public Set
<CdmBase
> getReferencingObjects(CdmBase referencedCdmBase
){
86 return this.genericDao
.getReferencingObjects(referencedCdmBase
);
89 // Set<Class<? extends CdmBase>> allCdmClasses = genericDao.getAllCdmClasses(false); //findAllCdmClasses();
91 // referencedCdmBase = (CdmBase)HibernateProxyHelper.deproxy(referencedCdmBase);
92 // Class referencedClass = referencedCdmBase.getClass();
93 // Set<CdmBase> result = new HashSet<CdmBase>();
94 // logger.debug("Referenced Class: " + referencedClass.getName());
96 // for (Class<? extends CdmBase> cdmClass : allCdmClasses){
97 // Set<Field> fields = getFields(cdmClass);
98 // for (Field field: fields){
99 // Class<?> type = field.getType();
101 // if (! type.isInterface()){
102 // if (referencedClass.isAssignableFrom(type)||
103 // type.isAssignableFrom(referencedClass) && CdmBase.class.isAssignableFrom(type)){
104 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
107 // }else if (type.isAssignableFrom(referencedClass)){
108 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, false);
109 // }else if (Collection.class.isAssignableFrom(type)){
111 // if (checkIsSetOfType(field, referencedClass, type) == true){
112 // handleSingleClass(referencedClass, type, field, cdmClass, result, referencedCdmBase, true);
115 //// Class[] interfaces = referencedClass.getInterfaces();
116 //// for (Class interfaze: interfaces){
117 //// if (interfaze == type){
118 ////// if(interfaze.isAssignableFrom(returnType)){
119 //// handleSingleClass(interfaze, type, field, cdmClass, result, referencedCdmBase);
125 // } catch (Exception e) {
126 // e.printStackTrace();
127 // throw new RuntimeException(e);
132 // private boolean checkIsSetOfType(Field field, Class referencedClass, Class<?> type){
133 // Type genericType = (ParameterizedTypeImpl)field.getGenericType();
134 // if (genericType instanceof ParameterizedTypeImpl){
135 // ParameterizedTypeImpl paraType = (ParameterizedTypeImpl)genericType;
136 // paraType.getRawType();
137 // Type[] arguments = paraType.getActualTypeArguments();
138 // //logger.debug(arguments.length);
139 // if (arguments.length == 1){
140 // Class collectionClass;
142 // if (arguments[0] instanceof Class){
143 // collectionClass = (Class)arguments[0];
144 // }else if(arguments[0] instanceof TypeVariableImpl){
145 // TypeVariableImpl typeVariable = (TypeVariableImpl)arguments[0];
146 // GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
147 // collectionClass = (Class)genericDeclaration;
149 // logger.warn("Unknown Type");
152 // if (CdmBase.class.isAssignableFrom(collectionClass) && collectionClass.isAssignableFrom(referencedClass) ){
155 // } catch (Exception e) {
156 // logger.warn(e.getMessage());
159 // logger.warn("Length of arguments <> 1");
162 // logger.warn("Not a generic type of type ParameterizedTypeImpl");
170 // private boolean handleSingleClass(Class itemClass, Class type, Field field, Class cdmClass, Set<CdmBase> result,CdmBase value, boolean isCollection){
171 // if (! Modifier.isStatic(field.getModifiers())){
172 // String methodName = StringUtils.rightPad(field.getName(), 30);
173 // String className = StringUtils.rightPad(cdmClass.getSimpleName(), 30);
174 // String returnTypeName = StringUtils.rightPad(type.getSimpleName(), 30);
176 // logger.debug(methodName + "\t\t" + className + "\t\t" + returnTypeName);
177 //// result_old.add(method);
178 // result.addAll(getCdmBasesByFieldAndClass(field, itemClass, cdmClass, value, isCollection));
183 // private Set<Field> getFields(Class clazz){
184 // Set<Field> result = new HashSet<Field>();
185 // for (Field field: clazz.getDeclaredFields()){
186 // if (!Modifier.isStatic(field.getModifiers())){
187 // result.add(field);
190 // Class superclass = clazz.getSuperclass();
191 // if (CdmBase.class.isAssignableFrom(superclass)){
192 // result.addAll(getFields(superclass));
197 // private Set<CdmBase> getCdmBasesByFieldAndClass(Field field, Class itemClass, Class otherClazz, CdmBase item, boolean isCollection){
198 // Set<CdmBase> result = new HashSet<CdmBase>();
199 // if (isCollection){
200 // result.addAll(genericDao.getCdmBasesWithItemInCollection(itemClass, otherClazz, field.getName(), item));
202 // result.addAll(genericDao.getCdmBasesByFieldAndClass(otherClazz, field.getName(), item));
208 public List
getHqlResult(String hqlQuery
){
209 return genericDao
.getHqlResult(hqlQuery
);
213 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, IMergeStrategy mergeStrategy
) throws MergeException
{
214 if (mergeStrategy
== null){
215 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
217 genericDao
.merge((CdmBase
)mergeFirst
, (CdmBase
)mergeSecond
, mergeStrategy
);
222 public <T
extends IMatchable
> List
<T
> findMatching(T objectToMatch
, IMatchStrategy matchStrategy
) throws MatchException
{
223 if (matchStrategy
== null){
224 matchStrategy
= DefaultMatchStrategy
.NewInstance(((objectToMatch
).getClass()));
226 return genericDao
.findMatching(objectToMatch
, matchStrategy
);
232 // * @see eu.etaxonomy.cdm.api.service.IService#list(java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
235 // public <TYPE extends OriginalSourceBase> Pager<TYPE> list(Class<TYPE> type,
236 // Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
237 // List<String> propertyPaths) {
238 // logger.warn("Not yet implemented");
242 @Transactional(readOnly
= false)
244 public void saveAllMetaData(Collection
<CdmMetaData
> metaData
) {
245 Iterator
<CdmMetaData
> iterator
= metaData
.iterator();
246 while(iterator
.hasNext()){
247 CdmMetaData cdmMetaData
= iterator
.next();
248 genericDao
.saveMetaData(cdmMetaData
);
253 public Map
<MetaDataPropertyName
, CdmMetaData
> getCdmMetaData() {
254 Map
<MetaDataPropertyName
, CdmMetaData
> result
= new HashMap
<MetaDataPropertyName
, CdmMetaData
>();
255 List
<CdmMetaData
> metaDataList
= genericDao
.getMetaData();
256 for (CdmMetaData metaData
: metaDataList
){
257 MetaDataPropertyName propertyName
= metaData
.getPropertyName();
258 result
.put(propertyName
, metaData
);