Project

General

Profile

Download (7.76 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
                "$",
118
                "annotations.*", // needed as log as we are using a table in FilterableAnnotationsField
119
                }
120
        );
121

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

    
131

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

    
141
    }
142

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

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

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

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

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

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

    
192
   }
193

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

    
202

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

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

    
228

    
229
        return bean;
230
    }
231

    
232

    
233

    
234

    
235
}
(1-1/3)