Project

General

Profile

Revision 426e647f

ID426e647f7dd19be6c0cd77d2a2a0f896b6f373d8
Parent d4a543be
Child 3d475852

Added by Andreas Müller almost 3 years ago

ref #6752 implement verbatimDate for Reference (Parser and Formatter still missing)

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TimePeriod.java
14 14
import java.util.Date;
15 15

  
16 16
import javax.persistence.Embeddable;
17
import javax.persistence.MappedSuperclass;
17 18
import javax.persistence.Transient;
18 19
import javax.xml.bind.annotation.XmlAccessType;
19 20
import javax.xml.bind.annotation.XmlAccessorType;
......
56 57
})
57 58
@XmlRootElement(name = "TimePeriod")
58 59
@Embeddable
60
@MappedSuperclass
59 61
public class TimePeriod implements Cloneable, Serializable {
60 62
    private static final long serialVersionUID = 3405969418194981401L;
61 63
    private static final Logger logger = Logger.getLogger(TimePeriod.class);
......
89 91
     * Factory method
90 92
     * @return
91 93
     */
92
    public static TimePeriod NewInstance(){
94
    public static final TimePeriod NewInstance(){
93 95
        return new TimePeriod();
94 96
    }
95 97

  
......
98 100
     * Factory method
99 101
     * @return
100 102
     */
101
    public static TimePeriod NewInstance(Partial startDate){
103
    public static final TimePeriod NewInstance(Partial startDate){
102 104
        return new TimePeriod(startDate);
103 105
    }
104 106

  
......
107 109
     * Factory method
108 110
     * @return
109 111
     */
110
    public static TimePeriod NewInstance(Partial startDate, Partial endDate){
112
    public static final TimePeriod NewInstance(Partial startDate, Partial endDate){
111 113
        return new TimePeriod(startDate, endDate);
112 114
    }
113 115

  
......
116 118
     * Factory method
117 119
     * @return
118 120
     */
119
    public static TimePeriod NewInstance(Integer year){
121
    public static final TimePeriod NewInstance(Integer year){
120 122
        Integer endYear = null;
121 123
        return NewInstance(year, endYear);
122 124
    }
......
125 127
     * Factory method
126 128
     * @return
127 129
     */
128
    public static TimePeriod NewInstance(Integer startYear, Integer endYear){
130
    public static final TimePeriod NewInstance(Integer startYear, Integer endYear){
129 131
        Partial startDate = null;
130 132
        Partial endDate = null;
131 133
        if (startYear != null){
......
143 145
     * Factory method to create a TimePeriod from a <code>Calendar</code>. The Calendar is stored as the starting instant.
144 146
     * @return
145 147
     */
146
    public static TimePeriod NewInstance(Calendar startCalendar){
148
    public static final TimePeriod NewInstance(Calendar startCalendar){
147 149
        return NewInstance(startCalendar, null);
148 150
    }
149 151

  
......
152 154
     * The <code>ReadableInstant</code> is stored as the starting instant.
153 155
     * @return
154 156
     */
155
    public static TimePeriod NewInstance(ReadableInstant readableInstant){
157
    public static final TimePeriod NewInstance(ReadableInstant readableInstant){
156 158
        return NewInstance(readableInstant, null);
157 159
    }
158 160

  
......
160 162
     * Factory method to create a TimePeriod from a starting and an ending <code>Calendar</code>
161 163
     * @return
162 164
     */
163
    public static TimePeriod NewInstance(Calendar startCalendar, Calendar endCalendar){
165
    public static final TimePeriod NewInstance(Calendar startCalendar, Calendar endCalendar){
164 166
        Partial startDate = null;
165 167
        Partial endDate = null;
166 168
        if (startCalendar != null){
......
176 178
     * Factory method to create a TimePeriod from a starting and an ending <code>Date</code>
177 179
     * @return TimePeriod
178 180
     */
179
    public static TimePeriod NewInstance(Date startDate, Date endDate){
181
    public static final TimePeriod NewInstance(Date startDate, Date endDate){
180 182
        //TODO conversion untested, implemented according to http://www.roseindia.net/java/java-conversion/datetocalender.shtml
181 183
        Calendar calStart = null;
182 184
        Calendar calEnd = null;
......
196 198
     * Factory method to create a TimePeriod from a starting and an ending <code>ReadableInstant</code>(e.g. <code>DateTime</code>)
197 199
     * @return
198 200
     */
199
    public static TimePeriod NewInstance(ReadableInstant startInstant, ReadableInstant endInstant){
201
    public static final TimePeriod NewInstance(ReadableInstant startInstant, ReadableInstant endInstant){
200 202
        Partial startDate = null;
201 203
        Partial endDate = null;
202 204
        if (startInstant != null){
......
492 494
     * Returns the concatenation of <code>start</code> and <code>end</code>
493 495
     *
494 496
     */
495

  
496 497
    public String getTimePeriod(){
497 498
        String result = null;
498 499
        DateTimeFormatter formatter = TimePeriodPartialFormatter.NewInstance();
......
524 525
        if (! CdmUtils.nullSafeEqual(this.freeText, that.freeText)){
525 526
            return false;
526 527
        }
528
        //see comment in verbatimTimePeriod#equals
529
        String thisVerbatimDate = (this instanceof VerbatimTimePeriod)?
530
                ((VerbatimTimePeriod)this).getVerbatimDate():null;
531
        String thatVerbatimDate = (obj instanceof VerbatimTimePeriod)?
532
                ((VerbatimTimePeriod)obj).getVerbatimDate():null;
533
        if (! CdmUtils.nullSafeEqual(thisVerbatimDate, thatVerbatimDate)){
534
            return false;
535
        }
527 536
        return true;
528 537
    }
529 538

  
......
544 553
    public Object clone()  {
545 554
        try {
546 555
            TimePeriod result = (TimePeriod)super.clone();
547
            result.setStart(this.start);   //DateTime is immutable
548
            result.setEnd(this.end);
549
            result.setFreeText(this.freeText);
556
            copyCloned(this, result);
550 557
            return result;
551 558
        } catch (CloneNotSupportedException e) {
552 559
            logger.warn("Clone not supported exception. Should never occurr !!");
......
555 562
    }
556 563

  
557 564

  
565
    /**
566
     * @param result
567
     */
568
    protected static void copyCloned(TimePeriod origin, TimePeriod target) {
569
        target.setStart(origin.start);   //DateTime is immutable
570
        target.setEnd(origin.end);
571
        target.setFreeText(origin.freeText);
572
    }
573

  
574

  
558 575
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VerbatimTimePeriod.java
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
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/reference/IReference.java
16 16
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
17 17
import eu.etaxonomy.cdm.model.common.IParsable;
18 18
import eu.etaxonomy.cdm.model.common.TimePeriod;
19
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
19 20
import eu.etaxonomy.cdm.strategy.cache.reference.INomenclaturalReferenceCacheStrategy;
20 21
import eu.etaxonomy.cdm.strategy.match.IMatchable;
21 22
import eu.etaxonomy.cdm.strategy.merge.IMergable;
......
93 94
	/**
94 95
	 * Returns the date when the reference was published as a {@link TimePeriod}
95 96
	 */
96
	public TimePeriod getDatePublished();
97
	public VerbatimTimePeriod getDatePublished();
97 98

  
98 99
	/**
99 100
	 * Sets the date when the reference was published.
100 101
	 */
102
    public void setDatePublished(VerbatimTimePeriod datePublished);
103

  
104

  
105
    /**
106
     * Sets the date when the reference was published.
107
     * <BR>
108
     * Note: The time period will be internally converted to
109
     * a VerbatimTimePeriod so later changes to it will not
110
     * be reflected in the reference time period.
111
     */
112
    @Transient
101 113
    public void setDatePublished(TimePeriod datePublished);
102 114

  
103 115
	/**
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/reference/Reference.java
58 58
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
59 59
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
60 60
import eu.etaxonomy.cdm.model.common.TimePeriod;
61
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
61 62
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
62 63
import eu.etaxonomy.cdm.model.name.TaxonName;
63 64
import eu.etaxonomy.cdm.strategy.cache.reference.DefaultReferenceCacheStrategy;
......
295 296
	@XmlElement(name ="DatePublished" )
296 297
	@Embedded
297 298
	@IndexedEmbedded
298
	private TimePeriod datePublished = TimePeriod.NewInstance();
299
	private VerbatimTimePeriod datePublished = VerbatimTimePeriod.NewVerbatimInstance();
299 300

  
300 301
    //#5258
301 302
    @XmlElement (name = "Accessed", type= String.class)
......
698 699
	 * <i>this</i> reference.
699 700
	 */
700 701
	@Override
701
    public TimePeriod getDatePublished(){
702
    public VerbatimTimePeriod getDatePublished(){
702 703
		return this.datePublished;
703 704
	}
704 705
	/**
705 706
	 * @see 	#getDatePublished()
706 707
	 */
707 708
	@Override
708
    public void setDatePublished(TimePeriod datePublished){
709
    public void setDatePublished(VerbatimTimePeriod datePublished){
709 710
		this.datePublished = datePublished;
710 711
	}
712
    @Override
713
    @Transient
714
    public void setDatePublished(TimePeriod datePublished){
715
        setDatePublished(VerbatimTimePeriod.toVerbatim(datePublished));
716
    }
711 717

  
712 718
	public boolean hasDatePublished(){
713 719
		boolean result =  ! ( (this.datePublished == null) || StringUtils.isBlank(datePublished.toString()));
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/StrategyBase.java
24 24
import eu.etaxonomy.cdm.model.common.CdmBase;
25 25
import eu.etaxonomy.cdm.model.common.LSID;
26 26
import eu.etaxonomy.cdm.model.common.TimePeriod;
27
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
27 28

  
28 29

  
29 30
public abstract class StrategyBase implements IStrategy, Serializable {
......
80 81
	 */
81 82
	protected boolean isUserType(Class<?> fieldType) {
82 83
		if (	fieldType == TimePeriod.class ||
83
				fieldType == DateTime.class ||
84
		        fieldType == VerbatimTimePeriod.class ||
85
                fieldType == DateTime.class ||
84 86
				fieldType == LSID.class ||
85 87
				fieldType == Contact.class ||
86 88
				fieldType == URI.class ||
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/parser/TimePeriodParser.java
23 23

  
24 24
import eu.etaxonomy.cdm.common.CdmUtils;
25 25
import eu.etaxonomy.cdm.model.common.TimePeriod;
26
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
26 27

  
27 28
/**
28 29
 * Class for parsing all types of date string to TimePeriod
......
55 56
	private static final String strDateWithMonthes = "([0-3]?\\d" + dotOrWs + ")?" + strMonthes + dotOrWs + "\\d{4,4}";
56 57
	private static final Pattern dateWithMonthNamePattern = Pattern.compile(strDateWithMonthes);
57 58

  
58
	public static TimePeriod parseString(TimePeriod timePeriod, String periodString){
59
	public static <T extends TimePeriod> T parseString(T timePeriod, String periodString){
59 60
		//TODO until now only quick and dirty (and partly wrong)
60
		TimePeriod result = timePeriod;
61
		T result = timePeriod;
61 62

  
62 63
		if(timePeriod == null){
63 64
			return timePeriod;
......
407 408
		return parseString(timePeriod, strPeriod);
408 409
	}
409 410

  
411
	public static VerbatimTimePeriod parseStringVerbatim(String strPeriod) {
412
	    VerbatimTimePeriod timePeriod = VerbatimTimePeriod.NewVerbatimInstance();
413
	    return parseString(timePeriod, strPeriod);
414
	}
415

  
410 416

  
411 417

  
412 418
	protected static Partial parseSingleDate(String singleDateString) throws IllegalArgumentException{
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/common/VerbatimTimePeriodTest.java
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 static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertNull;
15
import static org.junit.Assert.assertTrue;
16

  
17
import java.util.Calendar;
18

  
19
import org.apache.log4j.Logger;
20
import org.joda.time.DateTimeFieldType;
21
import org.joda.time.MutableDateTime;
22
import org.joda.time.Partial;
23
import org.joda.time.ReadableInstant;
24
import org.junit.Assert;
25
import org.junit.Before;
26
import org.junit.Test;
27

  
28
import eu.etaxonomy.cdm.strategy.cache.common.TimePeriodPartialFormatter;
29
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
30

  
31
/**
32
 * @author a.mueller
33
 * @since 08.05.2018
34
 */
35
public class VerbatimTimePeriodTest {
36

  
37
    @SuppressWarnings("unused")
38
    private static final Logger logger = Logger.getLogger(VerbatimTimePeriodTest.class);
39

  
40
    private VerbatimTimePeriod onlyStartYear;
41
    private VerbatimTimePeriod onlyEndYear;
42
    private VerbatimTimePeriod startAndEndYear;
43
    private VerbatimTimePeriod noStartAndEndYear;
44
    private static final Integer year = 1982;
45
    private static final Integer month = 1;
46
    private static final Integer day = 5;
47

  
48
    /**
49
     * @throws java.lang.Exception
50
     */
51
    @Before
52
    public void setUp() throws Exception {
53
        onlyStartYear = VerbatimTimePeriod.NewVerbatimInstance(1922);
54
        onlyEndYear = VerbatimTimePeriod.NewVerbatimInstance(null, 1857);;
55
        startAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(1931, 1957);
56
        Integer start = null;
57
        Integer end = null;
58
        noStartAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(start, end);
59
    }
60

  
61
  //************************ TESTS ******************************************
62

  
63
    /**
64
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance()}.
65
     */
66
    @Test
67
    public void testNewVerbatimInstance() {
68
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance();
69
        Assert.assertNotNull(tp);
70
        Assert.assertTrue("VerbatimTimeperiod should be empty",tp.isEmpty());
71
    }
72

  
73
    /**
74
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.Partial)}.
75
     */
76
    @Test
77
    public void testNewVerbatimInstancePartial() {
78
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(new Partial().with(DateTimeFieldType.dayOfWeek(), 5));
79
        Assert.assertNotNull(tp);
80
        Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
81
    }
82

  
83
    /**
84
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.Partial, org.joda.time.Partial)}.
85
     */
86
    @Test
87
    public void testNewVerbatimInstancePartialPartial() {
88
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(new Partial().with(DateTimeFieldType.dayOfMonth(),day));
89
        Assert.assertNotNull(tp);
90
        Assert.assertFalse("VerbatimTimeperiod should not be empty",tp.isEmpty());
91
        Assert.assertEquals("VerbatimTimeperiod's should not be empty", day, tp.getStartDay());
92
    }
93

  
94
    /**
95
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.lang.Integer)}.
96
     */
97
    @Test
98
    public void testNewVerbatimInstanceInteger() {
99
        onlyStartYear = VerbatimTimePeriod.NewVerbatimInstance(1922);
100
        assertEquals(Integer.valueOf(1922), onlyStartYear.getStartYear());
101
        assertNull(onlyStartYear.getEndYear());
102
        assertEquals("1922", onlyStartYear.getYear());
103
    }
104

  
105
    /**
106
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.lang.Integer, java.lang.Integer)}.
107
     */
108
    @Test
109
    public void testNewVerbatimInstanceIntegerInteger() {
110
        startAndEndYear = VerbatimTimePeriod.NewVerbatimInstance(1931, 1957);
111
        assertEquals(Integer.valueOf(1957), startAndEndYear.getEndYear());
112
        assertEquals(Integer.valueOf(1931), startAndEndYear.getStartYear());
113
        assertEquals("1931-1957", startAndEndYear.getYear());
114
    }
115

  
116
    /**
117
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.util.Calendar)}.
118
     */
119
    @Test
120
    public void testNewVerbatimInstanceCalendar() {
121
        Calendar cal = Calendar.getInstance();
122
        cal.set(1982, 1, day);
123
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(cal);
124
        Assert.assertNotNull(tp);
125
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
126
        Assert.assertEquals("Timeperiod's should not be empty", day, tp.getStartDay());
127
    }
128

  
129
    /**
130
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(java.util.Calendar, java.util.Calendar)}.
131
     */
132
    @Test
133
    public void testNewVerbatimInstanceCalendarCalendar() {
134
        Calendar cal = Calendar.getInstance();
135
        cal.set(year, month, day);
136
        Calendar cal2 = Calendar.getInstance();
137
        Integer day2 = 20;
138
        Integer month2 = 7;
139
        Integer year2 = 1985;
140
        cal2.set(year2, month2, day2);
141
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(cal, cal2);
142
        Assert.assertNotNull(tp);
143
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
144
        Assert.assertEquals("Timeperiod's start should not be equal with cal1", year, tp.getStartYear());
145
        //Calendar starts counting months with 0
146
        Assert.assertEquals("Timeperiod's start should not be equal with cal1 ", (Integer)(month +1), tp.getStartMonth());
147
        Assert.assertEquals("Timeperiod's start should not be equal with cal1", day, tp.getStartDay());
148

  
149
        Assert.assertEquals("Timeperiod's start should not be equal with cal2", year2, tp.getEndYear());
150
        //Calendar starts counting months with 0
151
        Assert.assertEquals("Timeperiod's start should not be equal with cal2", (Integer)(month2+1), tp.getEndMonth());
152
        Assert.assertEquals("Timeperiod's end should not be equal with cal2", day2, tp.getEndDay());
153
    }
154

  
155
    /**
156
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.ReadableInstant)}.
157
     */
158
    @Test
159
    public void testNewVerbatimInstanceReadableInstant() {
160
        ReadableInstant readInst = new MutableDateTime();
161
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(readInst);
162
        Assert.assertNotNull(tp);
163
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
164
        Assert.assertEquals("Timeperiod's should not be empty", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
165
    }
166

  
167
    /**
168
     * Test method for {@link eu.etaxonomy.cdm.model.common.VerbatimTimePeriod#NewVerbatimInstance(org.joda.time.ReadableInstant, org.joda.time.ReadableInstant)}.
169
     */
170
    @Test
171
    public void testNewVerbatimInstanceReadableInstantReadableInstant() {
172
        ReadableInstant readInst = new MutableDateTime();
173
        ReadableInstant readInst2 = new MutableDateTime();
174
        ((MutableDateTime)readInst).addDays(5);
175
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(readInst, readInst2);
176

  
177
        Assert.assertNotNull(tp);
178
        Assert.assertFalse("Timeperiod should not be empty",tp.isEmpty());
179
        Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)readInst.get(DateTimeFieldType.dayOfMonth()), tp.getStartDay());
180
        Assert.assertEquals("Timeperiod's day should not be equal to readable instant", (Integer)readInst2.get(DateTimeFieldType.dayOfMonth()), tp.getEndDay());
181
    }
182

  
183
    /**
184
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#calendarToPartial(java.util.Calendar)}.
185
     */
186
    @Test
187
    public void testCalendarToPartial() {
188
        Calendar cal = Calendar.getInstance();
189
        cal.set(year, month, day);
190
        Partial part = TimePeriod.calendarToPartial(cal);
191
        Assert.assertEquals("Partial's day should not be equal to calednars day", day, (Integer)part.get(DateTimeFieldType.dayOfMonth()));
192
    }
193

  
194
    /**
195
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#readableInstantToPartial(org.joda.time.ReadableInstant)}.
196
     */
197
    @Test
198
    public void testReadableInstantToPartial() {
199
        ReadableInstant readInst = new MutableDateTime();
200
        Partial part = TimePeriod.readableInstantToPartial(readInst);
201
        part.get(DateTimeFieldType.dayOfMonth());
202
        Assert.assertEquals("Partial's day should not be equal to calednars day", (Integer)part.get(DateTimeFieldType.dayOfMonth()), (Integer)part.get(DateTimeFieldType.dayOfMonth()));
203
    }
204

  
205
    /**
206
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#TimePeriod()}.
207
     */
208
    @Test
209
    public void testTimePeriod() {
210
        TimePeriod tp = new TimePeriod();
211
        Assert.assertNotNull("Time period must be created",tp);
212
    }
213

  
214
//  @Ignore
215
    @Test
216
    public void testSetStart(){
217
        Partial startDate = new Partial().with(DateTimeFieldType.year(), 2010)
218
                .with(DateTimeFieldType.monthOfYear(), 12)
219
                .with(DateTimeFieldType.dayOfMonth(), 16);
220
        Partial newStartDate = new Partial().with(DateTimeFieldType.year(), 1984)
221
            .with(DateTimeFieldType.monthOfYear(), 12)
222
            .with(DateTimeFieldType.dayOfMonth(), 14);
223

  
224
        VerbatimTimePeriod tp = VerbatimTimePeriod.NewVerbatimInstance(startDate);
225
        String startString = tp.toString();
226
        assertNull("Freetext should be not set", tp.getFreeText());
227
        tp.setStart(newStartDate);
228
        String changedString = tp.toString();
229
        Assert.assertTrue("Setting the partial should change the string representation of the TimePeriod", !startString.equals(changedString));
230

  
231
        //
232
        tp = TimePeriodParser.parseStringVerbatim("1752");
233
        assertNull("Freetext should be not set", tp.getFreeText());
234
        startString = tp.toString();
235
        tp.setStart(newStartDate);
236
        changedString = tp.toString();
237
        Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
238

  
239
        //
240
        tp = TimePeriodParser.parseStringVerbatim("any strange date");
241
        assertNotNull("Freetext should be set", tp.getFreeText());
242
        startString = tp.toString();
243
        tp.setStart(newStartDate);
244
        changedString = tp.toString();
245
        Assert.assertEquals("Setting a partial for a time period having the freetext set should not change the string representation of the TimePeriod  ", startString, changedString);
246

  
247

  
248
        //
249
//      tp = TimePeriodParser.parseString("15.12.1730"); //TODO currently not parsed
250
//
251
//      startString = tp.toString();
252
//      tp.setStart(newStartDate);
253
//      changedString = tp.toString();
254
//
255
//      Assert.assertTrue("Setting a partial for a parsed time period should change the string representation of the TimePeriod ", !startString.equals(changedString));
256
    }
257

  
258
    /**
259
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#isPeriod()}.
260
     */
261
    @Test
262
    public void testIsPeriod() {
263
        assertTrue(startAndEndYear.isPeriod());
264
        assertFalse(onlyStartYear.isPeriod());
265
        assertFalse(onlyEndYear.isPeriod());
266
        assertFalse(noStartAndEndYear.isPeriod());
267
        onlyStartYear.setEndDay(14);
268
        assertFalse(onlyStartYear.isPeriod());
269
        onlyStartYear.setEndYear(1988);
270
        assertTrue(onlyStartYear.isPeriod()); //may be discussed
271
    }
272

  
273
    /**
274
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getStart()}.
275
     */
276
    @Test
277
    public void testGetStart() {
278
        TimePeriod tp = new TimePeriod();
279
        Partial start = new Partial(DateTimeFieldType.year(), 1999);
280
        tp.setStart(start);
281
        Assert.assertEquals("Start year should be 1999", Integer.valueOf(1999), tp.getStartYear());
282
        Assert.assertEquals("Start should be 'start'", start, tp.getStart());
283
    }
284

  
285

  
286
    /**
287
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getEnd()}.
288
     */
289
    @Test
290
    public void testGetEnd() {
291
        TimePeriod tp = new TimePeriod();
292
        Partial end = new Partial(DateTimeFieldType.year(), 1999);
293
        tp.setEnd(end);
294
        Assert.assertEquals("End year should be 1999", Integer.valueOf(1999), tp.getEndYear());
295
        Assert.assertEquals("End should be 'end'", end, tp.getEnd());
296
    }
297

  
298
    /**
299
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#getYear()}.
300
     */
301
    @Test
302
    public void testGetYear() {
303
        TimePeriod tp = new TimePeriod();
304
        tp.setStartYear(1999);
305
        Assert.assertEquals("Year should be 1999", "1999", tp.getYear());
306
        tp.setEndYear(2002);
307
        Assert.assertEquals("Year should be 1999-2002", "1999-2002", tp.getYear());
308
    }
309

  
310

  
311
    /**
312
     * TODO should be partly moved to a test class for {@link TimePeriodPartialFormatter}
313
     */
314
    @Test
315
    public void testToStringTimePeriod() {
316
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(1788,1799);
317
        assertNotNull(tp1);
318
        Assert.assertEquals("1788-1799", tp1.toString());
319
        tp1.setStartDay(3);
320
        Assert.assertEquals("3.xx.1788-1799", tp1.toString());
321
        tp1.setEndMonth(11);
322
        Assert.assertEquals("3.xx.1788-11.1799", tp1.toString());
323
    }
324

  
325

  
326
    /**
327
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
328
     */
329
    @Test
330
    public void testClone() {
331
        Integer startYear = 1788;
332
        Integer startMonth = 6;
333
        Integer startDay = 25;
334
        Integer endDay = 21;
335
        Integer endMonth = 12;
336
        Integer endYear = 1799;
337
        String freeText = "A free period";
338
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(startYear,endYear);
339
        tp1.setStartDay(startDay);
340
        tp1.setStartMonth(startMonth);
341
        tp1.setEndDay(endDay);
342
        tp1.setEndMonth(endMonth);
343
        tp1.setFreeText(freeText);
344
        TimePeriod tpClone = (TimePeriod)tp1.clone();
345
        Assert.assertEquals("Start year must be 1788.", startYear, tpClone.getStartYear());
346
        Assert.assertEquals("Start month must be 6.", startMonth, tpClone.getStartMonth());
347
        Assert.assertEquals("Start day must be 25.", startDay, tpClone.getStartDay());
348
        Assert.assertEquals("End year must be 1799.", endYear, tpClone.getEndYear());
349
        Assert.assertEquals("End month must be 12.", endMonth, tpClone.getEndMonth());
350
        Assert.assertEquals("End day must be 21.", endDay, tpClone.getEndDay());
351
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
352
    }
353

  
354
    /**
355
     * Test method for {@link eu.etaxonomy.cdm.model.common.TimePeriod#clone()}.
356
     */
357
    @Test
358
    public void testEquals() {
359
        Integer startYear = 1788;
360
        Integer startMonth = 6;
361
        Integer endDay = 21;
362
        Integer endYear = 1799;
363
        String freeText = "A free period";
364

  
365
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(startYear);
366
        VerbatimTimePeriod tpClone = (VerbatimTimePeriod)tp1.clone();
367
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
368

  
369
        tp1.setStartMonth(startMonth);
370
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
371
        tpClone = (VerbatimTimePeriod)tp1.clone();
372
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
373

  
374

  
375
        tp1.setEndYear(endYear);
376
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
377
        tpClone = (VerbatimTimePeriod)tp1.clone();
378
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
379

  
380
        tp1.setEndDay(endDay);
381
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
382
        tpClone = (VerbatimTimePeriod)tp1.clone();
383
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
384

  
385
        tp1.setFreeText(freeText);
386
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
387
        tpClone = (VerbatimTimePeriod)tp1.clone();
388
        Assert.assertEquals("Cloned time period must be equal to originial", tp1, tpClone);
389

  
390
        tp1 = VerbatimTimePeriod.NewVerbatimInstance();
391
        Assert.assertFalse("Cloned time period must not be equal to originial", tp1.equals(tpClone));
392
        VerbatimTimePeriod tp2 = VerbatimTimePeriod.NewVerbatimInstance();
393
        Assert.assertEquals("Empty time periods must be equal", tp1, tp2);
394

  
395
        tp1.setFreeText(freeText);
396
        Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tp2));
397
        tp2.setFreeText("jldskjlfi");
398
        Assert.assertFalse("Tp2 must not be equal to originial", tp1.equals(tpClone));
399
        tp2.setFreeText(freeText);
400
        Assert.assertEquals("Tp2 must be equal", tp1, tp2);
401
    }
402

  
403
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/strategy/merge/DefaultMergeStrategyTest.java
264 264
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
265 265
		//TODO updated should have the actual date if any value has changed
266 266
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
267
		Assert.assertSame("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
267
		Assert.assertEquals("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
268 268
		//TODO this may not be correct
269 269
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
270 270

  
cdmlib-test/src/main/resources/dbscripts/001-cdm.h2.sql
3582 3582
    DATEPUBLISHED_END VARCHAR(255),
3583 3583
    DATEPUBLISHED_FREETEXT VARCHAR(255),
3584 3584
    DATEPUBLISHED_START VARCHAR(255),
3585
	DATEPUBLISHED_VERBATIMDATE VARCHAR(255),
3585 3586
    EDITION VARCHAR(255),
3586 3587
    EDITOR VARCHAR(255),
3587 3588
    ISBN VARCHAR(255),
......
3635 3636
    DATEPUBLISHED_END VARCHAR(255),
3636 3637
    DATEPUBLISHED_FREETEXT VARCHAR(255),
3637 3638
    DATEPUBLISHED_START VARCHAR(255),
3639
    DATEPUBLISHED_VERBATIMDATE VARCHAR(255),
3638 3640
    EDITION VARCHAR(255),
3639 3641
    EDITOR VARCHAR(255),
3640 3642
    ISBN VARCHAR(255),
cdmlib-test/src/main/resources/eu/etaxonomy/cdm/database/schema/dataset.dtd
2346 2346
    URI CDATA #IMPLIED
2347 2347
    DATEPUBLISHED_END CDATA #IMPLIED
2348 2348
    DATEPUBLISHED_START CDATA #IMPLIED
2349
    DATEPUBLISHED_FREETEXT CDATA #IMPLIED
2350
    DATEPUBLISHED_VERBATIMDATE CDATA #IMPLIED
2349 2351
    TITLE CDATA #IMPLIED
2350 2352
    PAGES CDATA #IMPLIED
2351 2353
    SERIES CDATA #IMPLIED
......
2426 2428
    AUTHORSHIP_ID CDATA #IMPLIED
2427 2429
    DATEPUBLISHED_END CDATA #IMPLIED
2428 2430
    DATEPUBLISHED_START CDATA #IMPLIED
2431
    DATEPUBLISHED_FREETEXT CDATA #IMPLIED
2432
    DATEPUBLISHED_VERBATIMDATE CDATA #IMPLIED
2429 2433
    TITLE CDATA #IMPLIED
2430 2434
    ABBREVTITLE CDATA #IMPLIED
2431 2435
    ABBREVTITLECACHE CDATA #IMPLIED

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)