Project

General

Profile

Download (8.18 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.TypeDesignationSetException;
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> preparedBlockingRegistrations) {
54

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

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

    
72

    
73
    @Override
74
    public boolean createRegistrationforExistingName(RegistrationWorkingSet workingset, TaxonName typifiedName) throws TypeDesignationSetException {
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
        registration = reloadRegistration(registration);
107
        getRepo().getRegistrationService().addTypeDesignation(registration, typeDesignationUuid);
108
        getRepo().getRegistrationService().saveOrUpdate(registration);
109
    }
110

    
111
    @Override
112
    public Registration prepareBlockingRegistration(UUID nameUUID) {
113

    
114
        TaxonName name = getRepo().getNameService().load(nameUUID);
115

    
116
        boolean registrationExists = false;
117
        for(Registration regForName : name.getRegistrations()){
118
            if(minter.identifierPattern().matcher(regForName.getIdentifier()).matches()){
119
                registrationExists = true;
120
                break;
121
            }
122
        }
123

    
124
        if(!registrationExists){
125
            Registration blockingRegistration = getRepo().getRegistrationService().createRegistrationForName(nameUUID);
126
            return blockingRegistration;
127
        }
128
        return null;
129
    }
130

    
131
    @Override
132
    public Registration addBlockingRegistration(UUID nameUUID, Registration registration) {
133

    
134
        Registration blockingRegistration = prepareBlockingRegistration(nameUUID);
135
        if(blockingRegistration != null){
136
            registration = reloadRegistration(registration);
137
            registration.getBlockedBy().add(blockingRegistration);
138
            if(registration.isPersited()){ // shoul't this be !registration.isPersited() ? since the saveOrUpdate might have been hit this code could be useless
139
                getRepo().getRegistrationService().saveOrUpdate(registration);
140
                logger.debug("Blocking registration created, added to registion and persited");
141
            }
142
            return blockingRegistration;
143
        }
144
        return null;
145
    }
146

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

    
163
        return registration;
164
    }
165

    
166
    /**
167
     * Checks
168
     * <ol>
169
     * <li>if there is NOT any registration for this name created in the current registration system</li>
170
     * <li>Checks if the name belongs to the current workingset</li>
171
     * </ol>
172
     * If both checks are successful the method returns <code>true</code>.
173
     */
174
    @Override
175
    public boolean canCreateNameRegistrationFor(RegistrationWorkingSet workingset, TaxonName name) {
176
        return !getRepo().getRegistrationService().checkRegistrationExistsFor(name) && checkWokingsetContainsProtologe(workingset, name);
177
    }
178

    
179
    /**
180
     * @param name
181
     * @return
182
     */
183
    @Override
184
    public boolean checkWokingsetContainsProtologe(RegistrationWorkingSet workingset, TaxonName name) {
185
        Reference nomRef = name.getNomenclaturalReference();
186
        UUID citationUuid = workingset.getCitationUuid();
187
        // @formatter:off
188
        return nomRef != null && (
189
                // nomref matches
190
                nomRef.getUuid().equals(citationUuid) ||
191
                // nomref.inreference matches
192
                (nomRef.getType() != null && nomRef.getType() == ReferenceType.Section && nomRef.getInReference() != null && nomRef.getInReference().getUuid().equals(citationUuid))
193
                );
194
        // @formatter:on
195
    }
196

    
197
}
(9-9/13)