Project

General

Profile

Download (6.59 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.model.common;
10

    
11
import java.util.Calendar;
12
import java.util.Date;
13

    
14
import javax.persistence.Embeddable;
15
import javax.persistence.Transient;
16
import javax.xml.bind.annotation.XmlAccessType;
17
import javax.xml.bind.annotation.XmlAccessorType;
18
import javax.xml.bind.annotation.XmlElement;
19
import javax.xml.bind.annotation.XmlRootElement;
20
import javax.xml.bind.annotation.XmlType;
21

    
22
import org.apache.log4j.Logger;
23
import org.joda.time.Partial;
24
import org.joda.time.ReadableInstant;
25

    
26
import eu.etaxonomy.cdm.format.common.VerbatimTimePeriodFormatter;
27

    
28
/**
29
 * @author a.mueller
30
 * @since 08.05.2018
31
 *
32
 */
33
@XmlAccessorType(XmlAccessType.FIELD)
34
@XmlType(name = "VerbatimTimePeriod", propOrder = {
35
    "verbatimDate"
36
})
37
@XmlRootElement(name = "VerbatimTimePeriod")
38
@Embeddable
39
public class VerbatimTimePeriod extends TimePeriod {
40

    
41
    private static final long serialVersionUID = -6543644293635460526L;
42
    @SuppressWarnings("unused")
43
    private static final Logger logger = Logger.getLogger(VerbatimTimePeriod.class);
44

    
45
    private static VerbatimTimePeriodFormatter formatter = VerbatimTimePeriodFormatter.NewDefaultInstance();
46

    
47
    @XmlElement(name = "FreeText")
48
    private String verbatimDate;
49

    
50
 // ********************** FACTORY METHODS **************************/
51

    
52
     public static final VerbatimTimePeriod NewVerbatimInstance(){
53
         return new VerbatimTimePeriod();
54
     }
55

    
56
     /**
57
      * Factory method a date representing the current date and time
58
      */
59
     public static final VerbatimTimePeriod NewVerbatimNowInstance(){
60
         return NewVerbatimInstance(Calendar.getInstance());
61
     }
62

    
63
     public static final VerbatimTimePeriod NewVerbatimInstance(Partial startDate){
64
         return new VerbatimTimePeriod(startDate, null, null);
65
     }
66

    
67
     public static final VerbatimTimePeriod NewVerbatimInstance(Partial startDate, Partial endDate){
68
         return new VerbatimTimePeriod(startDate, endDate, null);
69
     }
70

    
71
     public static final VerbatimTimePeriod NewVerbatimInstance(Partial startDate, Partial endDate, String verbatimDate){
72
         return new VerbatimTimePeriod(startDate, endDate, verbatimDate);
73
     }
74

    
75
     public static final VerbatimTimePeriod NewVerbatimInstance(Integer year){
76
         return NewVerbatimInstance(year, (Integer)null);
77
     }
78

    
79
     public static final VerbatimTimePeriod NewVerbatimInstance(Integer startYear, Integer endYear){
80
         return new VerbatimTimePeriod(yearToPartial(startYear), yearToPartial(endYear), null);
81
     }
82

    
83
     /**
84
      * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
85
      * @return
86
      */
87
     public static final VerbatimTimePeriod NewVerbatimInstance(Calendar startCalendar){
88
         return NewVerbatimInstance(startCalendar, null);
89
     }
90

    
91
     /**
92
      * Factory method to create a TimePeriod from a <code>ReadableInstant</code>(e.g. <code>DateTime</code>).
93
      * The <code>ReadableInstant</code> is stored as the starting instant.
94
      * @return
95
      */
96
     public static final VerbatimTimePeriod NewVerbatimInstance(ReadableInstant readableInstant){
97
         return NewVerbatimInstance(readableInstant, null);
98
     }
99

    
100
     /**
101
      * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
102
      * @return
103
      */
104
     public static final VerbatimTimePeriod NewVerbatimInstance(Calendar startCalendar, Calendar endCalendar){
105
         return new VerbatimTimePeriod(calendarToPartial(startCalendar), calendarToPartial(endCalendar), null);
106
     }
107

    
108
     /**
109
      * Factory method to create a TimePeriod from a starting and an ending <code>Date</code>
110
      * @return VerbatimTimePeriod
111
      */
112
     public static final VerbatimTimePeriod NewVerbatimInstance(Date startDate, Date endDate){
113
         return NewVerbatimInstance(dateToPartial(startDate), dateToPartial(endDate));
114
     }
115

    
116
     /**
117
      * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
118
      * @return
119
      */
120
     public static final VerbatimTimePeriod NewVerbatimInstance(ReadableInstant startInstant, ReadableInstant endInstant){
121
         return new VerbatimTimePeriod(readableInstantToPartial(startInstant), readableInstantToPartial(endInstant), null);
122
     }
123

    
124
//*********************** CONSTRUCTOR *********************************/
125

    
126
    protected VerbatimTimePeriod() {
127
        super();
128
    }
129
    private VerbatimTimePeriod(Partial startDate, Partial endDate, String verbatimDate) {
130
        super(startDate, endDate, null);
131
        this.verbatimDate = verbatimDate;
132
    }
133

    
134
// ***************************** GETTER /SETTER *********************/
135

    
136
    public String getVerbatimDate() {
137
        return verbatimDate;
138
    }
139
    public void setVerbatimDate(String verbatimDate) {
140
        this.verbatimDate = verbatimDate;
141
    }
142

    
143
// ************************************ TRANSIENT **************************/
144

    
145
    @Override
146
    /**
147
     * True, if there is no start date, no end date, no freetext representation
148
     * and no verbatimDate.
149
     * @return
150
     */
151
    @Transient
152
    public boolean isEmpty(){
153
        boolean result = super.isEmpty();
154
        return result && isBlank(this.getVerbatimDate());
155
    }
156

    
157
//*********** EQUALS **********************************/
158

    
159
    //we want VerbatimTimePeriod and TimePeriod to be equals
160
    //if both are equal in the TimePeriod part and if
161
    //VerbatimTimePeriod has no verbatimDate defined
162

    
163
    @Override
164
    public boolean equals(Object obj) {
165
        return super.equals(obj);
166
    }
167

    
168

    
169
    @Override
170
    public int hashCode() {
171
        int hashCode = super.hashCode();
172
        hashCode += (verbatimDate == null)? 0: verbatimDate.hashCode();
173
        return hashCode;
174
    }
175

    
176

    
177
//**************************** to String ****************************************
178

    
179
    /**
180
     * Returns the {@link #getFreeText()} value if free text is not <code>null</code>.
181
     * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned.
182
     *
183
     * @see java.lang.Object#toString()
184
     */
185
      @Override
186
      public String toString(){
187
         return formatter.format(this);
188
    }
189

    
190
//*********** CLONE **********************************/
191

    
192
    @Override
193
    public VerbatimTimePeriod clone()  {
194
            VerbatimTimePeriod result = (VerbatimTimePeriod)super.clone();
195
            result.setVerbatimDate(this.verbatimDate);
196
            return result;
197
    }
198
}
(53-53/56)