Project

General

Profile

Download (27.3 KB) Statistics
| Branch: | Tag: | Revision:
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
        long identifier = System.currentTimeMillis();
147

    
148
        Identifier<String> identifiers = minter.mint();
149
        if(identifiers.getIdentifier() == null){
150
            throw new RuntimeException("RegistrationIdentifierMinter configuration incomplete.");
151
        }
152
        Registration reg = Registration.NewInstance(
153
                identifiers.getIdentifier(),
154
                identifiers.getLocalId(),
155
                taxonNameId != null ? getRepo().getNameService().find(taxonNameId) : null,
156
                null);
157
        Authentication authentication = currentSecurityContext().getAuthentication();
158
        reg.setSubmitter((User)authentication.getPrincipal());
159
        EntityChangeEvent event = getRegistrationStore().saveBean(reg, (AbstractView) getView());
160
        UserHelper.fromSession().createAuthorityForCurrentUser(Registration.class, event.getEntityId(), Operation.UPDATE, RegistrationStatus.PREPARATION.name());
161
        getRepo().commitTransaction(txStatus);
162
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163
        return getRepo().getRegistrationService().find(event.getEntityId());
164
    }
165

    
166

    
167
    /**
168
     * @param doReload TODO
169
     *
170
     */
171
    protected void refreshView(boolean doReload) {
172
        if(workingset == null){
173
            return; // nothing to do
174
        }
175
        if(doReload){
176
            loadWorkingSet(workingset.getCitationId());
177
        }
178
        getView().setWorkingset(workingset);
179
    }
180

    
181

    
182
    /**
183
     * {@inheritDoc}
184
     */
185
    @Override
186
    public void handleViewEntered() {
187

    
188
        super.handleViewEntered();
189

    
190
        loadWorkingSet(getView().getCitationID());
191
        getView().setWorkingset(workingset);
192

    
193
        CdmFilterablePagingProvider<TaxonName, TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(
194
                getRepo().getNameService());
195
        CdmTitleCacheCaptionGenerator<TaxonName> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<TaxonName>();
196
        getView().getAddExistingNameCombobox().setCaptionGenerator(titleCacheGenrator);
197
        getView().getAddExistingNameCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
198
    }
199

    
200

    
201
    /**
202
     * @param referenceID
203
     */
204
    protected void loadWorkingSet(Integer referenceID) {
205
        try {
206
            workingset = getWorkingSetService().loadWorkingSetByReferenceID(referenceID);
207
        } catch (RegistrationValidationException error) {
208
            logger.error(error);
209
            Window errorDialog = new Window("Validation Error");
210
            errorDialog.setModal(true);
211
            VerticalLayout subContent = new VerticalLayout();
212
            subContent.setMargin(true);
213
            errorDialog.setContent(subContent);
214
            subContent.addComponent(new Label(error.getMessage()));
215
            UI.getCurrent().addWindow(errorDialog);
216
        }
217
        if(workingset == null || workingset.getCitationId() == null){
218
            Reference citation = getRepo().getReferenceService().find(referenceID);
219
            workingset = new RegistrationWorkingSet(citation);
220
        }
221
    }
222

    
223

    
224
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
225
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
226

    
227
        if(!checkFromOwnView(event)){
228
            return;
229
        }
230

    
231
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
232
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
233
        popup.loadInEditor(null);
234
    }
235

    
236
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
237
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
238

    
239
        if(!checkFromOwnView(event)){
240
            return;
241
        }
242
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
243
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
244
        popup.withDeleteButton(true);
245
        popup.loadInEditor(event.getEntityId());
246
    }
247

    
248
    @EventBusListenerMethod
249
    public void onDoneWithReferencePopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
250
        if(event.getPopup() instanceof ReferencePopupEditor){
251
            if(event.getReason().equals(Reason.SAVE)){
252
                refreshView(true);
253
            }
254
        }
255
    }
256

    
257
    @EventBusListenerMethod
258
    public void onDoneWithSpecimenTypeDesignationWorkingsetPopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
259
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
260
            if(event.getReason().equals(Reason.SAVE)){
261
                refreshView(true);
262
            }
263
        }
264
    }
265

    
266
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
267
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
268

    
269
        if(!checkFromOwnView(event)){
270
            return;
271
        }
272

    
273
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class, getView());
274
        popup.loadInEditor(event.getEntityId());
275
    }
276

    
277
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
278
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
279

    
280
        if(!checkFromOwnView(event)){
281
            return;
282
        }
283

    
284
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
285
        popup.setParentEditorActionContext(event.getContext());
286
        popup.withDeleteButton(true);
287
        configureTaxonNameEditor(popup);
288
        popup.loadInEditor(event.getEntityId());
289
        if(event.getSourceComponent() != null){
290
            popup.setReadOnly(event.getSourceComponent().isReadOnly());
291
        }
292

    
293
    }
294

    
295

    
296
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
297
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
298

    
299
        if(!checkFromOwnView(event)){
300
            return;
301
        }
302

    
303
        newTaxonNameForRegistration = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
304
        newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
305
        EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration, (AbstractView) getView());
306
        newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
307
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
308
        popup.setParentEditorActionContext(event.getContext());
309
        popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE,CRUD.DELETE));
