Project

General

Profile

Download (12.7 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.format.reference.ReferenceEllypsisFormatter;
30
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter.LabelType;
31
import eu.etaxonomy.cdm.model.common.Annotation;
32
import eu.etaxonomy.cdm.model.common.AnnotationType;
33
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
34
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.permission.CRUD;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
39
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
40
import eu.etaxonomy.cdm.service.CdmStore;
41
import eu.etaxonomy.cdm.service.UserHelperAccess;
42
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
43
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
44
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
45
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
46
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
47
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
48
import eu.etaxonomy.cdm.vaadin.ui.config.TaxonNamePopupEditorConfig;
49
import eu.etaxonomy.cdm.vaadin.util.ReferenceEllypsisCaptionGenerator;
50
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
51
import eu.etaxonomy.vaadin.mvp.AbstractView;
52
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
53
import eu.etaxonomy.vaadin.mvp.BoundField;
54
import eu.etaxonomy.vaadin.ui.view.PopupView;
55

    
56
/**
57
 * @author a.kohlbecker
58
 * @since Jan 26, 2018
59
 *
60
 */
61
@SpringComponent
62
@Scope("prototype")
63
public class NameTypeDesignationPresenter
64
        extends AbstractCdmEditorPresenter<NameTypeDesignation, NameTypeDesignationEditorView> {
65

    
66
    private static final long serialVersionUID = 896305051895903033L;
67

    
68
    @Autowired
69
    private IRegistrationWorkingSetService registrationWorkingSetService;
70

    
71
    HashSet<TaxonName> typifiedNamesAsLoaded;
72

    
73
    private TaxonName typifiedNameInContext;
74

    
75
    protected static BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator = new BeanInstantiator<NameTypeDesignation>() {
76

    
77
        @Override
78
        public NameTypeDesignation createNewBean() {
79
            return NameTypeDesignation.NewInstance();
80
        }
81
    };
82

    
83

    
84
    @Override
85
    protected BeanInstantiator<NameTypeDesignation> defaultBeanInstantiator(){
86
       return defaultBeanInstantiator;
87
    }
88

    
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    protected NameTypeDesignation loadBeanById(Object identifier) {
94
        if(identifier instanceof Integer || identifier == null){
95
            return super.loadBeanById(identifier);
96
//        } else if(identifier instanceof TypedEntityReference && ((TypedEntityReference)identifier).getType().equals(TaxonName.class)) {
97
//            typifiedNameInContext = getRepo().getNameService().find(((TypedEntityReference)identifier).getUuid());
98
//            bean = super.loadBeanById(null);
99
        } else {
100
            TypeDesignationWorkingsetEditorIdSet idset = (TypeDesignationWorkingsetEditorIdSet)identifier;
101
            RegistrationDTO regDTO = registrationWorkingSetService.loadDtoByUuid(idset.registrationUuid);
102
            typifiedNameInContext = regDTO.getTypifiedName();
103
            // find the working set
104
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.baseEntityRef);
105

    
106
            // NameTypeDesignation bameTypeDesignation = regDTO.getNameTypeDesignation(typeDesignationWorkingSet.getBaseEntityReference());
107
            if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(NameTypeDesignation.class)){
108
                throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
109
            }
110
            // TypeDesignationWorkingSet for NameTyped only contain one item!!!
111
            UUID nameTypeDesignationUuid = typeDesignationWorkingSet.getTypeDesignations().get(0).getUuid();
112
            return super.loadBeanById(nameTypeDesignationUuid);
113
        }
114
    }
115

    
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    protected NameTypeDesignation loadCdmEntity(UUID uuid) {
122
        EntityInitStrategy initStrategy = new EntityInitStrategy(Arrays.asList(new String []{
123
                "$",
124
                "annotations.*", // * is needed as log as we are using a table in FilterableAnnotationsField
125
                "typifiedNames.typeDesignations", // important !!
126
                "typeName.$",
127
                "citation"
128
                }
129
        ));
130

    
131
        initStrategy.extend("citation", ReferenceEllypsisFormatter.INIT_STRATEGY, false);
132
        NameTypeDesignation typeDesignation;
133
        if(uuid != null){
134
            typeDesignation = (NameTypeDesignation) getRepo().getNameService().loadTypeDesignation(uuid, initStrategy.getPropertyPaths());
135
        } else {
136
            typeDesignation = createNewBean();
137
        }
138

    
139
        typifiedNamesAsLoaded = new HashSet<>(typeDesignation.getTypifiedNames());
140

    
141
        return typeDesignation;
142
    }
143

    
144

    
145

    
146

    
147
    /**
148
     * {@inheritDoc}
149
     */
150
    @Override
151
    public void handleViewEntered() {
152

    
153
        getView().getTypeStatusSelect().setContainerDataSource(cdmBeanItemContainerFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
154
        getView().getTypeStatusSelect().setItemCaptionPropertyId("description");
155

    
156
        getView().getCitationCombobox().getSelect().setCaptionGenerator(
157
                new ReferenceEllypsisCaptionGenerator(LabelType.BIBLIOGRAPHIC, getView().getCitationCombobox().getSelect())
158
                );
159
        CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider = pagingProviderFactory.referencePagingProvider();
160
        getView().getCitationCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
161
        getView().getCitationCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getCitationCombobox()));
162
        getView().getCitationCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getCitationCombobox(), this));
