001: package ru.emdev.EmForge.web.bean;
002:
003: import java.io.IOException;
004:
005: import javax.faces.application.Application;
006: import javax.faces.application.FacesMessage;
007: import javax.faces.application.FacesMessage.Severity;
008: import javax.faces.component.UIParameter;
009: import javax.faces.context.FacesContext;
010: import javax.faces.el.ValueBinding;
011: import javax.faces.event.ActionEvent;
012: import javax.servlet.http.HttpServletRequest;
013:
014: import org.apache.commons.lang.StringUtils;
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.emforge.AttachmentService;
018: import org.springframework.beans.factory.InitializingBean;
019:
020: import ru.emdev.EmForge.EmForgeContext;
021: import ru.emdev.EmForge.security.EmForgeUserDetails;
022: import ru.emdev.EmForge.security.web.SiteRole;
023: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
024: import ru.emdev.EmForge.wiki.web.bean.Crumb;
025:
026: import com.ecyrd.jspwiki.WikiEngine;
027:
028: /**
029: * Base controller
030: *
031: * @author spopov
032: */
033: public abstract class BaseControllerImpl implements InitializingBean {
034:
035: protected final Log logger = LogFactory.getLog(getClass());
036:
037: protected EmForgeContext m_appContext;
038: protected WikiEngine m_wikiEngine;
039: protected AttachmentService m_attachmentService;
040: protected MainMenuController m_mainMenu;
041: protected TrailController m_trail;
042:
043: /**
044: * @param i_appContext
045: */
046: public void setAppContext(EmForgeContext i_appContext) {
047:
048: m_appContext = i_appContext;
049: }
050:
051: /**
052: * @param i_wikiEngine
053: */
054: public void setWikiEngine(WikiEngine i_wikiEngine) {
055:
056: m_wikiEngine = i_wikiEngine;
057: }
058:
059: public void setAttachmentService(
060: AttachmentService i_attachmentService) {
061: m_attachmentService = i_attachmentService;
062: }
063:
064: /**
065: * @param i_mainMenu
066: */
067: public void setMainMenu(MainMenuController i_mainMenu) {
068:
069: m_mainMenu = i_mainMenu;
070: }
071:
072: /**
073: * @param i_trailController
074: */
075: public void setTrail(TrailController i_trailController) {
076:
077: m_trail = i_trailController;
078: }
079:
080: /**
081: * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
082: */
083: public void afterPropertiesSet() throws Exception {
084:
085: MainMenuItem menuItem = getSelectionItemOnMainMenu();
086: if (menuItem != null) {
087: m_mainMenu.setSelected(menuItem.getTitle());
088: }
089:
090: init();
091: }
092:
093: /**
094: * push "crumb" to the trail and get title for page; don't use this method in a bean; it should be called on a page;
095: */
096: public String getTitle() {
097:
098: String title = getTitleImpl();
099:
100: try {
101: // set "crumb" to the trail
102: if (StringUtils.isNotEmpty(title)) {
103: Crumb crumb = getTrailCrumbInfo();
104: if (m_trail != null && crumb != null) {
105: m_trail.push(crumb);
106: }
107: }
108: } catch (Exception e) {
109: logger.debug("Cannot push crumb to the trail", e);
110: return title;
111: }
112: return title;
113: }
114:
115: /**
116: * @return
117: */
118: public EmForgeUserDetails getCurrentUser() {
119:
120: assert m_appContext != null;
121: return m_appContext.getUserService().getCurrentUser();
122: }
123:
124: /**
125: * returns title on the header
126: */
127: public abstract String getTitleImpl();
128:
129: /**
130: * returns a text(title) on button in the main menu; it's used to make selected button on the menu; if <null> button
131: * on the menu will not be selected.
132: */
133: public abstract MainMenuItem getSelectionItemOnMainMenu();
134:
135: /**
136: * gets the info (display name and url) to make "breadcrumb" for the trail
137: */
138: public abstract Crumb getTrailCrumbInfo();
139:
140: /**
141: *
142: */
143: protected abstract void init();
144:
145: /**
146: * @return
147: */
148: public EmForgeContext getAppContext() {
149:
150: return m_appContext;
151: }
152:
153: /**
154: * This is utilitary method used only in this class
155: */
156: protected HttpServletRequest getHttpRequest() {
157:
158: return (HttpServletRequest) FacesContext.getCurrentInstance()
159: .getExternalContext().getRequest();
160: }
161:
162: /**
163: * Read param from HttpRequest
164: *
165: * @param i_paramName
166: * @return
167: */
168: public String getParam(String i_paramName) {
169:
170: return getHttpRequest().getParameter(i_paramName);
171: }
172:
173: /**
174: * Reads param from HTTPRequest and convert it into ID
175: *
176: * @param i_paramName
177: * @return
178: */
179: public Long getId(String i_paramName) {
180:
181: String strValue = getParam(i_paramName);
182:
183: if (strValue == null) {
184: return null;
185: }
186:
187: try {
188: Long id = Long.valueOf(strValue);
189:
190: return id;
191: } catch (Exception ex) {
192: // catch any exception here and just ignore it - return null in this case
193: return null;
194: }
195: }
196:
197: /**
198: * @param i_newUrl
199: */
200: public void redirect(String i_newUrl) {
201:
202: try {
203: FacesContext.getCurrentInstance().getExternalContext()
204: .redirect(i_newUrl);
205: } catch (IOException e) {
206: logger.error("Cannot process redirection", e);
207: }
208: }
209:
210: /**
211: * @param i_for
212: * @param i_message
213: * @param i_detailedMessage
214: * @param i_severity
215: */
216: public void addMessage(String i_for, String i_message,
217: String i_detailedMessage, Severity i_severity) {
218:
219: FacesContext context = FacesContext.getCurrentInstance();
220: FacesMessage fm = new FacesMessage(i_message, i_detailedMessage);
221: fm.setSeverity(i_severity);
222: context.addMessage(i_for, fm);
223: }
224:
225: /**
226: * @param i_message
227: * @param i_detailedMessage
228: * @param i_severity
229: */
230: public void addMessage(String i_message, String i_detailedMessage,
231: Severity i_severity) {
232:
233: addMessage(null, i_message, i_detailedMessage, i_severity);
234: }
235:
236: /**
237: * @param i_message
238: * @param i_detailedMessage
239: */
240: public void addInfoMessage(String i_message,
241: String i_detailedMessage) {
242:
243: addMessage(null, i_message, i_detailedMessage,
244: FacesMessage.SEVERITY_INFO);
245: }
246:
247: /**
248: * @param i_message
249: * @param i_detailedMessage
250: */
251: public void addErrorMessage(String i_message,
252: String i_detailedMessage) {
253:
254: addMessage(null, i_message, i_detailedMessage,
255: FacesMessage.SEVERITY_ERROR);
256: }
257:
258: /**
259: * Returns a action parameter value, from its name
260: */
261: protected Object getActionParameterValue(
262: final String parameterName, ActionEvent event) {
263:
264: for (Object uiObject : event.getComponent().getChildren()) {
265: if (uiObject instanceof UIParameter) {
266: final UIParameter param = (UIParameter) uiObject;
267: if (param.getName().equals(parameterName)) {
268: return param.getValue();
269: }
270: }
271: }
272: return null;
273: }
274:
275: /**
276: * Return the <code>FacesContext</code> instance for the current request.
277: */
278: protected FacesContext getFacesContext() {
279:
280: return FacesContext.getCurrentInstance();
281: }
282:
283: /**
284: * Returns Bean with specified name
285: */
286: protected Object getBean(String beanName) {
287:
288: /*
289: * WebApplicationContext appContext = FacesContext.getCurrentInstance()
290: * WebApplicationContextUtils.getWebApplicationContext(getHttpRequest().getSession().getServletContext());
291: * assert appContext != null; return appContext.getBean(beanName);
292: */
293:
294: return FacesContext.getCurrentInstance().getApplication()
295: .getVariableResolver().resolveVariable(
296: FacesContext.getCurrentInstance(), beanName);
297: }
298:
299: /**
300: * <p>
301: * Return the <code>Application</code> instance for the current web application.
302: * </p>
303: */
304: protected Application getApplication() {
305:
306: return FacesContext.getCurrentInstance().getApplication();
307: }
308:
309: /**
310: * <p>
311: * Evaluate the specified value binding expression, and return the value that it points at.
312: * </p>
313: *
314: * @param expr Value binding expression (including delimiters)
315: */
316: protected Object getValue(String expr) {
317:
318: ValueBinding vb = getApplication().createValueBinding(expr);
319: return (vb.getValue(getFacesContext()));
320: }
321:
322: /**
323: * @return
324: */
325: public boolean isAdmin() {
326:
327: return getCurrentUser().hasRole(SiteRole.ADMIN.getId());
328: }
329:
330: }
|