Project

General

Profile

Download (7.79 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.service;
10

    
11
import java.util.Arrays;
12
import java.util.List;
13
import java.util.UUID;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Qualifier;
18
import org.springframework.stereotype.Service;
19

    
20
import eu.etaxonomy.cdm.api.application.CdmRepository;
21
import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
22
import eu.etaxonomy.cdm.api.service.dto.RegistrationWorkingSet;
23
import eu.etaxonomy.cdm.api.service.exception.RegistrationValidationException;
24
import eu.etaxonomy.cdm.api.service.idminter.RegistrationIdentifierMinter;
25
import eu.etaxonomy.cdm.model.name.Registration;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.model.reference.ReferenceType;
29

    
30
/**
31
 * @author a.kohlbecker
32
 * @since Mar 25, 2019
33
 *
34
 */
35
@Service("registrationWorkflowService")
36
public class RegistrationWorkflowService implements IRegistrationWorkflowService {
37

    
38
    private static Logger logger = Logger.getLogger(RegistrationWorkflowService.class);
39

    
40
    @Autowired
41
    @Qualifier("cdmRepository")
42
    private CdmRepository repo;
43

    
44
    @Autowired
45
    private RegistrationIdentifierMinter minter;
46

    
47
    private CdmRepository getRepo() {
48
        return repo;
49
    }
50

    
51

    
52
    @Override
53
    public Registration createRegistration(TaxonName taxonName, List<Registration> preparedBlockingResitrations) {
54

    
55
        if(taxonName.isPersited()){
56
            getRepo().getSession().refresh(taxonName);
57
        }
58

    
59
        Registration reg = getRepo().getRegistrationService().createRegistrationForName(taxonName.getUuid());
60
        if(!preparedBlockingResitrations.isEmpty()){
61
            for(Registration blockingReg : preparedBlockingResitrations){
62
                blockingReg = getRepo().getRegistrationService().load(blockingReg.getUuid());
63
                reg.getBlockedBy().add(blockingReg);
64
            }
65
            // save again
66
            getRepo().getRegistrationService().saveOrUpdate(reg);
67
            preparedBlockingResitrations.clear();
68
        }
69
        return reg;
70
    }
71

    
72

    
73
    @Override
74
    public boolean createRegistrationforExistingName(RegistrationWorkingSet workingset, TaxonName typifiedName) throws RegistrationValidationException {
75

    
76
        boolean doReloadWorkingSet = false;
77
        Reference citation = getRepo().getReferenceService().load(workingset.getCitationUuid(), Arrays.asList("authorship.$", "inReference.authorship.$"));
78
        // here we completely ignore the ExistingNameRegistrationType since the user should not have the choice
79
        // to create a typification only registration in the working (publication) set which contains
80
        // the protologe. This is known from the nomenclatural reference.
81
        if(canCreateNameRegistrationFor(workingset, typifiedName)){
82
            // the citation which is the base for workingset contains the protologe of the name and the name has not
83
            // been registered before:
84
            // create a registration for the name and the first typifications
85
            Registration newRegistrationWithExistingName = getRepo().getRegistrationService().createRegistrationForName(typifiedName.getUuid());
86
            workingset.add(new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation));
87
            doReloadWorkingSet = true;
88
        } else {
89
            if(!checkWokingsetContainsProtologe(workingset, typifiedName)){
90
                // create a typification only registration
91
                Registration typificationOnlyRegistration = getRepo().getRegistrationService().newRegistration();
92
                if(!getRepo().getRegistrationService().checkRegistrationExistsFor(typifiedName)){
93
                    // oops, yet no registration for this name, so we create it as blocking registration:
94
                    Registration blockingNameRegistration = getRepo().getRegistrationService().createRegistrationForName(typifiedName.getUuid());
95
                    typificationOnlyRegistration.getBlockedBy().add(blockingNameRegistration);
96
                }
97
                RegistrationDTO regDTO = new RegistrationDTO(typificationOnlyRegistration, typifiedName, citation);
98
                workingset.add(regDTO);
99
            }
100
        }
101
        return doReloadWorkingSet;
102
    }
103

    
104
    @Override
105
    public void addTypeDesignation(UUID typeDesignationUuid, Registration registration) {
106
        reloadRegistration(registration);
107
        getRepo().getRegistrationService().addTypeDesignation(registration, typeDesignationUuid);
108
        getRepo().getRegistrationService().saveOrUpdate(registration);
109
    }
110

    
111
    /**
112
     * @param taxonNameUUID
113
     * @param registration
114
     */
115
    @Override
116
    public Registration addBlockingRegistration(UUID nameUUID, Registration registration) {
117

    
118
        TaxonName name = getRepo().getNameService().load(nameUUID);
119

    
120
        boolean registrationExists = false;
121
        for(Registration regForName : name.getRegistrations()){
122
            if(minter.identifierPattern().matcher(regForName.getIdentifier()).matches()){
123
                registrationExists = true;
124
                break;
125
            }
126
        }
127

    
128
        if(!registrationExists){
129
            Registration blockingRegistration = getRepo().getRegistrationService().createRegistrationForName(nameUUID);
130
            reloadRegistration(registration);
131
            registration.getBlockedBy().add(blockingRegistration);
132
            if(registration.isPersited()){
133
                getRepo().getRegistrationService().saveOrUpdate(registration);
134
                logger.debug("Blocking registration created, added to registion and persited");
135
            }
136
            return blockingRegistration;
137
        }
138
        return null;
139
    }
140

    
141
    /**
142
     * @param registration
143
     * @return
144
     */
145
    @Override
146
    public void reloadRegistration(Registration registration) {
147
        if(registration.isPersited()){
148
             Registration registrationReloaded = getRepo().getRegistrationService().load(registration.getUuid());
149
             if(registrationReloaded == null){
150
                 throw new NullPointerException("Registration not found for Registration#" + registration.getUuid() + " which has been hold in the rootContext");
151
             }
152
             registration = registrationReloaded;
153
         } else {
154
             logger.trace("Registration is not yet persisted.");
155
         }
156
    }
157

    
158
    /**
159
     * Checks
160
     * <ol>
161
     * <li>if there is NOT any registration for this name created in the current registration system</li>
162
     * <li>Checks if the name belongs to the current workingset</li>
163
     * </ol>
164
     * If both checks are successful the method returns <code>true</code>.
165
     */
166
    @Override
167
    public boolean canCreateNameRegistrationFor(RegistrationWorkingSet workingset, TaxonName name) {
168
        return !getRepo().getRegistrationService().checkRegistrationExistsFor(name) && checkWokingsetContainsProtologe(workingset, name);
169
    }
170

    
171
    /**
172
     * @param name
173
     * @return
174
     */
175
    @Override
176
    public boolean checkWokingsetContainsProtologe(RegistrationWorkingSet workingset, TaxonName name) {
177
        Reference nomRef = name.getNomenclaturalReference();
178
        UUID citationUuid = workingset.getCitationUuid();
179
        // @formatter:off
180
        return nomRef != null && (
181
                // nomref matches
182
                nomRef.getUuid().equals(citationUuid) ||
183
                // nomref.inreference matches
184
                (nomRef.getType() != null && nomRef.getType() == ReferenceType.Section && nomRef.getInReference() != null && nomRef.getInReference().getUuid().equals(citationUuid))
185
                );
186
        // @formatter:on
187
    }
188

    
189
}
(8-8/12)