Project

General

Profile

Download (13.7 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.format.reference.ReferenceEllypsisFormatter;
27
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter.LabelType;
28
import eu.etaxonomy.cdm.model.agent.AgentBase;
29
import eu.etaxonomy.cdm.model.agent.Institution;
30
import eu.etaxonomy.cdm.model.agent.Person;
31
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
32
import eu.etaxonomy.cdm.model.common.Annotation;
33
import eu.etaxonomy.cdm.model.common.AnnotationType;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
import eu.etaxonomy.cdm.model.reference.ReferenceType;
37
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
38
import eu.etaxonomy.cdm.persistence.dao.common.Restriction.Operator;
39
import eu.etaxonomy.cdm.persistence.dao.initializer.EntityInitStrategy;
40
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
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.InstitutionEditorAction;
46
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
47
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
48
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
49
import eu.etaxonomy.cdm.vaadin.util.ReferenceEllypsisCaptionGenerator;
50
import eu.etaxonomy.cdm.vaadin.view.common.InstitutionPopupEditor;
51
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
52
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
53
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
54
import eu.etaxonomy.vaadin.mvp.BoundField;
55
import eu.etaxonomy.vaadin.ui.view.PopupView;
56

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

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

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

    
70
    ReferencePopupEditor inReferencePopup = null;
71

    
72
    CdmFilterablePagingProvider<Reference, Reference> inReferencePagingProvider;
73

    
74
    Restriction<UUID> includeCurrentInReference;
75

    
76
    public ReferenceEditorPresenter() {
77

    
78
    }
79

    
80

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

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

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

    
96
        });
97

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

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

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

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

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

    
120

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

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

    
141

    
142

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

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

    
151

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

    
157

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

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

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

    
180

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

    
190
    }
191

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
307
   }
308

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

    
317

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

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

    
343

    
344
        return bean;
345
    }
346

    
347

    
348

    
349

    
350
}
(1-1/4)