Project

General

Profile

Download (5.47 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.mock.Registration;
19
import eu.etaxonomy.cdm.mock.RegistrationStatus;
20
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
21
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
22

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

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

    
32
    private Integer citationId = null;
33

    
34
    private DateTime created = null;
35

    
36
    private String citation = null;
37

    
38
    /**
39
     * Creates an empty working set
40
     */
41
    public RegistrationWorkingSet() {
42

    
43
    }
44

    
45
    public RegistrationWorkingSet(List<RegistrationDTO> registrationDTOs) throws RegistrationValidationException {
46
        validateAndAddDTOs(registrationDTOs, null);
47
    }
48

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

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

    
91
        if(!problems.isEmpty()){
92
            throw new RegistrationValidationException("", problems);
93
        }
94

    
95
    }
96

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

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

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

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

    
146

    
147
    /**
148
     * @return the registrations
149
     */
150
    public List<RegistrationDTO> getRegistrationDTOs() {
151
        return registrationDTOs;
152
    }
153

    
154
    /**
155
     * @return the citationId
156
     */
157
    public Integer getCitationId() {
158
        return citationId;
159
    }
160

    
161
    /**
162
     * @return the citation
163
     */
164
    public String getCitation() {
165
        return citation;
166
    }
167

    
168
    public DateTime getRegistrationDate() {
169
        return registrationDTOs.get(0).getRegistrationDate();
170
    }
171

    
172
    public DateTime getCreationDate() {
173
        return registrationDTOs.get(0).getCreated();
174
    }
175

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

    
187
}
(1-1/2)