Project

General

Profile

Download (5.73 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.springframework.transaction.TransactionStatus;
17
import org.vaadin.viritin.fields.CaptionGenerator;
18
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
19
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
20

    
21
import eu.etaxonomy.cdm.api.service.DeleteResult;
22
import eu.etaxonomy.cdm.api.service.pager.Pager;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
25
import eu.etaxonomy.cdm.persistence.query.MatchMode;
26
import eu.etaxonomy.cdm.persistence.query.OrderHint;
27
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
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
        logger.trace("CONTRUCTOR");
48
    }
49

    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @Override
54
    public void handleViewEntered() {
55
        super.handleViewEntered();
56
        /*
57
        ListSelect select = getView().getInReferenceSelect().getSelect();
58
        BeanItemContainer<Reference> inReferenceSelectContainer = (BeanItemContainer<Reference>) select.getContainerDataSource();
59
        List<Reference> references = getRepo().getCommonService().list(Reference.class, (Integer)null, (Integer)null,
60
                OrderHint.ORDER_BY_TITLE_CACHE.asList(),
61
                Arrays.asList(new String[]{"$"}));
62
        inReferenceSelectContainer.addAll(references);
63
        select.setItemCaptionPropertyId("titleCache");
64
        select.markAsDirty();
65
        */
66

    
67
        getView().getInReferenceCombobox().getSelect().setCaptionGenerator(new CaptionGenerator<Reference>(){
68

    
69
            @Override
70
            public String getCaption(Reference option) {
71
                return option.getTitleCache();
72
            }
73

    
74
        });
75
        getView().getInReferenceCombobox().loadFrom(new FilterablePagingProvider<Reference>(){
76

    
77
            @Override
78
            public List<Reference> findEntities(int firstRow, String filter) {
79
                Pager<Reference> page = getRepo().getReferenceService().findByTitle(
80
                        null,
81
                        filter,
82
                        MatchMode.ANYWHERE,
83
                        null,
84
                        20,
85
                        firstRow,
86
                        OrderHint.ORDER_BY_TITLE_CACHE.asList(),
87
                        Arrays.asList("$")
88
                      );
89
                return page.getRecords();
90
            }},
91
            new FilterableCountProvider(){
92
                @Override
93
                public int size(String filter) {
94
                    Pager<Reference> page = getRepo().getReferenceService().findByTitle(
95
                            null,
96
                            filter,
97
                            MatchMode.ANYWHERE,
98
                            null,
99
                            1,
100
                            0,
101
                            null,
102
                            null
103
                          );
104
                    return page.getCount().intValue();
105
                }}
106
            , 20);
107
    }
108

    
109
    /**
110
    *
111
    * @param editorAction
112
    */
113
   @EventListener(condition = "#editorAction.sourceComponent != null")
114
   public void onReferenceEditorAction(ReferenceEditorAction editorAction){
115
       if(!isFromOwnView(editorAction)){
116
           return;
117
       }
118
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
119
           if(editorAction.isAddAction()){
120
               Reference reference = ReferenceFactory.newGeneric();
121
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
122
               inReferencePopup.showInEditor(reference);
123
           }
124
           if(editorAction.isEditAction()){
125
               TransactionStatus tx = getRepo().startTransaction(false);
126
               Reference reference = getRepo().getReferenceService().find(editorAction.getEntityId());
127
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
128
               popup.showInEditor(reference);
129
               getRepo().commitTransaction(tx);
130
           }
131
       }
132
   }
133

    
134
   @EventListener
135
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
136

    
137
       if(event.getPopup().equals(inReferencePopup)){
138
           if(event.getReason().equals(Reason.SAVE)){
139
               Reference bean = inReferencePopup.getBean();
140
               getView().getInReferenceCombobox().selectNewItem(bean);
141
           }
142
           if(event.getReason().equals(Reason.DELETE)){
143
               getView().getInReferenceCombobox().selectNewItem(null);
144
           }
145
           inReferencePopup = null;
146
       }
147

    
148
   }
149

    
150
    /**
151
     * {@inheritDoc}
152
     */
153
    @Override
154
    protected DeleteResult executeServiceDeleteOperation(Reference bean) {
155
        return getRepo().getReferenceService().delete(bean);
156
    }
157

    
158
}
(1-1/3)