cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / reference / Reference.java
index df6f2f3365721db0dce55873d923aa48aa1debc2..673ddf17aa35a30fa4b53c355520b2204be6db6f 100644 (file)
@@ -37,6 +37,7 @@ import javax.xml.bind.annotation.XmlTransient;
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.hibernate.annotations.Cascade;
@@ -49,9 +50,11 @@ import org.hibernate.search.annotations.Field;
 import org.hibernate.search.annotations.FieldBridge;
 import org.hibernate.search.annotations.IndexedEmbedded;
 import org.joda.time.DateTime;
+import org.joda.time.Partial;
 
 import eu.etaxonomy.cdm.common.DOI;
 import eu.etaxonomy.cdm.common.URI;
+import eu.etaxonomy.cdm.format.common.TimePeriodPartialFormatter;
 import eu.etaxonomy.cdm.format.reference.NomenclaturalSourceFormatter;
 import eu.etaxonomy.cdm.hibernate.search.DateTimeBridge;
 import eu.etaxonomy.cdm.hibernate.search.DoiBridge;
@@ -120,7 +123,9 @@ import eu.etaxonomy.cdm.validation.annotation.ReferenceCheck;
     "seriesPart",
     "datePublished",
     "publisher",
+    "publisher2",
     "placePublished",
+    "placePublished2",
     "institution",
     "school",
     "organization",
@@ -145,7 +150,7 @@ public class Reference
                    IIntextReferenceTarget {
 
     private static final long serialVersionUID = -2034764545042691295L;
-       private static final Logger logger = LogManager.getLogger(Reference.class);
+       private static final Logger logger = LogManager.getLogger();
 
 //  from E+M import (still needed?)
 //     @Column(length=255)
@@ -161,7 +166,7 @@ public class Reference
        value = "eu.etaxonomy.cdm.model.reference.ReferenceType")}
     )
        @Audited
-       protected ReferenceType type;
+       private ReferenceType type;
 
        //Title of the reference
        @XmlElement(name ="Title" )
@@ -201,28 +206,28 @@ public class Reference
     //TODO Val #3379
 //    @NullOrNotEmpty
     @Column(length=255)
-       protected String editor;
+    private String editor;
 
     @XmlElement(name = "Volume")
     @Field
     //TODO Val #3379
 //    @NullOrNotEmpty
     @Column(length=255)
-       protected String volume;
+    private String volume;
 
     @XmlElement(name = "Pages")
     @Field
     //TODO Val #3379
 //    @NullOrNotEmpty
     @Column(length=255)
-       protected String pages;
+    private String pages;
 
     @XmlElement(name = "Edition")
     @Field
     //TODO Val #3379
 //    @NullOrNotEmpty
     @Column(length=255)
-       protected String edition;
+    private String edition;
 
     @XmlElement(name = "ISBN")
     @Field
@@ -230,15 +235,14 @@ public class Reference
 //    @NullOrNotEmpty
     @Column(length=255)
        @Pattern(regexp = "(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$", groups = Level2.class, message = "{eu.etaxonomy.cdm.model.reference.Reference.isbn.message}")
-       protected String isbn;
+    private String isbn;
 
     @XmlElement(name = "Doi")
     @Field
     @FieldBridge(impl = DoiBridge.class)
     @Type(type="doiUserType")
     @Column(length=DOI.MAX_LENGTH)
-    protected DOI doi;
-
+    private DOI doi;
 
        @XmlElement(name = "ISSN")
     @Field
@@ -246,36 +250,45 @@ public class Reference
 //     @NullOrNotEmpty
     @Column(length=255)
        @Pattern(regexp = "(?=.{9}$)\\d{4}([- ])\\d{4} (\\d|X)$", groups = Level2.class, message = "{eu.etaxonomy.cdm.model.reference.Reference.issn.message}")
-       protected String issn;
+       private String issn;
 
     @XmlElement(name = "SeriesPart")
     @Field
     //TODO Val #3379
 //    @NullOrNotEmpty
     @Column(length=255)
-       protected String seriesPart;
+    private String seriesPart;
 
        @XmlElement(name = "Organization")
     @Field
     //TODO Val #3379
 //     @NullOrNotEmpty
     @Column(length=255)
-       protected String organization;
+       private String organization;
 
        @XmlElement(name = "Publisher")
     @Field
     //TODO Val #3379
 //     @NullOrNotEmpty
     @Column(length=255)
-       protected String publisher;
+       private String publisher;
 
+    @XmlElement(name = "Publisher2")
+    @Field
+    @Column(length=255)
+    private String publisher2;
 
        @XmlElement(name = "PlacePublished")
     @Field
     //TODO Val #3379
 //     @NullOrNotEmpty
     @Column(length=255)
-       protected String placePublished;
+       private String placePublished;
+
+    @XmlElement(name = "PlacePublished2")
+    @Field
+    @Column(length=255)
+    private String placePublished2;
 
        @XmlElement(name = "Institution")
        @XmlIDREF
