Project

General

Profile

« Previous | Next » 

Revision 5420c5f6

Added by Andreas Kohlbecker almost 7 years ago

#6564 TimePeriodFormatter more complete + Partial Formatter both also with support for dd.mm.yyyy formats

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/component/TimePeriodField.java
28 28
import eu.etaxonomy.cdm.model.common.TimePeriod;
29 29
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
30 30
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
31
import eu.etaxonomy.cdm.vaadin.util.TimePeriodFormatter;
31
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
32
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
32 33

  
33 34
/**
34 35
 * @author a.kohlbecker
......
61 62

  
62 63
    Set<Component> styledComponents = new HashSet<>();
63 64

  
64
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(TimePeriodFormatter.Format.ISO8601);
65
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
65 66

  
66 67
    /**
67 68
     *
src/main/java/eu/etaxonomy/cdm/vaadin/component/registration/RegistrationItem.java
30 30
import eu.etaxonomy.cdm.vaadin.event.ReferenceEvent;
31 31
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
32 32
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
33
import eu.etaxonomy.cdm.vaadin.util.TimePeriodFormatter;
33
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
34
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
34 35
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
35 36
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
36 37
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorkflowViewBean;
......
61 62

  
62 63
    private AbstractView<?> parentView;
63 64

  
64
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(TimePeriodFormatter.Format.ISO8601);
65
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
65 66

  
66 67
    // --------------------------------------------------
67 68
    private TypeStateLabel typeStateLabel = new TypeStateLabel();
src/main/java/eu/etaxonomy/cdm/vaadin/util/TimePeriodFormatter.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util;
10

  
11
import org.apache.commons.lang.StringUtils;
12
import org.joda.time.Partial;
13
import org.joda.time.format.DateTimeFormatter;
14
import org.joda.time.format.ISODateTimeFormat;
15

  
16
import eu.etaxonomy.cdm.model.common.TimePeriod;
17

  
18
/**
19
 *
20
 * FIXME move into cdmlib
21
 *
22
 * @author a.kohlbecker
23
 * @since Apr 26, 2017
24
 *
25
 */
26
public class TimePeriodFormatter {
27

  
28
    public enum Format {
29
        /**
30
         * yyyy-mm-dd or yyyy-mm or yyyy
31
         */
32
        ISO8601
33
    }
34

  
35
    private Format format;
36

  
37
    public TimePeriodFormatter(Format format) {
38
        this.format = format;
39
    }
40

  
41
    public String print(TimePeriod timePeriod) {
42

  
43
        if ( StringUtils.isNotBlank(timePeriod.getFreeText())){
44
           return timePeriod.getFreeText();
45
        }else{
46
            switch (format) {
47
            case ISO8601:
48
            default:
49
                return printISO8601(timePeriod);
50
            }
51
        }
52
    }
53

  
54
    /**
55
     * @param datePublished
56
     */
57
    private String printISO8601(TimePeriod datePublished) {
58
        StringBuffer sb = new StringBuffer();
59
        if (datePublished.getStart() != null) {
60
            sb.append(datePublished.getStart().toString(determineISO860Formatter(datePublished.getStart())));
61
        }
62
        if (datePublished.getEnd() != null) {
63
            if (sb.length() > 0) {
64
                sb.append('-');
65
                sb.append(datePublished.getEnd().toString(determineISO860Formatter(datePublished.getEnd())));
66
            }
67
        }
68
        return sb.toString();
69
    }
70

  
71
    /**
72
     * @param partial
73
     * @return
74
     */
75
    private DateTimeFormatter determineISO860Formatter(Partial partial) {
76
        if (partial.isSupported(TimePeriod.DAY_TYPE)) {
77
            return ISODateTimeFormat.yearMonthDay();
78
        }
79
        if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
80
            return ISODateTimeFormat.yearMonth();
81
        }
82
        return ISODateTimeFormat.year();
83

  
84
    }
85

  
86
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/TypeDesignationConverter.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util;
10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.HashMap;
16
import java.util.LinkedList;
17
import java.util.List;
18
import java.util.Map;
19

  
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
22
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
23
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
24
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
26
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
27

  
28
/**
29
 * Converts a collection of TypeDesignations, which should belong to the
30
 * same name of course, into a string representation.
31
 *
32
 * Order of TypeDesignations in the resulting string:
33
 *  Type, Holotype, Lectotype, Epitypes
34
 * @author a.kohlbecker
35
 * @since Mar 10, 2017
36
 *
37
 */
