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.service;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Optional;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.beans.factory.annotation.Qualifier;
|
22
|
import org.springframework.stereotype.Service;
|
23
|
import org.springframework.transaction.annotation.Transactional;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
26
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
27
|
import eu.etaxonomy.cdm.model.common.User;
|
28
|
import eu.etaxonomy.cdm.model.name.Registration;
|
29
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
30
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
31
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
33
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
|
37
|
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
|
38
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
|
39
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
|
40
|
|
41
|
/**
|
42
|
* Provides RegistrationDTOs and RegistrationWorkingsets for Registrations in the database.
|
43
|
* <p>
|
44
|
* Can create missing registrations for names which have Extensions of the Type <code>IAPTRegdata.json</code>.
|
45
|
* See https://dev.e-taxonomy.eu/redmine/issues/6621 for further details.
|
46
|
* This feature can be activated by by supplying one of the following jvm command line arguments:
|
47
|
* <ul>
|
48
|
* <li><code>-DregistrationCreate=iapt</code>: create all iapt Registrations if missing</li>
|
49
|
* <li><code>-DregistrationWipeout=iapt</code>: remove all iapt Registrations</li>
|
50
|
* <li><code>-DregistrationWipeout=all</code>: remove all Registrations</li>
|
51
|
* </ul>
|
52
|
* The <code>-DregistrationWipeout</code> commands are executed before the <code>-DregistrationCreate</code> and will not change the name and type designations.
|
53
|
*
|
54
|
*
|
55
|
* @author a.kohlbecker
|
56
|
* @since Mar 10, 2017
|
57
|
*
|
58
|
*/
|
59
|
@Service("registrationWorkingSetService")
|
60
|
@Transactional(readOnly=true)
|
61
|
public class RegistrationWorkingSetService implements IRegistrationWorkingSetService {
|
62
|
|
63
|
public static final List<String> REGISTRATION_INIT_STRATEGY = Arrays.asList(new String []{
|
64
|
// typeDesignation
|
65
|
"typeDesignations.typeStatus",
|
66
|
"typeDesignations.typifiedNames",
|
67
|
"typeDesignations.typeSpecimen",
|
68
|
"typeDesignations.typeName",
|
69
|
"typeDesignations.citation",
|
70
|
"typeDesignations.citation.authorship.$",
|
71
|
// name
|
72
|
"name.$",
|
73
|
"name.nomenclaturalReference.authorship",
|
74
|
"name.nomenclaturalReference.inReference",
|
75
|
"name.rank",
|
76
|
"name.status.type",
|
77
|
// institution
|
78
|
"institution",
|
79
|
}
|
80
|
);
|
81
|
|
82
|
/**
|
83
|
*
|
84
|
*/
|
85
|
private static final int PAGE_SIZE = 50;
|
86
|
|
87
|
private static final Logger logger = Logger.getLogger(RegistrationWorkingSetService.class);
|
88
|
|
89
|
@Autowired
|
90
|
@Qualifier("cdmRepository")
|
91
|
private CdmRepository repo;
|
92
|
|
93
|
@Autowired
|
94
|
protected IBeanInitializer defaultBeanInitializer;
|
95
|
|
96
|
public RegistrationWorkingSetService() {
|
97
|
|
98
|
}
|
99
|
|
100
|
|
101
|
/**
|
102
|
* @param id the Registration entity id
|
103
|
* @return
|
104
|
*/
|
105
|
@Override
|
106
|
public RegistrationDTO loadDtoById(Integer id) {
|
107
|
Registration reg = repo.getRegistrationService().find(id);
|
108
|
inititializeSpecimen(reg);
|
109
|
return new RegistrationDTO(reg);
|
110
|
}
|
111
|
|
112
|
|
113
|
@Override
|
114
|
public Collection<RegistrationDTO> listDTOs() {
|
115
|
|
116
|
List<Registration> regs = repo.getRegistrationService().list(null, PAGE_SIZE, 0, null, REGISTRATION_INIT_STRATEGY);
|
117
|
|
118
|
List<RegistrationDTO> dtos = makeDTOs(regs);
|
119
|
return dtos;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* {@inheritDoc}
|
124
|
*/
|
125
|
@Override
|
126
|
public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus) {
|
127
|
|
128
|
Pager<Registration> pager = repo.getRegistrationService().page(submitter, includedStatus, PAGE_SIZE, 0, null, REGISTRATION_INIT_STRATEGY);
|
129
|
List<Registration> registrations = pager.getRecords();
|
130
|
return makeDTOs(registrations);
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* {@inheritDoc}
|
135
|
* @throws RegistrationValidationException
|
136
|
*/
|
137
|
@Override
|
138
|
public RegistrationWorkingSet loadWorkingSetByReferenceID(Integer referenceID) throws RegistrationValidationException {
|
139
|
|
140
|
Reference reference = repo.getReferenceService().find(referenceID);
|
141
|
Pager<Registration> pager = repo.getRegistrationService().page(Optional.of(reference), null, null, null, REGISTRATION_INIT_STRATEGY);
|
142
|
return new RegistrationWorkingSet(makeDTOs(pager.getRecords()));
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* @param regs
|
147
|
* @return
|
148
|
*/
|
149
|
private List<RegistrationDTO> makeDTOs(List<Registration> regs) {
|
150
|
initializeSpecimens(regs);
|
151
|
List<RegistrationDTO> dtos = new ArrayList<>(regs.size());
|
152
|
regs.forEach(reg -> {dtos.add(new RegistrationDTO(reg));});
|
153
|
return dtos;
|
154
|
}
|
155
|
|
156
|
|
157
|
/**
|
158
|
* @param regs
|
159
|
*/
|
160
|
private void initializeSpecimens(List<Registration> regs) {
|
161
|
for(Registration reg : regs){
|
162
|
inititializeSpecimen(reg);
|
163
|
}
|
164
|
|
165
|
}
|
166
|
|
167
|
|
168
|
/**
|
169
|
* @param reg
|
170
|
*/
|
171
|
protected void inititializeSpecimen(Registration reg) {
|
172
|
for(TypeDesignationBase<?> td : reg.getTypeDesignations()){
|
173
|
if(td instanceof SpecimenTypeDesignation){
|
174
|
|
175
|
DerivedUnit derivedUnit = ((SpecimenTypeDesignation) td).getTypeSpecimen();
|
176
|
@SuppressWarnings("rawtypes")
|
177
|
Set<SpecimenOrObservationBase> sobs = new HashSet<>();
|
178
|
sobs.add(derivedUnit);
|
179
|
|
180
|
while(sobs != null && !sobs.isEmpty()){
|
181
|
@SuppressWarnings("rawtypes")
|
182
|
Set<SpecimenOrObservationBase> nextSobs = null;
|
183
|
for(@SuppressWarnings("rawtypes") SpecimenOrObservationBase sob : sobs){
|
184
|
if(sob instanceof DerivedUnit) {
|
185
|
defaultBeanInitializer.initialize(sob, Arrays.asList(new String[]{
|
186
|
"collection",
|
187
|
"storedUnder",
|
188
|
"preservation",
|
189
|
"recordBasis",
|
190
|
"sex",
|
191
|
"lifeStage",
|
192
|
"kindOfUnit",
|
193
|
"derivedFrom.$",
|
194
|
"derivedFrom.type"
|
195
|
}));
|
196
|
nextSobs = ((DerivedUnit)sob).getOriginals();
|
197
|
}
|
198
|
if(sob instanceof FieldUnit){
|
199
|
defaultBeanInitializer.initialize(sob, Arrays.asList(new String[]{
|
200
|
"$",
|
201
|
"gatheringEvent.$",
|
202
|
"gatheringEvent.country",
|
203
|
"gatheringEvent.collectingAreas",
|
204
|
"gatheringEvent.actor"
|
205
|
}));
|
206
|
int i = 0;
|
207
|
}
|
208
|
}
|
209
|
sobs = nextSobs;
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
}
|
214
|
|
215
|
|
216
|
|
217
|
|
218
|
|
219
|
|
220
|
}
|