Project

General

Profile

Download (25.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.UUID;
16

    
17
import org.hibernate.Session;
18
import org.hibernate.Transaction;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.context.event.EventListener;
21
import org.springframework.security.core.Authentication;
22
import org.springframework.transaction.TransactionStatus;
23

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

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

    
73
/**
74
 * @author a.kohlbecker
75
 * @since Mar 3, 2017
76
 *
77
 */
78
@SpringComponent
79
@ViewScope
80
public class RegistrationWorkingsetPresenter extends AbstractPresenter<RegistrationWorkingsetView> {
81

    
82
    private static final long serialVersionUID = 1L;
83

    
84
    @Autowired
85
    private IRegistrationWorkingSetService regWorkingSetService;
86

    
87
    @Autowired
88
    private RegistrationIdentifierMinter minter;
89

    
90
    /**
91
     * @return the regWorkingSetService
92
     */
93
    public IRegistrationWorkingSetService getWorkingSetService() {
94
        return regWorkingSetService;
95
    }
96

    
97
    private RegistrationWorkingSet workingset;
98

    
99
    private TaxonName newTaxonNameForRegistration = null;
100

    
101
    private RegistrationDTO newRegistrationDTOWithExistingName;
102

    
103
    private RegistrationDTO newNameTypeDesignationTarget;
104

    
105

    
106
    /**
107
     *
108
     */
109
    public RegistrationWorkingsetPresenter() {
110
    }
111

    
112

    
113
    /**
114
     * Always create a new Store
115
     *
116
     * @return
117
     */
118
    protected CdmStore<Registration, IRegistrationService> getRegistrationStore(){
119
        return new CdmStore<Registration, IRegistrationService>(getRepo(), getRepo().getRegistrationService());
120
    }
121

    
122
    /**
123
     * Always create a new Store
124
     *
125
     * @return
126
     */
127
    protected CdmStore<TaxonName, INameService> getTaxonNameStore(){
128
        return new  CdmStore<TaxonName, INameService>(getRepo(), getRepo().getNameService());
129
    }
130

    
131

    
132
    /**
133
     * @param taxonNameId
134
     * @return
135
     */
136
    protected Registration createNewRegistrationForName(Integer taxonNameId) {
137
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
        // move into RegistrationWorkflowStateMachine
139
        TransactionStatus txStatus = getRepo().startTransaction();
140
        long identifier = System.currentTimeMillis();
141

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

    
160

    
161
    /**
162
     * @param doReload TODO
163
     *
164
     */
165
    protected void refreshView(boolean doReload) {
166
        if(workingset == null){
167
            return; // nothing to do
168
        }
169
        if(doReload){
170
            loadWorkingSet(workingset.getCitationId());
171
        }
172
        getView().setWorkingset(workingset);
173
    }
174

    
175

    
176
    /**
177
     * {@inheritDoc}
178
     */
179
    @Override
180
    public void handleViewEntered() {
181

    
182
        super.handleViewEntered();
183

    
184
        loadWorkingSet(getView().getCitationID());
185
        getView().setWorkingset(workingset);
186

    
187
        CdmFilterablePagingProvider<TaxonName, TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(
188
                getRepo().getNameService());
189
        CdmTitleCacheCaptionGenerator<TaxonName> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<TaxonName>();
190
        getView().getAddExistingNameCombobox().setCaptionGenerator(titleCacheGenrator);
191
        getView().getAddExistingNameCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
192
    }
193

    
194

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

    
217
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD")
218
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
219

    
220
        if(!checkFromOwnView(event)){
221
            return;
222
        }
223

    
224
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
225
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
226
        popup.loadInEditor(null);
227
    }
228

    
229
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
230
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
231

    
232
        if(!checkFromOwnView(event)){
233
            return;
234
        }
235
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
236
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
237
        popup.withDeleteButton(true);
238
        popup.loadInEditor(event.getEntityId());
239
    }
240

    
241
    @EventListener
242
    public void onDoneWithReferencePopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
243
        if(event.getPopup() instanceof ReferencePopupEditor){
244
            if(event.getReason().equals(Reason.SAVE)){
245
                refreshView(true);
246
            }
247
        }
248
    }
249

    
250
    @EventListener
251
    public void onDoneWithSpecimenTypeDesignationWorkingsetPopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
252
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
253
            if(event.getReason().equals(Reason.SAVE)){
254
                refreshView(true);
255
            }
256
        }
257
    }
258

    
259
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
260
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
261

    
262
        if(!checkFromOwnView(event)){
263
            return;
264
        }
265

    
266
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
267
        popup.loadInEditor(event.getEntityId());
268
    }
269

    
270
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
271
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
272

    
273
        if(!checkFromOwnView(event)){
274
            return;
275
        }
276

    
277
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
278
        popup.withDeleteButton(true);
279
        configureTaxonNameEditor(popup);