38
public class TypeDesignationConverter {
39

  
40

  
41
    private final String separator = ", ";
42

  
43
    private Collection<TypeDesignationBase> typeDesignations;
44
    private Map<TypeDesignationStatusBase<?>, Collection<String>> orderedStrings;
45

  
46
    private String finalString = null;
47

  
48
    private String typifiedNameCache = null;
49

  
50
    /**
51
     * @param taxonNameBase
52
     *
53
     */
54
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations, TaxonNameBase taxonNameBase) {
55
        this.typeDesignations = typeDesignations;
56
        orderedStrings = new HashMap<>(typeDesignations.size());
57
        if(taxonNameBase != null){
58
            this.typifiedNameCache = taxonNameBase.getTitleCache();
59
        }
60
    }
61

  
62
    private void putString(TypeDesignationStatusBase<?> status, String string){
63
        // the cdm orderd term bases are ordered invers, fixing this for here
64
        if(status == null){
65
            status = SpecimenTypeDesignationStatus.TYPE();
66
        }
67
        if(!orderedStrings.containsKey(status)){
68
            orderedStrings.put(status, new ArrayList<String>());
69
        }
70
        orderedStrings.get(status).add(string);
71
    }
72

  
73

  
74
    public TypeDesignationConverter buildString(){
75

  
76
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), stringify(td)));
77

  
78
        StringBuilder sb = new StringBuilder();
79

  
80
        if(typifiedNameCache != null){
81
            sb.append(typifiedNameCache).append(": ");
82
        }
83
        List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(orderedStrings.keySet());
84

  
85
        Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
86
            @Override
87
            public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
88
                // fix inverted order of cdm terms by -1*
89
                return -1 * o1.compareTo(o2);
90
            }
91
        });
92

  
93
        keyList.forEach(key -> {
94
            if(key.equals( SpecimenTypeDesignationStatus.TYPE())){
95
                sb.append("Type");
96
            } else {
97
                sb.append(key.getPreferredRepresentation(Language.DEFAULT()));
98
            }
99
            sb.append(": ");
100
            orderedStrings.get(key).forEach(str -> {
101
                sb.append(str);
102
                if(sb.length() > 0){
103
                    sb.append(separator);
104
                }
105
            });
106
        });
107

  
108
        finalString  = sb.toString();
109
        return this;
110
    }
111

  
112
    /**
113
     * @param td
114
     * @return
115
     */
116
    private String stringify(TypeDesignationBase td) {
117

  
118
        if(td instanceof NameTypeDesignation){
119
            return stringify((NameTypeDesignation)td);
120
        } else {
121
            return stringify((SpecimenTypeDesignation)td);
122
        }
123
    }
124

  
125

  
126
    /**
127
     * @param td
128
     * @return
129
     */
130
    protected String stringify(NameTypeDesignation td) {
131

  
132
        StringBuffer sb = new StringBuffer();
133

  
134
        if(td.getTypeName() != null){
135
            sb.append(td.getTypeName().getTitleCache());
136
        }
137
        if(td.getCitation() != null){
138
            sb.append(" ").append(td.getCitation().getTitleCache());
139
            if(td.getCitationMicroReference() != null){
140
                sb.append(":").append(td.getCitationMicroReference());
141
            }
142
        }
143
        if(td.isNotDesignated()){
144
            sb.append(" not designated");
145
        }
146
        if(td.isRejectedType()){
147
            sb.append(" rejected");
148
        }
149
        if(td.isConservedType()){
150
            sb.append(" conserved");
151
        }
152
        return sb.toString();
153
    }
154

  
155
    /**
156
     * @param td
157
     * @return
158
     */
159
    private String stringify(SpecimenTypeDesignation td) {
160
        StringBuffer sb = new StringBuffer();
161

  
162
        if(td.getTypeSpecimen() != null){
163
            String nameTitleCache = td.getTypeSpecimen().getTitleCache();
164
            if(typifiedNameCache != null){
165
                nameTitleCache = nameTitleCache.replace(typifiedNameCache, "");
166
            }
167
            sb.append(nameTitleCache);
168
        }
169

  
170
        if(td.getCitation() != null){
171
            sb.append(" ").append(td.getCitation().getTitleCache());
172
            if(td.getCitationMicroReference() != null){
173
                sb.append(" :").append(td.getCitationMicroReference());
174
            }
175
        }
176
        if(td.isNotDesignated()){
177
            sb.append(" not designated");
178
        }
179

  
180
        return sb.toString();
181
    }
182

  
183
    public String print(){
184
        return finalString;
185
    }
