Project

General

Profile

Download (6.75 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.model.registration;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Optional;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.joda.time.DateTime;
20

    
21
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
22
import eu.etaxonomy.cdm.api.service.exception.RegistrationValidationException;
23
import eu.etaxonomy.cdm.model.name.Registration;
24
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.model.reference.ReferenceType;
27

    
28
/**
29
 * @author a.kohlbecker
30
 * @since Mar 22, 2017
31
 *
32
 */
33
public class RegistrationWorkingSet {
34

    
35
    private List<RegistrationDTO> registrationDTOs = new ArrayList<>();
36

    
37
    private UUID citationUuid = null;
38

    
39
    private DateTime created = null;
40

    
41
    private String citationString = null;
42

    
43
    /**
44
     * Creates an empty working set
45
     */
46
    public RegistrationWorkingSet(Reference citation) {
47
        citationUuid = citation.getUuid();
48
        this.citationString= citation.getTitleCache();
49

    
50
    }
51

    
52
    public RegistrationWorkingSet(List<RegistrationDTO> registrationDTOs) throws RegistrationValidationException {
53
        validateAndAddDTOs(registrationDTOs, null);
54
    }
55

    
56
    /**
57
     * @param candidated
58
     * @throws RegistrationValidationException
59
     *
60
     */
61
    private void validateAndAdd(Set<Registration> candidates) throws RegistrationValidationException {
62
        List<RegistrationDTO> dtos = new ArrayList<>(registrationDTOs.size());
63
        candidates.forEach(reg -> dtos.add(new RegistrationDTO(reg)));
64
        validateAndAddDTOs(dtos, null);
65
    }
66

    
67
    /**
68
     * Validate and add all Registrations to the working set which are referring to the same publication
69
     * which is either the citation of the nomenclatural reference of the {@link TaxonName} or the
70
     * citation of the {@link TypeDesignations}. In case the citation is a section and this section is
71
     * having an in-reference the in-reference will be used instead.
72
     * Registration with a differing publication are not added to
73
     * the working set, instead a {@link RegistrationValidationException} is thrown which is a container for
74
     * all validation problems.
75
     *
76
     * @param candidates
77
     * @param problems
78
     *    Problems detected in prior validation and processing passed to this method to be completed. Can be <code>null</code>.
79
     * @throws RegistrationValidationException
80
     */
81
    private void validateAndAddDTOs(List<RegistrationDTO> candidates, List<String> problems) throws RegistrationValidationException {
82
        if(problems == null){
83
            problems = new ArrayList<>();
84
        }
85
        for(RegistrationDTO regDto : candidates){
86
                Reference citation = publicationUnit(regDto);
87
                if(citationUuid == null){
88
                    citationUuid = citation.getUuid();
89
                    citationString = citation.getTitleCache();
90
                } else {
91
                    if(!citation.getUuid().equals(citationUuid)){
92
                        problems.add("Removing Registration " + regDto.getSummary() + " from set since this refers to a different citationString.");
93
                        continue;
94
                    }
95
                }
96
                this.registrationDTOs.add(regDto);
97
                if(created == null || created.isAfter(regDto.getCreated())){
98
                    created = regDto.getCreated();
99
                }
100
        }
101

    
102
        if(!problems.isEmpty()){
103
            throw new RegistrationValidationException("", problems);
104
        }
105

    
106
    }
107

    
108
    /**
109
     * @param regDto
110
     * @return
111
     */
112
    protected Reference publicationUnit(RegistrationDTO regDto) {
113
        Reference ref = regDto.getCitation();
114
        while(ref.isOfType(ReferenceType.Section)&& ref.getInReference() != null){
115
            ref = ref.getInReference();
116
            if(!ref.isOfType(ReferenceType.Section)){
117
                break;
118
            }
119
        }
120
        return ref;
121
    }
122

    
123
    /**
124
     * @param reg
125
     * @throws RegistrationValidationException
126
     */
127
    public void add(Registration reg) throws RegistrationValidationException {
128
        Set<Registration> candidates = new HashSet<>();
129
        candidates.add(reg);
130
        validateAndAdd(candidates);
131
    }
132

    
133
    public void add(RegistrationDTO regDTO) throws RegistrationValidationException {
134
        validateAndAddDTOs(Arrays.asList(regDTO), null);
135
    }
136

    
137
    /**
138
     * @return the registrations
139
     */
140
    public List<Registration> getRegistrations() {
141
        List<Registration> regs = new ArrayList<>(registrationDTOs.size());
142
        registrationDTOs.forEach(dto -> regs.add(dto.registration()));
143
        return regs;
144
    }
145

    
146
    /**
147
     * Calculates the total count of messages in the registrations contained
148
     * in the working set.
149
     *
150
     * @return
151
     */
152
    public int messagesCount() {
153
        int messagesCount = 0;
154
        for(RegistrationDTO dto : getRegistrationDTOs()) {
155
            messagesCount = messagesCount + dto.getValidationProblems().size();
156
        }
157
        return messagesCount;
158
    }
159

    
160
    /**
161
     * Finds the lowest status in the registrations contained
162
     * in the working set.
163
     *
164
     * @return
165
     */
166
    public RegistrationStatus lowestStatus() {
167
        RegistrationStatus status = RegistrationStatus.REJECTED;
168
        for(RegistrationDTO dto : getRegistrationDTOs()) {
169
            if(dto.getStatus().compareTo(status) < 0){
170
                status = dto.getStatus();
171
            }
172
        }
173
        return status;
174
    }
175

    
176

    
177
    /**
178
     * @return the registrations
179
     */
180
    public List<RegistrationDTO> getRegistrationDTOs() {
181
        return registrationDTOs;
182
    }
183

    
184
    public Optional<RegistrationDTO> getRegistrationDTO(UUID registrationUuid) {
185
        return registrationDTOs.stream().filter(r -> r.getUuid().equals(registrationUuid) ).findFirst();
186
    }
187

    
188
    /**
189
     * @return the citationId
190
     */
191
    public UUID getCitationUuid() {
192
        return citationUuid;
193
    }
194

    
195
    /**
196
     * @return the citationString
197
     */
198
    public String getCitation() {
199
        return citationString;
200
    }
201

    
202
    public DateTime getRegistrationDate() {
203
        return registrationDTOs.get(0).getRegistrationDate();
204
    }
205

    
206
    public DateTime getCreationDate() {
207
        return registrationDTOs.get(0).getCreated();
208
    }
209

    
210
    /**
211
     * The creation time stamp of a registration set always is
212
     * the creation DateTime of the oldest Registration contained
213
     * in the set.
214
     *
215
     * @return
216
     */
217
    public DateTime getCreated(){
218
        return created;
219
    }
220

    
221
}
(3-3/6)