Project

General

Profile

Download (29.1 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.server.SystemError;
27
import com.vaadin.spring.annotation.SpringComponent;
28
import com.vaadin.spring.annotation.ViewScope;
29
import com.vaadin.ui.Button;
30
import com.vaadin.ui.Label;
31
import com.vaadin.ui.UI;
32
import com.vaadin.ui.VerticalLayout;
33
import com.vaadin.ui.Window;
34

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

    
86
/**
87
 * @author a.kohlbecker
88
 * @since Mar 3, 2017
89
 *
90
 */
91
@SpringComponent
92
@ViewScope
93
public class RegistrationWorkingsetPresenter extends AbstractPresenter<RegistrationWorkingsetView> {
94

    
95
    private static final long serialVersionUID = 1L;
96

    
97
    @Autowired
98
    private IRegistrationWorkingSetService regWorkingSetService;
99

    
100
    @Autowired
101
    private RegistrationIdentifierMinter minter;
102

    
103
    @Autowired
104
    private IRegistrationMessageService messageService;
105

    
106
    /**
107
     * @return the regWorkingSetService
108
     */
109
    public IRegistrationWorkingSetService getWorkingSetService() {
110
        return regWorkingSetService;
111
    }
112

    
113
    private RegistrationWorkingSet workingset;
114

    
115
    private TaxonName newTaxonNameForRegistration = null;
116

    
117
    private RegistrationDTO newRegistrationDTOWithExistingName;
118

    
119
    private RegistrationDTO newNameTypeDesignationTarget;
120

    
121

    
122
    /**
123
     *
124
     */
125
    public RegistrationWorkingsetPresenter() {
126
    }
127

    
128
    /**
129
     * Always create a new Store
130
     *
131
     * @return
132
     */
133
    protected CdmStore<Registration, IRegistrationService> getRegistrationStore(){
134
        return new CdmStore<Registration, IRegistrationService>(getRepo(), getRepo().getRegistrationService());
135
    }
136

    
137
    /**
138
     * Always create a new Store
139
     *
140
     * @return
141
     */
142
    protected CdmStore<TaxonName, INameService> getTaxonNameStore(){
143
        return new  CdmStore<TaxonName, INameService>(getRepo(), getRepo().getNameService());
144
    }
145

    
146

    
147
    /**
148
     * @param taxonNameId
149
     * @return
150
     */
151
    protected Registration createNewRegistrationForName(Integer taxonNameId) {
152
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153
        // move into RegistrationWorkflowStateMachine
154
        TransactionStatus txStatus = getRepo().startTransaction();
155

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

    
174

    
175
    /**
176
     * @param doReload TODO
177
     *
178
     */
179
    protected void refreshView(boolean doReload) {
180
        if(workingset == null){
181
            return; // nothing to do
182
        }
183
        if(doReload){
184
            loadWorkingSet(workingset.getCitationId());
185
        }
186
        getView().setWorkingset(workingset);
187
    }
188

    
189

    
190
    /**
191
     * {@inheritDoc}
192
     */
193
    @Override
194
    public void handleViewEntered() {
195

    
196
        super.handleViewEntered();
197

    
198
        loadWorkingSet(getView().getCitationID());
199
        getView().setWorkingset(workingset);
200

    
201
        // PagingProviders and CacheGenerator for the existingNameCombobox
202
        CdmFilterablePagingProvider<TaxonName, TaxonName> pagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(
203
                getRepo().getNameService());
204
        CdmTitleCacheCaptionGenerator<TaxonName> titleCacheGenerator = new CdmTitleCacheCaptionGenerator<TaxonName>();
205
        getView().getAddExistingNameCombobox().setCaptionGenerator(titleCacheGenerator);
206
        getView().getAddExistingNameCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
207

    
208
        // update the messages
209
        User user = UserHelper.fromSession().user();
210
        for (Integer registrationId : getView().getRegistrationItemMap().keySet()) {
211
            Button messageButton = getView().getRegistrationItemMap().get(registrationId).regItemButtons.getMessagesButton();
212

    
213
            RegistrationDTO regDto = workingset.getRegistrationDTO(registrationId).get();
214
            try {
215
                int messageCount = messageService.countActiveMessagesFor(regDto.registration(), user);
216
                messageButton.setEnabled(UserHelper.fromSession().userIsRegistrationCurator() || messageCount > 0);
217
            } catch (ExternalServiceException e) {
218
                messageButton.setComponentError(new SystemError(e.getMessage(), e));
219
            }
220
        }
221

    
222
    }
223

    
224

    
225
    /**
226
     * @param referenceID
227
     */
228
    protected void loadWorkingSet(Integer referenceID) {
229
        try {
230
            workingset = getWorkingSetService().loadWorkingSetByReferenceID(referenceID);
231
        } catch (RegistrationValidationException error) {
232
            logger.error(error);
233
            Window errorDialog = new Window("Validation Error");
234
            errorDialog.setModal(true);
235
            VerticalLayout subContent = new VerticalLayout();
236
            subContent.setMargin(true);
237
            errorDialog.setContent(subContent);
238
            subContent.addComponent(new Label(error.getMessage()));
239
            UI.getCurrent().addWindow(errorDialog);
240
        }
241
        if(workingset == null || workingset.getCitationId() == null){
242
            Reference citation = getRepo().getReferenceService().find(referenceID);
243
            workingset = new RegistrationWorkingSet(citation);
244
        }
245
    }
246

    
247

    
248
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
249
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
250

    
251
        if(!checkFromOwnView(event)){
252
            return;
253
        }
254

    
255
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
256
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
257
        popup.loadInEditor(null);
258
    }
259

    
260
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
261
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
262

    
263
        if(!checkFromOwnView(event)){
264
            return;
265
        }
266
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
267
        popup.withReferenceTypes(RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES);
268
        popup.withDeleteButton(true);
269
        popup.loadInEditor(event.getEntityId());
270
    }
271

    
272
    @EventBusListenerMethod
273
    public void onDoneWithReferencePopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
274
        if(event.getPopup() instanceof ReferencePopupEditor){
275
            if(event.getReason().equals(Reason.SAVE)){
276
                refreshView(true);
277
            }
278
        }
279
    }
