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.component.CdmBeanItemContainerFactory;
40
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
41
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
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

    
85
        updateInReferencePageProvider();
86
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
87

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

    
93
        });
94

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

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

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

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

    
113
        getView().getAnnotationsField().setAnnotationTypeItemContainer(selectFieldFactory.buildTermItemContainer(
114
                AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
115
    }
116

    
117
    @Override
118
    protected void adaptDataProviders() {
119
//        updateInReferencePageProvider();
120
//        getView().getInReferenceCombobox().addValueChangeListener(e -> updateInReferencePageProvider());
121
    }
122

    
123

    
124
    /**
125
     * @param inReferencePagingProvider
126
     */
127
    public void updateInReferencePageProvider() {
128

    
129
        inReferencePagingProvider = pagingProviderFactory.inReferencePagingProvider((ReferenceType) getView().getTypeSelect().getValue());
130
        Reference inReference = getView().getInReferenceCombobox().getValue();
131
        if(inReference != null){
132
            if(includeCurrentInReference == null){
133
                includeCurrentInReference = new Restriction<UUID>("uuid", Operator.OR, null, inReference.getUuid());
134
            }
135
            inReferencePagingProvider.addRestriction(includeCurrentInReference);
136
        } else {
137
            inReferencePagingProvider.getRestrictions().remove(includeCurrentInReference);
138
            includeCurrentInReference = null;
139
        }
140
        getView().getInReferenceCombobox().reload();
141
        getView().getInReferenceCombobox().loadFrom(inReferencePagingProvider, inReferencePagingProvider, inReferencePagingProvider.getPageSize());
142
    }
143

    
144

    
145

    
146
    protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
147

    
148
        @Override
149
        public Reference createNewBean() {
150
            return ReferenceFactory.newGeneric();
151
        }
152
    };
153

    
154

    
155
    @Override
156
    protected BeanInstantiator<Reference> defaultBeanInstantiator(){
157
       return defaultBeanInstantiator;
158
    }
159

    
160

    
161
    /**
162
     * {@inheritDoc}
163
     */
164
    @Override
165
    protected Reference loadCdmEntity(UUID identifier) {
166

    
167
        List<String> initStrategy = Arrays.asList(new String []{
168
                "$",
169
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
170
                }
171
        );
172

    
173
        Reference reference;
174
        if(identifier != null){
175
            reference = getRepo().getReferenceService().load(identifier, initStrategy);
176
        } else {
177
            reference = createNewBean();
178
        }
179
        return reference;
180
    }
181

    
182

    
183
    /**
184
     * {@inheritDoc}
185
     */
186
    @Override
187
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
188
        if(crud != null){
189
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
190
        }
191

    
192
    }
193

    
194
    /**
195
     * {@inheritDoc}
196
     */
197
    @Override
198
    protected void guaranteePerEntityCRUDPermissions(Reference bean) {
199
        if(crud != null){
200
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
201
        }
202
    }
203

    
204
    /**
205
    *
206
    * @param editorAction
207
    * @throws EditorEntityBeanException
208
    */
209
   @EventBusListenerMethod
210
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
211

    
212
       if(!isFromOwnView(editorAction) || editorAction.getTarget() == null){
213
           return;
214
       }
215

    
216
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getTarget().getClass())){
217
           Set<ReferenceType> applicableTypes = ReferenceType.inReferenceContraints((ReferenceType) getView().getTypeSelect().getValue());
218
           if(editorAction.isAddAction()){
219
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
220
               if(!applicableTypes.isEmpty()){
221
                   inReferencePopup.withReferenceTypes(EnumSet.copyOf(applicableTypes));
222
               }
223
               inReferencePopup.loadInEditor(null);
224
               if(!applicableTypes.isEmpty()){
225
                   inReferencePopup.getTypeSelect().setValue(applicableTypes.iterator().next());
226
               }
227
           }
228
           if(editorAction.isEditAction()){
229
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
230
               if(!applicableTypes.isEmpty()){
231
                   inReferencePopup.withReferenceTypes(EnumSet.copyOf(applicableTypes));
232
               }
233
               inReferencePopup.withDeleteButton(true);
234
               inReferencePopup.loadInEditor(editorAction.getEntityUuid());
235
           }
236
       }
237
   }
238

    
239
   @EventBusListenerMethod(filter = EditorActionTypeFilter.Edit.class)
240
   public void onInstitutionEditorActionEdit(InstitutionEditorAction event) {
241

    
242
       if(!checkFromOwnView(event)){
243
           return;
244
       }
245

    
246
       InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
247

    
248
       institutionPopuEditor.grantToCurrentUser(this.crud);
249
       institutionPopuEditor.withDeleteButton(true);
250
       institutionPopuEditor.loadInEditor(event.getEntityUuid());
251
   }
252

    
253
   @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
254
   public void onInstitutionEditorActionAdd(InstitutionEditorAction event) {
255

    
256
       if(!checkFromOwnView(event)){
257
           return;
258
       }
259

    
260
       InstitutionPopupEditor institutionPopuEditor = openPopupEditor(InstitutionPopupEditor.class, event);
261

    
262
       institutionPopuEditor.grantToCurrentUser(this.crud);
263
       institutionPopuEditor.withDeleteButton(false);
264
       institutionPopuEditor.loadInEditor(null);
265
   }
266

    
267
   @EventBusListenerMethod
268
   public void onEntityChangeEvent(EntityChangeEvent<?> event){
269

    
270
       BoundField boundTargetField = boundTargetField((PopupView) event.getSourceView());
271

    
272
       if(boundTargetField != null){
273
           if(boundTargetField.matchesPropertyIdPath("inReference")){
274
               if(event.isCreateOrModifiedType()){
275
                   getCache().load(event.getEntity());
276
                   getView().getInReferenceCombobox().reload();
277
               }
278
               if(event.isRemovedType()){
279
                   getView().getInReferenceCombobox().selectNewItem(null);
280
               }
281
               inReferencePopup = null;
282
           }
283
           else if(boundTargetField.matchesPropertyIdPath("institute")){
284
               if(event.isCreateOrModifiedType()){
285
                   Institution newInstitution = (Institution) event.getEntity();
286
                   getCache().load(newInstitution);
287
                   if(event.isCreatedType()){
288
                       getView().getInstitutionCombobox().setValue(newInstitution);
289
                   } else {
290
                       getView().getInstitutionCombobox().reload();
291
                   }
292
               }
293
           } else if(boundTargetField.matchesPropertyIdPath("school")){
294
               if(event.isCreateOrModifiedType()){
295
                   Institution newInstitution = (Institution) event.getEntity();
296
                   getCache().load(newInstitution);
297
                   if(event.isCreatedType()){
298
                       getView().getSchoolCombobox().setValue(newInstitution);
299
                   } else {
300
                       getView().getSchoolCombobox().reload();
301
                   }
302
               }
303
           }
304
       }
305

    
306
   }
307

    
308
    /**
309
     * {@inheritDoc}
310
     */
311
    @Override
312
    protected IService<Reference> getService() {
313
        return getRepo().getReferenceService();
314
    }
315

    
316

    
317
    /**
318
     * {@inheritDoc}
319
     */
320
    @Override
321
    protected Reference preSaveBean(Reference bean) {
322

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

    
342

    
343
        return bean;
344
    }
345

    
346

    
347

    
348

    
349
}
(1-1/3)