310
        popup.withDeleteButton(true);
311
        configureTaxonNameEditor(popup);
312
        popup.loadInEditor(newTaxonNameForRegistration.getId());
313
    }
314

    
315
    /**
316
     * TODO consider putting this into a Configurer Bean per UIScope.
317
     * In the configurator bean this methods popup papamerter should be of the type
318
     * AbstractPopupEditor
319
     *
320
     * @param popup
321
     */
322
    protected void configureTaxonNameEditor(TaxonNamePopupEditor popup) {
323
        popup.enableMode(TaxonNamePopupEditorMode.suppressReplacementAuthorshipData);
324
        popup.enableMode(TaxonNamePopupEditorMode.nomenclaturalReferenceSectionEditingOnly);
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
                // clean up
511
                if(newRegistrationDTOWithExistingName != null){
512
                    getRegistrationStore().deleteBean(newRegistrationDTOWithExistingName.registration(), (AbstractView) getView());
513
                }
514
            }
515
            // set newRegistrationDTOWithExistingName NULL in any case
516
            newRegistrationDTOWithExistingName = null;
517
        } else if(event.getPopup() instanceof NameTypeDesignationPopupEditor){
518
            if(event.getReason().equals(Reason.SAVE)){
519
                UUID uuid = ((NameTypeDesignationPopupEditor)event.getPopup()).getBean().getUuid();
520

    
521
                Session session = getRepo().getSessionFactory().openSession();
522
                Transaction txstate = session.beginTransaction();
523
                TypeDesignationBase<?> nameTypeDesignation = getRepo().getNameService().loadTypeDesignation(uuid, Arrays.asList(""));
524
                // only load the typeDesignations with the registration so that the typified name can  not be twice in the session
525
                // otherwise multiple representation problems might occur
526
                Registration registration = getRepo().getRegistrationService().load(newNameTypeDesignationTarget.getUuid(), Arrays.asList("typeDesignations"));
527
                registration.getTypeDesignations().add(nameTypeDesignation);
528
                session.merge(registration);
529
                txstate.commit();
530
                session.close();
531

    
532
                newNameTypeDesignationTarget = null;
533
                refreshView(true);
534
            } else if(event.getReason().equals(Reason.CANCEL)){
535
                // noting to do
536
            }
537
        }
538
        // ignore other editors
539
    }
540

    
541

    
542
    @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationWorkingSet.class)
543
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) {
544
        List<String> messages = new ArrayList<>();
545
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
546
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
547
        }
548
        if(event.getProperty().equals("messages")){
549
            getView().openDetailsPopup("Messages", messages);
550
        }
551
    }
552

    
553

    
554
    @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationDTO.class)
555
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) {
556
        RegistrationDTO regDto = regWorkingSetService.loadDtoById((Integer)event.getIdentifier());
557
        if(event.getProperty().equals("messages")){
558
            if(getView() != null){
559
                getView().openDetailsPopup("Messages", regDto.getMessages());
560
            }
561
        }
562
    }
563

    
564
    @EventBusListenerMethod
565
    public void onEntityChangeEvent(EntityChangeEvent event){
566
        if(workingset == null){
567
            return;
568
        }
569
        if(Reference.class.isAssignableFrom(event.getEntityType())){
570
            if(workingset.getCitationId().equals(event.getEntityId())){
571
                refreshView(true);
572
            }
573
        } else
574
        if(Registration.class.isAssignableFrom(event.getEntityType())){
575
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
576
                refreshView(true);
577
            }
578
        } else
579
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
580
            if(event.getType().equals(EntityChangeEvent.Type.CREATED)){
581
                // new name! create a blocking registration
582
                Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getSourceView()).getEditorActionContext();
583
                EditorActionContext rootContext = context.get(0);
584
                if(rootContext.getParentView().equals(getView())){
585
                    TypedEntityReference<Registration> regReference = (TypedEntityReference<Registration>)rootContext.getParentEntity();
586
                    TaxonName newName = getRepo().getNameService().load(event.getEntityId(), Arrays.asList("$", "registrations"));
587
                    Registration blockingRegistration = Registration.NewInstance();
588
                    blockingRegistration = getRepo().getRegistrationService().save(blockingRegistration);
589
                    blockingRegistration.setName(newName);
590
                    Registration registration = getRepo().getRegistrationService().load(regReference.getId(), Arrays.asList("$", "blockedBy"));
591
                    registration.getBlockedBy().add(blockingRegistration);
592
                    getRepo().getRegistrationService().saveOrUpdate(registration);
593
                    logger.debug("Blocking registration created");
594
                } else {
595
                    throw new RuntimeException("huuu?");
596
                }
597
            }
598
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
599
                reg.getTypifiedNameRef() != null
600
                && reg.getTypifiedNameRef().getId() == event.getEntityId())){
601
                    refreshView(true);
602
            }
603
        } else
604
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
605
            if(workingset.getRegistrationDTOs().stream().anyMatch(
606
                    reg -> reg.getTypeDesignations() != null && reg.getTypeDesignations().stream().anyMatch(
607
                            td -> td.getId() == event.getEntityId()
608
                            )
609
                        )
610
                    ){
611
                refreshView(true);
612
            }
613
        }
614

    
615
    }
616

    
617
}
(12-12/19)