Project

General

Profile

Download (5.94 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.vaadin.mvp;
2

    
3
import java.io.Serializable;
4

    
5
import org.apache.log4j.Logger;
6
import org.hibernate.Session;
7
import org.hibernate.engine.internal.StatefulPersistenceContext;
8
import org.hibernate.engine.spi.SessionImplementor;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Qualifier;
11
import org.springframework.security.core.context.SecurityContext;
12
import org.springframework.security.core.context.SecurityContextHolder;
13
import org.vaadin.spring.events.EventBus;
14

    
15
import eu.etaxonomy.cdm.api.application.CdmRepository;
16
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
17
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
18

    
19
/**
20
 * AbstractPresenter is the base class of all presenter components. Presenter's
21
 * role is to govern the view and control the complex UI logic based on
22
 * notifications presenter receives from its view.
23
 *
24
 * @author Peter / Vaadin
25
 *
26
 * @param <V>
27
 *            type of the view this presenter governs
28
 */
29
public abstract class AbstractPresenter<V extends ApplicationView> implements Serializable {
30

    
31

    
32
    private static final long serialVersionUID = 5260910510283481832L;
33

    
34
    public static final Logger logger = Logger.getLogger(AbstractPresenter.class);
35

    
36
	private V view;
37

    
38
	protected V getView() {
39
	    if(view == null){
40
            Logger.getLogger(this.getClass()).warn("CDM-VAADIN#6562: presenter " + toString() + " without view.");
41
        }
42
		return view;
43
	}
44

    
45
	@Autowired
46
	@Qualifier("cdmRepository")
47
	private CdmRepository repo;
48

    
49
	@Autowired
50
	private NavigationManager navigationManager;
51

    
52
    protected EventBus.ViewEventBus viewEventBus;
53

    
54
    @Autowired
55
    protected void setViewEventBus(EventBus.ViewEventBus viewEventBus){
56
        this.viewEventBus = viewEventBus;
57
        eventViewBusSubscription(viewEventBus);
58
    }
59

    
60
    /**
61
     * Override if needed, e.g. to skip subscription
62
     *
63
     * @param viewEventBus
64
     */
65
    protected void eventViewBusSubscription(EventBus.ViewEventBus viewEventBus){
66
            viewEventBus.subscribe(this);
67
    }
68

    
69
    public void unsubscribeFromEventBuses(){
70
        viewEventBus.unsubscribe(this);
71
    }
72

    
73

    
74
	//	protected DefaultTransactionDefinition definition = null;
75

    
76
    //	protected TransactionDefinition getTransactionDefinition(){
77
    //	    if(definition == null){
78
    //    	    definition = new DefaultTransactionDefinition();
79
    //    	    definition.setReadOnly(true);
80
    //	    }
81
    //	    return definition;
82
    //	}
83

    
84

    
85
	/**
86
	 * @return the repo
87
	 */
88
	public CdmRepository getRepo() {
89
	    return repo;
90
	}
91

    
92
	/**
93
     * @return
94
     *
95
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
96
     */
97
    protected SecurityContext currentSecurityContext() {
98
        return SecurityContextHolder.getContext();
99
    }
100

    
101
    /**
102
     * @return
103
     */
104
    protected Session getSession() {
105
        Session session = getRepo().getSession();
106
        if(logger.isTraceEnabled()){
107
            if(session.isOpen()){
108
                logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() +", persistenceContext: " + ((SessionImplementor)session).getPersistenceContext() + " - " + session.toString());
109
            }  else {
110
                logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() +"  is closed ");
111
            }
112
        }
113
        return session;
114
    }
115

    
116
    protected String _toString(){
117
        return this.getClass().getSimpleName() + "@" + this.hashCode();
118
    }
119

    
120
	/**
121
	 * Notifies the presenter that its view is initialized so that presenter can
122
	 * start its own initialization if required.
123
	 *
124
	 * @param view
125
	 */
126
	protected void init(V view) {
127
	    logger.trace(String.format("Presenter %s init()", _toString()));
128
		this.view = view;
129
		onPresenterReady();
130
	}
131

    
132

    
133
    /**
134
	 * Extending classes should overwrite this method in order to perform logic
135
	 * after presenter has finished initializing.
136
	 *
137
	 * At this point in the life cycle of the MVP the {@link AbstractView#initContent() initContent()}
138
	 * method has been executed already.
139
	 */
140
	protected void onPresenterReady() {
141
	    logger.trace(String.format("Presenter %s ready", _toString()));
142
	}
143

    
144
    /**
145
     * @return
146
     */
147
    private StatefulPersistenceContext getPersitenceContext() {
148
        return (StatefulPersistenceContext)((SessionImplementor)getSession()).getPersistenceContext();
149
    }
150

    
151
    public final void onViewEnter() {
152
	    logger.trace(String.format("%s onViewEnter()", _toString()));
153
	    handleViewEntered();
154
	}
155

    
156
	public void onViewExit() {
157
	    logger.trace(String.format("%s onViewExit()", _toString()));
158
	    handleViewExit();
159
	}
160

    
161
	/**
162
	 * Extending classes should overwrite this method to react to the event when
163
	 * user has navigated into the view that this presenter governs.
164
	 * For implementations of {@link AbstractPopupEditor AbstractPopupEditors} this is usually
165
	 * called before the data item has been bound. This order is guaranteed since popup editors
166
	 * are managed through the {@link NavigationManagerBean}
167
	 */
168
	public void handleViewEntered() {
169
	}
170

    
171
    /**
172
     * Extending classes may overwrite this method to react to
173
     * the event when user leaves the view that this presenter
174
     * governs. This method is executed before un-binding and closing the
175
     * conversation holder.
176
     */
177
    public void handleViewExit() {
178
    }
179

    
180
    /**
181
     * @return the navigationManager
182
     */
183
    public NavigationManager getNavigationManager() {
184
        return navigationManager;
185
    }
186

    
187
    /**
188
     * @param repo the repo to set
189
     */
190
    protected void setRepo(CdmRepository repo) {
191
        this.repo = repo;
192
    }
193

    
194
    /**
195
     * @param navigationManager the navigationManager to set
196
     */
197
    protected void setNavigationManager(NavigationManager navigationManager) {
198
        this.navigationManager = navigationManager;
199
    }
200

    
201
    protected boolean checkFromOwnView(AbstractEditorAction event) {
202
        return getView() != null && getView() == event.getSourceView();
203
    }
204

    
205
}
(6-6/9)