Project

General

Profile

Download (7.84 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

    
205

    
206

    
207
// ************************************ TRANSIENT **************************/
208

    
209
    @Override
210
    /**
211
     * True, if there is no start date and no end date and no freetext representation exists.
212
     * @return
213
     */
214
    @Transient
215
    public boolean isEmpty(){
216
        boolean result = super.isEmpty();
217
        return result && StringUtils.isBlank(this.getVerbatimDate());
218
    }
219

    
220
//*********** EQUALS **********************************/
221

    
222
    //we want VerbatimTimePeriod and TimePeriod to be equals
223
    //if both are equal in the TimePeriod part and if
224
    //VerbatimTimePeriod has no verbatimDate defined
225

    
226
    @Override
227
    public boolean equals(Object obj) {
228
        return super.equals(obj);
229
    }
230

    
231

    
232
    @Override
233
    public int hashCode() {
234
        int hashCode = super.hashCode();
235
        hashCode += (verbatimDate == null)? 0: verbatimDate.hashCode();
236
        return hashCode;
237
    }
238

    
239

    
240
//**************************** to String ****************************************
241

    
242
    /**
243
     * Returns the {@link #getFreeText()} value if free text is not <code>null</code>.
244
     * Otherwise the concatenation of <code>start</code> and <code>end</code> is returned.
245
     *
246
     * @see java.lang.Object#toString()
247
     */
248
      @Override
249
      public String toString(){
250
         String result = super.toString();
251
         if (StringUtils.isNotBlank(this.verbatimDate)){
252
             result = CdmUtils.concat(" ", result, "[\""+this.verbatimDate+"\"]");
253
         }
254
         return result;
255
    }
256

    
257
//*********** CLONE **********************************/
258

    
259
    @Override
260
    public Object clone()  {
261
            VerbatimTimePeriod result = (VerbatimTimePeriod)super.clone();
262
            result.setVerbatimDate(this.verbatimDate);
263
            return result;
264
    }
265
}
(75-75/80)