ref #9541 further improve referencing object handling
authorAndreas Müller <a.mueller@bgbm.org>
Wed, 31 Mar 2021 22:46:11 +0000 (00:46 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Wed, 31 Mar 2021 22:46:11 +0000 (00:46 +0200)
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/CdmUtils.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/DescriptionElementFormatter.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/ReferencingObjectFormatter.java
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/CdmBase.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/reference/OriginalSourceDaoImpl.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/reference/IOriginalSourceDao.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dto/ReferencingObjectDto.java
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/CommonServiceImpl.java

index 0f74857106e82bdcb4dbb351cf3e1bd05834c61c..3f978f900d99195ae55d5ba055f0fb4e9d6bb2b2 100644 (file)
@@ -6,7 +6,6 @@
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
-
 package eu.etaxonomy.cdm.common;
 
 import java.io.BufferedReader;
@@ -550,4 +549,12 @@ public class CdmUtils {
         }
         return -1;
     }
+
+    public static String userFriendlyCamelCase(String camelCase){
+        return String.join(" ", StringUtils.splitByCharacterTypeCamelCase(camelCase));
+    }
+
+    public static String userFriendlyClassName(Class<?> clazz){
+        return userFriendlyCamelCase(clazz.getSimpleName());
+    }
 }
index 6130b4b0f9882dad834e38c26303fce7ddef46da..17ecf3e826009a0d42dc47c83c4913661473b37c 100644 (file)
@@ -10,22 +10,14 @@ package eu.etaxonomy.cdm.format;
 
 import org.apache.commons.lang3.StringUtils;
 
-import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Language;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
-import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
-import eu.etaxonomy.cdm.model.description.SpecimenDescription;
-import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonInteraction;
-import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
 import eu.etaxonomy.cdm.model.description.TextData;
 import eu.etaxonomy.cdm.model.location.NamedArea;
