Project

General

Profile

Download (6.09 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

    
18
import org.joda.time.DateTime;
19

    
20
import eu.etaxonomy.cdm.model.name.Registration;
21
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
22
import eu.etaxonomy.cdm.model.name.TaxonName;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
25
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
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 Integer citationId = null;
37

    
38
    private DateTime created = null;
39

    
40
    private String citation = null;
41

    
42
    /**
43
     * Creates an empty working set
44
     */
45
    public RegistrationWorkingSet(Reference citation) {
46
        citationId = citation.getId();
47
        this.citation= 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}. Registration with a differing publication are not added to
70
     * the working set, instead a {@link RegistrationValidationException} is thrown which is a container for
71
     * all validation problems.
72
     *
73
     * @param candidates
74
     * @param problems
75
     *    Problems detected in prior validation and processing passed to this method to be completed. Can be <code>null</code>.
76
     * @throws RegistrationValidationException
77
     */
78
    private void validateAndAddDTOs(List<RegistrationDTO> candidates, List<String> problems) throws RegistrationValidationException {
79
        if(problems == null){
80
            problems = new ArrayList<>();
81
        }
82
        for(RegistrationDTO regDto : candidates){
83
                if(citationId == null){
84
                    citationId = regDto.getCitationID();
85
                    citation = regDto.getCitation().getTitleCache();
86
                } else {
87
                    if(!regDto.getCitationID().equals(citationId)){
88
                        problems.add("Removing Registration " + regDto.getSummary() + " from set since this refers to a different citation.");
89
                        continue;
90
                    }
91
                }
92
                this.registrationDTOs.add(regDto);
93
                if(created == null || created.isAfter(regDto.getCreated())){
94
                    created = regDto.getCreated();
95
                }
96
        }
97

    
98
        if(!problems.isEmpty()){
99
            throw new RegistrationValidationException("", problems);
100
        }
101

    
102
    }
103

    
104
    /**
105
     * @param reg
106
     * @throws RegistrationValidationException
107
     */
108
    public void add(Registration reg) throws RegistrationValidationException {
109
        Set<Registration> candidates = new HashSet<>();
110
        candidates.add(reg);
111
        validateAndAdd(candidates);
112
    }
113

    
114
    public void add(RegistrationDTO regDTO) throws RegistrationValidationException {
115
        validateAndAddDTOs(Arrays.asList(regDTO), null);
116
    }
117

    
118
    /**
119
     * @return the registrations
120
     */
121
    public List<Registration> getRegistrations() {
122
        List<Registration> regs = new ArrayList<>(registrationDTOs.size());
123
        registrationDTOs.forEach(dto -> regs.add(dto.registration()));
124
        return regs;
125
    }
126

    
127
    /**
128
     * Calculates the total count of messages in the registrations contained
129
     * in the working set.
130
     *
131
     * @return
132
     */
133
    public int messagesCount() {
134
        int messagesCount = 0;
135
        for(RegistrationDTO dto : getRegistrationDTOs()) {
136
            messagesCount = messagesCount + dto.getMessages().size();
137
        }
138
        return messagesCount;
139
    }
140

    
141
    /**
142
     * Finds the lowest status in the registrations contained
143
     * in the working set.
144
     *
145
     * @return
146
     */
147
    public RegistrationStatus lowestStatus() {
148
        RegistrationStatus status = RegistrationStatus.REJECTED;
149
        for(RegistrationDTO dto : getRegistrationDTOs()) {
150
            if(dto.getStatus().compareTo(status) < 0){
151
                status = dto.getStatus();
152
            }
153
        }
154
        return status;
155
    }
156

    
157

    
158
    /**
159
     * @return the registrations
160
     */
161
    public List<RegistrationDTO> getRegistrationDTOs() {
162
        return registrationDTOs;
163
    }
164

    
165
    public Optional<RegistrationDTO> getRegistrationDTO(int registrationId) {
166
        return registrationDTOs.stream().filter(r -> r.getId() == registrationId).findFirst();
167
    }
168

    
169
    /**
170
     * @return the citationId
171
     */
172
    public Integer getCitationId() {
173
        return citationId;
174
    }
175

    
176
    /**
177
     * @return the citation
178
     */
179
    public String getCitation() {
180
        return citation;
181
    }
182

    
183
    public DateTime getRegistrationDate() {
184
        return registrationDTOs.get(0).getRegistrationDate();
185
    }
186

    
187
    public DateTime getCreationDate() {
188
        return registrationDTOs.get(0).getCreated();
189
    }
190

    
191
    /**
192
     * The creation time stamp of a registration set always is
193
     * the creation DateTime of the oldest Registration contained
194
     * in the set.
195
     *
196
     * @return
197
     */
198
    public DateTime getCreated(){
199
        return created;
200
    }
201

    
202
}
(3-3/6)