186
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverter.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util.converter;
10

  
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.HashMap;
16
import java.util.LinkedList;
17
import java.util.List;
18
import java.util.Map;
19

  
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
22
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
23
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
24
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
26
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
27

  
28
/**
29
 * Converts a collection of TypeDesignations, which should belong to the
30
 * same name of course, into a string representation.
31
 *
32
 * Order of TypeDesignations in the resulting string:
33
 *  Type, Holotype, Lectotype, Epitypes
34
 * @author a.kohlbecker
35
 * @since Mar 10, 2017
36
 *
37
 */
38
public class TypeDesignationConverter {
39

  
40

  
41
    private final String separator = ", ";
42

  
43
    private Collection<TypeDesignationBase> typeDesignations;
44
    private Map<TypeDesignationStatusBase<?>, Collection<String>> orderedStrings;
45

  
46
    private String finalString = null;
47

  
48
    private String typifiedNameCache = null;
49

  
50
    /**
51
     * @param taxonNameBase
52
     *
53
     */
54
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations, TaxonNameBase taxonNameBase) {
55
        this.typeDesignations = typeDesignations;
56
        orderedStrings = new HashMap<>(typeDesignations.size());
57
        if(taxonNameBase != null){
58
            this.typifiedNameCache = taxonNameBase.getTitleCache();
59
        }
60
    }
61

  
62
    private void putString(TypeDesignationStatusBase<?> status, String string){
63
        // the cdm orderd term bases are ordered invers, fixing this for here
64
        if(status == null){
65
            status = SpecimenTypeDesignationStatus.TYPE();
66
        }
67
        if(!orderedStrings.containsKey(status)){
68
            orderedStrings.put(status, new ArrayList<String>());
69
        }
70
        orderedStrings.get(status).add(string);
71
    }
72

  
73

  
74
    public TypeDesignationConverter buildString(){
75

  
76
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), stringify(td)));
77

  
78
        StringBuilder sb = new StringBuilder();
79

  
80
        if(typifiedNameCache != null){
81
            sb.append(typifiedNameCache).append(": ");
82
        }
83
        List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(orderedStrings.keySet());
84

  
85
        Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
86
            @Override
87
            public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
88
                // fix inverted order of cdm terms by -1*
89
                return -1 * o1.compareTo(o2);
90
            }
91
        });
92

  
93
        keyList.forEach(key -> {
94
            if(key.equals( SpecimenTypeDesignationStatus.TYPE())){
95
                sb.append("Type");
96
            } else {
97
                sb.append(key.getPreferredRepresentation(Language.DEFAULT()));
98
            }
99
            sb.append(": ");
100
            orderedStrings.get(key).forEach(str -> {
101
                sb.append(str);
102
                if(sb.length() > 0){
103
                    sb.append(separator);
104
                }
105
            });
106
        });
107

  
108
        finalString  = sb.toString();
109
        return this;
110
    }
111

  
112
    /**
113
     * @param td
114
     * @return
115
     */
116
    private String stringify(TypeDesignationBase td) {
117

  
118
        if(td instanceof NameTypeDesignation){
119
            return stringify((NameTypeDesignation)td);
120
        } else {
121
            return stringify((SpecimenTypeDesignation)td);
122
        }
123
    }
124

  
125

  
126
    /**
127
     * @param td
128
     * @return
129
     */
130
    protected String stringify(NameTypeDesignation td) {
131

  
132
        StringBuffer sb = new StringBuffer();
133

  
134
        if(td.getTypeName() != null){
135
            sb.append(td.getTypeName().getTitleCache());
136
        }
137
        if(td.getCitation() != null){
138
            sb.append(" ").append(td.getCitation().getTitleCache());
139
            if(td.getCitationMicroReference() != null){
140
                sb.append(":").append(td.getCitationMicroReference());
141
            }
142
        }
143
        if(td.isNotDesignated()){
144
            sb.append(" not designated");
145
        }
146
        if(td.isRejectedType()){
147
            sb.append(" rejected");
148
        }
149
        if(td.isConservedType()){
150
            sb.append(" conserved");
151
        }
152
        return sb.toString();
153
    }
154

  
155
    /**
156
     * @param td
157
     * @return
158
     */
