Project

General

Profile

Download (7.06 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 org.springframework.beans.factory.annotation.Autowired;
16

    
17
import eu.etaxonomy.cdm.api.service.IService;
18
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
19
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
23
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
24
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
25
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
26
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
27
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
28
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
29
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
30
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
31
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
32

    
33
/**
34
 * @author a.kohlbecker
35
 * @since Jan 26, 2018
36
 *
37
 */
38
public class NameTypeDesignationPresenter
39
        extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
40

    
41
    @Autowired
42
    private IRegistrationWorkingSetService registrationWorkingSetService;
43

    
44
    HashSet<TaxonName> typifiedNamesAsLoaded;
45

    
46

    
47
    /**
48
     * {@inheritDoc}
49
     */
50
    @Override
51
    protected NameTypeDesignation loadBeanById(Object identifier) {
52
        if(identifier instanceof Integer || identifier == null){
53
            return super.loadBeanById(identifier);
54
        } else {
55
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
56
            RegistrationDTO regDTO = registrationWorkingSetService.loadDtoById(idset.registrationId);
57
            // find the working set
58
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.workingsetId);
59
            int nameTypeDesignationId = typeDesignationWorkingSet.getBaseEntityReference().getId();
60
            // NameTypeDesignation bameTypeDesignation = regDTO.getNameTypeDesignation(typeDesignationWorkingSet.getBaseEntityReference());
61
            if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(NameTypeDesignation.class)){
62
                throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
63
            }
64
            return super.loadBeanById(nameTypeDesignationId);
65
        }
66
    }
67

    
68

    
69
    /**
70
     * {@inheritDoc}
71
     */
72
    @Override
73
    protected NameTypeDesignation loadCdmEntityById(Integer identifier) {
74
        List<String> initStrategy = Arrays.asList(new String []{
75
                "$",
76
                "typifiedNames.typeDesignations", // important !!
77
                "typeName.$",
78
                "citation.authorship.$",
79
                }
80
        );
81

    
82
        NameTypeDesignation typeDesignation;
83
        if(identifier != null){
84
            typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(identifier, initStrategy);
85
        } else {
86
            if(beanInstantiator != null){
87
                typeDesignation = beanInstantiator.createNewBean();
88
            } else {
89
                typeDesignation = NameTypeDesignation.NewInstance();
90
            }
91
        }
92

    
93
        typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
94

    
95
        return typeDesignation;
96
    }
97

    
98

    
99

    
100

    
101
    /**
102
     * {@inheritDoc}
103
     */
104
    @Override
105
    public void handleViewEntered() {
106

    
107
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
108
        getView().getTypeStatusSelect().setContainerDataSource(selectFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
109

    
110
        getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
111
        CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
112
        getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
113
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
114
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
115

    
116
        CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
117
        getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
118
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
119
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
120

    
121
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
122

    
123
    }
124

    
125

    
126
    /**
127
     * {@inheritDoc}
128
     */
129
    @Override
130
    protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
131
        if(crud != null){
132
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
133
        }
134
    }
135

    
136
    /**
137
     * {@inheritDoc}
138
     */
139
    @Override
140
    protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
141
        // TODO Auto-generated method stub
142

    
143
    }
144

    
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    protected IService<NameTypeDesignation> getService() {
150
        // TODO Auto-generated method stub
151
        return null;
152
    }
153

    
154

    
155
    /**
156
     * {@inheritDoc}
157
     */
158
    @Override
159
    protected NameTypeDesignation handleTransientProperties(NameTypeDesignation bean) {
160

    
161
        // the typifiedNames can only be set on the name side, so we need to
162
        // handle changes explicitly here
163
        HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
164

    
165
        // handle adds
166
        for(TaxonName name : typifiedNames){
167
            if(!name.getTypeDesignations().contains(bean)){
168
                name.addTypeDesignation(bean, false);
169
            }
170
        }
171
        // handle removed
172
        for(TaxonName name : typifiedNamesAsLoaded){
173
            if(!typifiedNames.contains(name)){
174
                name.removeTypeDesignation(bean);
175
            }
176
            // FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
177
        }
178

    
179
        return bean;
180
    }
181

    
182

    
183

    
184
}
(4-4/13)