280

    
281
    @EventBusListenerMethod
282
    public void onDoneWithSpecimenTypeDesignationWorkingsetPopupEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
283
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
284
            if(event.getReason().equals(Reason.SAVE)){
285
                refreshView(true);
286
            }
287
        }
288
    }
289

    
290
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
291
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
292

    
293
        if(!checkFromOwnView(event)){
294
            return;
295
        }
296

    
297
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class, getView());
298
        popup.loadInEditor(event.getEntityId());
299
    }
300

    
301
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
302
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
303

    
304
        if(!checkFromOwnView(event)){
305
            return;
306
        }
307

    
308
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
309
        popup.setParentEditorActionContext(event.getContext());
310
        popup.withDeleteButton(true);
311
        configureTaxonNameEditor(popup);
312
        popup.loadInEditor(event.getEntityId());
313
        if(event.getSourceComponent() != null){
314
            popup.setReadOnly(event.getSourceComponent().isReadOnly());
315
        }
316

    
317
    }
318

    
319

    
320
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
321
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
322

    
323
        if(!checkFromOwnView(event)){
324
            return;
325
        }
326

    
327
        newTaxonNameForRegistration = TaxonNameFactory.NewNameInstance(RegistrationUIDefaults.NOMENCLATURAL_CODE, Rank.SPECIES());
328
        newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
329
        EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration, (AbstractView) getView());
330
        newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
331
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
332
        popup.setParentEditorActionContext(event.getContext());
333
        popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE,CRUD.DELETE));
334
        popup.withDeleteButton(true);
335
        configureTaxonNameEditor(popup);
336
        popup.loadInEditor(newTaxonNameForRegistration.getId());
337
    }
338

    
339
    /**
340
     * TODO consider putting this into a Configurer Bean per UIScope.
341
     * In the configurator bean this methods popup papamerter should be of the type
342
     * AbstractPopupEditor
343
     *
344
     * @param popup
345
     */
346
    protected void configureTaxonNameEditor(TaxonNamePopupEditor popup) {
347
        popup.enableMode(TaxonNamePopupEditorMode.suppressReplacementAuthorshipData);
348
        popup.enableMode(TaxonNamePopupEditorMode.nomenclaturalReferenceSectionEditingOnly);
349
        popup.enableMode(TaxonNamePopupEditorMode.requireNomenclaturalReference);
350
    }
