Project

General

Profile

Download (7.52 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.log4j.Logger;
18
import org.joda.time.DateTime;
19

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

    
29
public class RegistrationDTO{
30

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

    
33
    private String summary = "";
34

    
35
    private RegistrationType registrationType;
36

    
37
    private IReference citation = null;
38

    
39
    private String citationDetail = null;
40

    
41
    private Registration reg;
42

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

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

    
47
    private TaxonNameBase<?, ?> typifiedName;
48

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

    
55
         this.reg = reg;
56

    
57
         registrationType = RegistrationType.from(reg);
58

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

    
91
        // trigger initialization of the reference
92
        getCitationString();
93

    
94
    }
95

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

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

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

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

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

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

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

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

    
155
        return typifiedName;
156
    }
157

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

    
169

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

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

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

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

    
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 IReference getCitation() {
237
        return citation;
238
    }
239

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

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

    
262
    /**
263
     * @return the blockedBy
264
     */
265
    public Set<Registration> getBlockedBy() {
266
        return blockedBy;
267
    }
268

    
269
    /**
270
     * @return
271
     */
272
    public List<String> getMessages() {
273
        return messages;
274
    }
275

    
276
}
(5-5/14)