Project

General

Profile

Download (8.13 KB) Statistics
| Branch: | Tag: | Revision:
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.view.registration;
10

    
11
import java.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
17
import org.apache.commons.lang3.StringUtils;
18
import org.apache.log4j.Logger;
19
import org.joda.time.DateTime;
20

    
21
import eu.etaxonomy.cdm.model.common.TimePeriod;
22
import eu.etaxonomy.cdm.model.name.Registration;
23
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
24
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
26
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
27
import eu.etaxonomy.cdm.model.reference.IReference;
28
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter;
29

    
30
public class RegistrationDTO{
31

    
32
    private static final Logger logger = Logger.getLogger(RegistrationDTO.class);
33

    
34
    private String summary = "";
35

    
36
    private RegistrationType registrationType;
37

    
38
    private IReference citation = null;
39

    
40
    private String citationDetail = null;
41

    
42
    private Registration reg;
43

    
44
    private List<String> messages = new ArrayList<>();
45

    
46
    private Set<eu.etaxonomy.cdm.model.name.Registration> blockedBy = new HashSet<>();
47

    
48
    private TaxonNameBase<?, ?> typifiedName;
49

    
50
    /**
51
     * @param reg
52
     * @param typifiedName should be provided in for Registrations for TypeDesignations
53
     */
54
    public RegistrationDTO(Registration reg) {
55

    
56
         this.reg = reg;
57

    
58
         registrationType = RegistrationType.from(reg);
59

    
60
        if(hasName(reg)){
61
            citation = reg.getName().getNomenclaturalReference();
62
            citationDetail = reg.getName().getNomenclaturalMicroReference();
63
        }
64
        if(hasTypifications(reg)){
65
            try {
66
                typifiedName = findTypifiedName();
67
            } catch (RegistrationValidationException e) {
68
                messages.add("Validation errors: " + e.getMessage());
69
            }
70
            if(!reg.getTypeDesignations().isEmpty()){
71
                if(citation == null) {
72
                    TypeDesignationBase first = reg.getTypeDesignations().iterator().next();
73
                    citation = first.getCitation();
74
                    citationDetail = first.getCitationMicroReference();
75
                }
76
            }
77
        }
78
        switch(registrationType) {
79
        case EMPTY:
80
            summary = "BLANK REGISTRATION";
81
            break;
82
        case NAME:
83
            summary = reg.getName().getTitleCache();
84
            break;
85
        case NAME_AND_TYPIFICATION:
86
        case TYPIFICATION:
87
        default:
88
            summary = new TypeDesignationConverter(reg.getTypeDesignations(), typifiedName).buildString().print();
89
            break;
90
        }
91

    
92
        // trigger initialization of the reference
93
        getNomenclaturalCitationString();
94

    
95
    }
96

    
97
    /**
98
     * @param reg
99
     * @return
100
     */
101
    private boolean hasTypifications(Registration reg) {
102
        return reg.getTypeDesignations() != null && reg.getTypeDesignations().size() > 0;
103
    }
104

    
105
    /**
106
     * @param reg
107
     * @return
108
     */
109
    private boolean hasName(Registration reg) {
110
        return reg.getName() != null;
111
    }
112

    
113
    /**
114
     * FIXME use the validation framework validators and to store the validation problems!!!
115
     *
116
     * @return
117
     * @throws RegistrationValidationException
118
     */
119
    private TaxonNameBase<?,?> findTypifiedName() throws RegistrationValidationException {
120

    
121
        List<String> problems = new ArrayList<>();
122

    
123
        TaxonNameBase<?,?> typifiedName = null;
124

    
125
        for(TypeDesignationBase<?> typeDesignation : reg.getTypeDesignations()){
126
            typeDesignation.getTypifiedNames();
127
            if(typeDesignation.getTypifiedNames().isEmpty()){
128

    
129
                //TODO instead throw RegistrationValidationException()
130
                problems.add("Missing typifiedName in " + typeDesignation.toString());
131
                continue;
132
            }
133
            if(typeDesignation.getTypifiedNames().size() > 1){
134
              //TODO instead throw RegistrationValidationException()
135
                problems.add("Multiple typifiedName in " + typeDesignation.toString());
136
                continue;
137
            }
138
            if(typifiedName == null){
139
                // remember
140
                typifiedName = typeDesignation.getTypifiedNames().iterator().next();
141
            } else {
142
                // compare
143
                TaxonNameBase<?,?> otherTypifiedName = typeDesignation.getTypifiedNames().iterator().next();
144
                if(typifiedName.getId() != otherTypifiedName.getId()){
145
                  //TODO instead throw RegistrationValidationException()
146
                    problems.add("Multiple typifiedName in " + typeDesignation.toString());
147
                }
148
            }
149

    
150
        }
151
        if(!problems.isEmpty()){
152
            // FIXME use the validation framework
153
            throw new RegistrationValidationException("Inconsistent Registration entity. " + reg.toString(), problems);
154
        }
155

    
156
        return typifiedName;
157
    }
158

    
159
    /**
160
     * Provides access to the Registration entity this DTO has been build from.
161
     * This method is purposely not a getter to hide the original Registration
162
     * from generic processes which are exposing, binding bean properties.
163
     *IReference
164
     * @return
165
     */
166
    public Registration registration() {
167
        return reg;
168
    }
169

    
170

    
171
    /**
172
     * @return the summary
173
     */
174
    public String getSummary() {
175
        return summary;
176
    }
177

    
178
    /**
179
     * @return the registrationType
180
     */
181
    public RegistrationType getRegistrationType() {
182
        return registrationType;
183
    }
184

    
185
    /**
186
     * @return the status
187
     */
188
    public RegistrationStatus getStatus() {
189
        return reg.getStatus();
190
    }
191

    
192
    /**
193
     * @return the identifier
194
     */
195
    public String getIdentifier() {
196
        return reg.getIdentifier();
197
    }
198

    
199

    
200
    public int getId() {
201
        return reg.getId();
202
    }
203

    
204

    
205
    public UUID getUuid() {
206
        return reg.getUuid();
207
    }
208

    
209
    /**
210
     * @return the specificIdentifier
211
     */
212
    public String getSpecificIdentifier() {
213
        return reg.getSpecificIdentifier();
214
    }
215

    
216
    /**
217
     * @return the registrationDate
218
     */
219
    public DateTime getRegistrationDate() {
220
        return reg.getRegistrationDate();
221
    }
222

    
223
    /**
224
     * @return the registrationDate
225
     */
226
    public TimePeriod getDatePublished() {
227
        return citation == null ? null : citation.getDatePublished();
228
    }
229

    
230
    /**
231
     * @return the created
232
     */
233
    public DateTime getCreated() {
234
        return reg.getCreated();
235
    }
236

    
237
    public IReference getCitation() {
238
        return citation;
239
    }
240

    
241
    /**
242
     * @return the citationID
243
     */
244
    public Integer getCitationID() {
245
        return citation == null ? null : citation.getId();
246
    }
247

    
248
    /**
249
     * @return the citationString
250
     */
251
    public String getNomenclaturalCitationString() {
252
        if(citation == null){
253
            return null;
254
        }
255
        if(INomenclaturalReference.class.isAssignableFrom(citation.getClass())){
256
            return ((INomenclaturalReference)citation).getNomenclaturalCitation(citationDetail);
257
        } else {
258
            logger.error("The citation is not a NomenclaturalReference");
259
            return citation.generateTitle();
260
        }
261
    }
262

    
263
    /**
264
     * @return the citationString
265
     */
266
    public String getBibliographicCitationString() {
267
        if(citation == null){
268
            return null;
269
        } else {
270
            if(StringUtils.isNotEmpty(citationDetail)){
271
                // TODO see https://dev.e-taxonomy.eu/redmine/issues/6623
272
                return citation.generateTitle().replaceAll("\\.$", "") + (StringUtils.isNotEmpty(citationDetail) ? ": " + citationDetail : "");
273
            } else {
274
                return citation.generateTitle();
275

    
276
            }
277

    
278
        }
279
    }
280

    
281
    /**
282
     * @return the blockedBy
283
     */
284
    public Set<Registration> getBlockedBy() {
285
        return blockedBy;
286
    }
287

    
288
    /**
289
     * @return
290
     */
291
    public List<String> getMessages() {
292
        return messages;
293
    }
294

    
295
}
(5-5/14)