Project

General

Profile

Download (12.6 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.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
    private CdmFilterablePagingProvider<Reference,Reference> referencePagingProvider;
84

    
85

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

    
91
    /**
92
     * {@inheritDoc}
93
     */
94
    @Override
95
    protected NameTypeDesignation loadBeanById(Object identifier) {
96
        if(identifier instanceof Integer || identifier == null){
97
            return super.loadBeanById(identifier);
98
        } else {
99
            TypeDesignationWorkingsetIds idset = (TypeDesignationWorkingsetIds)identifier;
100
            RegistrationDTO regDTO = registrationWorkingSetService.loadDtoByUuid(idset.registrationUuid);
101
            typifiedNameInContext = regDTO.typifiedName();
102
            // find the working set
103
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(idset.baseEntityRef);
104
            if(!typeDesignationWorkingSet.getBaseEntityReference().getType().equals(NameTypeDesignation.class)){
105
                throw new RuntimeException("TypeDesignationWorkingsetEditorIdSet references not a NameTypeDesignation");
106
            } else {
107
                // TypeDesignationWorkingSet for NameTyped only contain one item!!!
108
                UUID nameTypeDesignationUuid = typeDesignationWorkingSet.getTypeDesignations().get(0).getUuid();
109
                return super.loadBeanById(nameTypeDesignationUuid);
110
            }
111
        }
112
    }
113

    
114

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

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

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

    
142
        return typeDesignation;
143
    }
144

    
145

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

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

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

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

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

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

    
173
    }
174

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

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

    
186
    }
187

    
188

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

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

    
207

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

    
214
        if(!bean.hasDesignationSource()) {
215
            bean.setSource(null); // this effectively removes the designation reference and reference detail
216
        }
217

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

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

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

    
258

    
259
        return bean;
260
    }
261

    
262

    
263

    
264
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
265
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction action){
266

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

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

    
277
    }
278

    
279

    
280
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
281
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction action){
282

    
283
        if(!isFromOwnView(action)){
284
            return;
285
        }
286

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

    
293
    }
294

    
295
    @EventBusListenerMethod
296
    public void onEntityChangeEvent(EntityChangeEvent<?>event){
297

    
298
        BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
299

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

    
314
            }
315
        }
316
    }
317

    
318
}
(4-4/15)