Project

General

Profile

Download (6.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.presenter.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.joda.time.DateTime;
18

    
19
import eu.etaxonomy.cdm.mock.Registration;
20
import eu.etaxonomy.cdm.mock.RegistrationStatus;
21
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
23
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.vaadin.util.TypeDesignationConverter;
26

    
27
public class RegistrationDTO{
28

    
29
    private String summary = "";
30

    
31
    private String citationString = "";
32

    
33
    private int citationID;
34

    
35
    private RegistrationType registrationType;
36

    
37
    private Registration reg;
38

    
39
    private List<String> messages = new ArrayList<>();
40

    
41
    private Set<Registration> blockedBy = new HashSet<>();
42

    
43
    private TaxonNameBase<?, ?> typifiedName;
44

    
45
    /**
46
     * @param reg
47
     * @param typifiedName should be provided in for Registrations for TypeDesignations
48
     */
49
    public RegistrationDTO(Registration reg) {
50

    
51
         this.reg = reg;
52

    
53
        registrationType = RegistrationType.from(reg);
54
        if(registrationType.isName()){
55
            summary = reg.getName().getTitleCache();
56
            INomenclaturalReference citation = reg.getName().getNomenclaturalReference();
57
            if(citation != null){
58
                citationString = citation.generateTitle();
59
                citationID = citation.getId();
60
            }
61
        } else if(registrationType.isTypification()){
62
            try {
63
                typifiedName = findTypifiedName();
64
            } catch (RegistrationValidationException e) {
65
                messages.add("Validation errors: " + e.getMessage());
66
            }
67
            summary = new TypeDesignationConverter(reg.getTypeDesignations(), typifiedName)
68
                    .buildString().print();
69
            if(!reg.getTypeDesignations().isEmpty()){
70
                Reference citation = reg.getTypeDesignations().iterator().next().getCitation();
71
                if(citation != null) {
72
                    citationString = citation.generateTitle();
73
                    citationID = citation.getId();
74
                }
75
            }
76
        } else {
77
            summary = "- INVALID REGISTRATION -";
78
        }
79

    
80
        messages.add("dummy");
81
    }
82

    
83

    
84

    
85
    /**
86
     * FIXME use the validation framework validators and to store the validation problems!!!
87
     *
88
     * @return
89
     * @throws RegistrationValidationException
90
     */
91
    private TaxonNameBase<?,?> findTypifiedName() throws RegistrationValidationException {
92

    
93
        List<String> problems = new ArrayList<>();
94

    
95
        TaxonNameBase<?,?> typifiedName = null;
96

    
97
        for(TypeDesignationBase<?> typeDesignation : reg.getTypeDesignations()){
98
            typeDesignation.getTypifiedNames();
99
            if(typeDesignation.getTypifiedNames().isEmpty()){
100

    
101
                //TODO instead throw RegistrationValidationException()
102
                problems.add("Missing typifiedName in " + typeDesignation.toString());
103
                continue;
104
            }
105
            if(typeDesignation.getTypifiedNames().size() > 1){
106
              //TODO instead throw RegistrationValidationException()
107
                problems.add("Multiple typifiedName in " + typeDesignation.toString());
108
                continue;
109
            }
110
            if(typifiedName == null){
111
                // remember
112
                typifiedName = typeDesignation.getTypifiedNames().iterator().next();
113
            } else {
114
                // compare
115
                TaxonNameBase<?,?> otherTypifiedName = typeDesignation.getTypifiedNames().iterator().next();
116
                if(typifiedName.getId() != otherTypifiedName.getId()){
117
                  //TODO instead throw RegistrationValidationException()
118
                    problems.add("Multiple typifiedName in " + typeDesignation.toString());
119
                }
120
            }
121

    
122
        }
123
        if(!problems.isEmpty()){
124
            // FIXME use the validation framework
125
            throw new RegistrationValidationException("Inconsistent Registration entity. " + reg.toString(), problems);
126
        }
127

    
128
        return typifiedName;
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
     *
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

    
151
    /**
152
     * @return the registrationType
153
     */
154
    public RegistrationType getRegistrationType() {
155
        return registrationType;
156
    }
157

    
158

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

    
166

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

    
174

    
175
    public int getId() {
176
        return reg.getId();
177
    }
178

    
179

    
180
    public UUID getUuid() {
181
        return reg.getUuid();
182
    }
183

    
184
    /**
185
     * @return the specificIdentifier
186
     */
187
    public String getSpecificIdentifier() {
188
        return reg.getSpecificIdentifier();
189
    }
190

    
191
    /**
192
     * @return the registrationDate
193
     */
194
    public DateTime getRegistrationDate() {
195
        return reg.getRegistrationDate();
196
    }
197

    
198
    /**
199
     * @return the created
200
     */
201
    public DateTime getCreated() {
202
        return reg.getCreated();
203
    }
204

    
205
    public String getCitation() {
206
        return citationString;
207
    }
208

    
209
    /**
210
     * @return the blockedBy
211
     */
212
    public Set<Registration> getBlockedBy() {
213
        return blockedBy;
214
    }
215

    
216
    /**
217
     * @return the citationString
218
     */
219
    public String getCitationString() {
220
        return citationString;
221
    }
222

    
223
    /**
224
     * @return the citationID
225
     */
226
    public int getCitationID() {
227
        return citationID;
228
    }
229

    
230

    
231

    
232
    /**
233
     * @return
234
     */
235
    public List<String> getMessages() {
236
        return messages;
237
    }
238

    
239
}
(2-2/5)