Project

General

Profile

Download (27.8 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.Set;
16
import java.util.Stack;
17
import java.util.UUID;
18

    
19
import org.hibernate.Session;
20
import org.hibernate.Transaction;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.security.core.Authentication;
23
import org.springframework.transaction.TransactionStatus;
24
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
25

    
26
import com.vaadin.spring.annotation.SpringComponent;
27
import com.vaadin.spring.annotation.ViewScope;
28
import com.vaadin.ui.Label;
29
import com.vaadin.ui.UI;
30
import com.vaadin.ui.VerticalLayout;
31
import com.vaadin.ui.Window;
32

    
33
import eu.etaxonomy.cdm.api.service.INameService;
34
import eu.etaxonomy.cdm.api.service.IRegistrationService;
35
import eu.etaxonomy.cdm.api.service.idminter.IdentifierMinter.Identifier;
36
import eu.etaxonomy.cdm.api.service.idminter.RegistrationIdentifierMinter;
37
import eu.etaxonomy.cdm.model.common.User;
38
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.Registration;
41
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
42
import eu.etaxonomy.cdm.model.name.TaxonName;
43
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
44
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
45
import eu.etaxonomy.cdm.model.reference.Reference;
46
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
47
import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
48
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
49
import eu.etaxonomy.cdm.service.CdmStore;
50
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
51
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.EditorActionContext;
52
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
53
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
54
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
55
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
56
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
57
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEventEntityTypeFilter;
58
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
59
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
60
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction;
61
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
62
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
63
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
64
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
65
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
66
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
67
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
68
import eu.etaxonomy.cdm.vaadin.view.name.NameTypeDesignationPopupEditor;
69
import eu.etaxonomy.cdm.vaadin.view.name.SpecimenTypeDesignationWorkingsetPopupEditor;
70
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
71
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditorMode;
72
import eu.etaxonomy.cdm.vaadin.view.name.TypeDesignationWorkingsetEditorIdSet;
73
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
74
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
75
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
76
import eu.etaxonomy.vaadin.mvp.AbstractView;
77
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
78
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
79
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
80

    
81
/**
82
 * @author a.kohlbecker
83
 * @since Mar 3, 2017
84
 *
85
 */
86
@SpringComponent
87
@ViewScope
88
public class RegistrationWorkingsetPresenter extends AbstractPresenter<RegistrationWorkingsetView> {
89

    
90
    private static final long serialVersionUID = 1L;
91

    
92
    @Autowired
93
    private IRegistrationWorkingSetService regWorkingSetService;
94

    
95
    @Autowired
96
    private RegistrationIdentifierMinter minter;
97

    
98
    /**
99
     * @return the regWorkingSetService
100
     */
101
    public IRegistrationWorkingSetService getWorkingSetService() {
102
        return regWorkingSetService;
103
    }
104

    
105
    private RegistrationWorkingSet workingset;
106

    
107
    private TaxonName newTaxonNameForRegistration = null;
108

    
109
    private RegistrationDTO newRegistrationDTOWithExistingName;
110

    
111
    private RegistrationDTO newNameTypeDesignationTarget;
112

    
113

    
114
    /**
115
     *
116
     */
117
    public RegistrationWorkingsetPresenter() {
118
    }
119

    
120
    /**
121
     * Always create a new Store
122
     *
123
     * @return
124
     */
125
    protected CdmStore<Registration, IRegistrationService> getRegistrationStore(){
126
        return new CdmStore<Registration, IRegistrationService>(getRepo(), getRepo().getRegistrationService());
127
    }
128

    
129
    /**
130
     * Always create a new Store
131
     *
132
     * @return
133
     */
134
    protected CdmStore<TaxonName, INameService> getTaxonNameStore(){
135
        return new  CdmStore<TaxonName, INameService>(getRepo(), getRepo().getNameService());
136
    }
137

    
138

    
139
    /**
140
     * @param taxonNameId
141
     * @return
142
     */
143
    protected Registration createNewRegistrationForName(Integer taxonNameId) {
144
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145
        // move into RegistrationWorkflowStateMachine
146
        TransactionStatus txStatus = getRepo().startTransaction();
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
        popup.enableMode(TaxonNamePopupEditorMode.requireNomenclaturalReference);
326
    }
327

    
328
    /**
329
     * Creates a new <code>Registration</code> for a new name that has just been edited
330
     * using the <code>TaxonNamePopupEditor</code>. The new name was previously created
331
     * in this presenter as <code>newTaxonNameForRegistration</code> (see {@link #onTaxonNameEditorActionAdd(TaxonNameEditorAction)})
332
     * and is either filled with data (<code>Reason.SAVE</code>) or it is still empty
333
     * (<code>Reason.CANCEL</code>).
334
     *
335
     *
336
     * @param event
337
     * @throws RegistrationValidationException
338
     */
339
    @EventBusListenerMethod
340
    public void onDoneWithTaxonnameEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
341
        if(event.getPopup() instanceof TaxonNamePopupEditor){
342
            TransactionStatus txStatus = getRepo().startTransaction();
343
            if(event.getReason().equals(Reason.SAVE)){
344
                if(newTaxonNameForRegistration != null){
345
                    int taxonNameId = newTaxonNameForRegistration.getId();
346
                    getRepo().getSession().refresh(newTaxonNameForRegistration);
347
                    Registration reg = createNewRegistrationForName(taxonNameId);
348
                    // reload workingset into current session
349
                    loadWorkingSet(workingset.getCitationId());
350
                    workingset.add(reg);
351
                }
352
                refreshView(true);
353
            } else if(event.getReason().equals(Reason.CANCEL)){
354
                if(newTaxonNameForRegistration != null){
355
                    // clean up
356
                    getTaxonNameStore().deleteBean(newTaxonNameForRegistration, (AbstractView) getView());
357
                }
358
            }
359
            getRepo().commitTransaction(txStatus);
360
            newTaxonNameForRegistration = null;
361
        }
362
    }
363

    
364

    
365
    /**
366
     * Creates a new Registration for an exiting (previously published name).
367
     *
368
     * @param event
369
     * @throws RegistrationValidationException
370
     */
371
    @EventBusListenerMethod
372
    public void onRegistrationWorkflowEventActionStart(RegistrationWorkingsetAction event) throws RegistrationValidationException {
373

    
374
        if(!event.isStart()){
375
            return;
376
        }
377

    
378
        getView().getAddExistingNameCombobox().commit();
379
        TaxonName typifiedName = getView().getAddExistingNameCombobox().getValue();
380
        if(typifiedName != null){
381
            Registration newRegistrationWithExistingName = createNewRegistrationForName(null);
382
            Reference citation = getRepo().getReferenceService().find(workingset.getCitationId());
383
            newRegistrationDTOWithExistingName = new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation);
384
            workingset.add(newRegistrationDTOWithExistingName);
385
            // tell the view to update the workingset
386
            getView().setWorkingset(workingset);
387
            getView().getAddExistingNameRegistrationButton().setEnabled(false);
388
            getView().getAddExistingNameRegistrationButton().setDescription("You first need to add a type designation to the previously created registration.");
389
        } else {
390
            logger.error("Seletced name is NULL");
391
        }
392

    
393
    }
