Project

General

Profile

Download (5.65 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.HashSet;
13
import java.util.List;
14
import java.util.Set;
15

    
16
import org.joda.time.DateTime;
17

    
18
import eu.etaxonomy.cdm.model.name.Registration;
19
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
20
import eu.etaxonomy.cdm.model.reference.Reference;
21
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
22
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since Mar 22, 2017
27
 *
28
 */
29
public class RegistrationWorkingSet {
30

    
31
    private List<RegistrationDTO> registrationDTOs = new ArrayList<>();
32

    
33
    private Integer citationId = null;
34

    
35
    private DateTime created = null;
36

    
37
    private String citation = null;
38

    
39
    /**
40
     * Creates an empty working set
41
     */
42
    public RegistrationWorkingSet(Reference citation) {
43
        citationId = citation.getId();
44
        this.citation= citation.getTitleCache();
45

    
46
    }
47

    
48
    public RegistrationWorkingSet(List<RegistrationDTO> registrationDTOs) throws RegistrationValidationException {
49
        validateAndAddDTOs(registrationDTOs, null);
50
    }
51

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

    
63
    /**
64
     * Validate and add all Registrations to the working set which are referring to the same publication
65
     * which is either the citation of the nomenclatural reference of the {@link TaxonName} or the
66
     * citation of the {@link TypeDesignations}. Registration with a differing publication are not added to
67
     * the working set, instead a {@link RegistrationValidationException} is thrown which is a container for
68
     * all validation problems.
69
     *
70
     * @param candidates
71
     * @param problems Problems detected in prior validation and processing passed to this method to be completed.
72
     * @throws RegistrationValidationException
73
     */
74
    private void validateAndAddDTOs(List<RegistrationDTO> candidates, List<String> problems) throws RegistrationValidationException {
75
        if(problems == null){
76
            problems = new ArrayList<>();
77
        }
78
        for(RegistrationDTO regDto : candidates){
79
                if(citationId == null){
80
                    citationId = regDto.getCitationID();
81
                    citation = regDto.getCitation().getTitleCache();
82
                } else {
83
                    if(!regDto.getCitationID().equals(citationId)){
84
                        problems.add("Removing Registration " + regDto.registration().toString() + " from set since this refers to a different citation.");
85
                        continue;
86
                    }
87
                }
88
                this.registrationDTOs.add(regDto);
89
                if(created == null || created.isAfter(regDto.getCreated())){
90
                    created = regDto.getCreated();
91
                }
92
        }
93

    
94
        if(!problems.isEmpty()){
95
            throw new RegistrationValidationException("", problems);
96
        }
97

    
98
    }
99

    
100
    /**
101
     * @param reg
102
     * @throws RegistrationValidationException
103
     */
104
    public void add(Registration reg) throws RegistrationValidationException {
105
        Set<Registration> candidates = new HashSet<>();
106
        candidates.add(reg);
107
        validateAndAdd(candidates);
108
    }
109

    
110
    /**
111
     * @return the registrations
112
     */
113
    public List<Registration> getRegistrations() {
114
        List<Registration> regs = new ArrayList<>(registrationDTOs.size());
115
        registrationDTOs.forEach(dto -> regs.add(dto.registration()));
116
        return regs;
117
    }
118

    
119
    /**
120
     * Calculates the total count of messages in the registrations contained
121
     * in the working set.
122
     *
123
     * @return
124
     */
125
    public int messagesCount() {
126
        int messagesCount = 0;
127
        for(RegistrationDTO dto : getRegistrationDTOs()) {
128
            messagesCount = messagesCount + dto.getMessages().size();
129
        }
130
        return messagesCount;
131
    }
132

    
133
    /**
134
     * Finds the lowest status in the registrations contained
135
     * in the working set.
136
     *
137
     * @return
138
     */
139
    public RegistrationStatus lowestStatus() {
140
        RegistrationStatus status = RegistrationStatus.REJECTED;
141
        for(RegistrationDTO dto : getRegistrationDTOs()) {
142
            if(dto.getStatus().compareTo(status) < 0){
143
                status = dto.getStatus();
144
            }
145
        }
146
        return status;
147
    }
148

    
149

    
150
    /**
151
     * @return the registrations
152
     */
153
    public List<RegistrationDTO> getRegistrationDTOs() {
154
        return registrationDTOs;
155
    }
156

    
157
    /**
158
     * @return the citationId
159
     */
160
    public Integer getCitationId() {
161
        return citationId;
162
    }
163

    
164
    /**
165
     * @return the citation
166
     */
167
    public String getCitation() {
168
        return citation;
169
    }
170

    
171
    public DateTime getRegistrationDate() {
172
        return registrationDTOs.get(0).getRegistrationDate();
173
    }
174

    
175
    public DateTime getCreationDate() {
176
        return registrationDTOs.get(0).getCreated();
177
    }
178

    
179
    /**
180
     * The creation time stamp of a registration set always is
181
     * the creation DateTime of the oldest Registration contained
182
     * in the set.
183
     *
184
     * @return
185
     */
186
    public DateTime getCreated(){
187
        return created;
188
    }
189

    
190
}
(3-3/6)