351

    
352
    /**
353
     * Creates a new <code>Registration</code> for a new name that has just been edited
354
     * using the <code>TaxonNamePopupEditor</code>. The new name was previously created
355
     * in this presenter as <code>newTaxonNameForRegistration</code> (see {@link #onTaxonNameEditorActionAdd(TaxonNameEditorAction)})
356
     * and is either filled with data (<code>Reason.SAVE</code>) or it is still empty
357
     * (<code>Reason.CANCEL</code>).
358
     *
359
     *
360
     * @param event
361
     * @throws RegistrationValidationException
362
     */
363
    @EventBusListenerMethod
364
    public void onDoneWithTaxonnameEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
365
        if(event.getPopup() instanceof TaxonNamePopupEditor){
366
            TransactionStatus txStatus = getRepo().startTransaction();
367
            if(event.getReason().equals(Reason.SAVE)){
368
                if(newTaxonNameForRegistration != null){
369
                    int taxonNameId = newTaxonNameForRegistration.getId();
370
                    getRepo().getSession().refresh(newTaxonNameForRegistration);
371
                    Registration reg = createNewRegistrationForName(taxonNameId);
372
                    // reload workingset into current session
373
                    loadWorkingSet(workingset.getCitationId());
374
                    workingset.add(reg);
375
                }
376
                refreshView(true);
377
            } else if(event.getReason().equals(Reason.CANCEL)){
378
                if(newTaxonNameForRegistration != null){
379
                    // clean up
380
                    getTaxonNameStore().deleteBean(newTaxonNameForRegistration, (AbstractView) getView());
381
                }
382
            }
383
            getRepo().commitTransaction(txStatus);
384
            newTaxonNameForRegistration = null;
385
        }
386
    }
387

    
388

    
389
    /**
390
     * Creates a new Registration for an exiting (previously published name).
391
     *
392
     * @param event
393
     * @throws RegistrationValidationException
394
     */
395
    @EventBusListenerMethod
396
    public void onRegistrationWorkflowEventActionStart(RegistrationWorkingsetAction event) throws RegistrationValidationException {
397

    
398
        if(!event.isStart()){
399
            return;
400
        }
401

    
402
        getView().getAddExistingNameCombobox().commit();
403
        TaxonName typifiedName = getView().getAddExistingNameCombobox().getValue();
404
        if(typifiedName != null){
405
            Registration newRegistrationWithExistingName = createNewRegistrationForName(null);
406
            Reference citation = getRepo().getReferenceService().find(workingset.getCitationId());
407
            newRegistrationDTOWithExistingName = new RegistrationDTO(newRegistrationWithExistingName, typifiedName, citation);
408
            workingset.add(newRegistrationDTOWithExistingName);
409
            // tell the view to update the workingset
410
            getView().setWorkingset(workingset);
411
            getView().getAddExistingNameRegistrationButton().setEnabled(false);
412
            getView().getAddExistingNameRegistrationButton().setDescription("You first need to add a type designation to the previously created registration.");
413
        } else {
414
            logger.error("Seletced name is NULL");
415
        }
416

    
417
    }
418

    
419

    
420
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
421
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
422

    
423
        if(!checkFromOwnView(event)){
424
            return;
425
        }
426

    
427
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
428
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
429
            popup.setParentEditorActionContext(event.getContext());
430
            popup.withDeleteButton(true);
431
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
432
            if(event.getSourceComponent() != null){
433
                // propagate readonly state from source component to popup
434
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
435
            }
436
        } else {
437
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
438
            popup.setParentEditorActionContext(event.getContext());
439
            popup.withDeleteButton(true);
440
            popup.loadInEditor(new TypeDesignationWorkingsetEditorIdSet(event.getRegistrationId(), event.getBaseEntityRef()));
441

    
442
            popup.getCitationCombobox().setEnabled(false);
443
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
444

    
445
            if(event.getSourceComponent() != null){
446
                // propagate readonly state from source component to popup
447
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
448
            }
449
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
450
        }
451
    }
452

    
453
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
454
    public void onTypeDesignationWorkingsetAdd(TypeDesignationWorkingsetEditorAction event) {
455

    
456
        if(event.getSourceComponent() != null){
457
            return;
458
        }
459

    
460
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
461
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class, getView());
462
            popup.setParentEditorActionContext(event.getContext());
463
            TypeDesignationWorkingsetEditorIdSet identifierSet;
464
            Integer typifiedNameId;
