2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.vaadin
.mvp
;
11 import org
.hibernate
.FlushMode
;
12 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
13 import org
.springframework
.context
.ApplicationEventPublisher
;
14 import org
.springframework
.context
.event
.EventListener
;
16 import eu
.etaxonomy
.cdm
.vaadin
.event
.AbstractEditorAction
;
17 import eu
.etaxonomy
.vaadin
.mvp
.event
.EditorDeleteEvent
;
18 import eu
.etaxonomy
.vaadin
.mvp
.event
.EditorPreSaveEvent
;
19 import eu
.etaxonomy
.vaadin
.mvp
.event
.EditorSaveEvent
;
20 import eu
.etaxonomy
.vaadin
.mvp
.event
.EditorViewEvent
;
24 * @author a.kohlbecker
28 public abstract class AbstractEditorPresenter
<DTO
extends Object
, V
extends ApplicationView
<?
>> extends AbstractPresenter
<V
> {
31 private static final long serialVersionUID
= -6677074110764145236L;
33 FlushMode previousPreSaveEvenFlushMode
= null;
36 protected ApplicationEventPublisher eventBus
;
38 protected BeanInstantiator
<DTO
> beanInstantiator
= null;
41 * Load the bean to be edited in the editor freshly from the persistent storage.
42 * Ore create an new empty instance in case the supplied <code>identifier</code> is <code>null</code>.
47 protected abstract DTO
loadBeanById(Object identifier
);
50 * @param beanInstantiator the beanInstantiator to set
52 public void setBeanInstantiator(BeanInstantiator
<DTO
> beanInstantiator
) {
53 this.beanInstantiator
= beanInstantiator
;
57 * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
62 public void onEditorPreSaveEvent(EditorPreSaveEvent
<DTO
> preSaveEvent
){
63 if(!isFromOwnView(preSaveEvent
)){
66 // getSession().setFlushMode(FlushMode.AUTO);
71 public void onEditorSaveEvent(EditorSaveEvent
<DTO
> saveEvent
){
72 if(!isFromOwnView(saveEvent
)){
75 DTO bean
= saveEvent
.getBean();
79 // if(getSession().isOpen()){
80 // getSession().clear();
82 throw e
; // re-throw after cleaning up the session
84 // if(getSession().isOpen()){
85 // getSession().setFlushMode(previousPreSaveEvenFlushMode);
87 // previousPreSaveEvenFlushMode = null;
92 * Regarding changing the Flush mode see see also {@link ViewScopeConversationHolder}
97 public void onEditorDeleteEvent(EditorDeleteEvent
<DTO
> deleteEvent
){
98 if(!isFromOwnView(deleteEvent
)){
101 FlushMode previousFlushMode
= getSession().getFlushMode();
102 getSession().setFlushMode(FlushMode
.AUTO
);
103 deleteBean(deleteEvent
.getBean());
104 getSession().setFlushMode(previousFlushMode
);
111 protected boolean isFromOwnView(EditorViewEvent saveEvent
) {
112 return saveEvent
.getView().equals(getView());
115 protected Class
<V
> getViewType() {
116 return (Class
<V
>) super.getView().getClass();
119 protected boolean isFromOwnView(AbstractEditorAction action
){
120 return action
.getSourceView() != null && getView().equals(action
.getSourceView());
124 protected abstract void saveBean(DTO bean
);
129 protected abstract void deleteBean(DTO bean
);