Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.reference;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.EnumSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.context.annotation.Scope;
20
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
21
import org.vaadin.viritin.fields.CaptionGenerator;
22

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

    
25
import eu.etaxonomy.cdm.api.service.IService;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.agent.Institution;
28
import eu.etaxonomy.cdm.model.agent.Person;
29
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30
import eu.etaxonomy.cdm.model.common.Annotation;
31
import eu.etaxonomy.cdm.model.common.AnnotationType;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
34
import eu.etaxonomy.cdm.model.reference.ReferenceType;
35
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
36
import eu.etaxonomy.cdm.persistence.dao.common.Restriction.Operator;
37
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
38
import eu.etaxonomy.cdm.service.UserHelperAccess;
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.InstitutionEditorAction;
43
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
44
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
45
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
46
import eu.etaxonomy.cdm.vaadin.view.common.InstitutionPopupEditor;
47
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
48
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
49
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
50
import eu.etaxonomy.vaadin.mvp.BoundField;
51
import eu.etaxonomy.vaadin.ui.view.PopupView;
52

    
53
/**
54
 * @author a.kohlbecker
55
 * @since Apr 5, 2017
56
 *
57
 */
58
@SpringComponent
59
@Scope("prototype")
60
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
61

    
62
    private static final long serialVersionUID = -7926116447719010837L;
63

    
64
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
65

    
66
    ReferencePopupEditor inReferencePopup = null;
67

    
68
    CdmFilterablePagingProvider<Reference, Reference> inReferencePagingProvider;
69

    
70
    Restriction<UUID> includeCurrentInReference;
71

    
72
    public ReferenceEditorPresenter() {
73

    
74
    }
75

    
76

    
77
    /**
78
     * {@inheritDoc}
79
     */
80
    @Override
81
    public void handleViewEntered() {
82
        super.handleViewEntered();
83

    
84
        getView().getTypeSelect().addValueChangeListener(e -> updateInReferencePageProvider());
85
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
86

    
87
            @Override
88
            public String getCaption(Reference option) {
89
                return option.getTitleCache();
90
            }
91

    
92
        });
93

    
94
        CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = pagingProviderFactory.referencePagingProvider();
95
        getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
96
        getView().getInReferenceCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
97
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
98

    
99
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
100
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
101
        getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
102
        getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
103

    
104
        CdmFilterablePagingProvider<AgentBase, Institution> institutionPagingProvider = new CdmFilterablePagingProvider<AgentBase, Institution>(getRepo().getAgentService(), Institution.class);
105
        getView().getInstitutionCombobox().getSelect().loadFrom(institutionPagingProvider, institutionPagingProvider, institutionPagingProvider.getPageSize());
106
        getView().getInstitutionCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Institution>(getView().getInstitutionCombobox(), this));
107

    
108
        getView().getSchoolCombobox().getSelect().loadFrom(institutionPagingProvider, institutionPagingProvider, institutionPagingProvider.getPageSize());
109
        getView().getSchoolCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Institution>(getView().getSchoolCombobox(), this));
110

    
111
        getView().getAnnotationsField().setAnnotationTypeItemContainer(cdmBeanItemContainerFactory.buildTermItemContainer(
112
                AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
113
    }
114

    
115

    
116
    /**
117
     * @param inReferencePagingProvider
118
     */
119
    public void updateInReferencePageProvider() {
120

    
121
        inReferencePagingProvider = pagingProviderFactory.inReferencePagingProvider((ReferenceType) getView().getTypeSelect().getValue(), false);
122
        Reference inReference = getView().getInReferenceCombobox().getValue();
123
        if(inReference != null){
124
            if(includeCurrentInReference == null){
125
                includeCurrentInReference = new Restriction<UUID>("uuid", Operator.OR, null, inReference.getUuid());
126
            }
127
            inReferencePagingProvider.addRestriction(includeCurrentInReference);
128
        } else {
129
            inReferencePagingProvider.getRestrictions().remove(includeCurrentInReference);
130
            includeCurrentInReference = null;
131
        }
132
        getView().getInReferenceCombobox().reload();
133
        getView().getInReferenceCombobox().loadFrom(inReferencePagingProvider, inReferencePagingProvider, inReferencePagingProvider.getPageSize());
134
    }
135

    
136

    
137

    
138
    protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
139

    
140
        @Override
141
        public Reference createNewBean() {
142
            return ReferenceFactory.newGeneric();
143
        }
144
    };
145

    
146

    
147
    @Override
148
    protected BeanInstantiator<Reference> defaultBeanInstantiator(){
149
       return defaultBeanInstantiator;
150
    }
151

    
152

    
153
    /**
154
     * {@inheritDoc}
155
     */
156
    @Override
157
    protected Reference loadCdmEntity(UUID identifier) {
158

    
159
        List<String> initStrategy = Arrays.asList(new String []{
160
                "$",
161
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
162
                }
163
        );
164

    
165
        Reference reference;
166
        if(identifier != null){
167
            reference = getRepo().getReferenceService().load(identifier, initStrategy);
168
        } else {
169
            reference = createNewBean();
170
        }
171
        return reference;
172
    }
173

    
174

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

    
184
    }
