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(isViewLess()){
|
71
|
return;
|
72
|
}
|
73
|
if(ToOneRelatedEntityField.class.isAssignableFrom(editorAction.getSource().getClass())){
|
74
|
if(editorAction.isAddAction()){
|
75
|
Reference reference = ReferenceFactory.newGeneric();
|
76
|
getView().getTypeSelect().getValue();
|
77
|
inReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
|
78
|
inReferencePopup.showInEditor(reference);
|
79
|
}
|
80
|
if(editorAction.isEditAction()){
|
81
|
TransactionStatus tx = getRepo().startTransaction(false);
|
82
|
Reference reference = getRepo().getReferenceService().find(editorAction.getEntityId());
|
83
|
ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
|
84
|
popup.showInEditor(reference);
|
85
|
getRepo().commitTransaction(tx);
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
|
90
|
@EventListener
|
91
|
public void doDoneWithPopupEvent(DoneWithPopupEvent event){
|
92
|
if(isViewLess()){
|
93
|
return;
|
94
|
}
|
95
|
if(event.getPopup().equals(inReferencePopup)){
|
96
|
if(event.getReason().equals(Reason.SAVE)){
|
97
|
Reference bean = inReferencePopup.getBean();
|
98
|
// TODO update items from db instead of just adding the new item
|
99
|
Item selectItem = getView().getInReferenceSelect().getSelect().addItem(bean);
|
100
|
getView().getInReferenceSelect().getSelect().select(selectItem);
|
101
|
getView().getInReferenceSelect().getSelect().markAsDirty();
|
102
|
}
|
103
|
inReferencePopup = null;
|
104
|
}
|
105
|
|
106
|
}
|
107
|
|
108
|
}
|