Project

General

Profile

Download (5.42 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.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
        getView().getInReferenceCombobox().loadFrom(new FilterablePagingProvider<Reference>(){
66

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

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

    
101
    /**
102
     * {@inheritDoc}
103
     */
104
    @Override
105
    protected Reference loadBeanById(Object identifier) {
106
        Reference reference;
107
        if(identifier != null){
108
            reference = getRepo().getReferenceService().find((Integer)identifier);
109
        } else {
110
            reference = ReferenceFactory.newGeneric();
111
        }
112
        return reference;
113
    }
114

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

    
138
   @EventListener
139
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
140

    
141
       if(event.getPopup().equals(inReferencePopup)){
142
           if(event.getReason().equals(Reason.SAVE)){
143
               Reference bean = inReferencePopup.getBean();
144
               getView().getInReferenceCombobox().selectNewItem(bean);
145
           }
146
           if(event.getReason().equals(Reason.DELETE)){
147
               getView().getInReferenceCombobox().selectNewItem(null);
148
           }
149
           inReferencePopup = null;
150
       }
151

    
152
   }
153

    
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    protected IService<Reference> getService() {
159
        return getRepo().getReferenceService();
160
    }
161

    
162

    
163

    
164
}
(1-1/3)