Project

General

Profile

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

    
12
import eu.etaxonomy.cdm.api.application.CdmRepository;
13
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
14
import eu.etaxonomy.vaadin.ui.navigation.NavigationManagerBean;
15

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

    
28

    
29
    private static final long serialVersionUID = 5260910510283481832L;
30

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

    
33
	private V view;
34

    
35

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

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

    
47
	@Autowired
48
	private NavigationManager navigationManager;
49

    
50
	/**
51
	 * @return the repo
52
	 */
53
	public CdmRepository getRepo() {
54
	    return repo;
55
	}
56

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

    
66
	/**
67
	 * Notifies the presenter that its view is initialized so that presenter can
68
	 * start its own initialization if required.
69
	 *
70
	 * @param view
71
	 */
72
	protected final void init(V view) {
73
	    logger.trace("Presenter init");
74
		this.view = view;
75
		onPresenterReady();
76
	}
77

    
78
	/**
79
	 * Extending classes should overwrite this method in order to perform logic
80
	 * after presenter has finished initializing.
81
	 */
82
	protected void onPresenterReady() {
83
	    logger.trace("Presenter ready");
84
	}
85

    
86
	public final void onViewEnter() {
87
	    logger.trace("View entered");
88
	    TransactionStatus tx = getRepo().startTransaction();
89
	    handleViewEntered();
90
	    getRepo().commitTransaction(tx);
91
	}
92

    
93
	public final void onViewExit() {
94
	    handleViewExit();
95
	}
96

    
97
	/**
98
	 * Extending classes should overwrite this method to react to the event when
99
	 * user has navigated into the view that this presenter governs.
100
	 * For implementations of {@link AbstractPopupEditor AbstractPopupEditors} this is usually
101
	 * called before the data item has been bound. This order is guaranteed since popup editors
102
	 * are managed through the {@link NavigationManagerBean}
103
	 */
104
	public void handleViewEntered() {
105
	}
106

    
107
    /**
108
     * Extending classes may overwrite this method to react to
109
     * the event when user leaves the view that this presenter
110
     * governs.
111
     */
112
    public void handleViewExit() {
113
    }
114

    
115
    /**
116
     * @return the navigationManager
117
     */
118
    public NavigationManager getNavigationManager() {
119
        return navigationManager;
120
    }
121

    
122
    /**
123
     * @param repo the repo to set
124
     */
125
    protected void setRepo(CdmRepository repo) {
126
        this.repo = repo;
127
    }
128

    
129
    /**
130
     * @param navigationManager the navigationManager to set
131
     */
132
    protected void setNavigationManager(NavigationManager navigationManager) {
133
        this.navigationManager = navigationManager;
134
    }
135

    
136

    
137

    
138
}
(6-6/8)