465
            if(newRegistrationDTOWithExistingName != null){
466
                typifiedNameId = newRegistrationDTOWithExistingName.getTypifiedNameRef().getId();
467
            } else {
468
                RegistrationDTO registrationDTO = workingset.getRegistrationDTO(event.getRegistrationId()).get();
469
                EntityReference typifiedNameRef = registrationDTO.getTypifiedNameRef();
470
                if(typifiedNameRef != null){
471
                    // case for registrations without name, in which case the typifiedName is only defined via the typedesignations
472
                    typifiedNameId = typifiedNameRef.getId();
473
                } else {
474
                    // case of registrations with a name in the nomenclatural act.
475
                    typifiedNameId = registrationDTO.getNameRef().getId();
476
                }
477
            }
478
            identifierSet = new TypeDesignationWorkingsetEditorIdSet(
479
                    event.getRegistrationId(),
480
                    getView().getCitationID(),
481
                    typifiedNameId
482
                    );
483
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
484
            popup.loadInEditor(identifierSet);
485
            popup.withDeleteButton(true);
486
            if(event.getSourceComponent() != null){
487
                // propagate readonly state from source component to popup
488
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
489
            }
490
        } else {
491
            NameTypeDesignationPopupEditor popup = getNavigationManager().showInPopup(NameTypeDesignationPopupEditor.class, getView());
492
            popup.setParentEditorActionContext(event.getContext());
493
            popup.withDeleteButton(true);
494
            popup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
495
            newNameTypeDesignationTarget = workingset.getRegistrationDTO(event.getRegistrationId()).get();
496

    
497
            popup.setBeanInstantiator(new BeanInstantiator<NameTypeDesignation>() {
498

    
499
                @Override
500
                public NameTypeDesignation createNewBean() {
501
                    TaxonName typifiedName = newNameTypeDesignationTarget.getTypifiedName();
502
                    if(typifiedName == null){
503
                        // this will be the first type designation, so the nomenclatural act must contain a name
504
                        typifiedName = newNameTypeDesignationTarget.registration().getName();
505
                    }
506
                    NameTypeDesignation nameTypeDesignation  = NameTypeDesignation.NewInstance();
507
                    nameTypeDesignation.setCitation(newNameTypeDesignationTarget.getCitation());
508
                    nameTypeDesignation.getTypifiedNames().add(typifiedName);
509
                    return nameTypeDesignation;
510
                }
511
            });
512
            popup.loadInEditor(null);
513
            popup.getCitationCombobox().setEnabled(false);
514
            popup.getTypifiedNamesComboboxSelect().setEnabled(false);
515
            if(event.getSourceComponent() != null){
516
                // propagate readonly state from source component to popup
517
                popup.setReadOnly(event.getSourceComponent().isReadOnly());
518
            }
519
        }
520
    }
521

    
522
    /**
523
     * Performs final actions after a TypeDesignationEditor which has been
524
     * opened to add a TypeDesignation to a Registration object which was
525
     * created for an previously published name. Prior adding a typedesignation,
526
     * the according Registration object is dangling, that has no association to
527
     * any entity denoting an nomenclatural act which has a reference to a
528
     * publication. This means that the registration object is not in the
529
     * working set.
530
     *
531
     *
532
     * @param event
533
     * @throws RegistrationValidationException
534
     */
535
    @EventBusListenerMethod
536
    public void onDoneWithTypeDesignationEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
537
        if(event.getPopup() instanceof SpecimenTypeDesignationWorkingsetPopupEditor){
538
            if(event.getReason().equals(Reason.SAVE)){
539
                refreshView(true);
540
            } else if(event.getReason().equals(Reason.CANCEL)){
541
                // noting to do
542
            }
543
            newRegistrationDTOWithExistingName = null;
544
        } else if(event.getPopup() instanceof NameTypeDesignationPopupEditor){
545
            if(event.getReason().equals(Reason.SAVE)){
546
                UUID uuid = ((NameTypeDesignationPopupEditor)event.getPopup()).getBean().getUuid();
547

    
548
                Session session = getRepo().getSessionFactory().openSession();
549
                Transaction txstate = session.beginTransaction();
550
                TypeDesignationBase<?> nameTypeDesignation = getRepo().getNameService().loadTypeDesignation(uuid, Arrays.asList(""));
551
                // only load the typeDesignations with the registration so that the typified name can  not be twice in the session
552
                // otherwise multiple representation problems might occur
553
                Registration registration = getRepo().getRegistrationService().load(newNameTypeDesignationTarget.getUuid(), Arrays.asList("typeDesignations"));
554
                registration.getTypeDesignations().add(nameTypeDesignation);
555
                session.merge(registration);
556
                txstate.commit();
557
                session.close();
558

    
559
                refreshView(true);
560
            } else if(event.getReason().equals(Reason.CANCEL)){
561
                // noting to do
562
            }
563
            newNameTypeDesignationTarget = null;
564
        }
