Project

General

Profile

Download (11.6 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.Collection;
13
import java.util.HashSet;
14
import java.util.LinkedHashMap;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.commons.lang3.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.joda.time.DateTime;
22

    
23
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24
import eu.etaxonomy.cdm.model.common.TimePeriod;
25
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
26
import eu.etaxonomy.cdm.model.name.Registration;
27
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
28
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
29
import eu.etaxonomy.cdm.model.name.TaxonName;
30
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
31
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
34
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
35
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
36
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager;
37
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
38

    
39
public class RegistrationDTO{
40

    
41
    private static final Logger logger = Logger.getLogger(RegistrationDTO.class);
42

    
43
    private String summary = "";
44

    
45
    private RegistrationType registrationType;
46

    
47
    private Reference citation = null;
48

    
49
    private String citationDetail = null;
50

    
51
    private String submitterUserName = null;
52

    
53
    private EntityReference name = null;
54

    
55
    private TypeDesignationSetManager typeDesignationManager;
56

    
57
    private Registration reg;
58

    
59
    private List<String> messages = new ArrayList<>();
60

    
61
    // private Set<eu.etaxonomy.cdm.model.name.Registration> blockedBy = new HashSet<>();
62

    
63

    
64
    /**
65
     * @param reg
66
     * @param typifiedName should be provided in for Registrations for TypeDesignations
67
     * @throws RegistrationValidationException
68
     */
69
    public RegistrationDTO(Registration reg) {
70

    
71
         this.reg = reg;
72

    
73
         registrationType = RegistrationType.from(reg);
74

    
75
         if(reg.getSubmitter() != null ){
76
             submitterUserName = reg.getSubmitter().getUsername();
77
         }
78

    
79
        if(hasName(reg)){
80
            citation = (Reference) reg.getName().getNomenclaturalReference();
81
            citationDetail = reg.getName().getNomenclaturalMicroReference();
82
            name = new EntityReference(reg.getName().getId(), reg.getName().getTitleCache());
83
        }
84
        if(hasTypifications(reg)){
85
            if(!reg.getTypeDesignations().isEmpty()){
86
                for(TypeDesignationBase td : reg.getTypeDesignations()){
87
                    if(citation == null) {
88
                        citation = td.getCitation();
89
                        citationDetail = td.getCitationMicroReference();
90
                    }
91
                }
92
            }
93
        }
94
        switch(registrationType) {
95
        case EMPTY:
96
            summary = "BLANK REGISTRATION";
97
            break;
98
        case NAME:
99
            summary = reg.getName().getTitleCache();
100
            break;
101
        case NAME_AND_TYPIFICATION:
102
        case TYPIFICATION:
103
        default:
104
            try {
105
                typeDesignationManager = new TypeDesignationSetManager(reg.getTypeDesignations());
106
                summary = typeDesignationManager.buildString().print();
107
            } catch (RegistrationValidationException e) {
108
                messages.add("Validation errors: " + e.getMessage());
109
            }
110
            break;
111
        }
112

    
113
        // trigger initialization of the reference
114
        getNomenclaturalCitationString();
115

    
116
    }
117

    
118
    /**
119
     * To create an initially empty DTO for which only the <code>typifiedName</code> and the <code>publication</code> are defined.
120
     * All TypeDesignations added to the <code>Registration</code> need to refer to the same <code>typifiedName</code> and must be
121
     * published in the same <code>publication</code>.
122
     *
123
     * @param reg
124
     * @param typifiedName
125
     */
126
    public RegistrationDTO(Registration reg, TaxonName typifiedName, Reference publication) {
127
        this.reg = reg;
128
        citation = publication;
129
        // create a TypeDesignationSetManager with only a reference to the typifiedName for validation
130
        typeDesignationManager = new TypeDesignationSetManager(typifiedName);
131
    }
132

    
133
    /**
134
     * @param reg
135
     * @return
136
     */
137
    private boolean hasTypifications(Registration reg) {
138
        return reg.getTypeDesignations() != null && reg.getTypeDesignations().size() > 0;
139
    }
140

    
141
    /**
142
     * @param reg
143
     * @return
144
     */
145
    private boolean hasName(Registration reg) {
146
        return reg.getName() != null;
147
    }
148

    
149

    
150
    /**
151
     * Provides access to the Registration entity this DTO has been build from.
152
     * This method is purposely not a getter to hide the original Registration
153
     * from generic processes which are exposing, binding bean properties.
154
     *IReference
155
     * @return
156
     */
157
    public Registration registration() {
158
        return reg;
159
    }
160

    
161

    
162
    /**
163
     * @return the summary
164
     */
165
    public String getSummary() {
166
        return summary;
167
    }
168

    
169
    public String getSubmitterUserName(){
170
        return submitterUserName;
171
    }
172

    
173
    /**
174
     * @return the registrationType
175
     */
176
    public RegistrationType getRegistrationType() {
177
        return registrationType;
178
    }
179

    
180
    /**
181
     * @return the status
182
     */
183
    public RegistrationStatus getStatus() {
184
        return reg.getStatus();
185
    }
186

    
187
    /**
188
     * @return the identifier
189
     */
190
    public String getIdentifier() {
191
        return reg.getIdentifier();
192
    }
193

    
194

    
195
    /**
196
     * The entity ID of the Registration Item
197
     * @return
198
     */
199
    public int getId() {
200
        return reg.getId();
201
    }
202

    
203

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

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

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

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

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

    
236
    public Reference getCitation() {
237
        return citation;
238
    }
239

    
240
    public void setCitation(Reference citation) throws Exception {
241
        if(this.citation == null){
242
            this.citation = citation;
243
        } else {
244
            throw new Exception("Can not set the citation on a non emtpy RegistrationDTO");
245
        }
246
    }
247

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

    
255
    public EntityReference getTypifiedNameRef() {
256
        return typeDesignationManager != null ? typeDesignationManager.getTypifiedNameRef() : null;
257
    }
258

    
259
    public TaxonName getTypifiedName() {
260
        return typeDesignationManager != null ? typeDesignationManager.getTypifiedName() : null;
261
    }
262

    
263
    public EntityReference getNameRef() {
264
        return name;
265
    }
266

    
267
    public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationWorkingSets() {
268
        return typeDesignationManager != null ? typeDesignationManager.getOrderdTypeDesignationWorkingSets() : null;
269
    }
270

    
271
    /**
272
     * @param baseEntityReference
273
     */
274
    public TypeDesignationWorkingSet getTypeDesignationWorkingSet(TypedEntityReference baseEntityReference) {
275
        return typeDesignationManager != null ? typeDesignationManager.getOrderdTypeDesignationWorkingSets().get(baseEntityReference) : null;
276

    
277
    }
278

    
279
    /**
280
     * @param baseEntityReference
281
     */
282
    public Set<TypeDesignationBase> getTypeDesignationsInWorkingSet(TypedEntityReference baseEntityReference) {
283
        Set<TypeDesignationBase> typeDesignations = new HashSet<>();
284
        TypeDesignationWorkingSet workingSet = getTypeDesignationWorkingSet(baseEntityReference);
285
        for(EntityReference ref :  workingSet.getTypeDesignations()){
286
            typeDesignations.add(findTypeDesignation(ref));
287
        }
288
        return typeDesignations;
289
    }
290

    
291
    public SpecimenTypeDesignationWorkingSetDTO<Registration> getSpecimenTypeDesignationWorkingSetDTO(TypedEntityReference baseEntityReference) {
292
        Set<TypeDesignationBase> typeDesignations = getTypeDesignationsInWorkingSet(baseEntityReference);
293
        List<SpecimenTypeDesignation> specimenTypeDesignations = new ArrayList<>(typeDesignations.size());
294
        typeDesignations.forEach(td -> specimenTypeDesignations.add((SpecimenTypeDesignation)td));
295
        IdentifiableEntity<?> baseEntity = getTypeDesignationWorkingSet(baseEntityReference).getBaseEntity();
296

    
297
        SpecimenTypeDesignationWorkingSetDTO<Registration> dto = new SpecimenTypeDesignationWorkingSetDTO<Registration>(reg,
298
                baseEntity, specimenTypeDesignations, getCitation(), getTypifiedName());
299
        return dto;
300
    }
301

    
302
    public NameTypeDesignation getNameTypeDesignation(TypedEntityReference baseEntityReference) {
303
        Set<TypeDesignationBase> typeDesignations = getTypeDesignationsInWorkingSet(baseEntityReference);
304
        if(typeDesignations.size() == 1){
305
            TypeDesignationBase item = typeDesignations.iterator().next();
306
            return (NameTypeDesignation)item ;
307
        }
308
        if(typeDesignations.size() == 0){
309
            return null;
310
        }
311
        if(typeDesignations.size() > 1){
312
            throw new RuntimeException("Workingsets of NameTypeDesignations must contain exactly one item.");
313
        }
314
        return null;
315
    }
316

    
317
    /**
318
     * @param ref
319
     * @return
320
     */
321
    private TypeDesignationBase findTypeDesignation(EntityReference ref) {
322
        return typeDesignationManager != null ? typeDesignationManager.findTypeDesignation(ref) : null;
323
    }
324

    
325
    public Collection<TypeDesignationBase> getTypeDesignations() {
326
        return typeDesignationManager != null ? typeDesignationManager.getTypeDesignations() : null;
327
    }
328

    
329
    /**
330
     * @return the citationString
331
     */
332
    public String getNomenclaturalCitationString() {
333
        if(citation == null){
334
            return null;
335
        }
336
        if(INomenclaturalReference.class.isAssignableFrom(citation.getClass())){
337
            return ((INomenclaturalReference)citation).getNomenclaturalCitation(citationDetail);
338
        } else {
339
            logger.error("The citation is not a NomenclaturalReference");
340
            return citation.generateTitle();
341
        }
342
    }
343

    
344
    /**
345
     * @return the citationString
346
     */
347
    public String getBibliographicCitationString() {
348
        if(citation == null){
349
            return null;
350
        } else {
351
            if(StringUtils.isNotEmpty(citationDetail)){
352
                // TODO see https://dev.e-taxonomy.eu/redmine/issues/6623
353
                return citation.generateTitle().replaceAll("\\.$", "") + (StringUtils.isNotEmpty(citationDetail) ? ": " + citationDetail : "");
354
            } else {
355
                return citation.generateTitle();
356

    
357
            }
358

    
359
        }
360
    }
361

    
362
    /**
363
     * @return the blockedBy
364
     */
365
    public Set<Registration> getBlockedBy() {
366
        return reg.getBlockedBy();
367
    }
368

    
369
    /**
370
     * @return
371
     */
372
    public List<String> getMessages() {
373
        return messages;
374
    }
375

    
376
}
(5-5/19)