KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > Command


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Mar 3, 2003 / 10:55:19 AM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum;
44
45
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47
48 import net.jforum.exceptions.TemplateNotFoundException;
49 import net.jforum.repository.Tpl;
50 import net.jforum.util.preferences.ConfigKeys;
51 import net.jforum.util.preferences.SystemGlobals;
52 import net.jforum.util.preferences.TemplateKeys;
53 import freemarker.template.SimpleHash;
54 import freemarker.template.Template;
55
56 /**
57  * <code>Command</code> Pattern implementation.
58  * All View Helper classes, which are intead to configure and processs
59  * presentation actions must extend this class.
60  *
61  * @author Rafael Steil
62  * @version $Id: Command.java,v 1.19 2006/01/29 15:06:59 rafaelsteil Exp $
63  */

64 public abstract class Command
65 {
66     private static Class JavaDoc[] NO_ARGS_CLASS = new Class JavaDoc[0];
67     private static Object JavaDoc[] NO_ARGS_OBJECT = new Object JavaDoc[0];
68     
69     private boolean ignoreAction;
70     
71     protected String JavaDoc templateName;
72     protected ActionServletRequest request;
73     protected HttpServletResponse JavaDoc response;
74     protected SimpleHash context;
75     
76     protected void setTemplateName(String JavaDoc templateName)
77     {
78         this.templateName = Tpl.name(templateName);
79     }
80     
81     protected void ignoreAction()
82     {
83         this.ignoreAction = true;
84     }
85     
86     /**
87      * Base method for listings.
88      * May be used as general listing or as helper
89      * to another specialized type of listing. Subclasses
90      * must implement it to the cases where some invalid
91      * action is called ( which means that the exception will
92      * be caught and the general listing will be used )
93      *
94      * @throws Exception
95      */

96     public abstract void list() throws Exception JavaDoc;
97     
98     /**
99      * Process and manipulate a requisition.
100      * @param context TODO
101      * @throws Exception
102      * @return <code>Template</code> reference
103      */

104     public Template process(ActionServletRequest request,
105             HttpServletResponse JavaDoc response,
106             SimpleHash context) throws Exception JavaDoc
107     {
108         this.request = request;
109         this.response = response;
110         this.context = context;
111         
112         String JavaDoc action = this.request.getAction();
113
114         if (!this.ignoreAction) {
115             try {
116                 this.getClass().getMethod(action, NO_ARGS_CLASS).invoke(this, NO_ARGS_OBJECT);
117             }
118             catch (NoSuchMethodException JavaDoc e) {
119                 this.list();
120             }
121             catch (Exception JavaDoc e) {
122                 throw e;
123             }
124         }
125         
126         if (JForumExecutionContext.getRedirectTo() != null) {
127             this.setTemplateName(TemplateKeys.EMPTY);
128         }
129         else if (request.getAttribute("template") != null) {
130             this.setTemplateName((String JavaDoc)request.getAttribute("template"));
131         }
132         
133         if (JForumExecutionContext.isCustomContent()) {
134             return null;
135         }
136         
137         if (this.templateName == null) {
138             throw new TemplateNotFoundException("Template for action " + action + " is not defined");
139         }
140         
141         return JForumExecutionContext.templateConfig().getTemplate(
142                 new StringBuffer JavaDoc(SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR)).
143                 append('/').append(this.templateName).toString());
144     }
145 }
146
Popular Tags