394

    
395

    
396
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
397
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
398

    
399
        if(!checkFromOwnView(event)){
400
            return;
401
        }
402

    
403
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
404
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
405
            popup.setParentEditorActionContext(event.getContext());
406
            popup.withDeleteButton(true);
407
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
408
            if(event.getSourceComponent() != null){
409
                // propagate readonly state from source component to popup
410
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
411
            }
412
        } else {
413
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
414
            popup.setParentEditorActionContext(event.getContext());
415
            popup.withDeleteButton(true);
416
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
417

    
418
            popup.getCitationCombobox().setEnabled(false);
419
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
420

    
421
            if(event.getSourceComponent() != null){
422
                // propagate readonly state from source component to popup
423
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
424
            }
425
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
426
        }
427
    }
428

    
429
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
430
    public void onTypeDesignationWorkingsetAdd(TypeDesignationWorkingsetEditorAction event) {
431

    
432
        if(event.getSourceComponent() != null){
433
            return;
434
        }
435

    
436
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
437
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
438
            popup.setParentEditorActionContext(event.getContext());
439
            TypeDesignationWorkingsetEditorIdSet identifierSet;
440
            Integer typifiedNameId;
441
            if(newRegistrationDTOWithExistingName != null){
442
                typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedNameRef().getId();
443
            } else {
444
                RegistrationDTO registrationDTO = workingset.getRegistrationDTO(event.getRegistrationId()).get();
445
                EntityReference typifiedNameRef = registrationDTO.getTypifiedNameRef();
446
                if(typifiedNameRef != null){
447
                    // case for registrations without name, in which case the typifiedName is only defined via the typedesignations
448
                    typifiedNameId = typifiedNameRef.getId();
449
                } else {
450
                    // case of registrations with a name in the nomenclatural act.
451
                    typifiedNameId = registrationDTO.getNameRef().getId();
452
                }
453
            }
454
            identifierSet = new TypeDesignationWorkingsetEditorIdSet(
455
                    event.getRegistrationId(),
456
                    getView().getCitationID(),
457
                    typifiedNameId
458
                    );
459
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
460
            popup.loadInEditor(identifierSet);
461
            popup.withDeleteButton(true);
462
            if(event.getSourceComponent() != null){
463
                // propagate readonly state from source component to popup
464
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
465
            }
466
        } else {
467
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
468
            popup.setParentEditorActionContext(event.getContext());
469
            popup.withDeleteButton(true);
470
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
471
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
472

    
473
            popup.setBeanInstantiator(new BeanInstantiator<NameTypeDesignation>() {
474

    
475
                @Override
476
                public NameTypeDesignation createNewBean() {
477
                    TaxonName typifiedName = newNameTypeDesignationTarget.getTypifiedName();
478
                    if(typifiedName == null){
479
                        // this will be the first type designation, so the nomenclatural act must contain a name
480
                        typifiedName = newNameTypeDesignationTarget.registration().getName();
481
                    }
482
                    NameTypeDesignation nameTypeDesignation  = NameTypeDesignation.NewInstance();
483
                    nameTypeDesignation.setCitation(newNameTypeDesignationTarget.getCitation());
484
                    nameTypeDesignation.getTypifiedNames().add(typifiedName);
485
                    return nameTypeDesignation;
486
                }
487
            });
488
            popup.loadInEditor(null);
489
            popup.getCitationCombobox().setEnabled(false);
490
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
491
            if(event.getSourceComponent() != null){
492
                // propagate readonly state from source component to popup
493
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
494
            }
495
        }
