Project

General

Profile

Download (8.03 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
/**
28
 * @author a.mueller
29
 * @since 08.05.2018
30
 *
31
 */
32
@XmlAccessorType(XmlAccessType.FIELD)
33
@XmlType(name = "VerbatimTimePeriod", propOrder = {
34
    "verbatimDate"
35
})
36
@XmlRootElement(name = "VerbatimTimePeriod")
37
@Embeddable
38
public class VerbatimTimePeriod extends TimePeriod {
39

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

    
44
    @XmlElement(name = "FreeText")
45
    private String verbatimDate;
46

    
47

    
48
 // ********************** FACTORY METHODS **************************/
49

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

    
58

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

    
67

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

    
76

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

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

    
102

    
103

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

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

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

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

    
156

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

    
173

    
174
//*********************** CONSTRUCTOR *********************************/
175

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

    
193
// ***************************** GETTER /SETTER *********************/
194

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

    
202
//****************** CONVERTERS ******************/
203

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

    
225

    
226

    
227
// ************************************ TRANSIENT **************************/
228

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

    
240
//*********** EQUALS **********************************/
241

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

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

    
251

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

    
259

    
260

    
261
//*********** CLONE **********************************/
262

    
263
    @Override
264
    public Object clone()  {
265
            VerbatimTimePeriod result = (VerbatimTimePeriod)super.clone();
266
            result.setVerbatimDate(this.verbatimDate);
267
            return result;
268
    }
269
}
(75-75/80)