Project

General

Profile

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

    
18
import com.vaadin.data.Item;
19
import com.vaadin.data.util.BeanItemContainer;
20
import com.vaadin.ui.ListSelect;
21

    
22
import eu.etaxonomy.cdm.model.reference.Reference;
23
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
24
import eu.etaxonomy.cdm.persistence.query.OrderHint;
25
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
26
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityField;
27
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
28
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
29
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @since Apr 5, 2017
34
 *
35
 */
36
public class ReferenceEditorPresenter extends AbstractCdmEditorPresenter<Reference, ReferencePopupEditorView> {
37

    
38
    private static final long serialVersionUID = -7926116447719010837L;
39

    
40
    private static final Logger logger = Logger.getLogger(ReferenceEditorPresenter.class);
41

    
42
    ReferencePopupEditor inReferencePopup = null;
43

    
44
    public ReferenceEditorPresenter() {
45
        logger.trace("CONTRUCTOR");
46
    }
47

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

    
64
    /**
65
    *
66
    * @param editorAction
67
    */
68
   @EventListener(condition = "#editorAction.source != null")
69
   public void onReferenceEditorAction(ReferenceEditorAction editorAction){
70
       if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSource().getClass())){
71
           if(editorAction.isAddAction()){
72
               Reference reference = ReferenceFactory.newGeneric();
73
               getView().getTypeSelect().getValue();
74
               inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
75
               inReferencePopup.showInEditor(reference);
76
           }
77
           if(editorAction.isEditAction()){
78
               TransactionStatus tx = getRepo().startTransaction(false);
79
               Reference reference = getRepo().getReferenceService().find(editorAction.getEntityId());
80
               ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
81
               popup.showInEditor(reference);
82
               getRepo().commitTransaction(tx);
83
           }
84
       }
85
   }
86

    
87
   @EventListener
88
   public void doDoneWithPopupEvent(DoneWithPopupEvent event){
89

    
90
       if(event.getPopup().equals(inReferencePopup)){
91
           if(event.getReason().equals(Reason.SAVE)){
92
               Reference bean = inReferencePopup.getBean();
93
               // TODO update items from db instead of just adding the new item
94
               Item selectItem = getView().getInReferenceSelect().getSelect().addItem(bean);
95
               getView().getInReferenceSelect().getSelect().select(selectItem);
96
               getView().getInReferenceSelect().getSelect().markAsDirty();
97
           }
98
           inReferencePopup = null;
99
       }
100

    
101
   }
102

    
103
}
(1-1/3)