Project

General

Profile

Download (11.8 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.ArrayList;
12
import java.util.Arrays;
13
import java.util.EnumSet;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.UUID;
17

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

    
22
import com.vaadin.spring.annotation.SpringComponent;
23

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

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

    
61
    @Autowired
62
    private IRegistrationWorkingSetService registrationWorkingSetService;
63

    
64
    HashSet<TaxonName> typifiedNamesAsLoaded;
65

    
66
    private TaxonName typifiedNameInContext;
67

    
68

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

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

    
96

    
97
    /**
98
     * {@inheritDoc}
99
     */
100
    @Override
101
    protected NameTypeDesignation loadCdmEntity(UUID uuid) {
102
        List<String> initStrategy = Arrays.asList(new String []{
103
                "$",
104
                "annotations.*", // * is needed as log as we are using a table in FilterableAnnotationsField
105
                "typifiedNames.typeDesignations", // important !!
106
                "typeName.$",
107
                "citation.authorship.$",
108
                }
109
        );
110

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

    
122
        typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
123

    
124
        return typeDesignation;
125
    }
126

    
127

    
128

    
129

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

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

    
140
        getView().getCitationCombobox().getSelect().setCaptionGenerator(new CdmTitleCacheCaptionGenerator<Reference>());
141
        CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = pagingProviderFactory.referencePagingProvider();
142
        getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
143
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
144
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
145

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

    
151
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
152

    
153
    }
154

    
155

    
156
    /**
157
     * {@inheritDoc}
158
     */
159
    @Override
160
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
161
        if(crud != null){
162
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
163
        }
164
    }
165

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

    
173
    }
174

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

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

    
196

    
197
    /**
198
     * {@inheritDoc}
199
     */
200
    @Override
201
    protected NameTypeDesignation preSaveBean(NameTypeDesignation bean) {
202

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

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

    
224
        // handle annotation changes
225
        List<Annotation> annotations = getView().getAnnotationsField().getValue();
226
        List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
227
        List<Annotation> annotationsSeen = new ArrayList<>();
228
        for(Annotation a : annotations){
229
            if(a == null){
230
                continue;
231
            }
232
            if(!currentAnnotations.contains(a)){
233
                bean.addAnnotation(a);
234
            }
235
            annotationsSeen.add(a);
236
        }
237
        for(Annotation a : currentAnnotations){
238
            if(!annotationsSeen.contains(a)){
239
                bean.removeAnnotation(a);
240
            }
241
        }
242

    
243

    
244
        return bean;
245
    }
246

    
247

    
248

    
249
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
250
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction action){
251

    
252
        if(!isFromOwnView(action)){
253
            return;
254
        }
255

    
256
        TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
257
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
258
        typeNamePopup.withDeleteButton(true);
259
        // TODO configure Modes???
260
        typeNamePopup.loadInEditor(null);
261

    
262
    }
263

    
264

    
265
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
266
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction action){
267

    
268
        if(!isFromOwnView(action)){
269
            return;
270
        }
271

    
272
        TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
273
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
274
        typeNamePopup.withDeleteButton(true);
275
        // TODO configure Modes???
276
        typeNamePopup.loadInEditor(action.getEntityUuid());
277

    
278
    }
279

    
280
    @EventBusListenerMethod
281
    public void onEntityChangeEvent(EntityChangeEvent<?>event){
282

    
283
        BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
284

    
285
        if(boundTargetField != null){
286
            if(boundTargetField.matchesPropertyIdPath("typeName")){
287
                if(event.isCreateOrModifiedType()){
288
                    getCache().load(event.getEntity());
289
                    if(event.isCreatedType()){
290
                        getView().getTypeNameField().setValue((TaxonName) event.getEntity());
291
                    } else {
292
                        getView().getTypeNameField().reload();
293
                    }
294
                }
295
                if(event.isRemovedType()){
296
                    getView().getTypeNameField().selectNewItem(null);
297
                }
298

    
299
            }
300
        }
301
    }
302

    
303
}
(4-4/13)