no longer deleting registration when typedesignation editors are canceled
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / registration / RegistrationWorkingsetPresenter.java
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.vaadin.view.registration;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.EnumSet;
14 import java.util.List;
15 import java.util.Stack;
16 import java.util.UUID;
17
18 import org.hibernate.Session;
19 import org.hibernate.Transaction;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.springframework.security.core.Authentication;
22 import org.springframework.transaction.TransactionStatus;
23 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
24
25 import com.vaadin.spring.annotation.SpringComponent;
26 import com.vaadin.spring.annotation.ViewScope;
27 import com.vaadin.ui.Label;
28 import com.vaadin.ui.UI;
29 import com.vaadin.ui.VerticalLayout;
30 import com.vaadin.ui.Window;
31
32 import eu.etaxonomy.cdm.api.service.INameService;
33 import eu.etaxonomy.cdm.api.service.IRegistrationService;
34 import eu.etaxonomy.cdm.api.service.idminter.IdentifierMinter.Identifier;
35 import eu.etaxonomy.cdm.api.service.idminter.RegistrationIdentifierMinter;
36 import eu.etaxonomy.cdm.model.common.User;
37 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
38 import eu.etaxonomy.cdm.model.name.Rank;
39 import eu.etaxonomy.cdm.model.name.Registration;
40 import eu.etaxonomy.cdm.model.name.RegistrationStatus;
41 import eu.etaxonomy.cdm.model.name.TaxonName;
42 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
43 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
44 import eu.etaxonomy.cdm.model.reference.Reference;
45 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
46 import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
47 import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
48 import eu.etaxonomy.cdm.service.CdmStore;
49 import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
50 import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.EditorActionContext;
51 import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
52 import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
53 import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
54 import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
55 import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
56 import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEventEntityTypeFilter;
57 import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
58 import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
59 import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
60 import eu.etaxonomy.cdm.vaadin.model.EntityReference;
61 import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
62 import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
63 import eu.etaxonomy.cdm.vaadin.security.UserHelper;
64 import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
65 import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
66 import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
67 import eu.etaxonomy.cdm.vaadin.view.name.NameTypeDesignationPopupEditor;
68 import eu.etaxonomy.cdm.vaadin.view.name.SpecimenTypeDesignationWorkingsetPopupEditor;
69 import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
70 import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditorMode;
71 import eu.etaxonomy.cdm.vaadin.view.name.TypeDesignationWorkingsetEditorIdSet;
72 import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
73 import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
74 import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
75 import eu.etaxonomy.vaadin.mvp.AbstractView;
76 import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
77 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
78 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
79
80 /**
81 * @author a.kohlbecker
82 * @since Mar 3, 2017
83 *
84 */
85 @SpringComponent
86 @ViewScope
87 public class RegistrationWorkingsetPresenter extends AbstractPresenter<RegistrationWorkingsetView> {
88
89 private static final long serialVersionUID = 1L;
90
91 @Autowired
92 private IRegistrationWorkingSetService regWorkingSetService;
93
94 @Autowired
95 private RegistrationIdentifierMinter minter;
96
97 /**
98 * @return the regWorkingSetService
99 */
100 public IRegistrationWorkingSetService getWorkingSetService() {
101 return regWorkingSetService;
102 }
103
104 private RegistrationWorkingSet workingset;
105
106 private TaxonName newTaxonNameForRegistration = null;
107
108 private RegistrationDTO newRegistrationDTOWithExistingName;
109
110 private RegistrationDTO newNameTypeDesignationTarget;
111
112
113 /**
114 *
115 */
116 public RegistrationWorkingsetPresenter() {
117 }
118
119 /**
120 * Always create a new Store
121 *
122 * @return
123 */
124 protected CdmStore<Registration, IRegistrationService> getRegistrationStore(){
125 return new CdmStore<Registration, IRegistrationService>(getRepo(), getRepo().getRegistrationService());
126 }
127
128 /**
129 * Always create a new Store
130 *
131 * @return
132 */
133 protected CdmStore<TaxonName, INameService> getTaxonNameStore(){
134 return new CdmStore<TaxonName, INameService>(getRepo(), getRepo().getNameService());
135 }
136
137
138 /**
139 * @param taxonNameId
140 * @return
141 */
142 protected Registration createNewRegistrationForName(Integer taxonNameId) {
143 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144 // move into RegistrationWorkflowStateMachine
145 TransactionStatus txStatus = getRepo().startTransaction();
146
147 Identifier<String> identifiers = minter.mint();
148 if(identifiers.getIdentifier() == null){
149 throw new RuntimeException("RegistrationIdentifierMinter configuration incomplete.");
150 }
151 Registration reg = Registration.NewInstance(
152 identifiers.getIdentifier(),
153 identifiers.getLocalId(),
154 taxonNameId != null ? getRepo().getNameService().find(taxonNameId) : null,
155 null);
156 Authentication authentication = currentSecurityContext().getAuthentication();
157 reg.setSubmitter((User)authentication.getPrincipal());
158 EntityChangeEvent event = getRegistrationStore().saveBean(reg, (AbstractView) getView());
159 UserHelper.fromSession().createAuthorityForCurrentUser(Registration.class, event.getEntityId(), Operation.UPDATE, RegistrationStatus.PREPARATION.name());
160 getRepo().commitTransaction(txStatus);
161 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162 return getRepo().getRegistrationService().find(event.getEntityId());
163 }
164
165
166 /**
167 * @param doReload TODO
168 *
169 */
170 protected void refreshView(boolean doReload) {
171 if(workingset == null){
172 return; // nothing to do
173 }
174 if(doReload){
175 loadWorkingSet(workingset.getCitationId());
176 }
177 getView().setWorkingset(workingset);
178 }
179
180
181 /**
182 * {@inheritDoc}
183 */
184 @Override
185 public void handleViewEntered() {
186
187 super.handleViewEntered();
188
189 loadWorkingSet(getView().getCitationID());
190 getView().setWorkingset(workingset);
191
192 CdmFilterablePagingProvider<TaxonName, TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(
193 getRepo().getNameService());
194 CdmTitleCacheCaptionGenerator<TaxonName> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<TaxonName>();
195 getView().getAddExistingNameCombobox().setCaptionGenerator(titleCacheGenrator);
196 getView().getAddExistingNameCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
197 }
198
199
200 /**
201 * @param referenceID
202 */
203 protected void loadWorkingSet(Integer referenceID) {
204 try {
205 workingset = getWorkingSetService().loadWorkingSetByReferenceID(referenceID);
206 } catch (RegistrationValidationException error) {
207 logger.error(error);
208 Window errorDialog = new Window("Validation Error");
209 errorDialog.setModal(true);
210 VerticalLayout subContent = new VerticalLayout();
211 subContent.setMargin(true);
212 errorDialog.setContent(subContent);
213 subContent.addComponent(new Label(error.getMessage()));
214 UI.getCurrent().addWindow(errorDialog);
215 }
216 if(workingset == null || workingset.getCitationId() == null){
217 Reference citation = getRepo().getReferenceService().find(referenceID);
218 workingset = new RegistrationWorkingSet(citation);
219 }
220 }
221
222
223 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
224 public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
225
226 if(!checkFromOwnView(event)){
227 return;
228 }
229
230 ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
231 popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
232 popup.loadInEditor(null);
233 }
234
235 @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
236 public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
237
238 if(!checkFromOwnView(event)){
239 return;
240 }
241 ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
242 popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
243 popup.withDeleteButton(true);
244 popup.loadInEditor(event.getEntityId());
245 }
246
247 @EventBusListenerMethod
248 public void onDoneWithReferencePopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
249 if(event.getPopup() instanceof ReferencePopupEditor){
250 if(event.getReason().equals(Reason.SAVE)){
251 refreshView(true);
252 }
253 }
254 }
255
256 @EventBusListenerMethod
257 public void onDoneWithSpecimenTypeDesignationWorkingsetPopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
258 if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
259 if(event.getReason().equals(Reason.SAVE)){
260 refreshView(true);
261 }
262 }
263 }
264
265 @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
266 public void onRegistrationEditorAction(RegistrationEditorAction event) {
267
268 if(!checkFromOwnView(event)){
269 return;
270 }
271
272 RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class, getView());
273 popup.loadInEditor(event.getEntityId());
274 }
275
276 @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
277 public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
278
279 if(!checkFromOwnView(event)){
280 return;
281 }
282
283 TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
284 popup.setParentEditorActionContext(event.getContext());
285 popup.withDeleteButton(true);
286 configureTaxonNameEditor(popup);
287 popup.loadInEditor(event.getEntityId());
288 if(event.getSourceComponent() != null){
289 popup.setReadOnly(event.getSourceComponent().isReadOnly());
290 }
291
292 }
293
294
295 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
296 public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
297
298 if(!checkFromOwnView(event)){
299 return;
300 }
301
302 newTaxonNameForRegistration = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
303 newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
304 EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration, (AbstractView) getView());
305 newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
306 TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
307 popup.setParentEditorActionContext(event.getContext());
308 popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE,CRUD.DELETE));
309 popup.withDeleteButton(true);
310 configureTaxonNameEditor(popup);
311 popup.loadInEditor(newTaxonNameForRegistration.getId());
312 }
313
314 /**
315 * TODO consider putting this into a Configurer Bean per UIScope.
316 * In the configurator bean this methods popup papamerter should be of the type
317 * AbstractPopupEditor
318 *
319 * @param popup
320 */
321 protected void configureTaxonNameEditor(TaxonNamePopupEditor popup) {
322 popup.enableMode(TaxonNamePopupEditorMode.suppressReplacementAuthorshipData);
323 popup.enableMode(TaxonNamePopupEditorMode.nomenclaturalReferenceSectionEditingOnly);
324 popup.enableMode(TaxonNamePopupEditorMode.requireNomenclaturalReference);
325 }
326
327 /**
328 * Creates a new <code>Registration</code> for a new name that has just been edited
329 * using the <code>TaxonNamePopupEditor</code>. The new name was previously created
330 * in this presenter as <code>newTaxonNameForRegistration</code> (see {@link #onTaxonNameEditorActionAdd(TaxonNameEditorAction)})
331 * and is either filled with data (<code>Reason.SAVE</code>) or it is still empty
332 * (<code>Reason.CANCEL</code>).
333 *
334 *
335 * @param event
336 * @throws RegistrationValidationException
337 */
338 @EventBusListenerMethod
339 public void onDoneWithTaxonnameEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
340 if(event.getPopup() instanceof TaxonNamePopupEditor){
341 TransactionStatus txStatus = getRepo().startTransaction();
342 if(event.getReason().equals(Reason.SAVE)){
343 if(newTaxonNameForRegistration != null){
344 int taxonNameId = newTaxonNameForRegistration.getId();
345 getRepo().getSession().refresh(newTaxonNameForRegistration);
346 Registration reg = createNewRegistrationForName(taxonNameId);
347 // reload workingset into current session
348 loadWorkingSet(workingset.getCitationId());
349 workingset.add(reg);
350 }
351 refreshView(true);
352 } else if(event.getReason().equals(Reason.CANCEL)){
353 if(newTaxonNameForRegistration != null){
354 // clean up
355 getTaxonNameStore().deleteBean(newTaxonNameForRegistration, (AbstractView) getView());
356 }
357 }
358 getRepo().commitTransaction(txStatus);
359 newTaxonNameForRegistration = null;
360 }
361 }
362
363
364 /**
365 * Creates a new Registration for an exiting (previously published name).
366 *
367 * @param event
368 * @throws RegistrationValidationException
369 */
370 @EventBusListenerMethod
371 public void onRegistrationWorkflowEventActionStart(RegistrationWorkingsetAction event) throws RegistrationValidationException {
372
373 if(!event.isStart()){
374 return;
375 }
376
377 getView().getAddExistingNameCombobox().commit();
378 TaxonName typifiedName = getView().getAddExistingNameCombobox().getValue();
379 if(typifiedName != null){
380 Registration newRegistrationWithExistingName = createNewRegistrationForName(null);
381 Reference citation = getRepo().getReferenceService().find(workingset.getCitationId());
382 newRegistrationDTOWithExistingName = new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation);
383 workingset.add(newRegistrationDTOWithExistingName);
384 // tell the view to update the workingset
385 getView().setWorkingset(workingset);
386 getView().getAddExistingNameRegistrationButton().setEnabled(false);
387 getView().getAddExistingNameRegistrationButton().setDescription("You first need to add a type designation to the previously created registration.");
388 } else {
389 logger.error("Seletced name is NULL");
390 }
391
392 }
393
394
395 @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
396 public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
397
398 if(!checkFromOwnView(event)){
399 return;
400 }
401
402 if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
403 SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
404 popup.setParentEditorActionContext(event.getContext());
405 popup.withDeleteButton(true);
406 popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
407 if(event.getSourceComponent() != null){
408 // propagate readonly state from source component to popup
409 popup.setReadOnly(event.getSourceComponent().isReadOnly());
410 }
411 } else {
412 NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
413 popup.setParentEditorActionContext(event.getContext());
414 popup.withDeleteButton(true);
415 popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
416
417 popup.getCitationCombobox().setEnabled(false);
418 popup.getTypifiedNamesComboboxSelect().setEnabled(false);
419
420 if(event.getSourceComponent() != null){
421 // propagate readonly state from source component to popup
422 popup.setReadOnly(event.getSourceComponent().isReadOnly());
423 }
424 newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
425 }
426 }
427
428 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
429 public void onAddNewTypeDesignationWorkingset(TypeDesignationWorkingsetEditorAction event) {
430
431 if(event.getSourceComponent() != null){
432 return;
433 }
434
435 if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
436 SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
437 popup.setParentEditorActionContext(event.getContext());
438 TypeDesignationWorkingsetEditorIdSet identifierSet;
439 Integer typifiedNameId;
440 if(newRegistrationDTOWithExistingName != null){
441 typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedNameRef().getId();
442 } else {
443 RegistrationDTO registrationDTO = workingset.getRegistrationDTO(event.getRegistrationId()).get();
444 EntityReference typifiedNameRef = registrationDTO.getTypifiedNameRef();
445 if(typifiedNameRef != null){
446 // case for registrations without name, in which case the typifiedName is only defined via the typedesignations
447 typifiedNameId = typifiedNameRef.getId();
448 } else {
449 // case of registrations with a name in the nomenclatural act.
450 typifiedNameId = registrationDTO.getNameRef().getId();
451 }
452 }
453 identifierSet = new TypeDesignationWorkingsetEditorIdSet(
454 event.getRegistrationId(),
455 getView().getCitationID(),
456 typifiedNameId
457 );
458 popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
459 popup.loadInEditor(identifierSet);
460 popup.withDeleteButton(true);
461 if(event.getSourceComponent() != null){
462 // propagate readonly state from source component to popup
463 popup.setReadOnly(event.getSourceComponent().isReadOnly());
464 }
465 } else {
466 NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
467 popup.setParentEditorActionContext(event.getContext());
468 popup.withDeleteButton(true);
469 popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
470 newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
471 popup.setBeanInstantiator(new BeanInstantiator<NameTypeDesignation>() {
472
473 @Override
474 public NameTypeDesignation createNewBean() {
475 NameTypeDesignation nameTypeDesignation = NameTypeDesignation.NewInstance();
476 nameTypeDesignation.setCitation(newNameTypeDesignationTarget.getCitation());
477 nameTypeDesignation.getTypifiedNames().add(newNameTypeDesignationTarget.getTypifiedName());
478 return nameTypeDesignation;
479 }
480 });
481 popup.loadInEditor(null);
482 popup.getCitationCombobox().setEnabled(false);
483 popup.getTypifiedNamesComboboxSelect().setEnabled(false);
484 if(event.getSourceComponent() != null){
485 // propagate readonly state from source component to popup
486 popup.setReadOnly(event.getSourceComponent().isReadOnly());
487 }
488 }
489 }
490
491 /**
492 * Performs final actions after a TypeDesignationEditor which has been
493 * opened to add a TypeDesignation to a Registration object which was
494 * created for an previously published name. Prior adding a typedesignation,
495 * the according Registration object is dangling, that has no association to
496 * any entity denoting an nomenclatural act which has a reference to a
497 * publication. This means that the registration object is not in the
498 * working set.
499 *
500 *
501 * @param event
502 * @throws RegistrationValidationException
503 */
504 @EventBusListenerMethod
505 public void onDoneWithTypeDesignationEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
506 if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
507 if(event.getReason().equals(Reason.SAVE)){
508 refreshView(true);
509 } else if(event.getReason().equals(Reason.CANCEL)){
510 // noting to do
511 }
512 newRegistrationDTOWithExistingName = null;
513 } else if(event.getPopup() instanceof NameTypeDesignationPopupEditor){
514 if(event.getReason().equals(Reason.SAVE)){
515 UUID uuid = ((NameTypeDesignationPopupEditor)event.getPopup()).getBean().getUuid();
516
517 Session session = getRepo().getSessionFactory().openSession();
518 Transaction txstate = session.beginTransaction();
519 TypeDesignationBase<?> nameTypeDesignation = getRepo().getNameService().loadTypeDesignation(uuid, Arrays.asList(""));
520 // only load the typeDesignations with the registration so that the typified name can not be twice in the session
521 // otherwise multiple representation problems might occur
522 Registration registration = getRepo().getRegistrationService().load(newNameTypeDesignationTarget.getUuid(), Arrays.asList("typeDesignations"));
523 registration.getTypeDesignations().add(nameTypeDesignation);
524 session.merge(registration);
525 txstate.commit();
526 session.close();
527
528 refreshView(true);
529 } else if(event.getReason().equals(Reason.CANCEL)){
530 // noting to do
531 }
532 newNameTypeDesignationTarget = null;
533 }
534 // ignore other editors
535 }
536
537
538 @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationWorkingSet.class)
539 public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) {
540 List<String> messages = new ArrayList<>();
541 for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
542 dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
543 }
544 if(event.getProperty().equals("messages")){
545 getView().openDetailsPopup("Messages", messages);
546 }
547 }
548
549
550 @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationDTO.class)
551 public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) {
552 RegistrationDTO regDto = regWorkingSetService.loadDtoById((Integer)event.getIdentifier());
553 if(event.getProperty().equals("messages")){
554 if(getView() != null){
555 getView().openDetailsPopup("Messages", regDto.getMessages());
556 }
557 }
558 }
559
560 @EventBusListenerMethod
561 public void onEntityChangeEvent(EntityChangeEvent event){
562 if(workingset == null){
563 return;
564 }
565 if(Reference.class.isAssignableFrom(event.getEntityType())){
566 if(workingset.getCitationId().equals(event.getEntityId())){
567 refreshView(true);
568 }
569 } else
570 if(Registration.class.isAssignableFrom(event.getEntityType())){
571 if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
572 refreshView(true);
573 }
574 } else
575 if(TaxonName.class.isAssignableFrom(event.getEntityType())){
576 if(event.getType().equals(EntityChangeEvent.Type.CREATED)){
577 // new name! create a blocking registration
578 Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getSourceView()).getEditorActionContext();
579 EditorActionContext rootContext = context.get(0);
580 if(rootContext.getParentView().equals(getView())){
581 Registration blockingRegistration = createNewRegistrationForName(event.getEntityId());
582 TypedEntityReference<Registration> regReference = (TypedEntityReference<Registration>)rootContext.getParentEntity();
583 Registration registration = getRepo().getRegistrationService().load(regReference.getId(), Arrays.asList("$", "blockedBy"));
584 registration.getBlockedBy().add(blockingRegistration);
585 getRepo().getRegistrationService().saveOrUpdate(registration);
586 logger.debug("Blocking registration created");
587 } else {
588 logger.debug("Nn blocking registration, since a new name for a new registration has been created");
589 }
590 }
591 if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
592 reg.getTypifiedNameRef() != null
593 && reg.getTypifiedNameRef().getId() == event.getEntityId())){
594 refreshView(true);
595 }
596 } else
597 if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
598 if(workingset.getRegistrationDTOs().stream().anyMatch(
599 reg -> reg.getTypeDesignations() != null && reg.getTypeDesignations().stream().anyMatch(
600 td -> td.getId() == event.getEntityId()
601 )
602 )
603 ){
604 refreshView(true);
605 }
606 }
607
608 }
609
610 }