- re-enabled identifier set in IdentifiableEntity
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / IdentifiableEntity.java
index 6bc4f35c4fd0ddeff806927f79daae9f1134576c..5cbec1f13b8f2c02d5304bb0ab93478dda18cc52 100644 (file)
@@ -51,6 +51,7 @@ import eu.etaxonomy.cdm.hibernate.search.StripHtmlBridge;
 import eu.etaxonomy.cdm.jaxb.FormattedTextAdapter;
 import eu.etaxonomy.cdm.jaxb.LSIDAdapter;
 import eu.etaxonomy.cdm.model.media.Rights;
+import eu.etaxonomy.cdm.model.name.BotanicalName;
 import eu.etaxonomy.cdm.model.name.NonViralName;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.reference.Reference;
@@ -104,7 +105,7 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
 
     @XmlElement(name = "TitleCache", required = true)
     @XmlJavaTypeAdapter(FormattedTextAdapter.class)
-    @Column(length=255, name="titleCache")
+    @Column(name="titleCache")
     @Match(value=MatchMode.CACHE, cacheReplaceMode=ReplaceMode.ALL)
     @NotEmpty(groups = Level2.class) // implictly NotNull
     @Size(max = 800)  //see #1592
@@ -146,6 +147,14 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
     @NotNull
     private Set<Extension> extensions = new HashSet<Extension>();
 
+    @XmlElementWrapper(name = "Identifiers", nillable = true)
+    @XmlElement(name = "Identifier")
+    @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
+    @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.DELETE})
+    @Merge(MergeMode.ADD_CLONE)
+    @NotNull
+    private Set<Identifier> identifiers = new HashSet<Identifier>();
+
     @XmlElementWrapper(name = "Sources", nillable = true)
     @XmlElement(name = "IdentifiableSource")
     @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
