Project

General

Profile

Download (11.3 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.Optional;
17
import java.util.Set;
18
import java.util.UUID;
19

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

    
24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25
import eu.etaxonomy.cdm.model.common.TimePeriod;
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.IReference;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
35
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
36
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO;
37
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager;
38
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
39

    
40
public class RegistrationDTO{
41

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

    
44
    private String summary = "";
45

    
46
    private RegistrationType registrationType;
47

    
48
    private IReference citation = null;
49

    
50
    private String citationDetail = null;
51

    
52
    private String submitterUserName = null;
53

    
54
    private EntityReference name = null;
55

    
56
    private TypeDesignationSetManager typeDesignationManager;
57

    
58
    private Registration reg;
59

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

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

    
64

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

    
72
         this.reg = reg;
73

    
74
         registrationType = RegistrationType.from(reg);
75

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

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

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

    
117
    }
118

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

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

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

    
150

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

    
162

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

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

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

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

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

    
195

    
196
    /**
197
     * The entity ID of the Registration Item
198
     * @return
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
    public void setCitation(IReference citation) throws Exception {
242
        if(this.citation == null){
243
            this.citation = citation;
244
        } else {
245
            throw new Exception("Can not set the citation on a non emtpy RegistrationDTO");
246
        }
247
    }
248

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

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

    
260
    public EntityReference getName() {
261
        return name;
262
    }
263

    
264
    public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationWorkingSets() {
265
        return typeDesignationManager != null ? typeDesignationManager.getOrderdTypeDesignationWorkingSets() : null;
266
    }
267

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

    
274
    }
275

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

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

    
297
    /**
298
     *
299
     * @param workingSetId
300
     * @return the TypeDesignationWorkingSet in this DTO with the matching workingSetId or NULL
301
     */
302
    public TypeDesignationWorkingSet getTypeDesignationWorkingSet(int workingSetId) {
303
        Optional<TypeDesignationWorkingSet> workingSetOptional = getOrderdTypeDesignationWorkingSets().values().stream().filter(workingSet -> workingSet.getWorkingSetId() == workingSetId).findFirst();
304
        if(workingSetOptional.isPresent()){
305
            return workingSetOptional.get();
306
        }
307
        return null;
308

    
309
    }
310

    
311
    /**
312
     * @param ref
313
     * @return
314
     */
315
    private TypeDesignationBase findTypeDesignation(EntityReference ref) {
316
        return typeDesignationManager != null ? typeDesignationManager.findTypeDesignation(ref) : null;
317
    }
318

    
319
    public Collection<TypeDesignationBase> getTypeDesignations() {
320
        return typeDesignationManager != null ? typeDesignationManager.getTypeDesignations() : null;
321
    }
322

    
323
    /**
324
     * @return the citationString
325
     */
326
    public String getNomenclaturalCitationString() {
327
        if(citation == null){
328
            return null;
329
        }
330
        if(INomenclaturalReference.class.isAssignableFrom(citation.getClass())){
331
            return ((INomenclaturalReference)citation).getNomenclaturalCitation(citationDetail);
332
        } else {
333
            logger.error("The citation is not a NomenclaturalReference");
334
            return citation.generateTitle();
335
        }
336
    }
337

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

    
351
            }
352

    
353
        }
354
    }
355

    
356
    /**
357
     * @return the blockedBy
358
     */
359
    public Set<Registration> getBlockedBy() {
360
        return blockedBy;
361
    }
362

    
363
    /**
364
     * @return
365
     */
366
    public List<String> getMessages() {
367
        return messages;
368
    }
369

    
370
}
(5-5/19)