Project

General

Profile

Download (13.8 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.UUID;
16

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

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

    
24
import eu.etaxonomy.cdm.api.service.IService;
25
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter;
26
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter.LabelType;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.agent.Institution;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31
import eu.etaxonomy.cdm.model.common.Annotation;
32
import eu.etaxonomy.cdm.model.common.AnnotationType;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
35
import eu.etaxonomy.cdm.model.reference.ReferenceType;
36
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
37
import eu.etaxonomy.cdm.persistence.dao.common.Restriction.Operator;
38
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
39
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
40
import eu.etaxonomy.cdm.service.UserHelperAccess;
41
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
42
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
43
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent.Type;
44
import eu.etaxonomy.cdm.vaadin.event.InstitutionEditorAction;
45
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
46
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
47
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
48
import eu.etaxonomy.cdm.vaadin.util.ReferenceEllypsisCaptionGenerator;
49
import eu.etaxonomy.cdm.vaadin.view.common.InstitutionPopupEditor;
50
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
51
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
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 Apr 5, 2017
59
 *
60
 */
61
@SpringComponent
62
@Scope("prototype")
63
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
64

    
65
    private static final long serialVersionUID = -7926116447719010837L;
66

    
67
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
68

    
69
    ReferencePopupEditor inReferencePopup = null;
70

    
71
    CdmFilterablePagingProvider<Reference, Reference> inReferencePagingProvider;
72

    
73
    Restriction<UUID> includeCurrentInReference;
74

    
75
    public ReferenceEditorPresenter() {
76

    
77
    }
78

    
79

    
80
    /**
81
     * {@inheritDoc}
82
     */
83
    @Override
84
    public void handleViewEntered() {
85
        super.handleViewEntered();
86

    
87
        getView().getTypeSelect().addValueChangeListener(e -> updateInReferencePageProvider());
88
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
89

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

    
95
        });
96

    
97
        CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = pagingProviderFactory.referencePagingProvider();
98
        getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
99
        getView().getInReferenceCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
100
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
101
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new ReferenceEllypsisCaptionGenerator(LabelType.BIBLIOGRAPHIC, getView().getInReferenceCombobox().getSelect()));
102

    
103
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = pagingProviderFactory.teamOrPersonPagingProvider();
104
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = pagingProviderFactory.personPagingProvider();
105
        getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
106
        getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
107

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

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

    
115
        getView().getAnnotationsField().setAnnotationTypeItemContainer(cdmBeanItemContainerFactory.buildTermItemContainer(
116
                AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
117
    }
118

    
119

    
120
    /**
121
     * @param inReferencePagingProvider
122
     */
123
    public void updateInReferencePageProvider() {
124

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

    
140

    
141

    
142
    protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
143

    
144
        @Override
145
        public Reference createNewBean() {
146
            return ReferenceFactory.newGeneric();
147
        }
148
    };
149

    
150

    
151
    @Override
152
    protected BeanInstantiator<Reference> defaultBeanInstantiator(){
153
       return defaultBeanInstantiator;
154
    }
155

    
156

    
157
    /**
158
     * {@inheritDoc}
159
     */
160
    @Override
161
    protected Reference loadCdmEntity(UUID identifier) {
162

    
163
        EntityInitStrategy initStrategy = new EntityInitStrategy(Arrays.asList(new String []{
164
                "$",
165
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
166
                "inReference"
167
                }
168
        ));
169
        initStrategy.extend("", ReferenceEllypsisFormatter.INIT_STRATEGY, false);
170
        initStrategy.extend("inReference", ReferenceEllypsisFormatter.INIT_STRATEGY, false);
171

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

    
181

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

    
191
    }
192

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
308
   }
309

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

    
318

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

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

    
344

    
345
        return bean;
346
    }
347

    
348

    
349

    
350

    
351
}
(1-1/4)