001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Mar 3, 2003 / 10:55:19 AM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum;
044:
045: import java.io.IOException;
046:
047: import net.jforum.context.RequestContext;
048: import net.jforum.context.ResponseContext;
049: import net.jforum.exceptions.ForumException;
050: import net.jforum.exceptions.TemplateNotFoundException;
051: import net.jforum.repository.Tpl;
052: import net.jforum.util.preferences.ConfigKeys;
053: import net.jforum.util.preferences.SystemGlobals;
054: import net.jforum.util.preferences.TemplateKeys;
055: import freemarker.template.SimpleHash;
056: import freemarker.template.Template;
057:
058: /**
059: * <code>Command</code> Pattern implementation.
060: * All View Helper classes, which are intead to configure and processs
061: * presentation actions must extend this class.
062: *
063: * @author Rafael Steil
064: * @version $Id: Command.java,v 1.27 2007/07/28 14:17:11 rafaelsteil Exp $
065: */
066: public abstract class Command {
067: private static Class[] NO_ARGS_CLASS = new Class[0];
068: private static Object[] NO_ARGS_OBJECT = new Object[0];
069:
070: private boolean ignoreAction;
071:
072: protected String templateName;
073: protected RequestContext request;
074: protected ResponseContext response;
075: protected SimpleHash context;
076:
077: protected void setTemplateName(String templateName) {
078: this .templateName = Tpl.name(templateName);
079: }
080:
081: protected void ignoreAction() {
082: this .ignoreAction = true;
083: }
084:
085: /**
086: * Base method for listings.
087: * May be used as general listing or as helper
088: * to another specialized type of listing. Subclasses
089: * must implement it to the cases where some invalid
090: * action is called ( which means that the exception will
091: * be caught and the general listing will be used )
092: */
093: public abstract void list();
094:
095: /**
096: * Process and manipulate a requisition.
097: * @return <code>Template</code> reference
098: * @param request WebContextRequest
099: * @param response WebContextResponse
100: */
101: public Template process(RequestContext request,
102: ResponseContext response, SimpleHash context) {
103: this .request = request;
104: this .response = response;
105: this .context = context;
106:
107: String action = this .request.getAction();
108:
109: if (!this .ignoreAction) {
110: try {
111: this .getClass().getMethod(action, NO_ARGS_CLASS)
112: .invoke(this , NO_ARGS_OBJECT);
113: } catch (NoSuchMethodException e) {
114: this .list();
115: } catch (Exception e) {
116: throw new ForumException(e);
117: }
118: }
119:
120: if (JForumExecutionContext.getRedirectTo() != null) {
121: this .setTemplateName(TemplateKeys.EMPTY);
122: } else if (request.getAttribute("template") != null) {
123: this .setTemplateName((String) request
124: .getAttribute("template"));
125: }
126:
127: if (JForumExecutionContext.isCustomContent()) {
128: return null;
129: }
130:
131: if (this .templateName == null) {
132: throw new TemplateNotFoundException("Template for action "
133: + action + " is not defined");
134: }
135:
136: try {
137: return JForumExecutionContext.templateConfig().getTemplate(
138: new StringBuffer(SystemGlobals
139: .getValue(ConfigKeys.TEMPLATE_DIR)).append(
140: '/').append(this .templateName).toString());
141: } catch (IOException e) {
142: throw new ForumException(e);
143: }
144: }
145: }
|