Project

General

Profile

Download (7.75 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.List;
14
import java.util.UUID;
15

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

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

    
23
import eu.etaxonomy.cdm.api.service.IService;
24
import eu.etaxonomy.cdm.model.agent.AgentBase;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27
import eu.etaxonomy.cdm.model.common.Annotation;
28
import eu.etaxonomy.cdm.model.common.AnnotationType;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
31
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
32
import eu.etaxonomy.cdm.service.UserHelperAccess;
33
import eu.etaxonomy.cdm.vaadin.component.CdmBeanItemContainerFactory;
34
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
35
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
36
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
37
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
38
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
39
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
40
import eu.etaxonomy.vaadin.mvp.BeanInstantiator;
41

    
42
/**
43
 * @author a.kohlbecker
44
 * @since Apr 5, 2017
45
 *
46
 */
47
@SpringComponent
48
@Scope("prototype")
49
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
50

    
51
    private static final long serialVersionUID = -7926116447719010837L;
52

    
53
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
54

    
55
    ReferencePopupEditor inReferencePopup = null;
56

    
57
    public ReferenceEditorPresenter() {
58

    
59
    }
60

    
61

    
62
    /**
63
     * {@inheritDoc}
64
     */
65
    @Override
66
    public void handleViewEntered() {
67
        super.handleViewEntered();
68

    
69
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
70

    
71
            @Override
72
            public String getCaption(Reference option) {
73
                return option.getTitleCache();
74
            }
75

    
76
        });
77

    
78
        CdmBeanItemContainerFactory selectFieldFactory = new CdmBeanItemContainerFactory(getRepo());
79
        CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = pagingProviderFactory.referencePagingProvider();
80
        getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
81
        getView().getInReferenceCombobox().setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
82
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
83

    
84
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
85
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
86
        getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
87
        getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
88

    
89
        getView().getAnnotationsField().setAnnotationTypeItemContainer(selectFieldFactory.buildTermItemContainer(
90
                AnnotationType.EDITORIAL().getUuid(), AnnotationType.TECHNICAL().getUuid()));
91
    }
92

    
93

    
94
    protected static BeanInstantiator<Reference> defaultBeanInstantiator = new BeanInstantiator<Reference>() {
95

    
96
        @Override
97
        public Reference createNewBean() {
98
            return ReferenceFactory.newGeneric();
99
        }
100
    };
101

    
102

    
103
    @Override
104
    protected BeanInstantiator<Reference> defaultBeanInstantiator(){
105
       return defaultBeanInstantiator;
106
    }
107

    
108

    
109
    /**
110
     * {@inheritDoc}
111
     */
112
    @Override
113
    protected Reference loadCdmEntity(UUID identifier) {
114

    
115
        List<String> initStrategy = Arrays.asList(new String []{
116
                "$",
117
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
118
                }
119
        );
120

    
121
        Reference reference;
122
        if(identifier != null){
123
            reference = getRepo().getReferenceService().load(identifier, initStrategy);
124
        } else {
125
            reference = createNewBean();
126
        }
127
        return reference;
128
    }
129

    
130

    
131
    /**
132
     * {@inheritDoc}
133
     */
134
    @Override
135
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
136
        if(crud != null){
137
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
138
        }
139

    
140
    }
141

    
142
    /**
143
     * {@inheritDoc}
144
     */
145
    @Override
146
    protected void guaranteePerEntityCRUDPermissions(Reference bean) {
147
        if(crud != null){
148
            newAuthorityCreated = UserHelperAccess.userHelper().createAuthorityForCurrentUser(bean, crud, null);
149
        }
150
    }
151

    
152
    /**
153
    *
154
    * @param editorAction
155
     * @throws EditorEntityBeanException
156
    */
157
   @EventBusListenerMethod
158
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
159

    
160
       if(!isFromOwnView(editorAction) || editorAction.getTarget() == null){
161
           return;
162
       }
163

    
164
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getTarget().getClass())){
165
           if(editorAction.isAddAction()){
166
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
167
               inReferencePopup.loadInEditor(null);
168
           }
169
           if(editorAction.isEditAction()){
170
               inReferencePopup = openPopupEditor(ReferencePopupEditor.class, editorAction);
171
               inReferencePopup.withDeleteButton(true);
172
               inReferencePopup.loadInEditor(editorAction.getEntityUuid());
173
           }
174
       }
175
   }
176

    
177
   @EventBusListenerMethod
178
   public void onEntityChangeEvent(EntityChangeEvent<?> event){
179

    
180
       if(event.getSourceView() == inReferencePopup){
181
           if(event.isCreateOrModifiedType()){
182
               getCache().load(event.getEntity());
183
               getView().getInReferenceCombobox().reload();
184
           }
185
           if(event.isRemovedType()){
186
               getView().getInReferenceCombobox().selectNewItem(null);
187
           }
188
           inReferencePopup = null;
189
       }
190

    
191
   }
192

    
193
    /**
194
     * {@inheritDoc}
195
     */
196
    @Override
197
    protected IService<Reference> getService() {
198
        return getRepo().getReferenceService();
199
    }
200

    
201

    
202
    /**
203
     * {@inheritDoc}
204
     */
205
    @Override
206
    protected Reference preSaveBean(Reference bean) {
207

    
208
        // handle annotation changes
209
        List<Annotation> annotations = getView().getAnnotationsField().getValue();
210
        List<Annotation> currentAnnotations = new ArrayList<>(bean.getAnnotations());
211
        List<Annotation> annotationsSeen = new ArrayList<>();
212
        for(Annotation a : annotations){
213
            if(a == null){
214
                continue;
215
            }
216
            if(!currentAnnotations.contains(a)){
217
                bean.addAnnotation(a);
218
            }
219
            annotationsSeen.add(a);
220
        }
221
        for(Annotation a : currentAnnotations){
222
            if(!annotationsSeen.contains(a)){
223
                bean.removeAnnotation(a);
224
            }
225
        }
226

    
227

    
228
        return bean;
229
    }
230

    
231

    
232

    
233

    
234
}
(1-1/3)