Project

General

Profile

Download (5.99 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.security.UserHelper;
28
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
29
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
30
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
31
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
32

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

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

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

    
44
    ReferencePopupEditor inReferencePopup = null;
45

    
46
    public ReferenceEditorPresenter() {
47

    
48
    }
49

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

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

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

    
64
        });
65

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

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

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

    
82
        List<String> initStrategy = Arrays.asList(new String []{
83

    
84
                "$"
85
                }
86
        );
87

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

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

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

    
118
    }
119

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

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

    
153
   @EventListener
154
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
155

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

    
167
   }
168

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

    
177

    
178

    
179
}
(1-1/3)