185

    
186
    /**
187
     * {@inheritDoc}
188
     */
189
    @Override
190
    protected void guaranteePerEntityCRUDPermissions(Reference bean) {
191
        if(crud != null){
192
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
193
        }
194
    }
195

    
196
    /**
197
    *
198
    * @param editorAction
199
    * @throws EditorEntityBeanException
200
    */
201
   @EventBusListenerMethod
202
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
203

    
204
       if(!isFromOwnView(editorAction) || editorAction.getTarget() == null){
205
           return;
206
       }
207

    
208
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getTarget().getClass())){
209
           Set<ReferenceType> applicableTypes = ReferenceType.inReferenceContraints((ReferenceType) getView().getTypeSelect().getValue());
210
           if(editorAction.isAddAction()){
211
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
212
               if(!applicableTypes.isEmpty()){
213
                   inReferencePopup.withReferenceTypes(EnumSet.copyOf(applicableTypes));
214
               }
215
               inReferencePopup.loadInEditor(null);
216
               if(!applicableTypes.isEmpty()){
217
                   inReferencePopup.getTypeSelect().setValue(applicableTypes.iterator().next());
218
               }
219
           }
220
           if(editorAction.isEditAction()){
221
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
222
               if(!applicableTypes.isEmpty()){
223
                   inReferencePopup.withReferenceTypes(EnumSet.copyOf(applicableTypes));
224
               }
225
               inReferencePopup.withDeleteButton(true);
226
               inReferencePopup.loadInEditor(editorAction.getEntityUuid());
227
           }
228
       }
229
   }
230

    
231
   @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
232
   public void onInstitutionEditorActionEdit(InstitutionEditorAction event) {
233

    
234
       if(!checkFromOwnView(event)){
235
           return;
236
       }
237

    
238
       InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
239

    
240
       institutionPopuEditor.grantToCurrentUser(this.crud);
241
       institutionPopuEditor.withDeleteButton(true);
242
       institutionPopuEditor.loadInEditor(event.getEntityUuid());
243
   }
244

    
245
   @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
246
   public void onInstitutionEditorActionAdd(InstitutionEditorAction event) {
247

    
248
       if(!checkFromOwnView(event)){
249
           return;
250
       }
251

    
252
       InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
253

    
254
       institutionPopuEditor.grantToCurrentUser(this.crud);
255
       institutionPopuEditor.withDeleteButton(false);
256
       institutionPopuEditor.loadInEditor(null);
257
   }
258

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

    
262
       BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
263

    
264
       if(boundTargetField != null){
265
           if(boundTargetField.matchesPropertyIdPath("inReference")){
266
               if(event.isCreateOrModifiedType()){
267
                   Reference inReference = (Reference)getCache().load(event.getEntity());
268
                   getView().getInReferenceCombobox().reload();
269
                   if(event.getType() == Type.CREATED){
270
                       getView().getInReferenceCombobox().setValue(inReference);
271
                   }
272
               }
273
               if(event.isRemovedType()){
274
                   getView().getInReferenceCombobox().selectNewItem(null);
275
               }
276
               inReferencePopup = null;
277
           }
278
           else if(boundTargetField.matchesPropertyIdPath("institute")){
279
               if(event.isCreateOrModifiedType()){
280
                   Institution newInstitution = (Institution) event.getEntity();
281
                   getCache().load(newInstitution);
282
                   if(event.isCreatedType()){
283
                       getView().getInstitutionCombobox().setValue(newInstitution);
284
                   } else {
285
                       getView().getInstitutionCombobox().reload();
286
                   }
287
               }
288
           } else if(boundTargetField.matchesPropertyIdPath("school")){
289
               if(event.isCreateOrModifiedType()){
290
                   Institution newInstitution = (Institution) event.getEntity();
291
                   getCache().load(newInstitution);
292
                   if(event.isCreatedType()){
293
                       getView().getSchoolCombobox().setValue(newInstitution);
294
                   } else {
295
                       getView().getSchoolCombobox().reload();
296
                   }
297
               }
298
           }
299
       }
300

    
301
   }
302

    
303
    /**
304
     * {@inheritDoc}
305
     */
306
    @Override
307
    protected IService<Reference> getService() {
308
        return getRepo().getReferenceService();
309
    }
310

    
311

    
312
    /**
313
     * {@inheritDoc}
314
     */
315
    @Override
316
    protected Reference preSaveBean(Reference bean) {
317

    
318
        // handle annotation changes
319
        List<Annotation> annotations = getView().getAnnotationsField().getValue();
320
        List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
321
        List<Annotation> annotationsSeen = new ArrayList<>();
322
        for(Annotation a : annotations){
323
            if(a == null){
324
                continue;
325
            }
326
            if(!currentAnnotations.contains(a)){
327
                bean.addAnnotation(a);
328
            }
329
            annotationsSeen.add(a);
330
        }
331
        for(Annotation a : currentAnnotations){
332
            if(!annotationsSeen.contains(a)){
333
                bean.removeAnnotation(a);
334
            }
335
        }
336

    
337

    
338
        return bean;
339
    }
340

    
341

    
342

    
343

    
344
}
(1-1/3)