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 List<String> FIELDUNIT_INIT_STRATEGY = Arrays.asList(new String[]{
|
86
|
"$",
|
87
|
"gatheringEvent.$",
|
88
|
"gatheringEvent.country",
|
89
|
"gatheringEvent.collectingAreas",
|
90
|
"gatheringEvent.actor"
|
91
|
});
|
92
|
|
93
|
/**
|
94
|
*
|
95
|
*/
|
96
|
private static final List<String> DERIVEDUNIT_INIT_STRATEGY = Arrays.asList(new String[]{
|
97
|
"collection",
|
98
|
"storedUnder",
|
99
|
"preservation",
|
100
|
"recordBasis",
|
101
|
"sex",
|
102
|
"lifeStage",
|
103
|
"kindOfUnit",
|
104
|
"derivedFrom.$",
|
105
|
"derivedFrom.type",
|
106
|
"specimenTypeDesignations"
|
107
|
});
|
108
|
|
109
|
/**
|
110
|
*
|
111
|
*/
|
112
|
private static final int PAGE_SIZE = 50;
|
113
|
|
114
|
private static final Logger logger = Logger.getLogger(RegistrationWorkingSetService.class);
|
115
|
|
116
|
@Autowired
|
117
|
@Qualifier("cdmRepository")
|
118
|
private CdmRepository repo;
|
119
|
|
120
|
@Autowired
|
121
|
protected IBeanInitializer defaultBeanInitializer;
|
122
|
|
123
|
public RegistrationWorkingSetService() {
|
124
|
|
125
|
}
|
126
|
|
127
|
|
128
|
/**
|
129
|
* @param id the Registration entity id
|
130
|
* @return
|
131
|
*/
|
132
|
@Override
|
133
|
public RegistrationDTO loadDtoById(Integer id) {
|
134
|
Registration reg = repo.getRegistrationService().find(id);
|
135
|
inititializeSpecimen(reg);
|
136
|
return new RegistrationDTO(reg);
|
137
|
}
|
138
|
|
139
|
|
140
|
@Override
|
141
|
public Collection<RegistrationDTO> listDTOs() {
|
142
|
|
143
|
List<Registration> regs = repo.getRegistrationService().list(null, PAGE_SIZE, 0, null, REGISTRATION_INIT_STRATEGY);
|
144
|
|
145
|
List<RegistrationDTO> dtos = makeDTOs(regs);
|
146
|
return dtos;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* {@inheritDoc}
|
151
|
*/
|
152
|
@Override
|
153
|
public Collection<RegistrationDTO> listDTOs(User submitter, Collection<RegistrationStatus> includedStatus) {
|
154
|
|
155
|
Pager<Registration> pager = repo.getRegistrationService().page(submitter, includedStatus, PAGE_SIZE, 0, null, REGISTRATION_INIT_STRATEGY);
|
156
|
List<Registration> registrations = pager.getRecords();
|
157
|
return makeDTOs(registrations);
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* {@inheritDoc}
|
162
|
* @throws RegistrationValidationException
|
163
|
*/
|
164
|
@Override
|
165
|
public RegistrationWorkingSet loadWorkingSetByReferenceID(Integer referenceID) throws RegistrationValidationException {
|
166
|
|
167
|
Reference reference = repo.getReferenceService().find(referenceID);
|
168
|
Pager<Registration> pager = repo.getRegistrationService().page(Optional.of(reference), null, null, null, REGISTRATION_INIT_STRATEGY);
|
169
|
return new RegistrationWorkingSet(makeDTOs(pager.getRecords()));
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* @param regs
|
174
|
* @return
|
175
|
*/
|
176
|
private List<RegistrationDTO> makeDTOs(List<Registration> regs) {
|
177
|
initializeSpecimens(regs);
|
178
|
List<RegistrationDTO> dtos = new ArrayList<>(regs.size());
|
179
|
regs.forEach(reg -> {dtos.add(new RegistrationDTO(reg));});
|
180
|
return dtos;
|
181
|
}
|
182
|
|
183
|
|
184
|
/**
|
185
|
* @param regs
|
186
|
*/
|
187
|
private void initializeSpecimens(List<Registration> regs) {
|
188
|
for(Registration reg : regs){
|
189
|
inititializeSpecimen(reg);
|
190
|
}
|
191
|
|
192
|
}
|
193
|
|
194
|
|
195
|
/**
|
196
|
* @param reg
|
197
|
*/
|
198
|
protected void inititializeSpecimen(Registration reg) {
|
199
|
for(TypeDesignationBase<?> td : reg.getTypeDesignations()){
|
200
|
if(td instanceof SpecimenTypeDesignation){
|
201
|
|
202
|
DerivedUnit derivedUnit = ((SpecimenTypeDesignation) td).getTypeSpecimen();
|
203
|
@SuppressWarnings("rawtypes")
|
204
|
Set<SpecimenOrObservationBase> sobs = new HashSet<>();
|
205
|
sobs.add(derivedUnit);
|
206
|
|
207
|
while(sobs != null && !sobs.isEmpty()){
|
208
|
@SuppressWarnings("rawtypes")
|
209
|
Set<SpecimenOrObservationBase> nextSobs = null;
|
210
|
for(@SuppressWarnings("rawtypes") SpecimenOrObservationBase sob : sobs){
|
211
|
if(sob instanceof DerivedUnit) {
|
212
|
defaultBeanInitializer.initialize(sob, DERIVEDUNIT_INIT_STRATEGY);
|
213
|
nextSobs = ((DerivedUnit)sob).getOriginals();
|
214
|
}
|
215
|
if(sob instanceof FieldUnit){
|
216
|
defaultBeanInitializer.initialize(sob, FIELDUNIT_INIT_STRATEGY);
|
217
|
int i = 0;
|
218
|
}
|
219
|
}
|
220
|
sobs = nextSobs;
|
221
|
}
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
|
226
|
|
227
|
|
228
|
|
229
|
|
230
|
|
231
|
}
|