565
        // ignore other editors
566
    }
567

    
568

    
569
    @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationWorkingSet.class)
570
    public void onShowDetailsEventForRegistrationWorkingSet(ShowDetailsEvent<RegistrationWorkingSet,?> event) {
571

    
572
        if(event.getProperty().equals(RegistrationItem.VALIDATION_PROBLEMS)){
573
            List<String> messages = new ArrayList<>();
574
            for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
575
                dto.getValidationProblems().forEach(m -> messages.add(dto.getSummary() + ": " + m));
576
            }
577
            getView().openDetailsPopup("Validation Problems", messages);
578
        }
579
    }
580

    
581
    @EventBusListenerMethod
582
    public void onEntityChangeEvent(EntityChangeEvent event){
583
        if(workingset == null){
584
            return;
585
        }
586
        if(Reference.class.isAssignableFrom(event.getEntityType())){
587
            if(workingset.getCitationId().equals(event.getEntityId())){
588
                refreshView(true);
589
            }
590
        } else
591
        if(Registration.class.isAssignableFrom(event.getEntityType())){
592
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
593
                refreshView(true);
594
            }
595
        } else
596
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
597
            if(event.getType().equals(EntityChangeEvent.Type.CREATED)){
598
                // new name! create a blocking registration
599
                Stack<EditorActionContext>context = ((AbstractPopupEditor)event.getSourceView()).getEditorActionContext();
600
                EditorActionContext rootContext = context.get(0);
601
                if(rootContext.getParentView().equals(getView())){
602
                    Registration blockingRegistration = createNewRegistrationForName(event.getEntityId());
603
                    TypedEntityReference<Registration> regReference = (TypedEntityReference<Registration>)rootContext.getParentEntity();
604
                    Registration registration = getRepo().getRegistrationService().load(regReference.getId(), Arrays.asList("$", "blockedBy"));
605
                    registration.getBlockedBy().add(blockingRegistration);
606
                    getRepo().getRegistrationService().saveOrUpdate(registration);
607
                    logger.debug("Blocking registration created");
608
                } else {
609
                    logger.debug("Nn blocking registration, since a new name for a new registration has been created");
610
                }
611
            }
612
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
613
                reg.getTypifiedNameRef() != null
614
                && reg.getTypifiedNameRef().getId() == event.getEntityId())){
615
                    refreshView(true);
616
            }
617
        } else
618
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
619
            if(workingset.getRegistrationDTOs().stream().anyMatch(
620
                    reg -> reg.getTypeDesignations() != null && reg.getTypeDesignations().stream().anyMatch(
621
                            td -> td.getId() == event.getEntityId()
622
                            )
623
                        )
624
                    ){
625
                refreshView(true);
626
            }
627
        }
628
    }
629

    
630

    
631
    @EventBusListenerMethod(filter = ShowDetailsEventEntityTypeFilter.RegistrationDTO.class)
632
    public void onShowDetailsEventForRegistrationDTO(ShowDetailsEvent<RegistrationDTO, Integer> event) {
633

    
634
        // FIXME check from own view!!!
635
        if(getView() == null){
636
            return;
637
        }
638

    
639
        Integer registrationId = event.getIdentifier();
640

    
641
        RegistrationDTO regDto = getWorkingSetService().loadDtoById(registrationId);
642
        if(event.getProperty().equals(RegistrationItem.BLOCKED_BY)){
643

    
644
            Set<RegistrationDTO> blockingRegs = getWorkingSetService().loadBlockingRegistrations(registrationId);
645
            getView().setBlockingRegistrations(registrationId, blockingRegs);
646
        } else if(event.getProperty().equals(RegistrationItem.MESSAGES)){
647

    
648
            RegistrationMessagesPopup popup = getNavigationManager().showInPopup(RegistrationMessagesPopup.class, getView());
649
            popup.loadMessagesFor(regDto.getId());
650

    
651
        } else if(event.getProperty().equals(RegistrationItem.VALIDATION_PROBLEMS)){
652
            getView().openDetailsPopup("Validation Problems", regDto.getValidationProblems());
653
        }
654

    
655

    
656
    }
657

    
658
}
(16-16/23)