Revision 02ec8d6b
Added by Andreas Kohlbecker over 6 years ago
src/main/java/eu/etaxonomy/cdm/vaadin/view/name/SpecimenTypeDesignationWorkingsetEditorPresenter.java | ||
---|---|---|
9 | 9 |
package eu.etaxonomy.cdm.vaadin.view.name; |
10 | 10 |
|
11 | 11 |
import java.util.EnumSet; |
12 |
import java.util.HashSet; |
|
13 |
import java.util.Iterator; |
|
14 |
import java.util.Set; |
|
15 | 12 |
|
16 |
import org.hibernate.Session; |
|
17 |
import org.springframework.beans.factory.BeanFactory; |
|
18 | 13 |
import org.springframework.beans.factory.annotation.Autowired; |
19 | 14 |
import org.vaadin.viritin.fields.AbstractElementCollection; |
20 | 15 |
|
21 | 16 |
import eu.etaxonomy.cdm.api.service.IRegistrationService; |
17 |
import eu.etaxonomy.cdm.cache.CdmEntityCache; |
|
18 |
import eu.etaxonomy.cdm.cache.EntityCache; |
|
22 | 19 |
import eu.etaxonomy.cdm.model.common.DefinedTerm; |
23 |
import eu.etaxonomy.cdm.model.common.VersionableEntity; |
|
24 | 20 |
import eu.etaxonomy.cdm.model.location.Country; |
25 | 21 |
import eu.etaxonomy.cdm.model.name.Registration; |
26 |
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation; |
|
27 | 22 |
import eu.etaxonomy.cdm.model.name.TaxonName; |
28 |
import eu.etaxonomy.cdm.model.name.TypeDesignationBase; |
|
29 | 23 |
import eu.etaxonomy.cdm.model.occurrence.Collection; |
30 |
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent; |
|
31 |
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType; |
|
32 |
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit; |
|
33 |
import eu.etaxonomy.cdm.model.occurrence.FieldUnit; |
|
34 |
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent; |
|
35 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
|
36 | 24 |
import eu.etaxonomy.cdm.model.reference.Reference; |
37 | 25 |
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD; |
38 | 26 |
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider; |
39 | 27 |
import eu.etaxonomy.cdm.service.CdmStore; |
40 |
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService; |
|
41 |
import eu.etaxonomy.cdm.service.RegistrationWorkingSetService; |
|
28 |
import eu.etaxonomy.cdm.service.ISpecimenTypeDesignationWorkingSetService; |
|
42 | 29 |
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory; |
43 | 30 |
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater; |
44 |
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
|
|
31 |
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
|
|
45 | 32 |
import eu.etaxonomy.cdm.vaadin.model.registration.KindOfUnitTerms; |
46 | 33 |
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationTermLists; |
47 |
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationDTO; |
|
48 | 34 |
import eu.etaxonomy.cdm.vaadin.model.registration.SpecimenTypeDesignationWorkingSetDTO; |
49 | 35 |
import eu.etaxonomy.cdm.vaadin.security.UserHelper; |
50 | 36 |
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator; |
51 |
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet; |
|
52 |
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO; |
|
53 | 37 |
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter; |
54 |
import eu.etaxonomy.vaadin.ui.view.PopupEditorFactory;
|
|
38 |
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
|
|
55 | 39 |
/** |
56 | 40 |
* SpecimenTypeDesignationWorkingsetPopupEditorView implementation must override the showInEditor() method, |
57 | 41 |
* see {@link #prepareAsFieldGroupDataSource()} for details. |
... | ... | |
61 | 45 |
* |
62 | 46 |
*/ |
63 | 47 |
public class SpecimenTypeDesignationWorkingsetEditorPresenter |
64 |
extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView> { |
|
48 |
extends AbstractEditorPresenter<SpecimenTypeDesignationWorkingSetDTO , SpecimenTypeDesignationWorkingsetPopupEditorView> |
|
49 |
implements CachingPresenter { |
|
65 | 50 |
|
66 | 51 |
private static final long serialVersionUID = 4255636253714476918L; |
67 | 52 |
|
68 | 53 |
CdmStore<Registration, IRegistrationService> store; |
69 | 54 |
|
70 |
private Reference citation; |
|
71 |
|
|
72 |
private TaxonName typifiedName; |
|
73 | 55 |
|
74 | 56 |
/** |
75 | 57 |
* This object for this field will either be injected by the {@link PopupEditorFactory} or by a Spring |
76 | 58 |
* {@link BeanFactory} |
77 | 59 |
*/ |
78 | 60 |
@Autowired |
79 |
private IRegistrationWorkingSetService registrationWorkingSetService;
|
|
61 |
private ISpecimenTypeDesignationWorkingSetService specimenTypeDesignationWorkingSetService;
|
|
80 | 62 |
|
81 | 63 |
/** |
82 | 64 |
* if not null, this CRUD set is to be used to create a CdmAuthoritiy for the base entitiy which will be |
... | ... | |
84 | 66 |
*/ |
85 | 67 |
private EnumSet<CRUD> crud = null; |
86 | 68 |
|
69 |
private CdmEntityCache cache = null; |
|
70 |
|
|
87 | 71 |
protected CdmStore<Registration, IRegistrationService> getStore() { |
88 | 72 |
if(store == null){ |
89 | 73 |
store = new CdmStore<>(getRepo(), getRepo().getRegistrationService()); |
... | ... | |
104 | 88 |
* @param identifier a {@link TypeDesignationWorkingsetEditorIdSet} |
105 | 89 |
*/ |
106 | 90 |
@Override |
107 |
protected SpecimenTypeDesignationWorkingSetDTO loadBeanById(Object identifier) { |
|
91 |
protected SpecimenTypeDesignationWorkingSetDTO<Registration> loadBeanById(Object identifier) {
|
|
108 | 92 |
|
109 |
SpecimenTypeDesignationWorkingSetDTO workingSetDto; |
|
93 |
SpecimenTypeDesignationWorkingSetDTO<Registration> workingSetDto;
|
|
110 | 94 |
if(identifier != null){ |
111 | 95 |
|
112 | 96 |
TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier; |
113 | 97 |
|
114 | 98 |
if(idset.workingsetId != null){ |
115 |
RegistrationDTO regDTO = registrationWorkingSetService.loadDtoById(idset.registrationId); |
|
116 |
// find the working set |
|
117 |
TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.workingsetId); |
|
118 |
workingSetDto = regDTO.getSpecimenTypeDesignationWorkingSetDTO(typeDesignationWorkingSet.getBaseEntityReference()); |
|
119 |
citation = (Reference) regDTO.getCitation(); |
|
120 |
workingSetDto = fixMissingFieldUnit(workingSetDto); |
|
99 |
workingSetDto = specimenTypeDesignationWorkingSetService.loadDtoByIds(idset.registrationId, idset.workingsetId); |
|
100 |
if(workingSetDto.getFieldUnit() == null){ |
|
101 |
workingSetDto = specimenTypeDesignationWorkingSetService.fixMissingFieldUnit(workingSetDto); |
|
102 |
// FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations |
|
103 |
// This method must go again into the presenter !!!! |
|
104 |
logger.info("Basing all typeDesignations on a new fieldUnit"); |
|
105 |
} |
|
106 |
cache = new CdmEntityCache(workingSetDto.getOwner()); |
|
121 | 107 |
} else { |
122 | 108 |
// create a new workingset, for a new fieldunit which is the base for the workingset |
123 |
FieldUnit newfieldUnit = FieldUnit.NewInstance(); |
|
124 |
Registration reg = getRepo().getRegistrationService().load(idset.registrationId, |
|
125 |
RegistrationWorkingSetService.REGISTRATION_INIT_STRATEGY); |
|
126 |
//TODO checkif passing reg as owner parameter is needed at all |
|
127 |
workingSetDto = new SpecimenTypeDesignationWorkingSetDTO(reg, newfieldUnit, null); |
|
128 |
citation = getRepo().getReferenceService().find(idset.publicationId); |
|
129 |
typifiedName = getRepo().getNameService().find(idset.typifiedNameId); |
|
109 |
workingSetDto = specimenTypeDesignationWorkingSetService.create(idset.registrationId, idset.publicationId, idset.typifiedNameId); |
|
110 |
cache = new CdmEntityCache(workingSetDto.getOwner()); |
|
130 | 111 |
} |
131 | 112 |
|
132 | 113 |
} else { |
133 | 114 |
workingSetDto = null; |
134 | 115 |
} |
116 |
|
|
135 | 117 |
return workingSetDto; |
136 | 118 |
} |
137 | 119 |
|
... | ... | |
180 | 162 |
); |
181 | 163 |
row.collection.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Collection>()); |
182 | 164 |
row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Collection>(row.collection)); |
165 |
row.collection.getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Collection>(row.collection.getSelect(), SpecimenTypeDesignationWorkingsetEditorPresenter.this)); |
|
183 | 166 |
|
184 | 167 |
row.mediaSpecimenReference.loadFrom( |
185 | 168 |
referencePagingProvider, |
186 | 169 |
referencePagingProvider, |
187 | 170 |
collectionPagingProvider.getPageSize() |
188 | 171 |
); |
172 |
|
|
189 | 173 |
row.mediaSpecimenReference.getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>()); |
190 | 174 |
row.mediaSpecimenReference.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(row.mediaSpecimenReference)); |
191 | 175 |
|
... | ... | |
212 | 196 |
} |
213 | 197 |
|
214 | 198 |
|
215 |
private SpecimenTypeDesignationWorkingSetDTO fixMissingFieldUnit(SpecimenTypeDesignationWorkingSetDTO bean) { |
|
216 |
|
|
217 |
if(bean.getFieldUnit() == null){ |
|
218 |
// in case the base unit of the working set is not a FieldUnit all contained TypeDesignations must be modified |
|
219 |
// so that they are based on an empty FieldUnit with an associated Gathering Event |
|
220 |
if(Registration.class.isAssignableFrom(bean.getOwner().getClass())){ |
|
221 |
// FIXME open Dialog to warn user about adding an empty fieldUnit to the typeDesignations |
|
222 |
logger.info("Basing all typeDesignations on a new fieldUnit"); |
|
223 |
Session session = getSession(); |
|
224 |
Registration reg = getRepo().getRegistrationService().find(bean.getOwner().getId()); |
|
225 |
RegistrationDTO regDTO = new RegistrationDTO(reg); |
|
226 |
|
|
227 |
FieldUnit fieldUnit = FieldUnit.NewInstance(); |
|
228 |
GatheringEvent gatheringEvent = GatheringEvent.NewInstance(); |
|
229 |
fieldUnit.setGatheringEvent(gatheringEvent); |
|
230 |
getRepo().getOccurrenceService().save(fieldUnit); |
|
231 |
|
|
232 |
VersionableEntity baseEntity = bean.getBaseEntity(); |
|
233 |
Set<TypeDesignationBase> typeDesignations = regDTO.getTypeDesignationsInWorkingSet( |
|
234 |
new TypedEntityReference(baseEntity.getClass(), baseEntity.getId(), baseEntity.toString()) |
|
235 |
); |
|
236 |
for(TypeDesignationBase td : typeDesignations){ |
|
237 |
DerivationEvent de = DerivationEvent.NewInstance();// |
|
238 |
de.addOriginal(fieldUnit); |
|
239 |
de.addDerivative(((SpecimenTypeDesignation)td).getTypeSpecimen()); |
|
240 |
de.setType(DerivationEventType.GATHERING_IN_SITU()); |
|
241 |
} |
|
242 |
|
|
243 |
getRepo().getRegistrationService().saveOrUpdate(reg); |
|
244 |
session.flush(); |
|
245 |
session.close(); |
|
246 |
} else { |
|
247 |
throw new RuntimeException("Usupported owner type " + bean.getOwner().getClass() + ", needs to be implemented."); |
|
248 |
} |
|
249 |
} |
|
250 |
return bean; |
|
251 |
} |
|
252 |
|
|
253 |
|
|
254 |
|
|
255 | 199 |
/** |
256 | 200 |
* {@inheritDoc} |
257 | 201 |
*/ |
258 | 202 |
@Override |
259 | 203 |
protected void saveBean(SpecimenTypeDesignationWorkingSetDTO dto) { |
260 |
|
|
261 |
Registration reg = (Registration) dto.getOwner(); |
|
262 |
|
|
263 |
// associate all type designations with the fieldUnit |
|
264 |
// 1. new ones are not yet associated |
|
265 |
// 2. ones which had incomplete data are also not connected |
|
266 |
for(SpecimenTypeDesignationDTO stdDTO : dto.getSpecimenTypeDesignationDTOs()){ |
|
267 |
try { |
|
268 |
SpecimenOrObservationBase<?> original = findEarliestOriginal(stdDTO.asSpecimenTypeDesignation().getTypeSpecimen()); |
|
269 |
if(original instanceof DerivedUnit){ |
|
270 |
DerivedUnit du = (DerivedUnit)original; |
|
271 |
du.getDerivedFrom().addOriginal(dto.getFieldUnit()); |
|
272 |
} |
|
273 |
} catch (Exception e) { |
|
274 |
// TODO Auto-generated catch block |
|
275 |
e.printStackTrace(); |
|
276 |
} |
|
277 |
} |
|
278 |
|
|
279 |
// add newly added typeDesignations |
|
280 |
Set<SpecimenTypeDesignation> addCandidates = new HashSet<>(); |
|
281 |
for(SpecimenTypeDesignationDTO stdDTO : dto.getSpecimenTypeDesignationDTOs()){ |
|
282 |
SpecimenTypeDesignation std = stdDTO.asSpecimenTypeDesignation(); |
|
283 |
if(reg.getTypeDesignations().isEmpty() || !reg.getTypeDesignations().stream().filter(td -> td.equals(std)).findFirst().isPresent()){ |
|
284 |
std.setCitation(citation); |
|
285 |
typifiedName.addTypeDesignation(std, false); |
|
286 |
addCandidates.add(std); |
|
287 |
} |
|
288 |
} |
|
289 |
addCandidates.forEach(std -> reg.addTypeDesignation(std)); |
|
290 |
|
|
291 |
|
|
292 | 204 |
if(crud != null){ |
293 | 205 |
UserHelper.fromSession().createAuthorityForCurrentUser(dto.getFieldUnit(), crud, null); |
294 | 206 |
} |
295 | 207 |
|
296 |
getStore().saveBean(reg); |
|
208 |
Reference citation = cache.find(Reference.class, dto.getCitationEntityID()); |
|
209 |
TaxonName typifiedName = cache.find(TaxonName.class, dto.getTypifiedNameEntityID()); |
|
210 |
specimenTypeDesignationWorkingSetService.save(dto, citation, typifiedName); |
|
297 | 211 |
} |
298 | 212 |
|
299 | 213 |
/** |
... | ... | |
305 | 219 |
|
306 | 220 |
} |
307 | 221 |
|
308 |
|
|
309 | 222 |
/** |
310 |
* @param std |
|
311 |
* @return |
|
312 |
* @throws Exception |
|
223 |
* @param crud |
|
313 | 224 |
*/ |
314 |
private SpecimenOrObservationBase<?> findEarliestOriginal(DerivedUnit du) throws Exception { |
|
315 |
|
|
316 |
SpecimenOrObservationBase original = du; |
|
225 |
public void setGrantsForCurrentUser(EnumSet<CRUD> crud) { |
|
226 |
this.crud = crud; |
|
317 | 227 |
|
318 |
while(du != null && du.getDerivedFrom() != null && !du.getDerivedFrom().getOriginals().isEmpty()) { |
|
319 |
Iterator<SpecimenOrObservationBase> it = du.getDerivedFrom().getOriginals().iterator(); |
|
320 |
SpecimenOrObservationBase nextOriginal = it.next(); |
|
321 |
if(nextOriginal == null){ |
|
322 |
break; |
|
323 |
} |
|
324 |
original = nextOriginal; |
|
325 |
if(original instanceof DerivedUnit){ |
|
326 |
du = (DerivedUnit)original; |
|
327 |
} else { |
|
328 |
// so this must be a FieldUnit, |
|
329 |
break; |
|
330 |
} |
|
331 |
if(it.hasNext()){ |
|
332 |
throw new Exception(String.format("%s has more than one originals", du.toString())); |
|
333 |
} |
|
334 |
} |
|
335 |
return original; |
|
336 | 228 |
} |
337 | 229 |
|
338 |
|
|
339 | 230 |
/** |
340 |
* @param crud
|
|
231 |
* {@inheritDoc}
|
|
341 | 232 |
*/ |
342 |
public void setGrantsForCurrentUser(EnumSet<CRUD> crud) { |
|
343 |
this.crud = crud; |
|
344 |
|
|
233 |
@Override |
|
234 |
public EntityCache getCache() { |
|
235 |
if(((AbstractPopupEditor)getView()).isBeanLoaded()){ |
|
236 |
return cache; |
|
237 |
} else { |
|
238 |
return null; |
|
239 |
} |
|
345 | 240 |
} |
346 | 241 |
|
347 | 242 |
|
Also available in: Unified diff
ref #7059 ref #7046 using kindOfunit for TypeDesisgnations instead of DerivationEvent type and fixing save and LIE problems:
- intoducing EntityCache to allow re-using previously loaded entities
- CdmEntityCache implements EntityCache
- ToOneRelatedEntityReloader to replace entities loaded by the LazySelect
- SpecimenTypeDesignationWorkingsetEditorPresenter implements CachingPresenter so that EnityCache and
ToOneRelatedEntityReloader can interact
- completing init strategies in RegistrationWorkingSetService
- SpecimenTypeDesignationWorkingsetEditorPresenter.saveBean reuses entities stored in entityCache
- introducing SpecimenTypeDesignationWorkingSetService