@@ -283,7 +296,7 @@ public class Reference
        @ManyToOne(fetch = FetchType.LAZY)
        @IndexedEmbedded
        @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
-       protected Institution institution;
+       private Institution institution;
 
        @XmlElement(name = "School")
     @XmlIDREF
@@ -291,7 +304,7 @@ public class Reference
        @ManyToOne(fetch = FetchType.LAZY)
        @IndexedEmbedded
        @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
-       protected Institution school;
+       private Institution school;
 
     @XmlElement(name = "InReference")
     @XmlIDREF
@@ -299,7 +312,7 @@ public class Reference
     @ManyToOne(fetch = FetchType.LAZY)
     @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
 //  @InReference(groups=Level2.class)
-       protected Reference inReference;
+    private Reference inReference;
 
 //********************************************************/
 
@@ -578,33 +591,52 @@ public class Reference
     public String getPublisher() {
                return publisher;
        }
-
        @Override
     public void setPublisher(String publisher) {
-               this.publisher = isBlank(publisher)? null : publisher;
+               this.publisher = StringUtils.truncate(isBlank(publisher)? null : publisher, 255);
        }
-
-       @Override
+    @Override
     public void setPublisher(String publisher, String placePublished){
-               this.publisher = publisher;
-               this.placePublished = placePublished;
-       }
+        this.publisher = publisher;
+        this.placePublished = placePublished;
+    }
+
+    @Override
+    public String getPublisher2() {
+        return publisher2;
+    }
+    @Override
+    public void setPublisher2(String publisher2) {
+        this.publisher2 = StringUtils.truncate(isBlank(publisher2)? null : publisher2, 255);
+    }
+    @Override
+    public void setPublisher2(String publisher2, String placePublished2){
+        this.publisher2 = publisher2;
+        this.placePublished2 = placePublished2;
+    }
 
        @Override
     public String getPlacePublished() {
                return placePublished;
        }
-
        @Override
     public void setPlacePublished(String placePublished) {
                this.placePublished = isBlank(placePublished)? null: placePublished;
        }
 
+    @Override
+    public String getPlacePublished2() {
+        return placePublished2;
+    }
+    @Override
+    public void setPlacePublished2(String placePublished2) {
+        this.placePublished2 = isBlank(placePublished2)? null: placePublished2;
+    }
+
        @Override
     public Institution getInstitution() {
                return institution;
        }
-
        @Override
     public void setInstitution(Institution institution) {
                this.institution = institution;
@@ -614,7 +646,6 @@ public class Reference
     public Institution getSchool() {
                return school;
        }
-
        @Override
     public void setSchool(Institution school) {
                this.school = school;
@@ -624,7 +655,6 @@ public class Reference
     public Reference getInReference() {
                return inReference;
        }
-
        @Override
     public void setInReference(Reference inReference) {
                this.inReference = inReference;
@@ -645,9 +675,6 @@ public class Reference
 
        /**
         * Whether this reference is of the given type
-        *
-        * @param type
-        * @return
         */
        @Override
     public boolean isOfType(ReferenceType type){
@@ -838,7 +865,7 @@ public class Reference
        /**
         * Returns a string representation for the year of publication / creation
         * of <i>this</i> reference. If the {@link #getDatePublished() datePublished}
-        * of this reference contains more date information then (starting) year
+        * of this reference contains more date information than (starting) year
         * only the year is returned.
         */
        @Override
@@ -869,6 +896,31 @@ public class Reference
                }
        }
 
+    /**
+     * Returns a sortable string of the datePublished.start attribute.<BR>
+     * If datePublished is null in-references are called recursively.
+     * Only structured publication data is considered, no freetext or
+     * verbatim date.
+     */
+    @Transient
+    public String getSortableDateString(){
+        VerbatimTimePeriod datePublished = this.getDatePublished();
+        if (datePublished != null ){
+            Partial partial = getDatePublished().getStart();
+            if (partial == null) {
+                partial = getDatePublished().getEnd();
+            }
+            if (partial != null ) {
+                return TimePeriodPartialFormatter.INSTANCE().printSortableDateString(partial);
+            }
+        }
+        if (this.inReference != null){
+            return this.inReference.getSortableDateString();
+        }else {
+            return "zzzz-zz-zz";
+        }
+    }
+
        /**
      * Convenience method that returns a string representation for the publication date / creation
      * of <i>this</i> reference. The string is obtained by
@@ -1182,6 +1234,14 @@ public class Reference
     public boolean isPrintedUnit() {
         return this.getType().isPrintedUnit();
     }
+    /**
+     * @return <code>true</code> if the type of this reference
+     *         supports the {@link IDynamicReference} interface. Currently these are
+     *         webpages, databases and maps.
+     */
+    public boolean isDynamic() {
+        return this.getType().isDynamic();
+    }
 
 //*************************** CACHE STRATEGIES ******************************/