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.api.service.pager.impl.DefaultPagerImpl;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.model.common.User;
|
30
|
import eu.etaxonomy.cdm.model.name.Registration;
|
31
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
32
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
33
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
36
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
|
39
|
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
|
40
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
|
41
|
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
|
42
|
|
43
|
/**
|
44
|
* Provides RegistrationDTOs and RegistrationWorkingsets for Registrations in the database.
|
45
|
*
|
46
|
*
|
47
|
* @author a.kohlbecker
|
48
|
* @since Mar 10, 2017
|
49
|
*
|
50
|
*/
|
51
|
@Service("registrationWorkingSetService")
|
52
|
@Transactional(readOnly=true)
|
53
|
public class RegistrationWorkingSetService implements IRegistrationWorkingSetService {
|
54
|
|
55
|
public static final List<String> REGISTRATION_INIT_STRATEGY = Arrays.asList(new String []{
|
56
|
// typeDesignation
|
57
|
"typeDesignations.typeStatus",
|
58
|
"typeDesignations.typifiedNames.typeDesignations", // important !!
|
59
|
"typeDesignations.typeSpecimen",
|
60
|
"typeDesignations.typeName",
|
61
|
"typeDesignations.citation",
|
62
|
"typeDesignations.citation.authorship.$",
|
63
|
// name
|
64
|
"name.$",
|
65
|
"name.nomenclaturalReference.authorship",
|
66
|
"name.nomenclaturalReference.inReference",
|
67
|
"name.rank",
|
68
|
"name.homotypicalGroup.typifiedNames",
|
69
|
"name.status.type",
|
70
|
"name.typeDesignations", // important !!"
|
71
|
// institution
|
72
|
"institution",
|
73
|
}
|
74
|
);
|
75
|
|
76
|
/**
|
77
|
*
|
78
|
*/
|
79
|
private List<String> DERIVEDUNIT_INIT_STRATEGY = Arrays.asList(new String[]{
|
80
|
"*", // initialize all related entities to allow DerivedUnit conversion, see DerivedUnitConverter.copyPropertiesTo()
|
81
|
"derivedFrom.$",
|
82
|
"derivedFrom.type",
|
83
|
"derivedFrom.originals.derivationEvents", // important!!
|
84
|
"specimenTypeDesignations.typifiedNames.typeDesignations", // important!!
|
85
|
"mediaSpecimen.sources"
|
86
|
});
|
87
|
|
88
|
/**
|
89
|
*
|
90
|
*/
|
91
|
private List<String> FIELDUNIT_INIT_STRATEGY = Arrays.asList(new String[]{
|
92
|
"$",
|
93
|
"gatheringEvent.$",
|
94
|
"gatheringEvent.country",
|
95
|
"gatheringEvent.collectingAreas",
|
96
|
"gatheringEvent.actor.teamMembers",
|
97
|
"derivationEvents.derivatives" // important, otherwise the DerivedUnits are not included into the graph of initialized entities!!!
|
98
|
});
|
99
|
|
100
|
/**
|
101
|
*
|
102
|
*/
|
103
|
private static final int PAGE_SIZE = 50;
|
104
|
|
105
|
private static final Logger logger = Logger.getLogger(RegistrationWorkingSetService.class);
|
106
|
|
107
|
@Autowired
|
108
|
@Qualifier("cdmRepository")
|
109
|
private CdmRepository repo;
|
110
|
|
111
|
@Autowired
|
112
|
protected IBeanInitializer defaultBeanInitializer;
|
113
|
|
114
|
public RegistrationWorkingSetService() {
|
115
|
|
116
|
}
|
117
|
|
118
|
|
119
|
/**
|
120
|
* @param id the Registration entity id
|
121
|
* @return
|
122
|
*/
|
123
|
@Override
|
124
|
public RegistrationDTO loadDtoById(Integer id) {
|
125
|
Registration reg = repo.getRegistrationService().find(id);
|
126
|
inititializeSpecimen(reg);
|
127
|
return new RegistrationDTO(reg);
|
128
|
}
|
129
|
|
130
|
|
131
|
@Override
|
132
|
public Pager<RegistrationDTO> pageDTOs(Integer pageSize, Integer pageIndex) {
|
133
|
|
134
|
return pageDTOs(null, null, null, null, pageSize, pageIndex);
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* {@inheritDoc}
|
139
|
*/
|
140
|
@Override
|
141
|
public Pager<RegistrationDTO> pageDTOs(User submitter, Collection<RegistrationStatus> includedStatus,
|
142
|
String identifierFilterPattern, String taxonNameFilterPattern,
|
143
|
Integer pageSize, Integer pageIndex) {
|
144
|
|
145
|
if(pageSize == null){
|
146
|
pageSize = PAGE_SIZE;
|
147
|
}
|
148
|
|
149
|
Pager<Registration> pager = repo.getRegistrationService().page(submitter, includedStatus,
|
150
|
identifierFilterPattern, taxonNameFilterPattern,
|
151
|
PAGE_SIZE, 0, null, REGISTRATION_INIT_STRATEGY);
|
152
|
List<Registration> registrations = pager.getRecords();
|
153
|
Pager<RegistrationDTO> dtoPager = new DefaultPagerImpl(pager.getCurrentIndex(), pager.getCount(), pager.getPageSize(), makeDTOs(registrations));
|
154
|
return dtoPager;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* {@inheritDoc}
|
159
|
* @throws RegistrationValidationException
|
160
|
*/
|
161
|
@Override
|
162
|
public RegistrationWorkingSet loadWorkingSetByReferenceID(Integer referenceID) throws RegistrationValidationException {
|
163
|
|
164
|
Reference reference = repo.getReferenceService().find(referenceID);
|
165
|
Pager<Registration> pager = repo.getRegistrationService().page(Optional.of(reference), null, null, null, REGISTRATION_INIT_STRATEGY);
|
166
|
return new RegistrationWorkingSet(makeDTOs(pager.getRecords()));
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* @param regs
|
171
|
* @return
|
172
|
*/
|
173
|
private List<RegistrationDTO> makeDTOs(List<Registration> regs) {
|
174
|
initializeSpecimens(regs);
|
175
|
List<RegistrationDTO> dtos = new ArrayList<>(regs.size());
|
176
|
regs.forEach(reg -> {dtos.add(new RegistrationDTO(reg));});
|
177
|
return dtos;
|
178
|
}
|
179
|
|
180
|
|
181
|
/**
|
182
|
* @param regs
|
183
|
*/
|
184
|
private void initializeSpecimens(List<Registration> regs) {
|
185
|
for(Registration reg : regs){
|
186
|
inititializeSpecimen(reg);
|
187
|
}
|
188
|
|
189
|
}
|
190
|
|
191
|
|
192
|
/**
|
193
|
* @param reg
|
194
|
*/
|
195
|
protected void inititializeSpecimen(Registration reg) {
|
196
|
|
197
|
for(TypeDesignationBase<?> td : reg.getTypeDesignations()){
|
198
|
if(td instanceof SpecimenTypeDesignation){
|
199
|
|
200
|
DerivedUnit derivedUnit = ((SpecimenTypeDesignation) td).getTypeSpecimen();
|
201
|
@SuppressWarnings("rawtypes")
|
202
|
Set<SpecimenOrObservationBase> sobs = new HashSet<>();
|
203
|
sobs.add(derivedUnit);
|
204
|
|
205
|
while(sobs != null && !sobs.isEmpty()){
|
206
|
@SuppressWarnings("rawtypes")
|
207
|
Set<SpecimenOrObservationBase> nextSobs = null;
|
208
|
for(@SuppressWarnings("rawtypes") SpecimenOrObservationBase sob : sobs){
|
209
|
sob = HibernateProxyHelper.deproxy(sob);
|
210
|
if(sob == null){
|
211
|
continue;
|
212
|
}
|
213
|
if(DerivedUnit.class.isAssignableFrom(sob.getClass())) {
|
214
|
defaultBeanInitializer.initialize(sob, DERIVEDUNIT_INIT_STRATEGY);
|
215
|
nextSobs = ((DerivedUnit)sob).getOriginals();
|
216
|
}
|
217
|
if(sob instanceof FieldUnit){
|
218
|
defaultBeanInitializer.initialize(sob, FIELDUNIT_INIT_STRATEGY);
|
219
|
}
|
220
|
}
|
221
|
sobs = nextSobs;
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
}
|
226
|
|
227
|
|
228
|
|
229
|
|
230
|
|
231
|
|
232
|
}
|