163

    
164
        CdmFilterablePagingProvider<TaxonName,TaxonName> namePagingProvider = pagingProviderFactory.taxonNamesWithoutOrthophicIncorrect();
165
        getView().getTypeNameField().loadFrom(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize());
166
        getView().getTypeNameField().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<TaxonName>(getView().getTypeNameField()));
167
        getView().getTypeNameField().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<>(getView().getTypeNameField(), this));
168

    
169
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
170

    
171
        getView().getAnnotationsField().setAnnotationTypeItemContainer(cdmBeanItemContainerFactory.buildVocabularyTermsItemContainer(
172
                AnnotationType.EDITORIAL().getVocabulary().getUuid()));
173

    
174
    }
175

    
176
    @Override
177
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
178
        if(crud != null){
179
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(NameTypeDesignation.class, identifier, crud, null);
180
        }
181
    }
182

    
183
    @Override
184
    protected void guaranteePerEntityCRUDPermissions(NameTypeDesignation bean) {
185
        // TODO Auto-generated method stub
186

    
187
    }
188

    
189

    
190
    @Override
191
    protected IService<NameTypeDesignation> getService() {
192
        // No TypeDesignationService :( so I need override the generic save and delete methods
193
        return null;
194
    }
195

    
196
    @Override
197
    protected void deleteBean(NameTypeDesignation bean){
198
        // deleteTypedesignation(uuid, uuid) needs to be called so the name is loaded in the transaction of the method and is saved.
199
        DeleteResult deletResult = getRepo().getNameService().deleteTypeDesignation(typifiedNameInContext.getUuid(), bean.getUuid());
200
        if(deletResult.isOk()){
201
            EntityChangeEvent changeEvent = new EntityChangeEvent(bean, Type.REMOVED, (AbstractView) getView());
202
            viewEventBus.publish(this, changeEvent);
203
        } else {
204
            CdmStore.handleDeleteresultInError(deletResult);
205
        }
206
    }
207

    
208

    
209
    /**
210
     * {@inheritDoc}
211
     */
212
    @Override
213
    protected NameTypeDesignation preSaveBean(NameTypeDesignation bean) {
214

    
215
        // the typifiedNames can only be set on the name side, so we need to
216
        // handle changes explicitly here
217
        HashSet<TaxonName> typifiedNames = new HashSet<>(bean.getTypifiedNames());
218

    
219
        // handle adds
220
        for(TaxonName name : typifiedNames){
221
            if(name == null){
222
                throw new NullPointerException("typifiedName must not be null");
223
            }
224
            if(!name.getTypeDesignations().contains(bean)){
225
                name.addTypeDesignation(bean, false);
226
            }
227
        }
228
        // handle removed
229
        for(TaxonName name : typifiedNamesAsLoaded){
230
            if(!typifiedNames.contains(name)){
231
                name.removeTypeDesignation(bean);
232
            }
233
            // FIXME do we need to save the names here or is the delete cascaded from the typedesignation to the name?
234
        }
235

    
236
        // handle annotation changes
237
        List<Annotation> annotations = getView().getAnnotationsField().getValue();
238
        List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
239
        List<Annotation> annotationsSeen = new ArrayList<>();
240
        for(Annotation a : annotations){
241
            if(a == null){
242
                continue;
243
            }
244
            if(!currentAnnotations.contains(a)){
245
                bean.addAnnotation(a);
246
            }
247
            annotationsSeen.add(a);
248
        }
249
        for(Annotation a : currentAnnotations){
250
            if(!annotationsSeen.contains(a)){
251
                bean.removeAnnotation(a);
252
            }
253
        }
254

    
255

    
256
        return bean;
257
    }
258

    
259

    
260

    
261
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
262
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction action){
263

    
264
        if(!isFromOwnView(action)){
265
            return;
266
        }
267

    
268
        TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
269
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
270
        typeNamePopup.withDeleteButton(true);
271
        TaxonNamePopupEditorConfig.configure(typeNamePopup);
272
        typeNamePopup.loadInEditor(null);
273

    
274
    }
275

    
276

    
277
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
278
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction action){
279

    
280
        if(!isFromOwnView(action)){
281
            return;
282
        }
283

    
284
        TaxonNamePopupEditor typeNamePopup = openPopupEditor(TaxonNamePopupEditor.class, action);
285
        typeNamePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
286
        typeNamePopup.withDeleteButton(true);
287
        TaxonNamePopupEditorConfig.configure(typeNamePopup);
288
        typeNamePopup.loadInEditor(action.getEntityUuid());
289

    
290
    }
291

    
292
    @EventBusListenerMethod
293
    public void onEntityChangeEvent(EntityChangeEvent<?>event){
294

    
295
        BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
296

    
297
        if(boundTargetField != null){
298
            if(boundTargetField.matchesPropertyIdPath("typeName")){
299
                if(event.isCreateOrModifiedType()){
300
                    getCache().load(event.getEntity());
301
                    if(event.isCreatedType()){
302
                        getView().getTypeNameField().setValue((TaxonName) event.getEntity());
303
                    } else {
304
                        getView().getTypeNameField().reload();
305
                    }
306
                }
307
                if(event.isRemovedType()){
308
                    getView().getTypeNameField().selectNewItem(null);
309
                }
310

    
311
            }
312
        }
313
    }
314

    
315
}
(4-4/15)