280
        popup.loadInEditor(event.getEntityId());
281
        if(event.getSourceComponent() != null){
282
            popup.setReadOnly(event.getSourceComponent().isReadOnly());
283
        }
284

    
285
    }
286

    
287

    
288
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD")
289
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
290

    
291
        if(!checkFromOwnView(event)){
292
            return;
293
        }
294

    
295
        newTaxonNameForRegistration = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
296
        newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
297
        EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration);
298
        newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
299
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
300
        popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE,CRUD.DELETE));
301
        popup.withDeleteButton(true);
302
        configureTaxonNameEditor(popup);
303
        popup.loadInEditor(newTaxonNameForRegistration.getId());
304
    }
305

    
306
    /**
307
     * TODO consider putting this into a Configurer Bean per UIScope.
308
     * In the configurator bean this methods popup papamerter should be of the type
309
     * AbstractPopupEditor
310
     *
311
     * @param popup
312
     */
313
    protected void configureTaxonNameEditor(TaxonNamePopupEditor popup) {
314
        popup.enableMode(TaxonNamePopupEditorMode.suppressReplacementAuthorshipData);
315
        popup.enableMode(TaxonNamePopupEditorMode.nomenclaturalReferenceSectionEditingOnly);
316
    }
317

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

    
354

    
355
    /**
356
     * Creates a new Registration for an exiting (previously published name).
357
     *
358
     * @param event
359
     * @throws RegistrationValidationException
360
     */
361
    @EventListener(condition = "#event.action == T(eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkingsetAction.Action).start")
362
    public void onRegistrationWorkflowEventActionStart(RegistrationWorkingsetAction event) throws RegistrationValidationException {
363

    
364
        getView().getAddExistingNameCombobox().commit();
365
        TaxonName typifiedName = getView().getAddExistingNameCombobox().getValue();
366
        if(typifiedName != null){
367
            Registration newRegistrationWithExistingName = createNewRegistrationForName(null);
368
            Reference citation = getRepo().getReferenceService().find(workingset.getCitationId());
369
            newRegistrationDTOWithExistingName = new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation);
370
            workingset.add(newRegistrationDTOWithExistingName);
371
            // tell the view to update the workingset
372
            getView().setWorkingset(workingset);
373
            getView().getAddExistingNameRegistrationButton().setEnabled(false);
374
            getView().getAddExistingNameRegistrationButton().setDescription("You first need to add a type designation to the previously created registration.");
375
        } else {
376
            logger.error("Seletced name is NULL");
377
        }
378

    
379
    }
380

    
381
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).EDIT")
382
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
383

    
384
        if(!checkFromOwnView(event)){
385
            return;
386
        }
387

    
388
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
389
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
390
            popup.withDeleteButton(true);
391
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
392
            if(event.getSourceComponent() != null){
393
                // propagate readonly state from source component to popup
394
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
395
            }
396
        } else {
397
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class);
398
            popup.withDeleteButton(true);
399
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
400

    
401
            popup.getCitationCombobox().setEnabled(false);
402
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
403

    
404
            if(event.getSourceComponent() != null){
405
                // propagate readonly state from source component to popup
406
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
407
            }
408
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
409
        }
410
    }
411

    
412
    @EventListener(condition = "#event.type == T(eu.etaxonomy.vaadin.event.EditorActionType).ADD && #event.sourceComponent == null")
413
    public void onAddNewTypeDesignationWorkingset(TypeDesignationWorkingsetEditorAction event) {
414

    
415
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
416
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
417
            TypeDesignationWorkingsetEditorIdSet identifierSet;
418
            Integer typifiedNameId;
419
            if(newRegistrationDTOWithExistingName != null){
420
                typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedNameRef().getId();
421
            } else {
422
                RegistrationDTO registrationDTO = workingset.getRegistrationDTO(event.getRegistrationId()).get();
423
                EntityReference typifiedNameRef = registrationDTO.getTypifiedNameRef();
424
                if(typifiedNameRef != null){
425
                    // case for registrations without name, in which case the typifiedName is only defined via the typedesignations
426
                    typifiedNameId = typifiedNameRef.getId();
427
                } else {
428
                    // case of registrations with a name in the nomenclatural act.
429
                    typifiedNameId = registrationDTO.getNameRef().getId();
430
                }
431
            }
432
            identifierSet = new TypeDesignationWorkingsetEditorIdSet(
433
                    event.getRegistrationId(),
434
                    getView().getCitationID(),
435
                    typifiedNameId
436
                    );
437
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
438
            popup.loadInEditor(identifierSet);
439
            popup.withDeleteButton(true);
440
            if(event.getSourceComponent() != null){
441
                // propagate readonly state from source component to popup
442
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
443
            }
444
        } else {
445
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class);
446
            popup.withDeleteButton(true);
