Project

General

Profile

Download (7.71 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.Map;
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.TimePeriod;
25
import eu.etaxonomy.cdm.model.name.Registration;
26
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
27
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
28
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
29
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
30
import eu.etaxonomy.cdm.model.reference.IReference;
31
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
32
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
33
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter;
34

    
35
public class RegistrationDTO{
36

    
37
    private static final Logger logger = Logger.getLogger(RegistrationDTO.class);
38

    
39
    private String summary = "";
40

    
41
    private RegistrationType registrationType;
42

    
43
    private IReference citation = null;
44

    
45
    private String citationDetail = null;
46

    
47
    private String submitterUserName = null;
48

    
49
    private EntityReference name = null;
50

    
51
    private TypeDesignationConverter typeDesignationConverter;
52

    
53
    private Registration reg;
54

    
55
    private List<String> messages = new ArrayList<>();
56

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

    
59

    
60
    /**
61
     * @param reg
62
     * @param typifiedName should be provided in for Registrations for TypeDesignations
63
     * @throws RegistrationValidationException
64
     */
65
    public RegistrationDTO(Registration reg) {
66

    
67
         this.reg = reg;
68

    
69
         registrationType = RegistrationType.from(reg);
70

    
71
         if(reg.getSubmitter() != null ){
72
             submitterUserName = reg.getSubmitter().getUsername();
73
         }
74

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

    
109
        // trigger initialization of the reference
110
        getNomenclaturalCitationString();
111

    
112
    }
113

    
114
    /**
115
     * @param reg
116
     * @return
117
     */
118
    private boolean hasTypifications(Registration reg) {
119
        return reg.getTypeDesignations() != null && reg.getTypeDesignations().size() > 0;
120
    }
121

    
122
    /**
123
     * @param reg
124
     * @return
125
     */
126
    private boolean hasName(Registration reg) {
127
        return reg.getName() != null;
128
    }
129

    
130

    
131
    /**
132
     * Provides access to the Registration entity this DTO has been build from.
133
     * This method is purposely not a getter to hide the original Registration
134
     * from generic processes which are exposing, binding bean properties.
135
     *IReference
136
     * @return
137
     */
138
    public Registration registration() {
139
        return reg;
140
    }
141

    
142

    
143
    /**
144
     * @return the summary
145
     */
146
    public String getSummary() {
147
        return summary;
148
    }
149

    
150
    public String getSubmitterUserName(){
151
        return submitterUserName;
152
    }
153

    
154
    /**
155
     * @return the registrationType
156
     */
157
    public RegistrationType getRegistrationType() {
158
        return registrationType;
159
    }
160

    
161
    /**
162
     * @return the status
163
     */
164
    public RegistrationStatus getStatus() {
165
        return reg.getStatus();
166
    }
167

    
168
    /**
169
     * @return the identifier
170
     */
171
    public String getIdentifier() {
172
        return reg.getIdentifier();
173
    }
174

    
175

    
176
    /**
177
     * The entity ID of the Registration Item
178
     * @return
179
     */
180
    public int getId() {
181
        return reg.getId();
182
    }
183

    
184

    
185
    public UUID getUuid() {
186
        return reg.getUuid();
187
    }
188

    
189
    /**
190
     * @return the specificIdentifier
191
     */
192
    public String getSpecificIdentifier() {
193
        return reg.getSpecificIdentifier();
194
    }
195

    
196
    /**
197
     * @return the registrationDate
198
     */
199
    public DateTime getRegistrationDate() {
200
        return reg.getRegistrationDate();
201
    }
202

    
203
    /**
204
     * @return the registrationDate
205
     */
206
    public TimePeriod getDatePublished() {
207
        return citation == null ? null : citation.getDatePublished();
208
    }
209

    
210
    /**
211
     * @return the created
212
     */
213
    public DateTime getCreated() {
214
        return reg.getCreated();
215
    }
216

    
217
    public IReference getCitation() {
218
        return citation;
219
    }
220

    
221
    /**
222
     * @return the citationID
223
     */
224
    public Integer getCitationID() {
225
        return citation == null ? null : citation.getId();
226
    }
227

    
228
    public EntityReference getTypifiedName() {
229
        return typeDesignationConverter != null ? typeDesignationConverter.getTypifiedName() : null;
230
    }
231

    
232
    public EntityReference getName() {
233
        return name;
234
    }
235

    
236
    public LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> getOrderdTypeDesignationEntitiyReferences() {
237
        return typeDesignationConverter != null ? typeDesignationConverter.getOrderedTypeDesignations() : null;
238
    }
239

    
240
    public Collection<TypeDesignationBase> getTypeDesignations() {
241
        return typeDesignationConverter != null ? typeDesignationConverter.getTypeDesignations() : null;
242
    }
243

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

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

    
272
            }
273

    
274
        }
275
    }
276

    
277
    /**
278
     * @return the blockedBy
279
     */
280
    public Set<Registration> getBlockedBy() {
281
        return blockedBy;
282
    }
283

    
284
    /**
285
     * @return
286
     */
287
    public List<String> getMessages() {
288
        return messages;
289
    }
290

    
291
}
(5-5/17)