Project

General

Profile

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

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

    
98
    /**
99
     * {@inheritDoc}
100
     */
101
    @Override
102
    protected void guaranteePerEntityCRUDPermissions(Integer identifier) {
103
        if(crud != null){
104
            newAuthorityCreated = UserHelper.fromSession().createAuthorityForCurrentUser(Reference.class, identifier, crud, null);
105
        }
106

    
107
    }
108

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

    
119
    /**
120
    *
121
    * @param editorAction
122
     * @throws EditorEntityBeanException
123
    */
124
   @EventListener(condition = "#editorAction.sourceComponent != null")
125
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
126
       if(!isFromOwnView(editorAction)){
127
           return;
128
       }
129
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
130
           if(editorAction.isAddAction()){
131
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
132
               inReferencePopup.loadInEditor(null);
133
           }
134
           if(editorAction.isEditAction()){
135
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
136
               popup.withDeleteButton(true);
137
               popup.loadInEditor(editorAction.getEntityId());
138
           }
139
       }
140
   }
141

    
142
   @EventListener
143
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
144

    
145
       if(event.getPopup().equals(inReferencePopup)){
146
           if(event.getReason().equals(Reason.SAVE)){
147
               Reference bean = inReferencePopup.getBean();
148
               getView().getInReferenceCombobox().selectNewItem(bean);
149
           }
150
           if(event.getReason().equals(Reason.DELETE)){
151
               getView().getInReferenceCombobox().selectNewItem(null);
152
           }
153
           inReferencePopup = null;
154
       }
155

    
156
   }
157

    
158
    /**
159
     * {@inheritDoc}
160
     */
161
    @Override
162
    protected IService<Reference> getService() {
163
        return getRepo().getReferenceService();
164
    }
165

    
166

    
167

    
168
}
(1-1/3)