Project

General

Profile

Revision 3ee87683

ID3ee87683add63fabb35cab4a19628c78287b3e98
Parent dea49164
Child e25cc9dd

Added by Andreas Müller 5 months ago

ref #9071 add formatter for VerbatimTimePeriod

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/common/VerbatimTimePeriodFormatter.java
1
/**
2
* Copyright (C) 2020 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.format.common;
10

  
11
import org.apache.commons.lang.StringUtils;
12
import org.joda.time.DateTimeFieldType;
13

  
14
import eu.etaxonomy.cdm.common.CdmUtils;
15
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
16

  
17
/**
18
 * @author a.mueller
19
 * @since 27.06.2020
20
 */
21
public class VerbatimTimePeriodFormatter {
22

  
23
    public static final DateTimeFieldType YEAR_TYPE = DateTimeFieldType.year();
24
    public static final DateTimeFieldType MONTH_TYPE = DateTimeFieldType.monthOfYear();
25
    public static final DateTimeFieldType DAY_TYPE = DateTimeFieldType.dayOfMonth();
26
    public static final DateTimeFieldType HOUR_TYPE = DateTimeFieldType.hourOfDay();
27
    public static final DateTimeFieldType MINUTE_TYPE = DateTimeFieldType.minuteOfHour();
28

  
29
    private static final TimePeriodFormatter timePeriodFormatter = TimePeriodFormatter.NewDefaultInstance();
30

  
31
    public static final VerbatimTimePeriodFormatter NewDefaultInstance(){
32
        return new VerbatimTimePeriodFormatter();
33
    }
34

  
35
    private VerbatimTimePeriodFormatter(){}
36

  
37
    public String format (VerbatimTimePeriod tp){
38
        String result = timePeriodFormatter.format(tp);
39
        if (StringUtils.isNotBlank(tp.getVerbatimDate()) && StringUtils.isBlank(tp.getFreeText())){
40
            result = CdmUtils.concat(" ", result, "[\""+tp.getVerbatimDate()+"\"]");
41
        }
42
        return result;
43
    }
44
}
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/VerbatimTimePeriod.java
24 24
import org.joda.time.Partial;
25 25
import org.joda.time.ReadableInstant;
26 26

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

  
29 29
/**
30 30
 * @author a.mueller
......
43 43
    @SuppressWarnings("unused")
44 44
    private static final Logger logger = Logger.getLogger(VerbatimTimePeriod.class);
45 45

  
46
    private static VerbatimTimePeriodFormatter formatter = VerbatimTimePeriodFormatter.NewDefaultInstance();
47

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

  
49

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

  
52 53
     public static final VerbatimTimePeriod NewVerbatimInstance(){
......
184 185
     */
185 186
      @Override
186 187
      public String toString(){
187
         String result = super.toString();
188
         if (StringUtils.isNotBlank(this.verbatimDate) && StringUtils.isBlank(this.getFreeText())){
189
             result = CdmUtils.concat(" ", result, "[\""+this.verbatimDate+"\"]");
190
         }
191
         return result;
188
         return formatter.format(this);
192 189
    }
193 190

  
194 191
//*********** CLONE **********************************/
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/common/VerbatimTimePeriodFormatterTest.java
1
/**
2
* Copyright (C) 2020 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.format.common;
10

  
11
import static org.junit.Assert.assertNotNull;
12

  
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.Test;
16

  
17
import eu.etaxonomy.cdm.model.common.TimePeriod;
18
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
19

  
20
/**
21
 * @author a.mueller
22
 * @since 27.06.2020
23
 */
24
public class VerbatimTimePeriodFormatterTest {
25

  
26
    private VerbatimTimePeriodFormatter formatter;
27

  
28
    private String endash = TimePeriod.SEP;
29

  
30
    @Before
31
    public void setUp() throws Exception {
32
        formatter = VerbatimTimePeriodFormatter.NewDefaultInstance();
33
    }
34

  
35
    @Test
36
    public void test() {
37
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(1788,1799);
38
        assertNotNull(tp1);
39
        Assert.assertEquals("1788"+endash+"1799", formatter.format(tp1));
40
        tp1.setStartDay(3);
41
        Assert.assertEquals("3 MMM 1788"+endash+"1799", formatter.format(tp1));
42
        tp1.setEndMonth(11);
43
        Assert.assertEquals("3 MMM 1788"+endash+"Nov 1799", formatter.format(tp1));
44

  
45
    }
46

  
47
}
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/common/VerbatimTimePeriodTest.java
25 25
import org.junit.Before;
26 26
import org.junit.Test;
27 27

  
28
import eu.etaxonomy.cdm.common.UTF8;
29
import eu.etaxonomy.cdm.format.common.TimePeriodPartialFormatter;
30 28
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
31 29

  
32 30
/**
......
256 254
        Assert.assertEquals("Year should be 1999-2002", "1999"+SEP+"2002", tp.getYear());
257 255
    }
258 256

  
259
    /**
260
     * TODO should be partly moved to a test class for {@link TimePeriodPartialFormatter}
261
     */
262
    @Test
263
    public void testToStringTimePeriod() {
264
        String endash = UTF8.EN_DASH.toString();
265
        VerbatimTimePeriod tp1 = VerbatimTimePeriod.NewVerbatimInstance(1788,1799);
266
        assertNotNull(tp1);
267
        Assert.assertEquals("1788"+endash+"1799", tp1.toString());
268
        tp1.setStartDay(3);
269
        Assert.assertEquals("3 MMM 1788"+endash+"1799", tp1.toString());
270
        tp1.setEndMonth(11);
271
        Assert.assertEquals("3 MMM 1788"+endash+"Nov 1799", tp1.toString());
272
    }
273

  
274 257
    @Test
275 258
    public void testClone() {
276 259
        Integer startYear = 1788;

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)