2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.service
.dto
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
15 import java
.util
.Optional
;
17 import java
.util
.UUID
;
19 import org
.joda
.time
.DateTime
;
21 import eu
.etaxonomy
.cdm
.api
.service
.exception
.RegistrationValidationException
;
22 import eu
.etaxonomy
.cdm
.model
.name
.Registration
;
23 import eu
.etaxonomy
.cdm
.model
.name
.RegistrationStatus
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
28 * @author a.kohlbecker
32 public class RegistrationWorkingSet
{
34 private List
<RegistrationDTO
> registrationDTOs
= new ArrayList
<>();
36 private UUID citationUuid
= null;
38 private DateTime created
= null;
40 private String citationString
= null;
43 * Creates an empty working set
45 public RegistrationWorkingSet(Reference citation
) {
46 citationUuid
= citation
.getUuid();
47 this.citationString
= citation
.getTitleCache();
51 public RegistrationWorkingSet(List
<RegistrationDTO
> registrationDTOs
) throws RegistrationValidationException
{
52 validateAndAddDTOs(registrationDTOs
, null);
57 * @throws RegistrationValidationException
60 private void validateAndAdd(Set
<Registration
> candidates
) throws RegistrationValidationException
{
61 List
<RegistrationDTO
> dtos
= new ArrayList
<>(registrationDTOs
.size());
62 candidates
.forEach(reg
-> dtos
.add(new RegistrationDTO(reg
)));
63 validateAndAddDTOs(dtos
, null);
67 * Validate and add all Registrations to the working set which are referring to the same publication
68 * which is either the citation of the nomenclatural reference of the {@link TaxonName} or the
69 * citation of the {@link TypeDesignations}. In case the citation is a section and this section is
70 * having an in-reference the in-reference will be used instead.
71 * Registration with a differing publication are not added to
72 * the working set, instead a {@link RegistrationValidationException} is thrown which is a container for
73 * all validation problems.
77 * Problems detected in prior validation and processing passed to this method to be completed. Can be <code>null</code>.
78 * @throws RegistrationValidationException
80 private void validateAndAddDTOs(List
<RegistrationDTO
> candidates
, List
<String
> problems
) throws RegistrationValidationException
{
82 problems
= new ArrayList
<>();
84 for(RegistrationDTO regDto
: candidates
){
85 Reference citation
= publicationUnit(regDto
);
86 if(citationUuid
== null){
87 citationUuid
= citation
.getUuid();
88 citationString
= citation
.getTitleCache();
90 if(!citation
.getUuid().equals(citationUuid
)){
91 problems
.add("Removing Registration " + regDto
.getSummary() + " from set since this refers to a different citationString.");
95 this.registrationDTOs
.add(regDto
);
96 if(created
== null || created
.isAfter(regDto
.getCreated())){
97 created
= regDto
.getCreated();
101 if(!problems
.isEmpty()){
102 throw new RegistrationValidationException("", problems
);
111 protected Reference
publicationUnit(RegistrationDTO regDto
) {
112 Reference ref
= regDto
.getCitation();
113 while(ref
.isOfType(ReferenceType
.Section
)&& ref
.getInReference() != null){
114 ref
= ref
.getInReference();
115 if(!ref
.isOfType(ReferenceType
.Section
)){
124 * @throws RegistrationValidationException
126 public void add(Registration reg
) throws RegistrationValidationException
{
127 Set
<Registration
> candidates
= new HashSet
<>();
129 validateAndAdd(candidates
);
132 public void add(RegistrationDTO regDTO
) throws RegistrationValidationException
{
133 validateAndAddDTOs(Arrays
.asList(regDTO
), null);
137 * @return the registrations
139 public List
<Registration
> getRegistrations() {
140 List
<Registration
> regs
= new ArrayList
<>(registrationDTOs
.size());
141 registrationDTOs
.forEach(dto
-> regs
.add(dto
.registration()));
146 * Calculates the total count of validation problems in the registrations contained
147 * in the working set.
151 public int validationProblemsCount() {
152 int validationProblemsCount
= 0;
153 for(RegistrationDTO dto
: getRegistrationDTOs()) {
154 validationProblemsCount
= validationProblemsCount
+ dto
.getValidationProblems().size();
156 return validationProblemsCount
;
160 * Finds the lowest status in the registrations contained
161 * in the working set.
165 public RegistrationStatus
lowestStatus() {
166 RegistrationStatus status
= RegistrationStatus
.REJECTED
;
167 for(RegistrationDTO dto
: getRegistrationDTOs()) {
168 if(dto
.getStatus().compareTo(status
) < 0){
169 status
= dto
.getStatus();
177 * @return the registrations
179 public List
<RegistrationDTO
> getRegistrationDTOs() {
180 return registrationDTOs
;
183 public Optional
<RegistrationDTO
> getRegistrationDTO(UUID registrationUuid
) {
184 return registrationDTOs
.stream().filter(r
-> r
.getUuid().equals(registrationUuid
) ).findFirst();
188 * @return the citationId
190 public UUID
getCitationUuid() {
195 * @return the citationString
197 public String
getCitation() {
198 return citationString
;
201 public DateTime
getRegistrationDate() {
202 return registrationDTOs
.get(0).getRegistrationDate();
205 public DateTime
getCreationDate() {
206 return registrationDTOs
.get(0).getCreated();
210 * The creation time stamp of a registration set always is
211 * the creation DateTime of the oldest Registration contained
216 public DateTime
getCreated(){
221 public String
toString(){
222 StringBuilder str
= new StringBuilder();
223 registrationDTOs
.forEach(dto
-> str
.append(dto
.getIdentifier() + " : " + dto
.getSummary()).append("\n"));
224 return str
.toString();