Project

General

Profile

Download (10.9 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.EnumSet;
13
import java.util.HashSet;
14
import java.util.List;
15

    
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.context.annotation.Scope;
18
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
19

    
20
import com.vaadin.spring.annotation.SpringComponent;
21

    
22
import eu.etaxonomy.cdm.api.service.DeleteResult;
23
import eu.etaxonomy.cdm.api.service.IService;
24
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
25
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
29
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
30
import eu.etaxonomy.cdm.service.CdmStore;
31
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
32
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
33
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
34
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
35
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
36
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
37
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
38
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
39
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
40
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
41
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
42
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
43
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
44
import eu.etaxonomy.vaadin.mvp.AbstractView;
45
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
46
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
47

    
48
/**
49
 * @author a.kohlbecker
50
 * @since Jan 26, 2018
51
 *
52
 */
53
@SpringComponent
54
@Scope("prototype")
55
public class NameTypeDesignationPresenter
56
        extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
57

    
58
    @Autowired
59
    private IRegistrationWorkingSetService registrationWorkingSetService;
60

    
61
    HashSet<TaxonName> typifiedNamesAsLoaded;
62

    
63
    private TaxonNamePopupEditor typeNamePopup;
64

    
65
    private TaxonName typifiedNameInContext;
66

    
67

    
68
    /**
69
     * {@inheritDoc}
70
     */
71
    @Override
72
    protected NameTypeDesignation loadBeanById(Object identifier) {
73
        if(identifier instanceof Integer || identifier == null){
74
            return super.loadBeanById(identifier);
75
//        } else if(identifier instanceof TypedEntityReference && ((TypedEntityReference)identifier).getType().equals(TaxonName.class)) {
76
//            typifiedNameInContext = getRepo().getNameService().find(((TypedEntityReference)identifier).getId());
77
//            bean = super.loadBeanById(null);
78
        } else {
79
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
80
            RegistrationDTO regDTO = registrationWorkingSetService.loadDtoById(idset.registrationId);
81
            typifiedNameInContext = regDTO.getTypifiedName();
82
            // find the working set
83
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.baseEntityRef);
84

    
85
            // NameTypeDesignation bameTypeDesignation = regDTO.getNameTypeDesignation(typeDesignationWorkingSet.getBaseEntityReference());
86
            if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(NameTypeDesignation.class)){
87
                throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
88
            }
89
            // TypeDesignationWorkingSet for NameTyped only contain one item!!!
90
            int nameTypeDesignationId = typeDesignationWorkingSet.getTypeDesignations().get(0).getId();
91
            return super.loadBeanById(nameTypeDesignationId);
92
        }
93
    }
94

    
95

    
96
    /**
97
     * {@inheritDoc}
98
     */
99
    @Override
100
    protected NameTypeDesignation loadCdmEntityById(Integer identifier) {
101
        List<String> initStrategy = Arrays.asList(new String []{
102
                "$",
103
                "typifiedNames.typeDesignations", // important !!
104
                "typeName.$",
105
                "citation.authorship.$",
106
                }
107
        );
108

    
109
        NameTypeDesignation typeDesignation;
110
        if(identifier != null){
111
            typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(identifier, initStrategy);
112
        } else {
113
            if(beanInstantiator != null){
114
                typeDesignation = beanInstantiator.createNewBean();
115
            } else {
116
                typeDesignation = NameTypeDesignation.NewInstance();
117
            }
118
        }
119

    
120
        typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
121

    
122
        return typeDesignation;
123
    }
124

    
125

    
126

    
127

    
128
    /**
129
     * {@inheritDoc}
130
     */
131
    @Override
132
    public void handleViewEntered() {
133

    
134
        CdmBeanItemContainerFactory selectFactory = new CdmBeanItemContainerFactory(getRepo());
135
        getView().getTypeStatusSelect().setContainerDataSource(selectFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
136
        getView().getTypeStatusSelect().setItemCaptionPropertyId("description");
137

    
138
        getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
139
        CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
140
        getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
141
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
142
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
143

    
144
        CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = new CdmFilterablePagingProvider<TaxonName, TaxonName>(getRepo().getNameService());
145
        getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
146
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
147
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
148

    
149
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
150

    
151
    }
152

    
153

    
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
159
        if(crud != null){
160
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
161
        }
162
    }
163

    
164
    /**
165
     * {@inheritDoc}
166
     */
167
    @Override
168
    protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
169
        // TODO Auto-generated method stub
170

    
171
    }
172

    
173
    /**
174
     * {@inheritDoc}
175
     */
176
    @Override
177
    protected IService<NameTypeDesignation> getService() {
178
        // No TypeDesignationService :( so I need override the generic save and delete methods
179
        return null;
180
    }
181

    
182
    @Override
183
    protected void deleteBean(NameTypeDesignation bean){
184
        // deleteTypedesignation(uuid, uuid) needs to be called so the name is loaded in the transaction of the method and is saved.
185
        DeleteResult deletResult = getRepo().getNameService().deleteTypeDesignation(typifiedNameInContext.getUuid(), bean.getUuid());
186
        if(deletResult.isOk()){
187
            EntityChangeEvent changeEvent = new EntityChangeEvent(bean.getClass(), bean.getId(), Type.REMOVED, (AbstractView) getView());
188
            viewEventBus.publish(this, changeEvent);
189
        } else {
190
            CdmStore.handleDeleteresultInError(deletResult);
191
        }
192
    }
193

    
194

    
195
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    protected NameTypeDesignation handleTransientProperties(NameTypeDesignation bean) {
200

    
201
        // the typifiedNames can only be set on the name side, so we need to
202
        // handle changes explicitly here
203
        HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
204

    
205
        // handle adds
206
        for(TaxonName name : typifiedNames){
207
            if(name == null){
208
                throw new NullPointerException("typifiedName must not be null");
209
            }
210
            if(!name.getTypeDesignations().contains(bean)){
211
                name.addTypeDesignation(bean, false);
212
            }
213
        }
214
        // handle removed
215
        for(TaxonName name : typifiedNamesAsLoaded){
216
            if(!typifiedNames.contains(name)){
217
                name.removeTypeDesignation(bean);
218
            }
219
            // FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
220
        }
221

    
222
        return bean;
223
    }
224

    
225

    
226

    
227
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
228
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction action){
229

    
230
        if(!isFromOwnView(action)){
231
            return;
232
        }
233

    
234
        typeNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
235
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
236
        typeNamePopup.withDeleteButton(true);
237
        // TODO configure Modes???
238
        typeNamePopup.loadInEditor(null);
239

    
240
    }
241

    
242

    
243
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
244
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction action){
245

    
246
        if(!isFromOwnView(action)){
247
            return;
248
        }
249

    
250
        //  basionymSourceField = (AbstractField<TaxonName>)event.getSourceComponent();
251

    
252
        typeNamePopup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class, getView());
253
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
254
        typeNamePopup.withDeleteButton(true);
255
        // TODO configure Modes???
256
        typeNamePopup.loadInEditor(action.getEntityId());
257

    
258
    }
259

    
260
    @EventBusListenerMethod
261
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
262

    
263
        if(event.getPopup() == typeNamePopup){
264
            if(event.getReason() == Reason.SAVE){
265
                getCache().load(typeNamePopup.getBean());
266
                getView().getTypeNameField().reload();
267
            }
268
            if(event.getReason() == Reason.DELETE){
269
                getView().getTypeNameField().selectNewItem(null);
270
            }
271
            typeNamePopup = null;
272

    
273
        }
274
    }
275

    
276
}
(4-4/13)