Project

General

Profile

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

    
14
import eu.etaxonomy.cdm.api.application.CdmRepository;
15
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
16

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

    
29

    
30
    private static final long serialVersionUID = 5260910510283481832L;
31

    
32
    public static final Logger logger = Logger.getLogger(AbstractPresenter.class);
33

    
34
	private V view;
35

    
36

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

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

    
48
	@Autowired
49
	private NavigationManager navigationManager;
50

    
51

    
52
	//	protected DefaultTransactionDefinition definition = null;
53

    
54
    //	protected TransactionDefinition getTransactionDefinition(){
55
    //	    if(definition == null){
56
    //    	    definition = new DefaultTransactionDefinition();
57
    //    	    definition.setReadOnly(true);
58
    //	    }
59
    //	    return definition;
60
    //	}
61

    
62

    
63
	/**
64
	 * @return the repo
65
	 */
66
	public CdmRepository getRepo() {
67
	    return repo;
68
	}
69

    
70
	/**
71
     * @return
72
     *
73
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
74
     */
75
    protected SecurityContext currentSecurityContext() {
76
        return SecurityContextHolder.getContext();
77
    }
78

    
79
    /**
80
     * @return
81
     */
82
    protected Session getSession() {
83
        Session session = getRepo().getSession();
84
        if(logger.isTraceEnabled()){
85
            if(session.isOpen()){
86
                logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() +", persistenceContext: " + ((SessionImplementor)session).getPersistenceContext() + " - " + session.toString());
87
            }  else {
88
                logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() +"  is closed ");
89
            }
90
        }
91
        return session;
92
    }
93

    
94
    protected String _toString(){
95
        return this.getClass().getSimpleName() + "@" + this.hashCode();
96
    }
97

    
98
	/**
99
	 * Notifies the presenter that its view is initialized so that presenter can
100
	 * start its own initialization if required.
101
	 *
102
	 * @param view
103
	 */
104
	protected void init(V view) {
105
	    logger.trace(String.format("Presenter %s init()", _toString()));
106
		this.view = view;
107
		onPresenterReady();
108
	}
109

    
110

    
111
    /**
112
	 * Extending classes should overwrite this method in order to perform logic
113
	 * after presenter has finished initializing.
114
	 *
115
	 * At this point in the life cycle of the MVP the {@link AbstractView#initContent() initContent()}
116
	 * method has been executed already.
117
	 */
118
	protected void onPresenterReady() {
119
	    logger.trace(String.format("Presenter %s ready", _toString()));
120
	}
121

    
122
    /**
123
     * @return
124
     */
125
    private StatefulPersistenceContext getPersitenceContext() {
126
        return (StatefulPersistenceContext)((SessionImplementor)getSession()).getPersistenceContext();
127
    }
128

    
129
    public final void onViewEnter() {
130
	    logger.trace(String.format("%s onViewEnter()", _toString()));
131
	    handleViewEntered();
132
	}
133

    
134
	public void onViewExit() {
135
	    logger.trace(String.format("%s onViewExit()", _toString()));
136
	    handleViewExit();
137
	}
138

    
139
	/**
140
	 * Extending classes should overwrite this method to react to the event when
141
	 * user has navigated into the view that this presenter governs.
142
	 * For implementations of {@link AbstractPopupEditor AbstractPopupEditors} this is usually
143
	 * called before the data item has been bound. This order is guaranteed since popup editors
144
	 * are managed through the {@link NavigationManagerBean}
145
	 */
146
	public void handleViewEntered() {
147
	}
148

    
149
    /**
150
     * Extending classes may overwrite this method to react to
151
     * the event when user leaves the view that this presenter
152
     * governs. This method is executed before un-binding and closing the
153
     * conversation holder.
154
     */
155
    public void handleViewExit() {
156
    }
157

    
158
    /**
159
     * @return the navigationManager
160
     */
161
    public NavigationManager getNavigationManager() {
162
        return navigationManager;
163
    }
164

    
165
    /**
166
     * @param repo the repo to set
167
     */
168
    protected void setRepo(CdmRepository repo) {
169
        this.repo = repo;
170
    }
171

    
172
    /**
173
     * @param navigationManager the navigationManager to set
174
     */
175
    protected void setNavigationManager(NavigationManager navigationManager) {
176
        this.navigationManager = navigationManager;
177
    }
178

    
179
}
(6-6/8)