Project

General

Profile

Download (6.34 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.Arrays;
12
import java.util.List;
13
import java.util.UUID;
14

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

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

    
22
import eu.etaxonomy.cdm.api.service.IService;
23
import eu.etaxonomy.cdm.model.agent.AgentBase;
24
import eu.etaxonomy.cdm.model.agent.Person;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
28
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
29
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
30
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
31
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
32
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
33
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
34
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
35
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
36

    
37
/**
38
 * @author a.kohlbecker
39
 * @since Apr 5, 2017
40
 *
41
 */
42
@SpringComponent
43
@Scope("prototype")
44
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
45

    
46
    private static final long serialVersionUID = -7926116447719010837L;
47

    
48
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
49

    
50
    ReferencePopupEditor inReferencePopup = null;
51

    
52
    public ReferenceEditorPresenter() {
53

    
54
    }
55

    
56

    
57
    /**
58
     * {@inheritDoc}
59
     */
60
    @Override
61
    public void handleViewEntered() {
62
        super.handleViewEntered();
63

    
64
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
65

    
66
            @Override
67
            public String getCaption(Reference option) {
68
                return option.getTitleCache();
69
            }
70

    
71
        });
72

    
73
        CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
74
        getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
75
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
76
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
77

    
78
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService(), TeamOrPersonBase.class);
79
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
80
        getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
81
        getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
82
    }
83

    
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    protected Reference loadCdmEntity(UUID identifier) {
89

    
90
        List<String> initStrategy = Arrays.asList(new String []{
91

    
92
                "$"
93
                }
94
        );
95

    
96
        Reference reference;
97
        if(identifier != null){
98
            reference = getRepo().getReferenceService().load(identifier, initStrategy);
99
        } else {
100
            reference = createNewReference();
101
        }
102
        return reference;
103
    }
104

    
105
    /**
106
     * TODO this should better go into {@link AbstractCdmEditorPresenter}
107
     *
108
     * @return
109
     */
110
    protected Reference createNewReference() {
111
        if(this.beanInstantiator != null){
112
            return beanInstantiator.createNewBean();
113
        }
114
        return ReferenceFactory.newGeneric();
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    protected void guaranteePerEntityCRUDPermissions(UUID identifier) {
122
        if(crud != null){
123
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
124
        }
125

    
126
    }
127

    
128
    /**
129
     * {@inheritDoc}
130
     */
131
    @Override
132
    protected void guaranteePerEntityCRUDPermissions(Reference bean) {
133
        if(crud != null){
134
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(bean, crud, null);
135
        }
136
    }
137

    
138
    /**
139
    *
140
    * @param editorAction
141
     * @throws EditorEntityBeanException
142
    */
143
   @EventBusListenerMethod
144
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
145

    
146
       if(!isFromOwnView(editorAction) || editorAction.getSourceComponent() == null){
147
           return;
148
       }
149

    
150
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
151
           if(editorAction.isAddAction()){
152
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
153
               inReferencePopup.loadInEditor(null);
154
           }
155
           if(editorAction.isEditAction()){
156
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
157
               inReferencePopup.withDeleteButton(true);
158
               inReferencePopup.loadInEditor(editorAction.getEntityUuid());
159
           }
160
       }
161
   }
162

    
163
   @EventBusListenerMethod
164
   public void onEntityChangeEvent(EntityChangeEvent<?> event){
165

    
166
       if(event.getSourceView() == inReferencePopup){
167
           if(event.isCreateOrModifiedType()){
168
               getCache().load(event.getEntity());
169
               getView().getInReferenceCombobox().reload();
170
           }
171
           if(event.isRemovedType()){
172
               getView().getInReferenceCombobox().selectNewItem(null);
173
           }
174
           inReferencePopup = null;
175
       }
176

    
177
   }
178

    
179
    /**
180
     * {@inheritDoc}
181
     */
182
    @Override
183
    protected IService<Reference> getService() {
184
        return getRepo().getReferenceService();
185
    }
186

    
187

    
188

    
189
}
(1-1/3)