Project

General

Profile

Download (4.85 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 eu.etaxonomy.cdm.mock.Registration;
17
import eu.etaxonomy.cdm.mock.RegistrationStatus;
18
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
19
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationDTO;
20
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationValidationException;
21

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

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

    
31
    private int citationId = -1;
32

    
33
    private String citation = null;
34

    
35
    /**
36
     * Creates an empty working set
37
     */
38
    public RegistrationWorkingSet() {
39

    
40
    }
41

    
42
    public RegistrationWorkingSet(List<RegistrationDTO> registrationDTOs) throws RegistrationValidationException {
43
        validateAndAddDTOs(registrationDTOs, null);
44
    }
45

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

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

    
85
        if(!problems.isEmpty()){
86
            throw new RegistrationValidationException("", problems);
87
        }
88

    
89
    }
90

    
91
    /**
92
     * @param reg
93
     * @throws RegistrationValidationException
94
     */
95
    public void add(Registration reg) throws RegistrationValidationException {
96
        Set<Registration> candidates = new HashSet<>();
97
        candidates.add(reg);
98
        validateAndAdd(candidates);
99
    }
100

    
101
    /**
102
     * @return the registrations
103
     */
104
    public List<Registration> getRegistrations() {
105
        List<Registration> regs = new ArrayList<>(registrationDTOs.size());
106
        registrationDTOs.forEach(dto -> regs.add(dto.registration()));
107
        return regs;
108
    }
109

    
110
    /**
111
     * Calculates the total count of messages in the registrations contained
112
     * in the working set.
113
     *
114
     * @return
115
     */
116
    public int messagesCount() {
117
        int messagesCount = 0;
118
        for(RegistrationDTO dto : getRegistrationDTOs()) {
119
            messagesCount = messagesCount + dto.getMessages().size();
120
        }
121
        return messagesCount;
122
    }
123

    
124
    /**
125
     * Finds the lowest status in the registrations contained
126
     * in the working set.
127
     *
128
     * @return
129
     */
130
    public RegistrationStatus lowestStatus() {
131
        RegistrationStatus status = RegistrationStatus.REJECTED;
132
        for(RegistrationDTO dto : getRegistrationDTOs()) {
133
            if(dto.getStatus().compareTo(status) < 0){
134
                status = dto.getStatus();
135
            }
136
        }
137
        return status;
138
    }
139

    
140

    
141
    /**
142
     * @return the registrations
143
     */
144
    public List<RegistrationDTO> getRegistrationDTOs() {
145
        return registrationDTOs;
146
    }
147

    
148
    /**
149
     * @return the citationId
150
     */
151
    public int getCitationId() {
152
        return citationId;
153
    }
154

    
155
    /**
156
     * @return the citation
157
     */
158
    public String getCitation() {
159
        return citation;
160
    }
161

    
162
}
(1-1/2)