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
.database
.DatabaseSchemaMismatchException
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmMetaData
;
29 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
30 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
31 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmMetaData
.MetaDataPropertyName
;
33 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmGenericDao
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IOriginalSourceDao
;
35 import eu
.etaxonomy
.cdm
.strategy
.match
.DefaultMatchStrategy
;
36 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchStrategy
;
37 import eu
.etaxonomy
.cdm
.strategy
.match
.IMatchable
;
38 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
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(propagation
= Propagation
.SUPPORTS
, readOnly
= true)
47 public class CommonServiceImpl
extends ServiceBase
<OriginalSourceBase
,IOriginalSourceDao
> implements ICommonService
{
48 private static final Logger logger
= Logger
.getLogger(CommonServiceImpl
.class);
51 IOriginalSourceDao originalSourceDao
;
54 ICdmGenericDao genericDao
;
58 protected void setDao(IOriginalSourceDao dao
) {
63 * @see eu.etaxonomy.cdm.api.service.ICommonService#getSourcedObjectsByIdInSource(java.lang.Class, java.util.List, java.lang.String)
65 public Map
<String
, ?
extends ISourceable
> getSourcedObjectsByIdInSource(Class clazz
, Set
<String
> idInSourceSet
, String idNamespace
) {
66 Map
<String
, ?
extends ISourceable
> list
= originalSourceDao
.findOriginalSourcesByIdInSource(clazz
, idInSourceSet
, idNamespace
);
71 * @see eu.etaxonomy.cdm.api.service.ICommonService#getSourcedObjectById(java.lang.String, java.lang.String)
73 public ISourceable
getSourcedObjectByIdInSource(Class clazz
, String idInSource
, String idNamespace
) {
74 ISourceable result
= null;
75 List
<IdentifiableEntity
> list
= originalSourceDao
.findOriginalSourceByIdInSource(clazz
, idInSource
, idNamespace
);
76 if (! list
.isEmpty()){
83 * @see eu.etaxonomy.cdm.api.service.ICommonService#getReferencingObjects(eu.etaxonomy.cdm.model.common.CdmBase)
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));
207 public List
getHqlResult(String hqlQuery
){
208 return genericDao
.getHqlResult(hqlQuery
);
212 * @see eu.etaxonomy.cdm.api.service.ICommonService#merge(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergeStragegy)
214 public <T
extends IMergable
> void merge(T mergeFirst
, T mergeSecond
, IMergeStrategy mergeStrategy
) throws MergeException
{
215 if (mergeStrategy
== null){
216 mergeStrategy
= DefaultMergeStrategy
.NewInstance(((CdmBase
)mergeFirst
).getClass());
218 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)
234 // public <TYPE extends OriginalSourceBase> Pager<TYPE> list(Class<TYPE> type,
235 // Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
236 // List<String> propertyPaths) {
237 // logger.warn("Not yet implemented");
242 * @see eu.etaxonomy.cdm.api.service.ICommonService#saveAll(java.util.Collection)
244 @Transactional(readOnly
= false)
245 public void saveAllMetaData(Collection
<CdmMetaData
> metaData
) {
246 Iterator
<CdmMetaData
> iterator
= metaData
.iterator();
247 while(iterator
.hasNext()){
248 CdmMetaData cdmMetaData
= iterator
.next();
249 genericDao
.saveMetaData(cdmMetaData
);
254 * @see eu.etaxonomy.cdm.api.service.ICommonService#getCdmMetaData()
256 public Map
<MetaDataPropertyName
, CdmMetaData
> getCdmMetaData() {
257 Map
<MetaDataPropertyName
, CdmMetaData
> result
= new HashMap
<MetaDataPropertyName
, CdmMetaData
>();
258 List
<CdmMetaData
> metaDataList
= genericDao
.getMetaData();
259 for (CdmMetaData metaData
: metaDataList
){
260 MetaDataPropertyName propertyName
= metaData
.getPropertyName();
261 result
.put(propertyName
, metaData
);
267 * Checks whether the current database schema is compatible with the CDM Library
268 * version by checking the first 3(??) numbers of the schema version
270 * @throws DatabaseSchemaMismatchException
273 * @see eu.etaxonomy.cdm.api.service.ICommonService#isDatabaseSchemaCompatible()
275 public boolean isDatabaseSchemaCompatible() {
277 Map
<MetaDataPropertyName
, CdmMetaData
> allCommonData
= getCdmMetaData();
279 if (allCommonData
.containsKey(MetaDataPropertyName
.DB_SCHEMA_VERSION
)) {
281 String currentSchemaVersionPrefix
= CdmMetaData
.getCurrentSchemaVersion(index
);
282 String databaseSchemaVersionPrefix
= CdmMetaData
.getDatabaseSchemaVersion(allCommonData
, index
);
284 if (areStringsEqual(currentSchemaVersionPrefix
, databaseSchemaVersionPrefix
)) {
292 * Compares version numbers of the current schema version with the database schema version.
293 * @param currentSchemaVersion
294 * @param databaseSchemaVersion
295 * @throws DatabaseSchemaMismatchException
297 private boolean areStringsEqual(String first
,
299 return first
.equals(second
);