Project

General

Profile

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

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

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

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

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

    
57
    @Autowired
58
    private IRegistrationWorkingSetService registrationWorkingSetService;
59

    
60
    HashSet<TaxonName> typifiedNamesAsLoaded;
61

    
62
    private TaxonNamePopupEditor typeNamePopup;
63

    
64
    private TaxonName typifiedNameInContext;
65

    
66

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

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

    
94

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

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

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

    
121
        return typeDesignation;
122
    }
123

    
124

    
125

    
126

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

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

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

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

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

    
150
    }
151

    
152

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

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

    
170
    }
171

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

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

    
193

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

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

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

    
221
        return bean;
222
    }
223

    
224

    
225

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

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

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

    
239
    }
240

    
241

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

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

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

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

    
257
    }
258

    
259
    @EventBusListenerMethod
260
    public void onEntityChangeEvent(EntityChangeEvent<?>event){
261

    
262
        if(event.getSourceView() == typeNamePopup){
263
            if(event.isCreateOrModifiedType()){
264
                getCache().load(event.getEntity());
265
                getView().getTypeNameField().reload();
266
            }
267
            if(event.isRemovedType()){
268
                getView().getTypeNameField().selectNewItem(null);
269
            }
270
            typeNamePopup = null;
271

    
272
        }
273
    }
274

    
275
}
(4-4/13)