447
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
448
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
449
            popup.setBeanInstantiator(new BeanInstantiator<NameTypeDesignation>() {
450

    
451
                @Override
452
                public NameTypeDesignation createNewBean() {
453
                    NameTypeDesignation nameTypeDesignation  = NameTypeDesignation.NewInstance();
454
                    nameTypeDesignation.setCitation(newNameTypeDesignationTarget.getCitation());
455
                    nameTypeDesignation.getTypifiedNames().add(newNameTypeDesignationTarget.getTypifiedName());
456
                    return nameTypeDesignation;
457
                }
458
            });
459
            popup.loadInEditor(null);
460
            popup.getCitationCombobox().setEnabled(false);
461
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
462
            if(event.getSourceComponent() != null){
463
                // propagate readonly state from source component to popup
464
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
465
            }
466
        }
467
    }
468

    
469
    /**
470
     * Performs final actions after a TypeDesignationEditor which has been
471
     * opened to add a TypeDesignation to a Registration object which was
472
     * created for an previously published name. Prior adding a typedesignation,
473
     * the according Registration object is dangling, that has no association to
474
     * any entity denoting an nomenclatural act which has a reference to a
475
     * publication. This means that the registration object is not in the
476
     * working set.
477
     *
478
     *
479
     * @param event
480
     * @throws RegistrationValidationException
481
     */
482
    @EventListener
483
    public void onDoneWithTypeDesignationEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
484
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
485
            if(event.getReason().equals(Reason.SAVE)){
486
                refreshView(true);
487
            } else if(event.getReason().equals(Reason.CANCEL)){
488
                // clean up
489
                if(newRegistrationDTOWithExistingName != null){
490
                    getRegistrationStore().deleteBean(newRegistrationDTOWithExistingName.registration());
491
                }
492
            }
493
            // set newRegistrationDTOWithExistingName NULL in any case
494
            newRegistrationDTOWithExistingName = null;
495
        } else if(event.getPopup() instanceof NameTypeDesignationPopupEditor){
496
            if(event.getReason().equals(Reason.SAVE)){
497
                UUID uuid = ((NameTypeDesignationPopupEditor)event.getPopup()).getBean().getUuid();
498

    
499
                Session session = getRepo().getSessionFactory().openSession();
500
                Transaction txstate = session.beginTransaction();
501
                TypeDesignationBase<?> nameTypeDesignation = getRepo().getNameService().loadTypeDesignation(uuid, Arrays.asList(""));
502
                // only load the typeDesignations with the registration so that the typified name can  not be twice in the session
503
                // otherwise multiple representation problems might occur
504
                Registration registration = getRepo().getRegistrationService().load(newNameTypeDesignationTarget.getUuid(), Arrays.asList("typeDesignations"));
505
                registration.getTypeDesignations().add(nameTypeDesignation);
506
                session.merge(registration);
507
                txstate.commit();
508
                session.close();
509

    
510
                newNameTypeDesignationTarget = null;
511
                refreshView(true);
512
            } else if(event.getReason().equals(Reason.CANCEL)){
513
                // noting to do
514
            }
515
        }
516
        // ignore other editors
517
    }
518

    
519

    
520
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
521
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
522
        List<String> messages = new ArrayList<>();
523
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
524
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
525
        }
526
        if(event.getProperty().equals("messages")){
527
            getView().openDetailsPopup("Messages", messages);
528
        }
529
    }
530

    
531
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
532
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
533
        RegistrationDTO regDto = regWorkingSetService.loadDtoById((Integer)event.getIdentifier());
534
        if(event.getProperty().equals("messages")){
535
            if(getView() != null){
536
                getView().openDetailsPopup("Messages", regDto.getMessages());
537
            }
538
        }
539
    }
540

    
541
    @EventListener
542
    public void onEntityChangeEvent(EntityChangeEvent event){
543
        if(workingset == null){
544
            return;
545
        }
546
        if(Reference.class.isAssignableFrom(event.getEntityType())){
547
            if(workingset.getCitationId().equals(event.getEntityId())){
548
                refreshView(true);
549
            }
550
        } else
551
        if(Registration.class.isAssignableFrom(event.getEntityType())){
552
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
553
                refreshView(true);
554
            }
555
        } else
556
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
557
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
558
                reg.getTypifiedNameRef() != null
559
                && reg.getTypifiedNameRef().getId() == event.getEntityId())){
560
                    refreshView(true);
561
            }
562
        } else
563
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
564
            if(workingset.getRegistrationDTOs().stream().anyMatch(
565
                    reg -> reg.getTypeDesignations() != null && reg.getTypeDesignations().stream().anyMatch(
566
                            td -> td.getId() == event.getEntityId()
567
                            )
568
                        )
569
                    ){
570
                refreshView(true);
571
            }
572
        }
573

    
574
    }
575

    
576
}
(12-12/19)