+
+ /**
+ * 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
+ */
+ @SuppressWarnings("unchecked")
+ private void updateTitleCacheForSingleEntity(
+ IIdentifiableEntityCacheStrategy<T> cacheStrategy,
+ List<T> entitiesToUpdate,
+ T entity) {
+
+ //assert (entity.isProtectedTitleCache() == false );
+ entity = CdmBase.deproxy(entity);
+
+ //exclude recursive inreferences
+ if (entity.isInstanceOf(Reference.class)){
+ Reference ref = (Reference)entity;
+ 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();
+ }
+ }
+
+
+ //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
+ entity.setCacheStrategy(entityCacheStrategy);
+ //NonViralNames and Reference have more caches //TODO handle in NameService
+ String oldNameCache = null;
+ String oldFullTitleCache = null;
+ String oldAbbrevTitleCache = null;
+ if (entity instanceof TaxonName ){
+ if (((TaxonName) entity).isNonViral()) {
+ try{
+ INonViralName nvn = (INonViralName) 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);
+ }
+ }catch(ClassCastException e){
+ System.out.println("entity: " + entity.getTitleCache());
+ }
+ }
+
+ }else if (entity instanceof Reference){
+ Reference ref = (Reference) entity;
+ if (!ref.isProtectedAbbrevTitleCache()){
+ ref.setProtectedAbbrevTitleCache(true);
+ oldAbbrevTitleCache = ref.getAbbrevTitleCache();
+ ref.setProtectedAbbrevTitleCache(false);
+ }
+ }
+ setOtherCachesNull(entity);
+ String newTitleCache= null;
+ INonViralName nvn = null; //TODO find better solution
+ try{
+ if (entity instanceof TaxonName){
+ nvn = (ITaxonName) entity;
+ newTitleCache = entityCacheStrategy.getTitleCache(nvn);
+ } else{
+ newTitleCache = entityCacheStrategy.getTitleCache(entity);
+ }
+ }catch (ClassCastException e){
+ nvn = HibernateProxyHelper.deproxy(entity, TaxonName.class);
+ newTitleCache = entityCacheStrategy.getTitleCache(nvn);
+ }
+
+ if ( 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 (nvn != null){
+ nvn.getNameCache();
+ nvn.getFullTitleCache();
+ }
+ if (entity instanceof Reference){
+ Reference ref = (Reference) entity;
+ ref.getAbbrevTitleCache();
+ }
+ entitiesToUpdate.add(entity);
+ }else if (nvn != null){
+ String newNameCache = nvn.getNameCache();
+ String newFullTitleCache = nvn.getFullTitleCache();
+ if ((oldNameCache == null && !nvn.isProtectedNameCache()) || (oldNameCache != null && !oldNameCache.equals(newNameCache))){
+ entitiesToUpdate.add(entity);
+ }else if ((oldFullTitleCache == null && !nvn.isProtectedFullTitleCache()) || (oldFullTitleCache != null && !oldFullTitleCache.equals(newFullTitleCache))){
+ entitiesToUpdate.add(entity);
+ }
+ }else if (entity instanceof Reference){
+ Reference ref = (Reference) entity;
+ String newAbbrevTitleCache = ref.getAbbrevTitleCache();
+ if ( (oldAbbrevTitleCache == null && !ref.isProtectedAbbrevTitleCache() ) || (oldAbbrevTitleCache != null && !oldAbbrevTitleCache.equals(newAbbrevTitleCache))){
+ entitiesToUpdate.add(entity);
+ }
+ }
+
+ }
+