import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
import eu.etaxonomy.cdm.api.service.pager.Pager;
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
+import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
+import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
import eu.etaxonomy.cdm.model.common.CdmBase;
import eu.etaxonomy.cdm.model.common.ISourceable;
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
import eu.etaxonomy.cdm.model.common.LSID;
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
import eu.etaxonomy.cdm.model.media.Rights;
-import eu.etaxonomy.cdm.model.reference.ReferenceBase;
+import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.reference.Reference;
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
+import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
+import eu.etaxonomy.cdm.persistence.dao.initializer.AutoPropertyInitializer;
import eu.etaxonomy.cdm.persistence.query.MatchMode;
import eu.etaxonomy.cdm.persistence.query.OrderHint;
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO extends IIdentifiableDao<T>> extends AnnotatableServiceBase<T,DAO>
implements IIdentifiableEntityService<T>{
- @Autowired
- protected ICommonService commonService;
+// @Autowired
+// protected ICommonService commonService;
protected static final int UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE = 1000;
Integer numberOfResults = dao.countRights(t);
List<Rights> results = new ArrayList<Rights>();
- if(numberOfResults > 0) { // no point checking again
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
}
Integer numberOfResults = dao.countSources(t);
List<IdentifiableSource> results = new ArrayList<IdentifiableSource>();
- if(numberOfResults > 0) { // no point checking again
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
results = dao.getSources(t, pageSize, pageNumber,propertyPaths);
}
return new DefaultPagerImpl<IdentifiableSource>(pageNumber, numberOfResults, pageSize, results);
}
- @Transactional(readOnly = true)
- protected List<T> findByTitle(IIdentifiableEntityServiceConfigurator config){
- return ((IIdentifiableDao)dao).findByTitle(config.getTitleSearchString(),
- config.getMatchMode(), 0, -1, null);
- // TODO: Implement parameters pageSize, pageNumber, and criteria
- }
@Transactional(readOnly = false)
public T replace(T x, T y) {
Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);\r
\r
List<T> results = new ArrayList<T>();\r
- if(numberOfResults > 0) { // no point checking again\r
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)\r
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths); \r
}\r
\r
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);\r
}
+ @Transactional(readOnly = true)
+ public Pager<T> findByTitle(IIdentifiableEntityServiceConfigurator<T> config){
+ return findByTitle(config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getCriteria(), config.getPageSize(), config.getPageNumber(), config.getOrderHints(), config.getPropertyPaths());
+ }
+
@Transactional(readOnly = true)
public List<T> listByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
List<T> results = new ArrayList<T>();
- if(numberOfResults > 0) { // no point checking again
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
}
return results;
}
+
+ @Transactional(readOnly = true)
+ public Pager<T> findTitleCache(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, MatchMode matchMode){
+ long numberOfResults = dao.countTitleCache(clazz, queryString, matchMode);
+
+ List<T> results = new ArrayList<T>();
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
+ results = dao.findTitleCache(clazz, queryString, pageSize, pageNumber, orderHints, matchMode);
+ }
+ int r = 0;
+ r += numberOfResults;
+
+ return new DefaultPagerImpl<T>(pageNumber, r , pageSize, results);
+ }
@Transactional(readOnly = true)
public List<T> listByReferenceTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
Integer numberOfResults = dao.countByReferenceTitle(clazz, queryString, matchmode, criteria);
List<T> results = new ArrayList<T>();
- if(numberOfResults > 0) { // no point checking again
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
results = dao.findByReferenceTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
}
return results;
Integer numberOfResults = dao.count(clazz,queryString);
List<T> results = new ArrayList<T>();
- if(numberOfResults > 0) { // no point checking again
+ if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
results = dao.search(clazz,queryString, pageSize, pageNumber, orderHints, propertyPaths);
}
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
}
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache()
+ */
+ @Override
@Transactional(readOnly = false)
- public void updateTitleCache(Class<? extends T> clazz) {
- IIdentifiableEntityCacheStrategy<T> cacheStrategy = null;
- updateTitleCache(clazz, UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE, cacheStrategy);
+ public void updateTitleCache() {
+ updateTitleCache(null, null, null, null);
}
-
@Transactional(readOnly = false) //TODO check transactional behaviour, e.g. what happens with the session if count is very large
- public void updateTitleCache(Class<? extends T> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<T> cacheStrategy) {
+ protected <S extends T > void updateTitleCacheImpl(Class<S> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<T> cacheStrategy, IProgressMonitor monitor) {
if (stepSize == null){
stepSize = UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE;
}
-
+ if (monitor == null){
+ monitor = DefaultProgressMonitor.NewInstance();
+ }
+
int count = dao.count(clazz);
+ monitor.beginTask("update titles", count);
+ int worked = 0;
for(int i = 0 ; i < count ; i = i + stepSize){
// not sure if such strict ordering is necessary here, but for safety reasons I do it
ArrayList<OrderHint> orderHints = new ArrayList<OrderHint>();
orderHints.add( new OrderHint("id", OrderHint.SortOrder.ASCENDING));
- List<T> list = this.list(clazz, stepSize, i, orderHints, null);
+
+
+ Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit = switchOfAutoinitializer();
+ List<S> list = this.list(clazz, stepSize, i, orderHints, null);
+ switchOnOldAutoInitializer(oldAutoInit);
+
List<T> entitiesToUpdate = new ArrayList<T>();
for (T entity : list){
if (entity.isProtectedTitleCache() == false){
- IIdentifiableEntityCacheStrategy entityCacheStrategy = cacheStrategy;
- if (entityCacheStrategy == null){
- entityCacheStrategy = entity.getCacheStrategy();
- //FIXME find out why the wrong cache strategy is loaded here, see #1876
- if (entity instanceof ReferenceBase){
- entityCacheStrategy = ReferenceFactory.newReference(((ReferenceBase)entity).getType()).getCacheStrategy();
- }
- }
- entity.setCacheStrategy(entityCacheStrategy);
- //TODO this won't work for those classes that always generate the title cache new
- String titleCache = entity.getTitleCache();
- setOtherCachesNull(entity); //TODO find better solution
- String newTitleCache = entityCacheStrategy.getTitleCache(entity);
- if (titleCache == null || titleCache != null && ! titleCache.equals(newTitleCache)){
- entity.setTitleCache(null, false);
- entity.getTitleCache();
- entitiesToUpdate.add(entity);
- }
+ updateTitleCacheForSingleEntity(cacheStrategy, entitiesToUpdate, entity);
+ }
+ worked++;
+ }
+ for (T entity: entitiesToUpdate){
+ if (entity.getTitleCache() != null){
+ //System.err.println(entity.getTitleCache());
+ }else{
+ //System.err.println("no titleCache" + ((NonViralName)entity).getNameCache());
}
}
saveOrUpdate(entitiesToUpdate);
-
+ monitor.worked(list.size());
+ if (monitor.isCanceled()){
+ monitor.done();
+ return;
+ }
+ }
+ monitor.done();
+ }
+
+ /**
+ * Brings back all auto initializers to the bean initializer
+ * @see #switchOfAutoinitializer()
+ * @param oldAutoInit
+ */
+ protected void switchOnOldAutoInitializer(
+ Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit) {
+ HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
+ initializer.setBeanAutoInitializers(oldAutoInit);
+ }
+
+ /**
+ * Removes all auto initializers from the bean initializer
+ *
+ * @see #switchOnOldAutoInitializer(Map)
+ * @return
+ */
+ protected Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> switchOfAutoinitializer() {
+ HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
+ Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInitializers = initializer.getBeanAutoInitializers();
+ Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> map = new HashMap<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>>();
+ initializer.setBeanAutoInitializers(map);
+ return oldAutoInitializers;
+ }
+
+ /**
+ * @param cacheStrategy
+ * @param entitiesToUpdate
+ * @param entity
+ */
+ /**
+ * @param cacheStrategy
+ * @param entitiesToUpdate
+ * @param entity
+ */
+ private void updateTitleCacheForSingleEntity(
+ IIdentifiableEntityCacheStrategy<T> cacheStrategy,
+ List<T> entitiesToUpdate, T entity) {
+
+ assert (entity.isProtectedTitleCache() == false );
+
+ //exclude recursive inreferences
+ if (entity.isInstanceOf(Reference.class)){
+ Reference<?> ref = CdmBase.deproxy(entity, Reference.class);
+ if (ref.getInReference() != null && ref.getInReference().equals(ref)){
+ return;
+ }
+ }
+
+
+ //define the correct cache strategy
+ IIdentifiableEntityCacheStrategy entityCacheStrategy = cacheStrategy;
+ if (entityCacheStrategy == null){
+ entityCacheStrategy = entity.getCacheStrategy();
+ //FIXME find out why the wrong cache strategy is loaded here, see #1876
+ if (entity instanceof Reference){
+ entityCacheStrategy = ReferenceFactory.newReference(((Reference)entity).getType()).getCacheStrategy();
+ }
+ }
+ entity.setCacheStrategy(entityCacheStrategy);
+
+
+ //old titleCache
+ entity.setProtectedTitleCache(true);
+ String oldTitleCache = entity.getTitleCache();
+ entity.setTitleCache(oldTitleCache, false); //before we had entity.setProtectedTitleCache(false) but this deleted the titleCache itself
+
+ //NonViralNames and Reference have more caches //TODO handle in NameService
+ String oldNameCache = null;
+ String oldFullTitleCache = null;
+ String oldAbbrevTitleCache = null;
+ if (entity instanceof NonViralName ){
+ NonViralName<?> nvn = (NonViralName) entity;
+ if (!nvn.isProtectedNameCache()){
+ nvn.setProtectedNameCache(true);
+ oldNameCache = nvn.getNameCache();
+ nvn.setProtectedNameCache(false);
+ }
+ if (!nvn.isProtectedFullTitleCache()){
+ nvn.setProtectedFullTitleCache(true);
+ oldFullTitleCache = nvn.getFullTitleCache();
+ nvn.setProtectedFullTitleCache(false);
+ }
+ }else if (entity instanceof Reference){
+ Reference<?> ref = (Reference<?>) entity;
+ if (!ref.isProtectedAbbrevTitleCache()){
+ ref.setProtectedAbbrevTitleCache(true);
+ oldAbbrevTitleCache = ref.getAbbrevTitleCache();
+ ref.setProtectedAbbrevTitleCache(false);
+ }
+ }
+ setOtherCachesNull(entity); //TODO find better solution
+
+ String newTitleCache = entityCacheStrategy.getTitleCache(entity);
+ if (oldTitleCache == null || oldTitleCache != null && ! oldTitleCache.equals(newTitleCache) ){
+ entity.setTitleCache(null, false);
+ String newCache = entity.getTitleCache();
+ if (newCache == null){
+ logger.warn("newCache should never be null");
+ }
+ if (oldTitleCache == null){
+ logger.info("oldTitleCache should never be null");
+ }
+ if (entity instanceof NonViralName){
+ NonViralName<?> nvn = (NonViralName) entity;
+ nvn.getNameCache();
+ nvn.getFullTitleCache();
+ }
+ if (entity instanceof Reference){
+ Reference<?> ref = (Reference<?>) entity;
+ ref.getAbbrevTitleCache();
+ }
+ entitiesToUpdate.add(entity);
+ }else if (entity instanceof NonViralName){
+ NonViralName<?> nvn = (NonViralName) entity;
+ String newNameCache = nvn.getNameCache();
+ String newFullTitleCache = nvn.getFullTitleCache();
+ if (oldNameCache == null || (oldNameCache != null && !oldNameCache.equals(newNameCache))){
+ entitiesToUpdate.add(entity);
+ }else if (oldFullTitleCache == null || (oldFullTitleCache != null && !oldFullTitleCache.equals(newFullTitleCache))){
+ entitiesToUpdate.add(entity);
+ }
+ }else if (entity instanceof Reference){
+ Reference<?> ref = (Reference<?>) entity;
+ String newAbbrevTitleCache = ref.getAbbrevTitleCache();
+ if (oldAbbrevTitleCache == null || (oldAbbrevTitleCache != null && !oldAbbrevTitleCache.equals(newAbbrevTitleCache))){
+ entitiesToUpdate.add(entity);
+ }
}
}
return;
}
-// @Override
-// public int deduplicate(Class<? extends IdentifiableEntity> clazz, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy);
+ private class DeduplicateState{
+ String lastTitleCache;
+ Integer pageSize = 50;
+ int nPages = 3;
+ int startPage = 0;
+ boolean isCompleted = false;
+ int result;
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#deduplicate(java.lang.Class, eu.etaxonomy.cdm.strategy.match.IMatchStrategy, eu.etaxonomy.cdm.strategy.merge.IMergeStrategy)
+ */
@Override
+ @Transactional(readOnly = false)
public int deduplicate(Class<? extends T> clazz, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
-// if (clazz == null){
-// clazz = this.getCT.class;
-// }
+ DeduplicateState dedupState = new DeduplicateState();
+
+ if (clazz == null){
+ logger.warn("Deduplication clazz must not be null!");
+ return 0;
+ }
if (! ( IMatchable.class.isAssignableFrom(clazz) && IMergable.class.isAssignableFrom(clazz) ) ){
logger.warn("Deduplication implemented only for classes implementing IMatchable and IMergeable. No deduplication performed!");
return 0;
if (matchStrategy == null){
matchStrategy = DefaultMatchStrategy.NewInstance(matchableClass);
}
-
- int result = 0;
- double countTotal = count(clazz);
- Integer pageSize = 1000;
List<T> nextGroup = new ArrayList<T>();
- String lastTitleCache = null;
- Number countPagesN = Math.ceil(countTotal/pageSize.doubleValue()) ;
- int countPages = countPagesN.intValue();
+ int result = 0;
+// double countTotal = count(clazz);
+//
+// Number countPagesN = Math.ceil(countTotal/dedupState.pageSize.doubleValue()) ;
+// int countPages = countPagesN.intValue();
+//
- for (int i = 0; i< countPages ; i++){
- List<OrderHint> orderHints = Arrays.asList(new OrderHint[]{new OrderHint("titleCache", SortOrder.ASCENDING)});
- List<T> objectList = listByTitle(clazz, null, null, null, pageSize, i, orderHints, null);
+ List<OrderHint> orderHints = Arrays.asList(new OrderHint[]{new OrderHint("titleCache", SortOrder.ASCENDING)});
- for (T object : objectList){
- String currentTitleCache = object.getTitleCache();
- if (currentTitleCache != null && currentTitleCache.equals(lastTitleCache)){
- //=titleCache
- nextGroup.add(object);
- }else{
- //<> titleCache
- result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
- nextGroup = new ArrayList<T>();
- nextGroup.add(object);
- }
- lastTitleCache = currentTitleCache;
- }
+ while (! dedupState.isCompleted){
+ //get x page sizes
+ List<T> objectList = getPages(clazz, dedupState, orderHints);
+ //after each page check if any changes took place
+ int nUnEqualPages = handleAllPages(objectList, dedupState, nextGroup, matchStrategy, mergeStrategy);
+ nUnEqualPages = nUnEqualPages + dedupState.pageSize * dedupState.startPage;
+ //refresh start page counter
+ int finishedPages = nUnEqualPages / dedupState.pageSize;
+ dedupState.startPage = finishedPages;
}
+
result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
return result;
}
+
+ private int handleAllPages(List<T> objectList, DeduplicateState dedupState, List<T> nextGroup, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
+ int nUnEqual = 0;
+ for (T object : objectList){
+ String currentTitleCache = object.getTitleCache();
+ if (currentTitleCache != null && currentTitleCache.equals(dedupState.lastTitleCache)){
+ //=titleCache
+ nextGroup.add(object);
+ }else{
+ //<> titleCache
+ dedupState.result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
+ nextGroup = new ArrayList<T>();
+ nextGroup.add(object);
+ nUnEqual++;
+ }
+ dedupState.lastTitleCache = currentTitleCache;
+ }
+ handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
+ return nUnEqual;
+ }
+
+ private List<T> getPages(Class<? extends T> clazz, DeduplicateState dedupState, List<OrderHint> orderHints) {
+ List<T> result = new ArrayList<T>();
+ for (int pageNo = dedupState.startPage; pageNo < dedupState.startPage + dedupState.nPages; pageNo++){
+ List<T> objectList = listByTitle(clazz, null, null, null, dedupState.pageSize, pageNo, orderHints, null);
+ result.addAll(objectList);
+ }
+ if (result.size()< dedupState.nPages * dedupState.pageSize ){
+ dedupState.isCompleted = true;
+ }
+ return result;
+ }
+
private int handleLastGroup(List<T> group, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
int result = 0;
int size = group.size();
}
return result;
}
+
+ public Integer countByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria){
+ Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
+
+ return numberOfResults;
+ }
+
+ @Transactional(readOnly = true)
+ public Integer countByTitle(IIdentifiableEntityServiceConfigurator<T> config){
+ return countByTitle(config.getClazz(), config.getTitleSearchStringSqlized(),
+ config.getMatchMode(), config.getCriteria());
+
+ }
+
+
+
}\r