Project

General

Profile

Download (6.21 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

    
14
import org.apache.log4j.Logger;
15
import org.springframework.context.event.EventListener;
16
import org.vaadin.viritin.fields.CaptionGenerator;
17

    
18
import eu.etaxonomy.cdm.api.service.IService;
19
import eu.etaxonomy.cdm.model.agent.AgentBase;
20
import eu.etaxonomy.cdm.model.agent.Person;
21
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
24
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
25
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
26
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
27
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
28
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
29
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
30
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
31
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
32
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
33

    
34
/**
35
 * @author a.kohlbecker
36
 * @since Apr 5, 2017
37
 *
38
 */
39
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
40

    
41
    private static final long serialVersionUID = -7926116447719010837L;
42

    
43
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
44

    
45
    ReferencePopupEditor inReferencePopup = null;
46

    
47
    public ReferenceEditorPresenter() {
48

    
49
    }
50

    
51
    /**
52
     * {@inheritDoc}
53
     */
54
    @Override
55
    public void handleViewEntered() {
56
        super.handleViewEntered();
57

    
58
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
59

    
60
            @Override
61
            public String getCaption(Reference option) {
62
                return option.getTitleCache();
63
            }
64

    
65
        });
66

    
67
        CdmFilterablePagingProvider<Reference, Reference> collectionPagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(getRepo().getReferenceService());
68
        getView().getInReferenceCombobox().loadFrom(collectionPagingProvider, collectionPagingProvider, collectionPagingProvider.getPageSize());
69
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
70
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityReloader<Reference>(getView().getInReferenceCombobox(),this));
71

    
72
        CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase> teamOrPersonPagingProvider = new CdmFilterablePagingProvider<AgentBase, TeamOrPersonBase>(getRepo().getAgentService());
73
        CdmFilterablePagingProvider<AgentBase, Person> personPagingProvider = new CdmFilterablePagingProvider<AgentBase, Person>(getRepo().getAgentService(), Person.class);
74
        getView().getAuthorshipField().setFilterableTeamPagingProvider(teamOrPersonPagingProvider, this);
75
        getView().getAuthorshipField().setFilterablePersonPagingProvider(personPagingProvider, this);
76
    }
77

    
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    protected Reference loadCdmEntityById(Integer identifier) {
83

    
84
        List<String> initStrategy = Arrays.asList(new String []{
85

    
86
                "$"
87
                }
88
        );
89

    
90
        Reference reference;
91
        if(identifier != null){
92
            reference = getRepo().getReferenceService().load(identifier, initStrategy);
93
        } else {
94
            reference = createNewReference();
95
        }
96
        return reference;
97
    }
98

    
99
    /**
100
     * TODO this should better go into {@link AbstractCdmEditorPresenter}
101
     *
102
     * @return
103
     */
104
    protected Reference createNewReference() {
105
        if(this.beanInstantiator != null){
106
            return beanInstantiator.createNewBean();
107
        }
108
        return ReferenceFactory.newGeneric();
109
    }
110

    
111
    /**
112
     * {@inheritDoc}
113
     */
114
    @Override
115
    protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
116
        if(crud != null){
117
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
118
        }
119

    
120
    }
121

    
122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    protected void guaranteePerEntityCRUDPermissions(Reference bean) {
127
        if(crud != null){
128
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(bean, crud, null);
129
        }
130
    }
131

    
132
    /**
133
    *
134
    * @param editorAction
135
     * @throws EditorEntityBeanException
136
    */
137
   @EventListener(condition = "#editorAction.sourceComponent != null")
138
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
139
       if(!isFromOwnView(editorAction)){
140
           return;
141
       }
142
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
143
           if(editorAction.isAddAction()){
144
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
145
               inReferencePopup.loadInEditor(null);
146
           }
147
           if(editorAction.isEditAction()){
148
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
149
               popup.withDeleteButton(true);
150
               popup.loadInEditor(editorAction.getEntityId());
151
           }
152
       }
153
   }
154

    
155
   @EventListener
156
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
157

    
158
       if(event.getPopup().equals(inReferencePopup)){
159
           if(event.getReason().equals(Reason.SAVE)){
160
               Reference bean = inReferencePopup.getBean();
161
               getView().getInReferenceCombobox().selectNewItem(bean);
162
           }
163
           if(event.getReason().equals(Reason.DELETE)){
164
               getView().getInReferenceCombobox().selectNewItem(null);
165
           }
166
           inReferencePopup = null;
167
       }
168

    
169
   }
170

    
171
    /**
172
     * {@inheritDoc}
173
     */
174
    @Override
175
    protected IService<Reference> getService() {
176
        return getRepo().getReferenceService();
177
    }
178

    
179

    
180

    
181
}
(1-1/3)