Project

General

Profile

Download (5.36 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.cdm.vaadin.event.AbstractEditorAction;
16
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
17

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

    
30

    
31
    private static final long serialVersionUID = 5260910510283481832L;
32

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

    
35
	private V view;
36

    
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

    
53
	//	protected DefaultTransactionDefinition definition = null;
54

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

    
63

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

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

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

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

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

    
111

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

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

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

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

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

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

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

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

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

    
180
    protected boolean checkFromOwnView(AbstractEditorAction event) {
181
        return getView() != null && getView() == event.getSourceView();
182
    }
183

    
184
}
(6-6/9)