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.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.typifiedName();
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
                "source.citation",
128
                "source.annotations",
129
                "source.markers",
130
                }
131
        ));
132

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

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

    
143
        return typeDesignation;
144
    }
145

    
146

    
147

    
148

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

    
155
        getView().getTypeStatusSelect().setContainerDataSource(cdmBeanItemContainerFactory.buildBeanItemContainer(NameTypeDesignationStatus.class));
156
        getView().getTypeStatusSelect().setItemCaptionPropertyId("description");
157

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

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

    
171
        getView().getTypifiedNamesComboboxSelect().setPagingProviders(namePagingProvider, namePagingProvider, namePagingProvider.getPageSize(), this);
172

    
173
        getView().getAnnotationsField().setAnnotationTypeItemContainer(cdmBeanItemContainerFactory.buildVocabularyTermsItemContainer(
174
                AnnotationType.EDITORIAL().getVocabulary().getUuid()));
175

    
176
    }
177

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

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

    
189
    }
190

    
191

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

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

    
210

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

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

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

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

    
257

    
258
        return bean;
259
    }
260

    
261

    
262

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

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

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

    
276
    }
277

    
278

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

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

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

    
292
    }
293

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

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

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

    
313
            }
314
        }
315
    }
316

    
317
}
(4-4/15)