Project

General

Profile

Download (5.52 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.name;
10

    
11
import java.util.Arrays;
12
import java.util.HashSet;
13
import java.util.List;
14

    
15
import eu.etaxonomy.cdm.api.service.IService;
16
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
17
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
18
import eu.etaxonomy.cdm.model.name.TaxonName;
19
import eu.etaxonomy.cdm.model.reference.Reference;
20
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
21
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
22
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
23
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
24
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
25
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
26
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
27

    
28
/**
29
 * @author a.kohlbecker
30
 * @since Jan 26, 2018
31
 *
32
 */
33
public class NameTypeDesignationPresenter
34
        extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
35

    
36
    HashSet<TaxonName> typifiedNamesAsLoaded;
37

    
38
    /**
39
     * {@inheritDoc}
40
     */
41
    @Override
42
    protected NameTypeDesignation loadCdmEntityById(Integer identifier) {
43
        List<String> initStrategy = Arrays.asList(new String []{
44
                "$",
45
                "typifiedNames.typeDesignations", // important !!
46
                "typeName.$",
47
                "citation.authorship.$",
48
                }
49
        );
50

    
51
        NameTypeDesignation typeDesignation;
52
        if(identifier != null){
53
            typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(identifier, initStrategy);
54
        } else {
55
            if(beanInstantiator != null){
56
                typeDesignation = beanInstantiator.createNewBean();
57
            } else {
58
                typeDesignation = NameTypeDesignation.NewInstance();
59
            }
60
        }
61

    
62
        typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
63

    
64
        return typeDesignation;
65
    }
66

    
67

    
68

    
69

    
70
    /**
71
     * {@inheritDoc}
72
     */
73
    @Override
74
    public void handleViewEntered() {
75

    
76
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
77
        getView().getTypeStatusSelect().setContainerDataSource(selectFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
78

    
79
        getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
80
        CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
81
        getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
82
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
83
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
84

    
85
        CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
86
        getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
87
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
88
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
89

    
90
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
91

    
92
    }
93

    
94

    
95
    /**
96
     * {@inheritDoc}
97
     */
98
    @Override
99
    protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
100
        if(crud != null){
101
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
102
        }
103
    }
104

    
105
    /**
106
     * {@inheritDoc}
107
     */
108
    @Override
109
    protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
110
        // TODO Auto-generated method stub
111

    
112
    }
113

    
114
    /**
115
     * {@inheritDoc}
116
     */
117
    @Override
118
    protected IService<NameTypeDesignation> getService() {
119
        // TODO Auto-generated method stub
120
        return null;
121
    }
122

    
123

    
124
    /**
125
     * {@inheritDoc}
126
     */
127
    @Override
128
    protected NameTypeDesignation handleTransientProperties(NameTypeDesignation bean) {
129

    
130
        // the typifiedNames can only be set on the name side, so we need to
131
        // handle changes explicitly here
132
        HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
133

    
134
        // handle adds
135
        for(TaxonName name : typifiedNames){
136
            if(!name.getTypeDesignations().contains(bean)){
137
                name.addTypeDesignation(bean, false);
138
            }
139
        }
140
        // handle removed
141
        for(TaxonName name : typifiedNamesAsLoaded){
142
            if(!typifiedNames.contains(name)){
143
                name.removeTypeDesignation(bean);
144
            }
145
            // FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
146
        }
147

    
148
        return bean;
149
    }
150

    
151

    
152

    
153
}
(4-4/13)