Project

General

Profile

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

    
99
    /**
100
    *
101
    * @param editorAction
102
     * @throws EditorEntityBeanException
103
    */
104
   @EventListener(condition = "#editorAction.sourceComponent != null")
105
   public void onReferenceEditorAction(ReferenceEditorAction editorAction) {
106
       if(!isFromOwnView(editorAction)){
107
           return;
108
       }
109
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSourceComponent().getClass())){
110
           if(editorAction.isAddAction()){
111
               Reference reference = ReferenceFactory.newGeneric();
112
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
113
               inReferencePopup.showInEditor(reference);
114
           }
115
           if(editorAction.isEditAction()){
116
               TransactionStatus tx = getRepo().startTransaction(false);
117
               Reference reference = getRepo().getReferenceService().find(editorAction.getEntityId());
118
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
119
               popup.withDeleteButton(true);
120
               popup.showInEditor(reference);
121
               getRepo().commitTransaction(tx);
122
           }
123
       }
124
   }
125

    
126
   @EventListener
127
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
128

    
129
       if(event.getPopup().equals(inReferencePopup)){
130
           if(event.getReason().equals(Reason.SAVE)){
131
               Reference bean = inReferencePopup.getBean();
132
               getView().getInReferenceCombobox().selectNewItem(bean);
133
           }
134
           if(event.getReason().equals(Reason.DELETE)){
135
               getView().getInReferenceCombobox().selectNewItem(null);
136
           }
137
           inReferencePopup = null;
138
       }
139

    
140
   }
141

    
142
    /**
143
     * {@inheritDoc}
144
     */
145
    @Override
146
    protected DeleteResult executeServiceDeleteOperation(Reference bean) {
147
        return getRepo().getReferenceService().delete(bean);
148
    }
149

    
150
}
(1-1/3)