-import eu.etaxonomy.cdm.model.name.TaxonName;
-import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 
 /**
@@ -42,28 +34,18 @@ public class DescriptionElementFormatter {
 
     public static String format(DescriptionElementBase element, Language defaultLanguage){
 
-        String mainElementLabel= null;
-        DescriptionBase<?> descr = element.getInDescription();
-        descr = CdmBase.deproxy(descr);
-
-        if (descr != null){
-            if (descr.isInstanceOf(TaxonDescription.class)){
-                Taxon taxon = CdmBase.deproxy(descr, TaxonDescription.class).getTaxon();
-                if (taxon != null){
-                    mainElementLabel = taxon.getTitleCache();
-                }
-            }else if (descr.isInstanceOf(SpecimenDescription.class)){
-                SpecimenOrObservationBase<?> specimen = CdmBase.deproxy(descr, SpecimenDescription.class).getDescribedSpecimenOrObservation();
-                if (specimen != null){
-                    mainElementLabel = specimen.getTitleCache();
-                }
-            }else if (descr.isInstanceOf(TaxonNameDescription.class)){
-                TaxonName name = CdmBase.deproxy(descr, TaxonNameDescription.class).getTaxonName();
-                if (name != null){
-                    mainElementLabel = name.getTitleCache();
-                }
-            }
-        }
+//        String mainElementLabel= null;
+//        DescriptionBase<?> descr = element.getInDescription();
+//        descr = CdmBase.deproxy(descr);
+//
+//        if (descr != null){
+//            IDescribable<?> target = CdmBase.deproxy(descr.describedEntity());
+//            if (target != null){
+//                mainElementLabel = target.getTitleCache();
+//            }else{
+//                return descr.getTitleCache();
+//            }
+//        }
 
         String cache = null;
         if (element instanceof TextData) {
@@ -83,7 +65,6 @@ public class DescriptionElementFormatter {
         }
         if (element instanceof Distribution) {
             Distribution distribution = (Distribution) element;
-
             NamedArea area = distribution.getArea();
             if(area != null){
                 cache =  area.getLabel();
@@ -97,9 +78,9 @@ public class DescriptionElementFormatter {
             }
         }
         String result = cache == null ? "" : cache;
-        if (isNotBlank(mainElementLabel)){
-            result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
-        }
+//        if (isNotBlank(mainElementLabel)){
+//            result = CdmUtils.concat(" ", result, "(" + mainElementLabel + ")");
+//        }
         return result;
 
     }
index 53d68879363334371bcf3a76367ad6a8ac51867f..e40925a0f6982c287f0c8cbe5fbdd614d25e330b 100644 (file)
@@ -420,7 +420,7 @@ public class ReferencingObjectFormatter {
     }
 
     //from taxeditor DescriptionHelper
-    public static String getCache(DescriptionElementBase element,
+    private static String getCache(DescriptionElementBase element,
             Language defaultLanguage) {
 
         String mainElementLabel= null;
index 848f278b90864cd59b13599308a28ec1da16b12c..9a72a752370ce2ece7e45d41bd757fb780e21957 100644 (file)
@@ -52,6 +52,7 @@ import org.hibernate.search.annotations.Index;
 import org.hibernate.search.annotations.Store;
 import org.joda.time.DateTime;
 
+import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
 import eu.etaxonomy.cdm.hibernate.search.NotNullAwareIdBridge;
@@ -475,7 +476,7 @@ public abstract class CdmBase implements Serializable, ICdmBase, ISelfDescriptiv
     @Transient
        @Override
        public String getUserFriendlyTypeName(){
-        return String.join(" ",StringUtils.splitByCharacterTypeCamelCase(getClass().getSimpleName()));
+        return CdmUtils.userFriendlyClassName(getClass());
        }
 
        @Transient
index 21171dd59a75859088754e10a363cc145d142b82..6353e61404d0d37f73c9930555ff4220551380f9 100644 (file)
@@ -23,8 +23,8 @@ import org.hibernate.criterion.Restrictions;
 import org.springframework.stereotype.Repository;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
-import eu.etaxonomy.cdm.model.name.HybridRelationship;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+import eu.etaxonomy.cdm.model.common.SingleSourcedEntityBase;
 import eu.etaxonomy.cdm.model.reference.ISourceable;
 import eu.etaxonomy.cdm.model.reference.NamedSourceBase;
 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
@@ -48,6 +48,42 @@ public class OriginalSourceDaoImpl
                super(OriginalSourceBase.class);
        }
 
+    @Override
+    public <S extends SingleSourcedEntityBase> S findSingleSourceBySourceId(Class<S> clazz, int sourceId){
+        if (clazz == null){
+            clazz = (Class)SingleSourcedEntityBase.class;
+        }
+        Query q = getSession().createQuery(
+                "SELECT c " +
+                "FROM " + clazz.getSimpleName() + " AS c " +
+                "INNER JOIN c.source AS source " +
+                "WHERE source.id= :sourceId "
+            );
+        q.setInteger("sourceId", sourceId);
+
+        @SuppressWarnings("unchecked")
+        S result = (S)q.uniqueResult();
+        return result;
+    }
+
+       @Override
+       public <S extends IdentifiableEntity> S findIdentifiableBySourceId(Class<S> clazz, int sourceId){
+        if (clazz == null){
+            clazz = (Class)IdentifiableEntity.class;
+        }
+           Query q = getSession().createQuery(
+                "SELECT c " +
+                "FROM " + clazz.getSimpleName() + " AS c " +
+                "INNER JOIN c.sources AS source " +
+                "WHERE source.id= :sourceId "
+            );
+           q.setInteger("sourceId", sourceId);
+
+           @SuppressWarnings("unchecked")
+        S result = (S)q.uniqueResult();
+           return result;
+       }
+
        @Override
     public <S extends ISourceable> Map<String, S> findOriginalSourcesByIdInSource(Class<S> clazz, Set<String> idInSourceSet, String idNamespace) {
 
@@ -108,21 +144,20 @@ public class OriginalSourceDaoImpl
                        crit.add(Restrictions.eq("idNamespace", idNamespace));
                }
                crit.addOrder(Order.desc("created"));
-               @SuppressWarnings({ "unchecked", "rawtypes" })
+               @SuppressWarnings({ "unchecked"})
         List<OriginalSourceBase> results = crit.list();
 
                return results;
        }
 
        @Override
-    public <T extends NamedSourceBase>  Long countWithNameUsedInSource(Class<T> clazz){
-        Criteria criteria = getSession().createCriteria(HybridRelationship.class);
+    public <T extends NamedSourceBase> Long countWithNameUsedInSource(Class<T> clazz){
 
-        clazz = clazz != null? clazz : (Class<T>) DescriptionElementSource.class;
+        clazz = clazz != null? clazz : (Class<T>)NamedSourceBase.class;
         Criteria crit = getSession().createCriteria(clazz);
         //count
-        criteria.setProjection(Projections.rowCount());
-        long result = (Long)criteria.uniqueResult();
+        crit.setProjection(Projections.rowCount());
+        long result = (Long)crit.uniqueResult();
 
         return result;
        }
@@ -131,6 +166,7 @@ public class OriginalSourceDaoImpl
        @Override
        public <T extends NamedSourceBase> List<T> listWithNameUsedInSource(Class<T> clazz,
                Integer pageSize, Integer pageNumber,List<OrderHint> orderHints, List<String> propertyPaths){
+
            clazz = clazz != null? clazz : (Class<T>) NamedSourceBase.class;
            Criteria crit = getSession().createCriteria(clazz);
            crit.add(Restrictions.isNotNull("nameUsedInSource"));
index dce9ba5ecb03586a507071027855aa15a8a793fe..f3cf452b6689849c657a47f710fcccfb916e1854 100644 (file)
@@ -12,6 +12,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+import eu.etaxonomy.cdm.model.common.SingleSourcedEntityBase;
 import eu.etaxonomy.cdm.model.reference.ISourceable;
 import eu.etaxonomy.cdm.model.reference.NamedSourceBase;
 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
@@ -43,4 +45,8 @@ public interface IOriginalSourceDao extends ICdmEntityDao<OriginalSourceBase>{
 
     public <T extends NamedSourceBase> Long countWithNameUsedInSource(Class<T> clazz);
 
+    public <S extends IdentifiableEntity> S findIdentifiableBySourceId(Class<S> clazz, int sourceId);
+
+    public <S extends SingleSourcedEntityBase> S findSingleSourceBySourceId(Class<S> clazz, int sourceId);
+
 }
index 0b0d6b999462578e3d79bafa44cf9b2487823bc4..810ce885218073acd4421fa7d594c13e9c41593e 100644 (file)
@@ -46,7 +46,7 @@ public class ReferencingObjectDto extends UuidAndTitleCache<CdmBase> {
     }
 
     public ReferencingObjectDto(CdmBase referencedEntity) {
-        super((Class<CdmBase>)referencedEntity.getClass(), referencedEntity.getUuid(), referencedEntity.getId(), null);
+        super(referencedEntity.getClass(), referencedEntity.getUuid(), referencedEntity.getId(), null);
         this.referencedEntity = referencedEntity;
     }
 
@@ -110,8 +110,6 @@ public class ReferencingObjectDto extends UuidAndTitleCache<CdmBase> {
 
     @Override
     public String toString() {
-        return "RefObjDto[type=" + getType() + ":" + getId() + "]";
+        return "RefObjDto[type=" + (getType()!=null? getType().getSimpleName():"-") + ":" + getId() + "]";
     }
-
-
 }
\ No newline at end of file
index 7c3cc6c441ae358fece58629cae0e2f7cd05b0b4..6e9d83133c26754074310cd62e757ab6d81b68a6 100644 (file)
@@ -23,13 +23,18 @@ import org.springframework.transaction.annotation.Transactional;
 \r
 import eu.etaxonomy.cdm.format.ReferencingObjectFormatter;\r
 import eu.etaxonomy.cdm.model.common.CdmBase;\r
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;\r
+import eu.etaxonomy.cdm.model.common.IdentifiableSource;\r
 import eu.etaxonomy.cdm.model.common.Language;\r
+import eu.etaxonomy.cdm.model.common.SingleSourcedEntityBase;\r
+import eu.etaxonomy.cdm.model.description.DescriptionBase;\r
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;\r
 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;\r
 import eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName;\r
 import eu.etaxonomy.cdm.model.name.NomenclaturalSource;\r
 import eu.etaxonomy.cdm.model.reference.ISourceable;\r
+import eu.etaxonomy.cdm.model.reference.NamedSource;\r
 import eu.etaxonomy.cdm.model.taxon.SecundumSource;\r
 import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;\r
 import eu.etaxonomy.cdm.persistence.dao.reference.IOriginalSourceDao;\r
@@ -127,7 +132,6 @@ public class CommonServiceImpl
     public Set<ReferencingObjectDto> initializeReferencingObjectDtos(Set<ReferencingObjectDto> dtos,\r
             boolean doReferencingEntity, boolean doTargetEntity, boolean doDescription, Language language) {\r
 \r
-//        Set<ReferencingObjectDto> result = new HashSet<>();\r
         for (ReferencingObjectDto dto : dtos){\r
             //TODO or load()?\r
             CdmBase entity = this.genericDao.find(dto.getType(), dto.getUuid());\r
@@ -159,6 +163,20 @@ public class CommonServiceImpl
             targetEntity = getTarget(element);\r
         }else if (entity instanceof DescriptionElementBase){\r
            targetEntity = getTarget((DescriptionElementBase)entity);\r
+        }else if (entity instanceof IdentifiableSource){\r
+            IdentifiableSource source = ((IdentifiableSource) entity);\r
+            targetEntity = originalSourceDao.findIdentifiableBySourceId(IdentifiableEntity.class, source.getId());\r
+        }else if (entity instanceof NamedSource){\r
+            NamedSource source = ((NamedSource) entity);\r
+            SingleSourcedEntityBase singleSourced = originalSourceDao.findSingleSourceBySourceId(SingleSourcedEntityBase.class, source.getId());\r
+            if (singleSourced != null){\r
+                targetEntity = singleSourced;\r
+            }else{\r
+                //TODO\r
+                targetEntity = entity;\r
+            }\r
+        }else if (entity instanceof DescriptionBase){\r
+            targetEntity = getTarget((DescriptionBase)entity);\r
         }else{\r
             targetEntity = entity;\r
         }\r
@@ -168,7 +186,11 @@ public class CommonServiceImpl
     }\r
 \r
     private CdmBase getTarget(DescriptionElementBase element) {\r
-        return (CdmBase)element.getInDescription().describedEntity();\r
+        return getTarget(element.getInDescription());\r
+    }\r
+\r
+    private CdmBase getTarget(DescriptionBase db) {\r
+        return db.describedEntity() != null ? (CdmBase)db.describedEntity() : db;\r
     }\r
 \r
     private String getReferencingObjectDescription(CdmBase entity, Language language) {\r