@@ -165,8 +174,8 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
     protected void initListener(){
         PropertyChangeListener listener = new PropertyChangeListener() {
             @Override
-            public void propertyChange(PropertyChangeEvent e) {
-                if (!e.getPropertyName().equals("titleCache") && !e.getPropertyName().equals("cacheStrategy") && ! isProtectedTitleCache()){
+            public void propertyChange(PropertyChangeEvent ev) {
+                if (!ev.getPropertyName().equals("titleCache") && !ev.getPropertyName().equals("cacheStrategy") && ! isProtectedTitleCache()){
                     titleCache = null;
                 }
             }
@@ -187,10 +196,6 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
 
 //******************************** CACHE *****************************************************/
 
-
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.cdm.model.common.IIdentifiableEntity#getTitleCache()
-     */
     // @Transient  - must not be transient, since this property needs to to be included in all serializations produced by the remote layer
     @Override
     public String getTitleCache(){
@@ -220,7 +225,7 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
     @Deprecated
     @Override
     public void setTitleCache(String titleCache){
-       //TODO shouldn't we call setTitleCache(String, boolean),but is this conformant with Java Bean Specification?  
+       //TODO shouldn't we call setTitleCache(String, boolean),but is this conformant with Java Bean Specification?
        this.titleCache = getTruncatedCache(titleCache);
     }
 
@@ -308,6 +313,55 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
         getCredits().remove(index);
     }
 
+    @Override
+    public Set<Identifier> getIdentifiers(){
+        if(this.identifiers == null) {
+            this.identifiers = new HashSet<Identifier>();
+        }
+        return this.identifiers;
+    }
+    /**
+     * @param type
+     * @return a Set of extension value strings
+     */
+    public Set<String> getIdentifiers(DefinedTerm type){
+       return getIdentifiers(type.getUuid());
+    }
+    /**
+     * @param extensionTypeUuid
+     * @return a Set of extension value strings
+     */
+    public Set<String> getIdentifiers(UUID identifierTypeUuid){
+        Set<String> result = new HashSet<String>();
+        for (Identifier identifier : getIdentifiers()){
+            if (identifier.getType().getUuid().equals(identifierTypeUuid)){
+                result.add(identifier.getIdentifier());
+            }
+        }
+        return result;
+    }
+
+    public Identifier addIdentifier(String identifier, DefinedTerm identifierType){
+       Identifier result = Identifier.NewInstance(this, identifier, identifierType);
+       return result;
+    }
+
+    @Override
+    public void addIdentifier(Identifier identifier){
+        if (identifier != null){
+               identifier.setIdentifiedObj(this);
+            getIdentifiers().add(identifier);
+        }
+    }
+    @Override
+    public void removeIdentifier(Identifier identifier){
+        if (identifier != null){
+               logger.warn("TODO");
+//             identifier.setExtendedObj(null);
+            getIdentifiers().remove(identifier);
+        }
+    }
+
     @Override
     public Set<Extension> getExtensions(){
         if(extensions == null) {
@@ -385,6 +439,25 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
         }
     }
 
+    @Override
+    public void addSources(Set<IdentifiableSource> sources) {
+        if (sources != null){
+               for (IdentifiableSource source: sources){
+                   IdentifiableEntity<?> oldSourcedObj = source.getSourcedObj();
+                   if (oldSourcedObj != null && oldSourcedObj != this){
+                       oldSourcedObj.getSources().remove(source);
+                   }
+                   getSources().add(source);
+                   source.setSourcedObj(this);
+               }
+        }
+    }
+    
+    @Override
+    public void removeSources() {
+       this.sources.clear();
+    }
+    
     @Override
     public IdentifiableSource addSource(OriginalSourceType type, String id, String idNamespace, Reference citation, String microCitation) {
         if (id == null && idNamespace == null && citation == null && microCitation == null){
@@ -394,8 +467,8 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
         addSource(source);
         return source;
     }
-    
-    
+
+
     @Override
     public IdentifiableSource addImportSource(String id, String idNamespace, Reference<?> citation, String microCitation) {
         if (id == null && idNamespace == null && citation == null && microCitation == null){
@@ -456,6 +529,20 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
          if(identifiableEntity instanceof NonViralName) {
              specifiedNameCache = HibernateProxyHelper.deproxy(identifiableEntity, NonViralName.class).getNameCache();
              specifiedTitleCache = identifiableEntity.getTitleCache();
+            if (identifiableEntity instanceof BotanicalName){
+                if (((BotanicalName)identifiableEntity).isAutonym()){
+                        boolean isProtected = false;
+                        String oldNameCache = ((BotanicalName) identifiableEntity).getNameCache();
+                        if ( ((BotanicalName)identifiableEntity).isProtectedNameCache()){
+                                isProtected = true;
+                        }
+                        ((BotanicalName)identifiableEntity).setProtectedNameCache(false);
+                        ((BotanicalName)identifiableEntity).setNameCache(null, false);
+                        specifiedNameCache = ((BotanicalName) identifiableEntity).getNameCache();
+                        ((BotanicalName)identifiableEntity).setNameCache(oldNameCache, isProtected);
+
+                }
+             }
 
          } else if(identifiableEntity instanceof TaxonBase) {
              TaxonBase taxonBase = HibernateProxyHelper.deproxy(identifiableEntity, TaxonBase.class);
@@ -478,6 +565,20 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
          if(this.isInstanceOf(NonViralName.class)) {
              thisNameCache = HibernateProxyHelper.deproxy(this, NonViralName.class).getNameCache();
              thisTitleCache = getTitleCache();
+
+             if (this instanceof BotanicalName){
+                if (((BotanicalName)this).isAutonym()){
+                        boolean isProtected = false;
+                        String oldNameCache = ((BotanicalName) this).getNameCache();
+                        if ( ((BotanicalName)this).isProtectedNameCache()){
+                                isProtected = true;
+                        }
+                        ((BotanicalName)this).setProtectedNameCache(false);
+                        ((BotanicalName)this).setNameCache(null, false);
+                        thisNameCache = ((BotanicalName) this).getNameCache();
+                        ((BotanicalName)this).setNameCache(oldNameCache, isProtected);
+                }
+             }
          } else if(this.isInstanceOf(TaxonBase.class)) {
              TaxonNameBase<?,?> taxonNameBase= HibernateProxyHelper.deproxy(this, TaxonBase.class).getName();
              NonViralName nonViralName = HibernateProxyHelper.deproxy(taxonNameBase, NonViralName.class);
@@ -489,6 +590,8 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
 
          // Compare name cache of taxon names
 
+
+
          if (!specifiedNameCache.equals("") && !thisNameCache.equals("")) {
              result = thisNameCache.compareTo(specifiedNameCache);
          }
@@ -529,11 +632,11 @@ public abstract class IdentifiableEntity<S extends IIdentifiableEntityCacheStrat
 
         @Override
         public String generateTitle() {
-            if (cacheStrategy == null){
+            if (getCacheStrategy() == null){
                 //logger.warn("No CacheStrategy defined for "+ this.getClass() + ": " + this.getUuid());
                 return this.getClass() + ": " + this.getUuid();
             }else{
-                return cacheStrategy.getTitleCache(this);
+                return getCacheStrategy().getTitleCache(this);
             }
         }