159
    private String stringify(SpecimenTypeDesignation td) {
160
        StringBuffer sb = new StringBuffer();
161

  
162
        if(td.getTypeSpecimen() != null){
163
            String nameTitleCache = td.getTypeSpecimen().getTitleCache();
164
            if(typifiedNameCache != null){
165
                nameTitleCache = nameTitleCache.replace(typifiedNameCache, "");
166
            }
167
            sb.append(nameTitleCache);
168
        }
169

  
170
        if(td.getCitation() != null){
171
            sb.append(" ").append(td.getCitation().getTitleCache());
172
            if(td.getCitationMicroReference() != null){
173
                sb.append(" :").append(td.getCitationMicroReference());
174
            }
175
        }
176
        if(td.isNotDesignated()){
177
            sb.append(" not designated");
178
        }
179

  
180
        return sb.toString();
181
    }
182

  
183
    public String print(){
184
        return finalString;
185
    }
186
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/formatter/DateTimeFormat.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util.formatter;
10

  
11
public enum DateTimeFormat {
12
    /**
13
     * yyyy-mm-dd or yyyy-mm or yyyy
14
     */
15
    ISO8601_DATE,
16
    /**
17
     * dd.mm.yyyy or mm.yyyy or yyyy
18
     */
19
    DMY_DOT,
20
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/formatter/PartialFormatter.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util.formatter;
10

  
11
import org.joda.time.Partial;
12
import org.joda.time.format.DateTimeFormatter;
13
import org.joda.time.format.ISODateTimeFormat;
14

  
15
import eu.etaxonomy.cdm.model.common.TimePeriod;
16

  
17
/**
18
 * @author a.kohlbecker
19
 * @since Apr 28, 2017
20
 *
21
 */
22
public class PartialFormatter {
23

  
24
    private DateTimeFormat format;
25

  
26
    private static final DateTimeFormatter dmyDotDayMonthYear =  org.joda.time.format.DateTimeFormat.forPattern("dd.MM.y");
27

  
28
    private static final DateTimeFormatter dmyDotMonthYear =  org.joda.time.format.DateTimeFormat.forPattern("MM.y");
29

  
30
    private static final DateTimeFormatter dmyDotYear =  org.joda.time.format.DateTimeFormat.forPattern("y");
31

  
32
    public PartialFormatter(DateTimeFormat format) {
33
        this.format = format;
34
    }
35

  
36
    public String print(Partial partial) {
37
        switch (format) {
38
            case DMY_DOT:
39
                return partial.toString(determine_DMY_DOT_Formatter(partial));
40
            case ISO8601_DATE:
41
            default:
42
                return partial.toString(determine_ISO860_Formatter(partial));
43
        }
44
    }
45

  
46
    /**
47
     * @param partial
48
     * @return
49
     */
50
    private DateTimeFormatter determine_ISO860_Formatter(Partial partial) {
51
        if (partial.isSupported(TimePeriod.DAY_TYPE)) {
52
            return ISODateTimeFormat.yearMonthDay();
53
        }
54
        if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
55
            return ISODateTimeFormat.yearMonth();
56
        }
57
        return ISODateTimeFormat.year();
58

  
59
    }
60

  
61
    /**
62
     * @param partial
63
     * @return
64
     */
65
    private DateTimeFormatter determine_DMY_DOT_Formatter(Partial partial) {
66
        if (partial.isSupported(TimePeriod.DAY_TYPE)) {
67
            return dmyDotDayMonthYear;
68
        }
69
        if (partial.isSupported(TimePeriod.MONTH_TYPE)) {
70
            return dmyDotMonthYear;
71
        }
72
        return dmyDotYear;
73

  
74
    }
75

  
76
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/formatter/TimePeriodFormatter.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util.formatter;
10

  
11
import org.apache.commons.lang.StringUtils;
12

  
13
import eu.etaxonomy.cdm.model.common.TimePeriod;
14

  
15
/**
16
 *
17
 * FIXME move into cdmlib
18
 *
19
 * @author a.kohlbecker
20
 * @since Apr 26, 2017
21
 *
22
 */
23
public class TimePeriodFormatter {
24

  
25
    private PartialFormatter partialFormatter;
26

  
27
    public TimePeriodFormatter(DateTimeFormat format) {
28
        partialFormatter = new PartialFormatter(format);
29
    }
30

  
31
    public String print(TimePeriod timePeriod) {
32

  
33
        if ( StringUtils.isNotBlank(timePeriod.getFreeText())){
34
           return timePeriod.getFreeText();
35
        }else{
36
            StringBuffer sb = new StringBuffer();
37
            if (timePeriod.getStart() != null) {
38
                sb.append(partialFormatter.print(timePeriod.getStart()));
39
            }
40
            if (timePeriod.getEnd() != null) {
41
                if (sb.length() > 0) {
42
                    sb.append(" - ");
43
                }
44
                sb.append(partialFormatter.print(timePeriod.getEnd()));
45
            }
46
            return sb.toString();
47
        }
48
    }
49

  
50
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationDTO.java
23 23
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
24 24
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
25 25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.vaadin.util.TypeDesignationConverter;
26
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter;
27 27

  
28 28
public class RegistrationDTO{
29 29

  
src/test/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverterTest.java
28 28
import eu.etaxonomy.cdm.model.reference.Reference;
29 29
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30 30
import eu.etaxonomy.cdm.vaadin.CdmVaadinBaseTest;
31
import eu.etaxonomy.cdm.vaadin.util.TypeDesignationConverter;
32 31

  
33 32
/**
34 33
 * @author a.kohlbecker
src/test/java/eu/etaxonomy/cdm/vaadin/util/formatter/PartialAndTimePeriodFormatterTest.java
1
/**
2
* Copyright (C) 2017 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.vaadin.util.formatter;
10

  
11
import org.joda.time.DateTimeFieldType;
12
import org.joda.time.Partial;
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

  
19
/**
20
 * @author a.kohlbecker
21
 * @since Apr 28, 2017
22
 *
23
 */
24
public class PartialAndTimePeriodFormatterTest extends Assert {
25

  
26
    Partial dmy;
27
    Partial my;
28
    Partial y;
29

  
30
    @Before
31
    public void init(){
32
        dmy = new Partial(new DateTimeFieldType[]{
33
                DateTimeFieldType.year(),
34
                DateTimeFieldType.monthOfYear(),
35
                DateTimeFieldType.dayOfMonth()
36

  
37
        }, new int[] {1969, 04, 12});
38

  
39
        my = new Partial(new DateTimeFieldType[]{
40
                DateTimeFieldType.year(),
41
                DateTimeFieldType.monthOfYear()
42

  
43
        }, new int[] {1969, 04});
44

  
45
        y = new Partial(new DateTimeFieldType[]{
46
                DateTimeFieldType.year()
47

  
48
        }, new int[] {1969});
49
    }
50

  
51
    @Test
52
    public void test_Partial_ISO8601_DATE() {
53
        PartialFormatter f = new PartialFormatter(DateTimeFormat.ISO8601_DATE);
54
        assertEquals("1969-04-12", f.print(dmy));
55
        assertEquals("1969-04", f.print(my));
56
        assertEquals("1969", f.print(y));
57
    }
58

  
59
    @Test
60
    public void test_Partial_DMY_DOT() {
61
        PartialFormatter f = new PartialFormatter(DateTimeFormat.DMY_DOT);
62
        assertEquals("12.04.1969", f.print(dmy));
63
        assertEquals("04.1969", f.print(my));
64
        assertEquals("1969", f.print(y));
65
    }
66

  
67
    @Test
68
    public void test_TimePeriod_ISO8601_DATE() {
69
        TimePeriodFormatter f = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
70

  
71
        TimePeriod p1 = TimePeriod.NewInstance();
72
        p1.setStart(my);
73
        assertEquals("1969-04", f.print(p1));
74

  
75
        TimePeriod p2 = TimePeriod.NewInstance();
76
        p2.setEnd(dmy);
77
        assertEquals("1969-04-12", f.print(p2));
78

  
79
        TimePeriod p3 = TimePeriod.NewInstance(my, dmy);
80
        assertEquals("1969-04 - 1969-04-12", f.print(p3));
81

  
82
        TimePeriod p4 = TimePeriod.NewInstance(my, dmy);
83
        p4.setFreeText("in April 1969");
84
        assertEquals("in April 1969", f.print(p4));
85
    }
86

  
87
    @Test
88
    public void test_TimePeriod_DMY_DOT() {
89
        TimePeriodFormatter f = new TimePeriodFormatter(DateTimeFormat.DMY_DOT);
90

  
91
        TimePeriod p1 = TimePeriod.NewInstance();
92
        p1.setStart(my);
93
        assertEquals("04.1969", f.print(p1));
94

  
95
        TimePeriod p2 = TimePeriod.NewInstance();
96
        p2.setEnd(dmy);
97
        assertEquals("12.04.1969", f.print(p2));
98

  
99
        TimePeriod p3 = TimePeriod.NewInstance(my, dmy);
100
        assertEquals("04.1969 - 12.04.1969", f.print(p3));
101

  
102
        TimePeriod p4 = TimePeriod.NewInstance(my, dmy);
103
        p4.setFreeText("in April 1969");
104
        assertEquals("in April 1969", f.print(p4));
105
    }
106

  
107
}

Also available in: Unified diff