Project

General

Profile

Download (8.21 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
    /**
201
     * The entity ID of the Registration Item
202
     * @return
203
     */
204
    public int getId() {
205
        return reg.getId();
206
    }
207

    
208

    
209
    public UUID getUuid() {
210
        return reg.getUuid();
211
    }
212

    
213
    /**
214
     * @return the specificIdentifier
215
     */
216
    public String getSpecificIdentifier() {
217
        return reg.getSpecificIdentifier();
218
    }
219

    
220
    /**
221
     * @return the registrationDate
222
     */
223
    public DateTime getRegistrationDate() {
224
        return reg.getRegistrationDate();
225
    }
226

    
227
    /**
228
     * @return the registrationDate
229
     */
230
    public TimePeriod getDatePublished() {
231
        return citation == null ? null : citation.getDatePublished();
232
    }
233

    
234
    /**
235
     * @return the created
236
     */
237
    public DateTime getCreated() {
238
        return reg.getCreated();
239
    }
240

    
241
    public IReference getCitation() {
242
        return citation;
243
    }
244

    
245
    /**
246
     * @return the citationID
247
     */
248
    public Integer getCitationID() {
249
        return citation == null ? null : citation.getId();
250
    }
251

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

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

    
280
            }
281

    
282
        }
283
    }
284

    
285
    /**
286
     * @return the blockedBy
287
     */
288
    public Set<Registration> getBlockedBy() {
289
        return blockedBy;
290
    }
291

    
292
    /**
293
     * @return
294
     */
295
    public List<String> getMessages() {
296
        return messages;
297
    }
298

    
299
}
(5-5/17)