merge hibernate4 migration branch into trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / TimePeriod.java
index 602a85700df0b8ac9a8b8f0012b49979ab15876b..63b2950d6fd69443cbf10f601289d012521896d7 100644 (file)
@@ -1,8 +1,8 @@
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * 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.
 */
@@ -26,9 +26,9 @@ import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
-import org.apache.commons.lang.time.DateUtils;
 import org.apache.log4j.Logger;
 import org.hibernate.annotations.Type;
+import org.hibernate.search.annotations.Analyze;
 import org.hibernate.search.annotations.Field;
 import org.hibernate.search.annotations.FieldBridge;
 import org.joda.time.DateTime;
@@ -62,26 +62,26 @@ public class TimePeriod implements Cloneable, Serializable {
        public static final DateTimeFieldType MONTH_TYPE = DateTimeFieldType.monthOfYear();
        public static final DateTimeFieldType YEAR_TYPE = DateTimeFieldType.year();
        public static final DateTimeFieldType DAY_TYPE = DateTimeFieldType.dayOfMonth();
-       
+
        @XmlElement(name = "Start")
        @XmlJavaTypeAdapter(value = PartialAdapter.class)
        @Type(type="partialUserType")
-       @Field(index = org.hibernate.search.annotations.Index.UN_TOKENIZED)
+       @Field(analyze = Analyze.NO)
        @FieldBridge(impl = PartialBridge.class)
        private Partial start;
-       
+
        @XmlElement(name = "End")
        @XmlJavaTypeAdapter(value = PartialAdapter.class)
        @Type(type="partialUserType")
-       @Field(index = org.hibernate.search.annotations.Index.UN_TOKENIZED)
+       @Field(analyze = Analyze.NO)
        @FieldBridge(impl = PartialBridge.class)
        private Partial end;
 
-       
+
        @XmlElement(name = "FreeText")
        private String freeText;
-       
-       
+
+
        /**
         * Factory method
         * @return
@@ -89,8 +89,8 @@ public class TimePeriod implements Cloneable, Serializable {
        public static TimePeriod NewInstance(){
                return new TimePeriod();
        }
-       
-       
+
+
        /**
         * Factory method
         * @return
@@ -98,8 +98,8 @@ public class TimePeriod implements Cloneable, Serializable {
        public static TimePeriod NewInstance(Partial startDate){
                return new TimePeriod(startDate);
        }
-       
-       
+
+
        /**
         * Factory method
         * @return
@@ -107,8 +107,8 @@ public class TimePeriod implements Cloneable, Serializable {
        public static TimePeriod NewInstance(Partial startDate, Partial endDate){
                return new TimePeriod(startDate, endDate);
        }
-       
-       
+
+
        /**
         * Factory method
         * @return
@@ -117,7 +117,7 @@ public class TimePeriod implements Cloneable, Serializable {
                Integer endYear = null;
                return NewInstance(year, endYear);
        }
-       
+
        /**
         * Factory method
         * @return
@@ -134,10 +134,10 @@ public class TimePeriod implements Cloneable, Serializable {
                return new TimePeriod(startDate, endDate);
        }
 
-       
-       
+
+
        /**
-        * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.   
+        * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
         * @return
         */
        public static TimePeriod NewInstance(Calendar startCalendar){
@@ -146,15 +146,15 @@ public class TimePeriod implements Cloneable, Serializable {
 
        /**
         * Factory method to create a TimePeriod from a <code>ReadableInstant</code>(e.g. <code>DateTime</code>).
-        * The <code>ReadableInstant</code> is stored as the starting instant.   
+        * The <code>ReadableInstant</code> is stored as the starting instant.
         * @return
         */
        public static TimePeriod NewInstance(ReadableInstant readableInstant){
                return NewInstance(readableInstant, null);
        }
-       
+
        /**
-        * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>   
+        * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
         * @return
         */
        public static TimePeriod NewInstance(Calendar startCalendar, Calendar endCalendar){
@@ -168,9 +168,9 @@ public class TimePeriod implements Cloneable, Serializable {
                }
                return new TimePeriod(startDate, endDate);
        }
-       
+
        /**
-        * Factory method to create a TimePeriod from a starting and an ending <code>Date</code>   
+        * Factory method to create a TimePeriod from a starting and an ending <code>Date</code>
         * @return TimePeriod
         */
        public static TimePeriod NewInstance(Date startDate, Date endDate){
@@ -188,9 +188,9 @@ public class TimePeriod implements Cloneable, Serializable {
                return NewInstance(calStart, calEnd);
        }
 
-       
+
        /**
-        * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)   
+        * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
         * @return
         */
        public static TimePeriod NewInstance(ReadableInstant startInstant, ReadableInstant endInstant){
@@ -205,7 +205,7 @@ public class TimePeriod implements Cloneable, Serializable {
                return new TimePeriod(startDate, endDate);
        }
 
-       
+
        /**
         * Transforms a <code>Calendar</code> into a <code>Partial</code>
         * @param calendar
@@ -216,7 +216,7 @@ public class TimePeriod implements Cloneable, Serializable {
                Partial partial = new Partial(ld);
                return partial;
        }
-       
+
        /**
         * Transforms a <code>Calendar</code> into a <code>Partial</code>
         * @param calendar
@@ -228,7 +228,7 @@ public class TimePeriod implements Cloneable, Serializable {
                Partial partial = new Partial(ld);
                return partial;
        }
-       
+
        /**
         * Constructor
         */
@@ -257,7 +257,7 @@ public class TimePeriod implements Cloneable, Serializable {
                        return false;
                }
        }
-       
+
        /**
         * True, if there is no start date and no end date and no freetext representation exists.
         * @return
@@ -270,24 +270,24 @@ public class TimePeriod implements Cloneable, Serializable {
                        return false;
                }
        }
-       
-       
+
+
        public Partial getStart() {
                return start;
        }
-       
+
        public void setStart(Partial start) {
                this.start = start;
        }
-       
+
        public Partial getEnd() {
                return end;
        }
-       
+
        public void setEnd(Partial end) {
                this.end = end;
        }
-       
+
        /**
         * For time periods that need to store more information than the one
         * that can be stored in <code>start</code> and <code>end</code>.
@@ -325,12 +325,12 @@ public class TimePeriod implements Cloneable, Serializable {
                }
                return result;
        }
-       
+
        @Transient
        public Integer getStartYear(){
                return getPartialValue(start, YEAR_TYPE);
        }
-       
+
        @Transient
        public Integer getStartMonth(){
                return getPartialValue(start, MONTH_TYPE);
@@ -355,20 +355,20 @@ public class TimePeriod implements Cloneable, Serializable {
        public Integer getEndDay(){
                return getPartialValue(end, DAY_TYPE);
        }
-       
+
        public static Integer getPartialValue(Partial partial, DateTimeFieldType type){
                if (partial == null || ! partial.isSupported(type)){
                        return null;
                }else{
                        return partial.get(type);
                }
-               
+
        }
-       
+
        public TimePeriod setStartYear(Integer year){
                return setStartField(year, YEAR_TYPE);
        }
-       
+
        public TimePeriod setStartMonth(Integer month) throws IndexOutOfBoundsException{
                return setStartField(month, MONTH_TYPE);
        }
@@ -376,7 +376,7 @@ public class TimePeriod implements Cloneable, Serializable {
        public TimePeriod setStartDay(Integer day) throws IndexOutOfBoundsException{
                return setStartField(day, DAY_TYPE);
        }
-       
+
        public TimePeriod setEndYear(Integer year){
                return setEndField(year, YEAR_TYPE);
        }
@@ -388,8 +388,8 @@ public class TimePeriod implements Cloneable, Serializable {
        public TimePeriod setEndDay(Integer day) throws IndexOutOfBoundsException{
                return setEndField(day, DAY_TYPE);
        }
-       
-       public static Partial setPartialField(Partial partial, Integer value, DateTimeFieldType type) 
+
+       public static Partial setPartialField(Partial partial, Integer value, DateTimeFieldType type)
                        throws IndexOutOfBoundsException{
                if (partial == null){
                        partial = new Partial();
@@ -401,8 +401,8 @@ public class TimePeriod implements Cloneable, Serializable {
                        return partial.with(type, value);
                }
        }
-       
-       private TimePeriod setStartField(Integer value, DateTimeFieldType type) 
+
+       private TimePeriod setStartField(Integer value, DateTimeFieldType type)
                        throws IndexOutOfBoundsException{
                start = setPartialField(start, value, type);
                return this;
@@ -413,7 +413,7 @@ public class TimePeriod implements Cloneable, Serializable {
                end = setPartialField(end, value, type);
                return this;
        }
-       
+
        /**
         * Throws an IndexOutOfBoundsException if the value does not have a valid value
         * (e.g. month > 12, month < 1, day > 31, etc.)
@@ -437,7 +437,7 @@ public class TimePeriod implements Cloneable, Serializable {
                                if (month == 2){
                                        max = 29;
                                }else if (month == 4 ||month == 6 ||month == 9 ||month == 11){
-                                       max = 30; 
+                                       max = 30;
                                }
                        }
                }
@@ -445,20 +445,20 @@ public class TimePeriod implements Cloneable, Serializable {
                        throw new IndexOutOfBoundsException("Value must be between 1 and " +  max);
                }
        }
-       
+
        private void initStart(){
                if (start == null){
                        start = new Partial();
                }
        }
-       
+
        private void initEnd(){
                if (end == null){
                        end = new Partial();
                }
        }
-       
-       
+
+
        //patter for first year in string;
        private static final Pattern firstYearPattern =  Pattern.compile("\\d{4}");
        //case "1806"[1807];
@@ -470,25 +470,25 @@ public class TimePeriod implements Cloneable, Serializable {
        private static final String strDotDate = "[0-3]?\\d\\.[01]?\\d\\.\\d{4,4}";
        private static final String strDotDatePeriodPattern = String.format("%s(\\s*-\\s*%s?)?", strDotDate, strDotDate);
        private static final Pattern dotDatePattern =  Pattern.compile(strDotDatePeriodPattern);
-       
-       
+
+
        public static TimePeriod parseString(TimePeriod timePeriod, String periodString){
                //TODO move to parser class
                //TODO until now only quick and dirty (and partly wrong)
                TimePeriod result = timePeriod;
-               
+
                if(timePeriod == null){
                        return timePeriod;
                }
-               
+
                if (periodString == null){
                        return result;
                }
                periodString = periodString.trim();
-               
+
                result.setFreeText(null);
                Date date;
-               
+
                //case "1806"[1807];
                if (uncorrectYearPatter.matcher(periodString).matches()){
                        result.setFreeText(periodString);
@@ -511,7 +511,7 @@ public class TimePeriod implements Cloneable, Serializable {
                        parseDotDatePattern(periodString, result);
                }else if (standardPattern.matcher(periodString).matches()){
                        parseStandardPattern(periodString, result);
-//TODO first check ambiguity of parser results e.g. for 7/12/11 
+//TODO first check ambiguity of parser results e.g. for 7/12/11
 //             }else if (isDateString(periodString)){
 //                     String[] startEnd = makeStartEnd(periodString);
 //                     String start = startEnd[0];
@@ -522,7 +522,7 @@ public class TimePeriod implements Cloneable, Serializable {
 //                             ;
 //                             result.setEnd(endDateTime.toLocalDate());
 //                     }
-                       
+
                }else{
                        result.setFreeText(periodString);
                }
@@ -581,7 +581,7 @@ public class TimePeriod implements Cloneable, Serializable {
                String[] dates = periodString.split("-");
                Partial dtStart = null;
                Partial dtEnd = null;
-               
+
                if (dates.length > 2 || dates.length <= 0){
                        logger.warn("More than 1 '-' in period String: " + periodString);
                        result.setFreeText(periodString);
@@ -591,12 +591,12 @@ public class TimePeriod implements Cloneable, Serializable {
                                if (! CdmUtils.isEmpty(dates[0])){
                                        dtStart = parseSingleDotDate(dates[0].trim());
                                }
-                               
+
                                //end
                                if (dates.length >= 2 && ! CdmUtils.isEmpty(dates[1])){
                                        dtEnd = parseSingleDotDate(dates[1].trim());
                                }
-                               
+
                                result.setStart(dtStart);
                                result.setEnd(dtEnd);
                        } catch (IllegalArgumentException e) {
@@ -605,8 +605,8 @@ public class TimePeriod implements Cloneable, Serializable {
                        }
                }
        }
-       
-       
+
+
        /**
         * @param periodString
         * @param result
@@ -616,7 +616,7 @@ public class TimePeriod implements Cloneable, Serializable {
                String[] years = periodString.split("-");
                Partial dtStart = null;
                Partial dtEnd = null;
-               
+
                if (years.length > 2 || years.length <= 0){
                        logger.warn("More than 1 '-' in period String: " + periodString);
                }else {
@@ -625,7 +625,7 @@ public class TimePeriod implements Cloneable, Serializable {
                                if (! CdmUtils.isEmpty(years[0])){
                                        dtStart = parseSingleDate(years[0].trim());
                                }
-                               
+
                                //end
                                if (years.length >= 2 && ! CdmUtils.isEmpty(years[1])){
                                        years[1] = years[1].trim();
@@ -634,7 +634,7 @@ public class TimePeriod implements Cloneable, Serializable {
                                        }
                                        dtEnd = parseSingleDate(years[1]);
                                }
-                               
+
                                result.setStart(dtStart);
                                result.setEnd(dtEnd);
                        } catch (IllegalArgumentException e) {
@@ -643,13 +643,13 @@ public class TimePeriod implements Cloneable, Serializable {
                        }
                }
        }
-       
+
        public static TimePeriod parseString(String strPeriod) {
                TimePeriod timePeriod = TimePeriod.NewInstance();
                return parseString(timePeriod, strPeriod);
        }
-       
-       
+
+
        protected static Partial parseSingleDate(String singleDateString) throws IllegalArgumentException{
                //FIXME until now only quick and dirty and incomplete
                Partial partial =  new Partial();
@@ -675,7 +675,7 @@ public class TimePeriod implements Cloneable, Serializable {
                return partial;
 
        }
-       
+
        protected static Partial parseSingleDotDate(String singleDateString) throws IllegalArgumentException{
                Partial partial =  new Partial();
                singleDateString = singleDateString.trim();
@@ -687,8 +687,8 @@ public class TimePeriod implements Cloneable, Serializable {
                String strYear = split[split.length-1];
                String strMonth = length >= 2? split[split.length-2]: null;
                String strDay = length >= 3? split[split.length-3]: null;
-               
-               
+
+
                try {
                        Integer year = Integer.valueOf(strYear.trim());
                        Integer month = Integer.valueOf(strMonth.trim());
@@ -714,20 +714,21 @@ public class TimePeriod implements Cloneable, Serializable {
                return partial;
 
        }
-       
-       
-       
+
+
+
        private class TimePeriodPartialFormatter extends DateTimeFormatter{
                private TimePeriodPartialFormatter(){
                        super(null, null);
                }
-               public String print(ReadablePartial partial){
+               @Override
+        public String print(ReadablePartial partial){
                        //TODO
                        String result = "";
                        String year = (partial.isSupported(YEAR_TYPE))? String.valueOf(partial.get(YEAR_TYPE)):null;
                        String month = (partial.isSupported(MONTH_TYPE))? String.valueOf(partial.get(MONTH_TYPE)):null;;
                        String day = (partial.isSupported(DAY_TYPE))? String.valueOf(partial.get(DAY_TYPE)):null;;
-                       
+
                        if (month !=null){
                                if (year == null){
                                        year = "xxxx";
@@ -744,21 +745,22 @@ public class TimePeriod implements Cloneable, Serializable {
                        result = (day != null)? day + "." : "";
                        result += (month != null)? month + "." : "";
                        result += (year != null)? year : "";
-                       
+
                        return result;
                }
-               
+
        }
-       
-//**************************** to String ****************************************      
-       
-       /** 
+
+//**************************** to String ****************************************
+
+       /**
         * Returns the {@link #getFreeText()} value if free text is not <code>null</code>.
-        * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned. 
-        * 
+        * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned.
+        *
         * @see java.lang.Object#toString()
         */
-       public String toString(){
+       @Override
+    public String toString(){
                String result = null;
                DateTimeFormatter formatter = new TimePeriodPartialFormatter();
                if ( CdmUtils.isNotEmpty(this.getFreeText())){
@@ -770,9 +772,9 @@ public class TimePeriod implements Cloneable, Serializable {
                }
                return result;
        }
-       
-//*********** EQUALS **********************************/       
-       
+
+//*********** EQUALS **********************************/
+
 
        /* (non-Javadoc)
         * @see java.lang.Object#equals(java.lang.Object)
@@ -786,7 +788,7 @@ public class TimePeriod implements Cloneable, Serializable {
                        return false;
                }
                TimePeriod that = (TimePeriod)obj;
-               
+
                if (! CdmUtils.nullSafeEqual(this.start, that.start)){
                        return false;
                }
@@ -798,23 +800,23 @@ public class TimePeriod implements Cloneable, Serializable {
                }
                return true;
        }
-       
+
        /* (non-Javadoc)
         * @see java.lang.Object#hashCode()
         */
        @Override
        public int hashCode() {
                int hashCode = 7;
-               hashCode = 29*hashCode +  
-                                       (start== null? 33: start.hashCode()) + 
-                                       (end== null? 39: end.hashCode()) + 
-                                       (freeText== null? 41: freeText.hashCode()); 
+               hashCode = 29*hashCode +
+                                       (start== null? 33: start.hashCode()) +
+                                       (end== null? 39: end.hashCode()) +
+                                       (freeText== null? 41: freeText.hashCode());
                return super.hashCode();
-       }       
-       
-       
-//*********** CLONE **********************************/        
-       
+       }
+
+
+//*********** CLONE **********************************/
+
 
        /* (non-Javadoc)
         * @see java.lang.Object#clone()
@@ -824,7 +826,7 @@ public class TimePeriod implements Cloneable, Serializable {
                try {
                        TimePeriod result = (TimePeriod)super.clone();
                        result.setStart(this.start);   //DateTime is immutable
-                       result.setEnd(this.end);        
+                       result.setEnd(this.end);
                        result.setFreeText(this.freeText);
                        return result;
                } catch (CloneNotSupportedException e) {
@@ -833,5 +835,5 @@ public class TimePeriod implements Cloneable, Serializable {
                }
        }
 
-       
+
 }
\ No newline at end of file