Project

General

Profile

Download (2.54 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.vaadin.presenter.registration.RegistrationDTO;
18
import eu.etaxonomy.cdm.vaadin.presenter.registration.RegistrationValidationException;
19

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

    
27
    private Set<Registration> registrations = new HashSet<>();
28

    
29
    private int citationId = -1;
30

    
31
    private String citation = null;
32

    
33
    public RegistrationWorkingSet(Set<Registration> registrations) throws RegistrationValidationException {
34

    
35
        validateAndAdd(registrations);
36
    }
37

    
38
    /**
39
     * @param candidated
40
     * @throws RegistrationValidationException
41
     *
42
     */
43
    private void validateAndAdd(Set<Registration> candidated) throws RegistrationValidationException {
44
        List<String> problems = new ArrayList<>();
45
        for(Registration reg : candidated){
46
            try {
47
                RegistrationDTO regDto = new RegistrationDTO(reg);
48
                if(citationId == -1){
49
                    citationId = regDto.getCitationID();
50
                    citation = regDto.getCitation();
51
                } else {
52
                    if(regDto.getCitationID() != citationId){
53
                        problems.add("Removing Registration " + reg.toString() + " from set since this refers to a different citation.\n");
54
                        continue;
55
                    }
56
                }
57
                this.registrations.add(reg);
58

    
59
            } catch (RegistrationValidationException e) {
60
                problems.add(e.getMessage());
61
            }
62
        }
63

    
64
        if(!problems.isEmpty()){
65
            throw new RegistrationValidationException(problems.toString());
66
        }
67

    
68
    }
69

    
70
    public boolean add(Registration registration){
71
        return registrations.add(registration);
72
    }
73

    
74
    /**
75
     * @return the registrations
76
     */
77
    public Set<Registration> getRegistrations() {
78
        return registrations;
79
    }
80

    
81
    /**
82
     * @return the citationId
83
     */
84
    public int getCitationId() {
85
        return citationId;
86
    }
87

    
88
    /**
89
     * @return the citation
90
     */
91
    public String getCitation() {
92
        return citation;
93
    }
94

    
95

    
96

    
97
}
    (1-1/1)