496
    }
497

    
498
    /**
499
     * Performs final actions after a TypeDesignationEditor which has been
500
     * opened to add a TypeDesignation to a Registration object which was
501
     * created for an previously published name. Prior adding a typedesignation,
502
     * the according Registration object is dangling, that has no association to
503
     * any entity denoting an nomenclatural act which has a reference to a
504
     * publication. This means that the registration object is not in the
505
     * working set.
506
     *
507
     *
508
     * @param event
509
     * @throws RegistrationValidationException
510
     */
511
    @EventBusListenerMethod
512
    public void onDoneWithTypeDesignationEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
513
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
514
            if(event.getReason().equals(Reason.SAVE)){
515
                refreshView(true);
516
            } else if(event.getReason().equals(Reason.CANCEL)){
517
                // noting to do
518
            }
519
            newRegistrationDTOWithExistingName = null;
520
        } else if(event.getPopup() instanceof NameTypeDesignationPopupEditor){
521
            if(event.getReason().equals(Reason.SAVE)){
522
                UUID uuid = ((NameTypeDesignationPopupEditor)event.getPopup()).getBean().getUuid();
523

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

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

    
544

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

    
556

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

    
567
    @EventBusListenerMethod
568
    public void onEntityChangeEvent(EntityChangeEvent event){
569
        if(workingset == null){
570
            return;
571
        }
572
        if(Reference.class.isAssignableFrom(event.getEntityType())){
573
            if(workingset.getCitationId().equals(event.getEntityId())){
574
                refreshView(true);
575
            }
576
        } else
577
        if(Registration.class.isAssignableFrom(event.getEntityType())){
578
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
579
                refreshView(true);
580
            }
581
        } else
582
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
583
            if(event.getType().equals(EntityChangeEvent.Type.CREATED)){
584
                // new name! create a blocking registration
585
                Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getSourceView()).getEditorActionContext();
586
                EditorActionContext rootContext = context.get(0);
587
                if(rootContext.getParentView().equals(getView())){
588
                    Registration blockingRegistration = createNewRegistrationForName(event.getEntityId());
589
                    TypedEntityReference<Registration> regReference = (TypedEntityReference<Registration>)rootContext.getParentEntity();
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
                    logger.debug("Nn blocking registration, since a new name for a new registration has been created");
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
    @EventBusListenerMethod
617
    public void onShowDetailsEvent(ShowDetailsEvent<RegistrationDTO, Integer> event) {
618

    
619
        // FIXME check from own view!!!
620
        if(getView() == null){
621
            return;
622
        }
623

    
624
        Integer registrationId = event.getIdentifier();
625

    
626
        RegistrationDTO regDto = getWorkingSetService().loadDtoById(registrationId);
627
        if(event.getProperty().equals("blockedBy")){
628

    
629
            Set<RegistrationDTO> blockingRegs = getWorkingSetService().loadBlockingRegistrations(registrationId);
630
            getView().setBlockingRegistrations(registrationId, blockingRegs);
631
        }
632

    
633
    }
634

    
635
}
(12-12/19)