refactor update title cache
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / IdentifiableServiceBase.java
index 4d9bf61ce76544b199d1e949b831b90d35b41969..72afcf728d7cba0ba4dd7e8446d8f57ed9d96446 100644 (file)
@@ -25,8 +25,8 @@ 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.DefaultProgressMonitor;
-import eu.etaxonomy.cdm.common.IProgressMonitor;
+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;
@@ -34,6 +34,7 @@ import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 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.name.NonViralName;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
 import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
@@ -162,6 +163,20 @@ public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO e
                 }
                 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) {
@@ -196,6 +211,7 @@ public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO e
         * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache()
         */
        @Override
+       @Transactional(readOnly = false)
        public void updateTitleCache() {
                updateTitleCache(null, null, null, null);
        }
@@ -220,28 +236,20 @@ public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO e
                        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 Reference){
-                                                       entityCacheStrategy = ReferenceFactory.newReference(((Reference)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(worked++);
+                       monitor.worked(list.size());
                        if (monitor.isCanceled()){
                                monitor.done();
                                return;
@@ -249,6 +257,65 @@ public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO e
                }
                monitor.done();
        }
+
+       /**
+        * @param cacheStrategy
+        * @param entitiesToUpdate
+        * @param entity
+        */
+       private void updateTitleCacheForSingleEntity(
+                       IIdentifiableEntityCacheStrategy<T> cacheStrategy,
+                       List<T> entitiesToUpdate, T entity) {
+               //exclude recursive inreferences
+               if (entity.isInstanceOf(Reference.class)){
+                       Reference ref = CdmBase.deproxy(entity, Reference.class);
+                       if (ref.getInReference() != null && ref.getInReference().equals(ref)){
+                               return;
+                       }
+               }
+               
+               
+               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);
+               entity.setProtectedTitleCache(true);
+               String titleCache = entity.getTitleCache();
+               entity.setProtectedTitleCache(false);
+               String nameCache = null;
+               if (entity instanceof NonViralName ){
+                       NonViralName nvn = (NonViralName) entity;
+                       if (!nvn.isProtectedNameCache()){
+                               nvn.setProtectedNameCache(true);
+                               nameCache = nvn.getNameCache();
+                               nvn.setProtectedNameCache(false);
+                       }
+                       
+                       
+               }
+               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();
+                       if (entity instanceof NonViralName){
+                               NonViralName nvn = (NonViralName) entity;
+                               String newnameCache = nvn.getNameCache();
+                       }
+                       entitiesToUpdate.add(entity);
+               }else if (entity instanceof NonViralName){
+                       NonViralName nvn = (NonViralName) entity;
+                       String newnameCache = nvn.getNameCache();
+                       if (nameCache == null || (nameCache != null && !nameCache.equals(newnameCache))){
+                               entitiesToUpdate.add(entity);
+                       }
+               }
+       }