Project

General

Profile

Download (4.25 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.spi.SessionImplementor;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Qualifier;
10
import org.springframework.security.core.context.SecurityContext;
11
import org.springframework.security.core.context.SecurityContextHolder;
12
import org.springframework.transaction.TransactionStatus;
13

    
14
import eu.etaxonomy.cdm.api.application.CdmRepository;
15
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
16
import eu.etaxonomy.vaadin.ui.navigation.NavigationManagerBean;
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
	 * @return the repo
54
	 */
55
	public CdmRepository getRepo() {
56
	    return repo;
57
	}
58

    
59
	/**
60
     * @return
61
     *
62
     * FIXME is it ok to use the SecurityContextHolder or do we need to hold the context in the vaadin session?
63
     */
64
    protected SecurityContext currentSecurityContext() {
65
        return SecurityContextHolder.getContext();
66
    }
67

    
68
    /**
69
     * @return
70
     */
71
    protected Session getSession() {
72
        Session session = getRepo().getSession(false);
73
        logger.trace(this._toString() + ".getSession() - session:" + session.hashCode() +", persistenceContext: " + ((SessionImplementor)session).getPersistenceContext() + " - " + session.toString());
74
        return session;
75
    }
76

    
77
    protected String _toString(){
78
        return this.getClass().getSimpleName() + "@" + this.hashCode();
79
    }
80

    
81
	/**
82
	 * Notifies the presenter that its view is initialized so that presenter can
83
	 * start its own initialization if required.
84
	 *
85
	 * @param view
86
	 */
87
	protected final void init(V view) {
88
	    logger.trace(String.format("Presenter %s init", this.toString()));
89
		this.view = view;
90
		onPresenterReady();
91
	}
92

    
93
	/**
94
	 * Extending classes should overwrite this method in order to perform logic
95
	 * after presenter has finished initializing.
96
	 */
97
	protected void onPresenterReady() {
98
	    logger.trace(String.format("Presenter %s ready", this.toString()));
99
	}
100

    
101
	public final void onViewEnter() {
102
	    logger.trace("View entered");
103
	    TransactionStatus tx = getRepo().startTransaction();
104
	    handleViewEntered();
105
	    getRepo().commitTransaction(tx);
106
	}
107

    
108
	public final void onViewExit() {
109
	    handleViewExit();
110
	}
111

    
112
	/**
113
	 * Extending classes should overwrite this method to react to the event when
114
	 * user has navigated into the view that this presenter governs.
115
	 * For implementations of {@link AbstractPopupEditor AbstractPopupEditors} this is usually
116
	 * called before the data item has been bound. This order is guaranteed since popup editors
117
	 * are managed through the {@link NavigationManagerBean}
118
	 */
119
	public void handleViewEntered() {
120
	}
121

    
122
    /**
123
     * Extending classes may overwrite this method to react to
124
     * the event when user leaves the view that this presenter
125
     * governs.
126
     */
127
    public void handleViewExit() {
128
    }
129

    
130
    /**
131
     * @return the navigationManager
132
     */
133
    public NavigationManager getNavigationManager() {
134
        return navigationManager;
135
    }
136

    
137
    /**
138
     * @param repo the repo to set
139
     */
140
    protected void setRepo(CdmRepository repo) {
141
        this.repo = repo;
142
    }
143

    
144
    /**
145
     * @param navigationManager the navigationManager to set
146
     */
147
    protected void setNavigationManager(NavigationManager navigationManager) {
148
        this.navigationManager = navigationManager;
149
    }
150

    
151

    
152

    
153
}
(6-6/8)