1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.model.common;
|
11
|
|
12
|
import java.util.Calendar;
|
13
|
|
14
|
import javax.persistence.Embeddable;
|
15
|
import javax.persistence.Temporal;
|
16
|
import javax.persistence.TemporalType;
|
17
|
import javax.persistence.Transient;
|
18
|
import javax.xml.bind.annotation.XmlAccessType;
|
19
|
import javax.xml.bind.annotation.XmlAccessorType;
|
20
|
import javax.xml.bind.annotation.XmlElement;
|
21
|
import javax.xml.bind.annotation.XmlRootElement;
|
22
|
import javax.xml.bind.annotation.XmlType;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
import org.hibernate.annotations.Type;
|
26
|
import org.joda.time.DateTime;
|
27
|
import org.joda.time.DateTimeFieldType;
|
28
|
import org.joda.time.LocalDate;
|
29
|
import org.joda.time.Months;
|
30
|
import org.joda.time.Partial;
|
31
|
import org.joda.time.ReadableInstant;
|
32
|
|
33
|
/**
|
34
|
* @author m.doering
|
35
|
* @version 1.0
|
36
|
* @created 08-Nov-2007 13:07:00
|
37
|
*/
|
38
|
@XmlAccessorType(XmlAccessType.FIELD)
|
39
|
@XmlType(name = "TimePeriod", propOrder = {
|
40
|
"start",
|
41
|
"end"
|
42
|
})
|
43
|
@XmlRootElement(name = "TimePeriod")
|
44
|
@Embeddable
|
45
|
public class TimePeriod implements Cloneable {
|
46
|
|
47
|
private static final Logger logger = Logger.getLogger(TimePeriod.class);
|
48
|
|
49
|
@XmlElement(name = "Start")
|
50
|
private Partial start;
|
51
|
|
52
|
@XmlElement(name = "End")
|
53
|
private Partial end;
|
54
|
|
55
|
|
56
|
/**
|
57
|
* Factory method
|
58
|
* @return
|
59
|
*/
|
60
|
public static TimePeriod NewInstance(){
|
61
|
return new TimePeriod();
|
62
|
}
|
63
|
|
64
|
|
65
|
/**
|
66
|
* Factory method
|
67
|
* @return
|
68
|
*/
|
69
|
public static TimePeriod NewInstance(Partial startDate){
|
70
|
return new TimePeriod(startDate);
|
71
|
}
|
72
|
|
73
|
|
74
|
/**
|
75
|
* Factory method
|
76
|
* @return
|
77
|
*/
|
78
|
public static TimePeriod NewInstance(Partial startDate, Partial endDate){
|
79
|
return new TimePeriod(startDate, endDate);
|
80
|
}
|
81
|
|
82
|
|
83
|
/**
|
84
|
* Factory method
|
85
|
* @return
|
86
|
*/
|
87
|
public static TimePeriod NewInstance(Integer year){
|
88
|
Integer endYear = null;
|
89
|
return NewInstance(year, endYear);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Factory method
|
94
|
* @return
|
95
|
*/
|
96
|
public static TimePeriod NewInstance(Integer startYear, Integer endYear){
|
97
|
Partial startDate = null;
|
98
|
Partial endDate = null;
|
99
|
if (startYear != null){
|
100
|
startDate = new Partial().with(DateTimeFieldType.year(), startYear);
|
101
|
}
|
102
|
if (endYear != null){
|
103
|
endDate = new Partial().with(DateTimeFieldType.year(), endYear);
|
104
|
}
|
105
|
return new TimePeriod(startDate, endDate);
|
106
|
}
|
107
|
|
108
|
|
109
|
|
110
|
/**
|
111
|
* Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
|
112
|
* @return
|
113
|
*/
|
114
|
public static TimePeriod NewInstance(Calendar startCalendar){
|
115
|
return NewInstance(startCalendar, null);
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Factory method to create a TimePeriod from a <code>ReadableInstant</code>(e.g. <code>DateTime</code>).
|
120
|
* The <code>ReadableInstant</code> is stored as the starting instant.
|
121
|
* @return
|
122
|
*/
|
123
|
public static TimePeriod NewInstance(ReadableInstant readableInstant){
|
124
|
return NewInstance(readableInstant, null);
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
|
129
|
* @return
|
130
|
*/
|
131
|
public static TimePeriod NewInstance(Calendar startCalendar, Calendar endCalendar){
|
132
|
Partial startDate = null;
|
133
|
Partial endDate = null;
|
134
|
if (startCalendar != null){
|
135
|
startDate = calendarToPartial(startCalendar);
|
136
|
}
|
137
|
if (endCalendar != null){
|
138
|
endDate = calendarToPartial(endCalendar);
|
139
|
}
|
140
|
return new TimePeriod(startDate, endDate);
|
141
|
}
|
142
|
|
143
|
|
144
|
/**
|
145
|
* Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
|
146
|
* @return
|
147
|
*/
|
148
|
public static TimePeriod NewInstance(ReadableInstant startInstant, ReadableInstant endInstant){
|
149
|
Partial startDate = null;
|
150
|
Partial endDate = null;
|
151
|
if (startInstant != null){
|
152
|
startDate = readableInstantToPartial(startInstant);
|
153
|
}
|
154
|
if (endInstant != null){
|
155
|
endDate = readableInstantToPartial(endInstant);
|
156
|
}
|
157
|
return new TimePeriod(startDate, endDate);
|
158
|
}
|
159
|
|
160
|
|
161
|
/**
|
162
|
* Transforms a <code>Calendar</code> into a <code>Partial</code>
|
163
|
* @param calendar
|
164
|
* @return
|
165
|
*/
|
166
|
public static Partial calendarToPartial(Calendar calendar){
|
167
|
LocalDate ld = new LocalDate(calendar);
|
168
|
Partial partial = new Partial(ld);
|
169
|
return partial;
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Transforms a <code>Calendar</code> into a <code>Partial</code>
|
174
|
* @param calendar
|
175
|
* @return
|
176
|
*/
|
177
|
public static Partial readableInstantToPartial(ReadableInstant readableInstant){
|
178
|
DateTime dt = readableInstant.toInstant().toDateTime();
|
179
|
LocalDate ld = dt.toLocalDate();
|
180
|
Partial partial = new Partial(ld);
|
181
|
return partial;
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* Constructor
|
186
|
*/
|
187
|
protected TimePeriod() {
|
188
|
super();
|
189
|
}
|
190
|
public TimePeriod(Partial startDate) {
|
191
|
start=startDate;
|
192
|
}
|
193
|
public TimePeriod(Partial startDate, Partial endDate) {
|
194
|
start=startDate;
|
195
|
end=endDate;
|
196
|
}
|
197
|
|
198
|
//@Temporal(TemporalType.TIMESTAMP)
|
199
|
@Type(type="partialUserType")
|
200
|
public Partial getStart() {
|
201
|
return start;
|
202
|
}
|
203
|
public void setStart(Partial start) {
|
204
|
this.start = start;
|
205
|
}
|
206
|
|
207
|
//@Temporal(TemporalType.TIMESTAMP)
|
208
|
@Type(type="partialUserType")
|
209
|
public Partial getEnd() {
|
210
|
return end;
|
211
|
}
|
212
|
public void setEnd(Partial end) {
|
213
|
this.end = end;
|
214
|
}
|
215
|
|
216
|
@Transient
|
217
|
public String getYear(){
|
218
|
String result = "";
|
219
|
if (getStartYear() != null){
|
220
|
result += String.valueOf(getStartYear());
|
221
|
if (getEndYear() != null){
|
222
|
result += "-" + String.valueOf(getEndYear());
|
223
|
}
|
224
|
}else{
|
225
|
if (getEndYear() != null){
|
226
|
result += String.valueOf(getEndYear());
|
227
|
}
|
228
|
}
|
229
|
return result;
|
230
|
}
|
231
|
|
232
|
@Transient
|
233
|
public Integer getStartYear(){
|
234
|
return getPartialValue(start, DateTimeFieldType.year());
|
235
|
}
|
236
|
|
237
|
@Transient
|
238
|
public Integer getStartMonth(){
|
239
|
return getPartialValue(start, DateTimeFieldType.monthOfYear());
|
240
|
}
|
241
|
|
242
|
@Transient
|
243
|
public Integer getStartDay(){
|
244
|
return getPartialValue(start, DateTimeFieldType.dayOfMonth());
|
245
|
}
|
246
|
|
247
|
@Transient
|
248
|
public Integer getEndYear(){
|
249
|
return getPartialValue(end, DateTimeFieldType.year());
|
250
|
}
|
251
|
|
252
|
@Transient
|
253
|
public Integer getEndMonth(){
|
254
|
return getPartialValue(end, DateTimeFieldType.monthOfYear());
|
255
|
}
|
256
|
|
257
|
@Transient
|
258
|
public Integer getEndDay(){
|
259
|
return getPartialValue(end, DateTimeFieldType.dayOfMonth());
|
260
|
}
|
261
|
|
262
|
|
263
|
@Transient
|
264
|
private Integer getPartialValue(Partial partial, DateTimeFieldType type){
|
265
|
if (partial == null || ! partial.isSupported(type)){
|
266
|
return null;
|
267
|
}else{
|
268
|
return partial.get(type);
|
269
|
}
|
270
|
|
271
|
}
|
272
|
|
273
|
public TimePeriod setStartYear(Integer year){
|
274
|
return setStartField(year, DateTimeFieldType.year());
|
275
|
}
|
276
|
|
277
|
public TimePeriod setStartMonth(Integer month) throws IndexOutOfBoundsException{
|
278
|
return setStartField(month, DateTimeFieldType.monthOfYear());
|
279
|
}
|
280
|
|
281
|
public TimePeriod setStartDay(Integer day) throws IndexOutOfBoundsException{
|
282
|
return setStartField(day, DateTimeFieldType.dayOfMonth());
|
283
|
}
|
284
|
|
285
|
public TimePeriod setEndYear(Integer year){
|
286
|
return setEndField(year, DateTimeFieldType.year());
|
287
|
}
|
288
|
|
289
|
public TimePeriod setEndMonth(Integer month) throws IndexOutOfBoundsException{
|
290
|
return setEndField(month, DateTimeFieldType.monthOfYear());
|
291
|
}
|
292
|
|
293
|
public TimePeriod setEndDay(Integer day) throws IndexOutOfBoundsException{
|
294
|
return setEndField(day, DateTimeFieldType.dayOfMonth());
|
295
|
}
|
296
|
|
297
|
private TimePeriod setStartField(Integer value, DateTimeFieldType type) throws IndexOutOfBoundsException{
|
298
|
initStart();
|
299
|
if (value == null){
|
300
|
start = start.without(type);
|
301
|
}else{
|
302
|
int max = 9999999;
|
303
|
if (type.equals(DateTimeFieldType.monthOfYear())){
|
304
|
max = 31;
|
305
|
}
|
306
|
if (type.equals(DateTimeFieldType.dayOfMonth())){
|
307
|
max = 12;
|
308
|
}
|
309
|
if (value < 1 || value > max ){
|
310
|
throw new IndexOutOfBoundsException("Value must be between 1 and " + max);
|
311
|
}
|
312
|
start = this.start.with(type, value);
|
313
|
}
|
314
|
return this;
|
315
|
}
|
316
|
|
317
|
private TimePeriod setEndField(Integer value, DateTimeFieldType type){
|
318
|
initEnd();
|
319
|
if (value == null){
|
320
|
end = end.without(type);
|
321
|
}else{
|
322
|
int max = 9999999;
|
323
|
if (type.equals(DateTimeFieldType.monthOfYear())){
|
324
|
max = 31;
|
325
|
}
|
326
|
if (type.equals(DateTimeFieldType.dayOfMonth())){
|
327
|
max = 12;
|
328
|
}
|
329
|
if ( (value < 1 || value > max) ){
|
330
|
throw new IndexOutOfBoundsException("Value must be between 1 and " + max);
|
331
|
}
|
332
|
end = this.end.with(type, value);
|
333
|
}
|
334
|
return this;
|
335
|
}
|
336
|
|
337
|
private void initStart(){
|
338
|
if (start == null){
|
339
|
start = new Partial();
|
340
|
}
|
341
|
}
|
342
|
|
343
|
private void initEnd(){
|
344
|
if (end == null){
|
345
|
end = new Partial();
|
346
|
}
|
347
|
}
|
348
|
|
349
|
|
350
|
|
351
|
//*********** CLONE **********************************/
|
352
|
|
353
|
/* (non-Javadoc)
|
354
|
* @see java.lang.Object#clone()
|
355
|
*/
|
356
|
@Override
|
357
|
public Object clone() {
|
358
|
try {
|
359
|
TimePeriod result = (TimePeriod)super.clone();
|
360
|
result.setStart(this.start); //DateTime is immutable
|
361
|
result.setEnd(this.end);
|
362
|
return result;
|
363
|
} catch (CloneNotSupportedException e) {
|
364
|
logger.warn("Clone not supported exception. Should never occurr !!");
|
365
|
return null;
|
366
|
}
|
367
|
}
|
368
|
|
369
|
}
|