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.model.name.TaxonNameBase;
|
18
|
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationDTO;
|
19
|
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationValidationException;
|
20
|
|
21
|
/**
|
22
|
* @author a.kohlbecker
|
23
|
* @since Mar 22, 2017
|
24
|
*
|
25
|
*/
|
26
|
public class RegistrationWorkingSet {
|
27
|
|
28
|
private List<RegistrationDTO> registrationDTOs = new ArrayList<>();
|
29
|
|
30
|
private int citationId = -1;
|
31
|
|
32
|
private String citation = null;
|
33
|
|
34
|
/**
|
35
|
* Creates an empty working set
|
36
|
*/
|
37
|
public RegistrationWorkingSet() {
|
38
|
|
39
|
}
|
40
|
|
41
|
public RegistrationWorkingSet(List<RegistrationDTO> registrationDTOs) throws RegistrationValidationException {
|
42
|
validateAndAddDTOs(registrationDTOs, null);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* @param candidated
|
47
|
* @throws RegistrationValidationException
|
48
|
*
|
49
|
*/
|
50
|
private void validateAndAdd(Set<Registration> candidates) throws RegistrationValidationException {
|
51
|
List<RegistrationDTO> dtos = new ArrayList<>(registrationDTOs.size());
|
52
|
candidates.forEach(reg -> dtos.add(new RegistrationDTO(reg)));
|
53
|
validateAndAddDTOs(dtos, null);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Validate and add all Registrations to the working set which are referring to the same publication
|
58
|
* which is either the citation of the nomenclatural reference of the {@link TaxonNameBase} or the
|
59
|
* citation of the {@link TypeDesignations}. Registration with a differing publication are not added to
|
60
|
* the working set, instead a {@link RegistrationValidationException} is thrown which is a container for
|
61
|
* all validation problems.
|
62
|
*
|
63
|
* @param candidates
|
64
|
* @param problems Problems detected in prior validation and processing passed to this method to be completed.
|
65
|
* @throws RegistrationValidationException
|
66
|
*/
|
67
|
private void validateAndAddDTOs(List<RegistrationDTO> candidates, List<String> problems) throws RegistrationValidationException {
|
68
|
if(problems == null){
|
69
|
problems = new ArrayList<>();
|
70
|
}
|
71
|
for(RegistrationDTO regDto : candidates){
|
72
|
if(citationId == -1){
|
73
|
citationId = regDto.getCitationID();
|
74
|
citation = regDto.getCitation();
|
75
|
} else {
|
76
|
if(regDto.getCitationID() != citationId){
|
77
|
problems.add("Removing Registration " + regDto.registration().toString() + " from set since this refers to a different citation.");
|
78
|
continue;
|
79
|
}
|
80
|
}
|
81
|
this.registrationDTOs.add(regDto);
|
82
|
}
|
83
|
|
84
|
if(!problems.isEmpty()){
|
85
|
throw new RegistrationValidationException("", problems);
|
86
|
}
|
87
|
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* @param reg
|
92
|
* @throws RegistrationValidationException
|
93
|
*/
|
94
|
public void add(Registration reg) throws RegistrationValidationException {
|
95
|
Set<Registration> candidates = new HashSet<>();
|
96
|
candidates.add(reg);
|
97
|
validateAndAdd(candidates);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* @return the registrations
|
102
|
*/
|
103
|
public List<Registration> getRegistrations() {
|
104
|
List<Registration> regs = new ArrayList<>(registrationDTOs.size());
|
105
|
registrationDTOs.forEach(dto -> regs.add(dto.registration()));
|
106
|
return regs;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* @return the registrations
|
111
|
*/
|
112
|
public List<RegistrationDTO> getRegistrationDTOs() {
|
113
|
return registrationDTOs;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* @return the citationId
|
118
|
*/
|
119
|
public int getCitationId() {
|
120
|
return citationId;
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* @return the citation
|
125
|
*/
|
126
|
public String getCitation() {
|
127
|
return citation;
|
128
|
}
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|
}
|