Project

General

Profile

Download (6.03 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
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
18
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
19

    
20
import eu.etaxonomy.cdm.api.service.IService;
21
import eu.etaxonomy.cdm.api.service.pager.Pager;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
24
import eu.etaxonomy.cdm.persistence.query.MatchMode;
25
import eu.etaxonomy.cdm.persistence.query.OrderHint;
26
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
27
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
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
        getView().getInReferenceCombobox().loadFrom(new FilterablePagingProvider<Reference>(){
67

    
68
            @Override
69
            public List<Reference> findEntities(int firstRow, String filter) {
70
                Pager<Reference> page = getRepo().getReferenceService().findByTitle(
71
                        null,
72
                        filter,
73
                        MatchMode.ANYWHERE,
74
                        null,
75
                        20,
76
                        firstRow,
77
                        OrderHint.ORDER_BY_TITLE_CACHE.asList(),
78
                        Arrays.asList("$")
79
                      );
80
                return page.getRecords();
81
            }},
82
            new FilterableCountProvider(){
83
                @Override
84
                public int size(String filter) {
85
                    Pager<Reference> page = getRepo().getReferenceService().findByTitle(
86
                            null,
87
                            filter,
88
                            MatchMode.ANYWHERE,
89
                            null,
90
                            1,
91
                            0,
92
                            null,
93
                            null
94
                          );
95
                    return page.getCount().intValue();
96
                }}
97
            , 20);
98

    
99
        getView().getInReferenceCombobox().getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(getView().getInReferenceCombobox()));
100
    }
101

    
102
    /**
103
     * {@inheritDoc}
104
     */
105
    @Override
106
    protected Reference loadCdmEntityById(Integer identifier) {
107

    
108
        Reference reference;
109
        if(identifier != null){
110
            reference = getRepo().getReferenceService().find(identifier);
111
        } else {
112
            reference = ReferenceFactory.newGeneric();
113
        }
114
        return reference;
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    protected void guaranteePerEntityCRUDPermissions(Integer 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
   @EventListener(condition = "#editorAction.sourceComponent != null")
144
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
145
       if(!isFromOwnView(editorAction)){
146
           return;
147
       }
148
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
149
           if(editorAction.isAddAction()){
150
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
151
               inReferencePopup.loadInEditor(null);
152
           }
153
           if(editorAction.isEditAction()){
154
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
155
               popup.withDeleteButton(true);
156
               popup.loadInEditor(editorAction.getEntityId());
157
           }
158
       }
159
   }
160

    
161
   @EventListener
162
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
163

    
164
       if(event.getPopup().equals(inReferencePopup)){
165
           if(event.getReason().equals(Reason.SAVE)){
166
               Reference bean = inReferencePopup.getBean();
167
               getView().getInReferenceCombobox().selectNewItem(bean);
168
           }
169
           if(event.getReason().equals(Reason.DELETE)){
170
               getView().getInReferenceCombobox().selectNewItem(null);
171
           }
172
           inReferencePopup = null;
173
       }
174

    
175
   }
176

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

    
185

    
186

    
187
}
(1-1/3)