Project

General

Profile

Download (5.52 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.model.name.TaxonNameBase;
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() {
43

    
44
    }
45

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

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

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

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

    
96
    }
97

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

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

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

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

    
147

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

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

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

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

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

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

    
188
}
(1-1/2)