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.api.service.dto;
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.exception.RegistrationValidationException;
22
import eu.etaxonomy.cdm.model.name.Registration;
23
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.reference.ReferenceType;
26

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

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

    
36
    private UUID citationUuid = null;
37

    
38
    private DateTime created = null;
39

    
40
    private String citationString = null;
41

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

    
49
    }
50

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

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

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

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

    
105
    }
106

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

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

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

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

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

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

    
175

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

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

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

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

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

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

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

    
220
}
(21-21/30)