Project

General

Profile

Download (8.65 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.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
24
import org.joda.time.Partial;
25
import org.joda.time.ReadableInstant;
26

    
27
import eu.etaxonomy.cdm.common.CdmUtils;
28

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

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

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

    
49

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

    
52
     /**
53
      * Factory method
54
      * @return
55
      */
56
     public static final VerbatimTimePeriod NewVerbatimInstance(){
57
         return new VerbatimTimePeriod();
58
     }
59

    
60

    
61
     /**
62
      * Factory method
63
      * @return
64
      */
65
     public static final VerbatimTimePeriod NewVerbatimInstance(Partial startDate){
66
         return new VerbatimTimePeriod(startDate);
67
     }
68

    
69

    
70
     /**
71
      * Factory method
72
      * @return
73
      */
74
     public static final VerbatimTimePeriod NewVerbatimInstance(Partial startDate, Partial endDate){
75
         return new VerbatimTimePeriod(startDate, endDate);
76
     }
77

    
78

    
79
     /**
80
      * Factory method
81
      * @return
82
      */
83
     public static final VerbatimTimePeriod NewVerbatimInstance(Integer year){
84
         Integer endYear = null;
85
         return NewVerbatimInstance(year, endYear);
86
     }
87

    
88
     /**
89
      * Factory method
90
      * @return
91
      */
92
     public static final VerbatimTimePeriod NewVerbatimInstance(Integer startYear, Integer endYear){
93
         Partial startDate = null;
94
         Partial endDate = null;
95
         if (startYear != null){
96
             startDate = new Partial().with(YEAR_TYPE, startYear);
97
         }
98
         if (endYear != null){
99
             endDate = new Partial().with(YEAR_TYPE, endYear);
100
         }
101
         return new VerbatimTimePeriod(startDate, endDate);
102
     }
103

    
104

    
105

    
106
     /**
107
      * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
108
      * @return
109
      */
110
     public static final VerbatimTimePeriod NewVerbatimInstance(Calendar startCalendar){
111
         return NewVerbatimInstance(startCalendar, null);
112
     }
113

    
114
     /**
115
      * Factory method to create a TimePeriod from a <code>ReadableInstant</code>(e.g. <code>DateTime</code>).
116
      * The <code>ReadableInstant</code> is stored as the starting instant.
117
      * @return
118
      */
119
     public static final VerbatimTimePeriod NewVerbatimInstance(ReadableInstant readableInstant){
120
         return NewVerbatimInstance(readableInstant, null);
121
     }
122

    
123
     /**
124
      * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
125
      * @return
126
      */
127
     public static final VerbatimTimePeriod NewVerbatimInstance(Calendar startCalendar, Calendar endCalendar){
128
         Partial startDate = null;
129
         Partial endDate = null;
130
         if (startCalendar != null){
131
             startDate = calendarToPartial(startCalendar);
132
         }
133
         if (endCalendar != null){
134
             endDate = calendarToPartial(endCalendar);
135
         }
136
         return new VerbatimTimePeriod(startDate, endDate);
137
     }
138

    
139
     /**
140
      * Factory method to create a TimePeriod from a starting and an ending <code>Date</code>
141
      * @return TimePeriod
142
      */
143
     public static final VerbatimTimePeriod NewVerbatimInstance(Date startDate, Date endDate){
144
         //TODO conversion untested, implemented according to http://www.roseindia.net/java/java-conversion/datetocalender.shtml
145
         Calendar calStart = null;
146
         Calendar calEnd = null;
147
         if (startDate != null){
148
             calStart = Calendar.getInstance();
149
             calStart.setTime(startDate);
150
         }
151
         if (endDate != null){
152
             calEnd = Calendar.getInstance();
153
             calEnd.setTime(endDate);
154
         }
155
         return NewVerbatimInstance(calStart, calEnd);
156
     }
157

    
158

    
159
     /**
160
      * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
161
      * @return
162
      */
163
     public static final VerbatimTimePeriod NewVerbatimInstance(ReadableInstant startInstant, ReadableInstant endInstant){
164
         Partial startDate = null;
165
         Partial endDate = null;
166
         if (startInstant != null){
167
             startDate = readableInstantToPartial(startInstant);
168
         }
169
         if (endInstant != null){
170
             endDate = readableInstantToPartial(endInstant);
171
         }
172
         return new VerbatimTimePeriod(startDate, endDate);
173
     }
174

    
175

    
176
//*********************** CONSTRUCTOR *********************************/
177

    
178
    /**
179
     * Constructor
180
     */
181
    protected VerbatimTimePeriod() {
182
        super();
183
    }
184
    public VerbatimTimePeriod(Partial startDate) {
185
        super(startDate);
186
    }
187
    public VerbatimTimePeriod(Partial startDate, Partial endDate) {
188
        super(startDate, endDate);
189
    }
190
    public VerbatimTimePeriod(Partial startDate, Partial endDate, String verbatimDate) {
191
        super(startDate, endDate);
192
        this.verbatimDate = verbatimDate;
193
    }
194

    
195
// ***************************** GETTER /SETTER *********************/
196

    
197
    public String getVerbatimDate() {
198
        return verbatimDate;
199
    }
200
    public void setVerbatimDate(String verbatimDate) {
201
        this.verbatimDate = verbatimDate;
202
    }
203

    
204
//****************** CONVERTERS ******************/
205

    
206
    public static TimePeriod fromVerbatim(VerbatimTimePeriod verbatimTimePeriod){
207
        if (verbatimTimePeriod == null){
208
            return null;
209
        }
210
        TimePeriod result = TimePeriod.NewInstance();
211
        copyCloned(verbatimTimePeriod, result);
212
        if (StringUtils.isNotBlank(verbatimTimePeriod.verbatimDate) &&
213
                StringUtils.isBlank(result.getFreeText())){
214
            result.setFreeText(verbatimTimePeriod.toString());
215
        }
216
        return result;
217
    }
218
    public static VerbatimTimePeriod toVerbatim(TimePeriod timePeriod){
219
        if (timePeriod == null){
220
            return null;
221
        }
222
        VerbatimTimePeriod result = VerbatimTimePeriod.NewVerbatimInstance();
223
        copyCloned(timePeriod, result);
224
        return result;
225
    }
226

    
227

    
228

    
229
// ************************************ TRANSIENT **************************/
230

    
231
    @Override
232
    /**
233
     * True, if there is no start date and no end date and no freetext representation exists.
234
     * @return
235
     */
236
    @Transient
237
    public boolean isEmpty(){
238
        boolean result = super.isEmpty();
239
        return result && StringUtils.isBlank(this.getVerbatimDate());
240
    }
241

    
242
//*********** EQUALS **********************************/
243

    
244
    //we want VerbatimTimePeriod and TimePeriod to be equals
245
    //if both are equal in the TimePeriod part and if
246
    //VerbatimTimePeriod has no verbatimDate defined
247

    
248
    @Override
249
    public boolean equals(Object obj) {
250
        return super.equals(obj);
251
    }
252

    
253

    
254
    @Override
255
    public int hashCode() {
256
        int hashCode = super.hashCode();
257
        hashCode += (verbatimDate == null)? 0: verbatimDate.hashCode();
258
        return hashCode;
259
    }
260

    
261

    
262
//**************************** to String ****************************************
263

    
264
    /**
265
     * Returns the {@link #getFreeText()} value if free text is not <code>null</code>.
266
     * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned.
267
     *
268
     * @see java.lang.Object#toString()
269
     */
270
      @Override
271
      public String toString(){
272
         String result = super.toString();
273
         if (StringUtils.isNotBlank(this.verbatimDate)){
274
             result = CdmUtils.concat(" ", result, "[\""+this.verbatimDate+"\"]");
275
         }
276
         return result;
277
    }
278

    
279
//*********** CLONE **********************************/
280

    
281
    @Override
282
    public Object clone()  {
283
            VerbatimTimePeriod result = (VerbatimTimePeriod)super.clone();
284
            result.setVerbatimDate(this.verbatimDate);
285
            return result;
286
    }
287
}
(75-75/80)