001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.jsp.JspWriter;
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionForward;
028: import org.apache.struts.action.DynaActionForm;
029: import com.methodhead.persistable.Persistable;
030: import com.methodhead.persistable.PersistableException;
031: import org.apache.commons.beanutils.DynaClass;
032: import org.apache.commons.beanutils.DynaProperty;
033: import org.apache.commons.beanutils.BasicDynaClass;
034: import java.io.IOException;
035: import com.methodhead.sitecontext.SiteContext;
036: import javax.servlet.RequestDispatcher;
037: import javax.servlet.ServletException;
038: import org.apache.commons.lang.exception.ExceptionUtils;
039:
040: /**
041: * A Shim module to manage HTML text.
042: * <ul>
043: * <li><tt>int page_id = 0</tt></li>
044: * <li><tt>String panel = ""</tt></li>
045: * <li><tt>String jsp = ""</tt></li>
046: * </ul>
047: */
048: public class IncludeModule extends Persistable implements Module {
049:
050: private static DynaClass dynaClass_ = null;
051:
052: static {
053: DynaProperty[] dynaProperties = new DynaProperty[] {
054: new DynaProperty("page_id", Integer.class),
055: new DynaProperty("panel", String.class),
056: new DynaProperty("jsp", String.class) };
057:
058: dynaClass_ = new BasicDynaClass("shim_include",
059: IncludeModule.class, dynaProperties);
060: }
061:
062: // constructors /////////////////////////////////////////////////////////////
063:
064: public IncludeModule() {
065: super (dynaClass_);
066: init();
067: }
068:
069: public IncludeModule(DynaClass dynaClass) {
070: super (dynaClass);
071: init();
072: }
073:
074: // constants ////////////////////////////////////////////////////////////////
075:
076: // classes //////////////////////////////////////////////////////////////////
077:
078: // methods //////////////////////////////////////////////////////////////////
079:
080: /**
081: * Initializes the module's fields.
082: */
083: private void init() {
084: setInt("page_id", 0);
085: setString("panel", "");
086: setString("jsp", "");
087: }
088:
089: /**
090: * Sets the module's <tt>page</tt> and <tt>panel</tt>. This method is called
091: * before any other operations are performed.
092: */
093: public void init(Page page, String panel) {
094:
095: if ((page == null) || (panel == null))
096: throw new ShimException("Page and/or panel is null.");
097:
098: if (page.getPanels().get(panel) == null)
099: throw new ShimException("Page has no panel \"" + panel
100: + "\"");
101:
102: setInt("page_id", page.getInt("id"));
103: setString("panel", panel);
104: }
105:
106: /**
107: * Loads the module for <tt>page</tt> and <tt>panel</tt> set by {@link
108: * #init init()}.
109: */
110: public void load() {
111: super .load("page_id=" + getInt("page_id") + " AND panel="
112: + getSqlLiteral(getString("panel")));
113: }
114:
115: /**
116: * Saves the module for <tt>page</tt> and <tt>panel</tt> set by {@link
117: * #init init()}. The module should already have been created with
118: * {@link #create create()}.
119: */
120: public void save() {
121: super .save("page_id=" + getInt("page_id") + " AND panel="
122: + getSqlLiteral(getString("panel")));
123: }
124:
125: /**
126: * Returns a short descriptive name for the module.
127: */
128: public String getName() {
129: return "Include";
130: }
131:
132: /**
133: * Returns <tt>true</tt> if the module has a configuration interface; that
134: * is, if {@link #configure} actually does something.
135: */
136: public boolean isConfigurable() {
137: return true;
138: }
139:
140: /**
141: * Creates the module. This method is called when a module is instantiated
142: * for a page and panel. The module should be initialized such that future
143: * calls to {@link #configure} and {@link #display} are successful.
144: */
145: public void create() {
146: //
147: // try to load first and create it only if it doesn't already exist
148: //
149: try {
150: load();
151: } catch (PersistableException e) {
152: setString("jsp", "");
153: saveNew();
154: }
155: }
156:
157: /**
158: * Manages the configuration interface for the module.
159: */
160: public ActionForward configure(ActionMapping mapping,
161: DynaActionForm form, HttpServletRequest request,
162: HttpServletResponse response) {
163:
164: return new ActionForward(
165: "/configureIncludeModuleForm.do?pageid="
166: + form.get("pageid") + "&panel="
167: + form.get("panel"));
168: }
169:
170: public boolean isEditable() {
171: return false;
172: }
173:
174: public void update(String text) {
175:
176: throw new ShimException("Method not implemented.");
177: }
178:
179: /**
180: * Displays the module.
181: */
182: public void display(HttpServletRequest request,
183: HttpServletResponse response, JspWriter out)
184: throws IOException {
185:
186: try {
187: load();
188:
189: SiteContext siteContext = SiteContext.getContext(request);
190:
191: out.flush();
192:
193: RequestDispatcher dispatcher = request.getSession()
194: .getServletContext().getRequestDispatcher(
195: "/WEB-INF/resources/"
196: + siteContext.getInt("id") + "/"
197: + getString("jsp"));
198:
199: dispatcher.include(request, response);
200: } catch (ServletException e) {
201: out.println(ExceptionUtils.getStackTrace(e));
202: }
203: }
204:
205: /**
206: * Destroys the module, freeing any resources associated with the module.
207: */
208: public void destroy() {
209: deleteAll(dynaClass_, "page_id=" + getInt("page_id")
210: + " AND panel=" + getSqlLiteral(getString("panel")));
211: }
212:
213: public void copyTo(Page page) {
214:
215: //
216: // load this module
217: //
218: load();
219:
220: //
221: // load the module to copy to
222: //
223: IncludeModule includeModule = new IncludeModule();
224: includeModule.init(page, getString("panel"));
225:
226: //
227: // already exists?
228: //
229: try {
230: includeModule.load();
231:
232: //
233: // update it
234: //
235: includeModule.setString("jsp", getString("jsp"));
236: includeModule.save();
237: } catch (PersistableException e) {
238:
239: //
240: // save new
241: //
242: includeModule.setString("jsp", getString("jsp"));
243: includeModule.saveNew();
244: }
245: }
246:
247: // properties ///////////////////////////////////////////////////////////////
248:
249: // attributes ///////////////////////////////////////////////////////////////
250: }
|