1 20 21 package com.methodhead.shim; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.jsp.JspWriter ; 26 import org.apache.struts.action.ActionMapping; 27 import org.apache.struts.action.ActionForward; 28 import org.apache.struts.action.DynaActionForm; 29 import com.methodhead.persistable.Persistable; 30 import com.methodhead.persistable.PersistableException; 31 import org.apache.commons.beanutils.DynaClass; 32 import org.apache.commons.beanutils.DynaProperty; 33 import org.apache.commons.beanutils.BasicDynaClass; 34 import java.io.IOException ; 35 import com.methodhead.sitecontext.SiteContext; 36 import javax.servlet.RequestDispatcher ; 37 import javax.servlet.ServletException ; 38 import org.apache.commons.lang.exception.ExceptionUtils; 39 40 48 public class IncludeModule 49 extends 50 Persistable 51 implements 52 Module { 53 54 private static DynaClass dynaClass_ = null; 55 56 static { 57 DynaProperty[] dynaProperties = 58 new DynaProperty[] { 59 new DynaProperty( "page_id", Integer .class ), 60 new DynaProperty( "panel", String .class ), 61 new DynaProperty( "jsp", String .class ) 62 }; 63 64 dynaClass_ = 65 new BasicDynaClass( 66 "shim_include", IncludeModule.class, dynaProperties ); 67 } 68 69 71 public IncludeModule() { 72 super( dynaClass_ ); 73 init(); 74 } 75 76 public IncludeModule( 77 DynaClass dynaClass ) { 78 super( dynaClass ); 79 init(); 80 } 81 82 84 86 88 91 private void init() { 92 setInt( "page_id", 0 ); 93 setString( "panel", "" ); 94 setString( "jsp", "" ); 95 } 96 97 101 public void init( 102 Page page, 103 String panel ) { 104 105 if ( ( page == null ) || ( panel == null ) ) 106 throw new ShimException( "Page and/or panel is null." ); 107 108 if ( page.getPanels().get( panel ) == null ) 109 throw new ShimException( "Page has no panel \"" + panel + "\"" ); 110 111 setInt( "page_id", page.getInt( "id" ) ); 112 setString( "panel", panel ); 113 } 114 115 119 public void load() { 120 super.load( 121 "page_id=" + getInt( "page_id" ) + " AND panel=" + 122 getSqlLiteral( getString( "panel" ) ) ); 123 } 124 125 130 public void save() { 131 super.save( 132 "page_id=" + getInt( "page_id" ) + " AND panel=" + 133 getSqlLiteral( getString( "panel" ) ) ); 134 } 135 136 139 public String getName() { 140 return "Include"; 141 } 142 143 147 public boolean isConfigurable() { 148 return true; 149 } 150 151 156 public void create() { 157 try { 161 load(); 162 } 163 catch ( PersistableException e ) { 164 setString( "jsp", "" ); 165 saveNew(); 166 } 167 } 168 169 172 public ActionForward configure( 173 ActionMapping mapping, 174 DynaActionForm form, 175 HttpServletRequest request, 176 HttpServletResponse response ) { 177 178 return new ActionForward( 179 "/configureIncludeModuleForm.do?pageid=" + form.get( "pageid" ) + 180 "&panel=" + form.get( "panel" ) ); 181 } 182 183 public boolean isEditable() { 184 return false; 185 } 186 187 public void update( 188 String text ) { 189 190 throw new ShimException( "Method not implemented." ); 191 } 192 193 196 public void display( 197 HttpServletRequest request, 198 HttpServletResponse response, 199 JspWriter out ) 200 throws 201 IOException { 202 203 try { 204 load(); 205 206 SiteContext siteContext = SiteContext.getContext( request ); 207 208 out.flush(); 209 210 RequestDispatcher dispatcher = 211 request.getSession().getServletContext().getRequestDispatcher( 212 "/WEB-INF/resources/" + siteContext.getInt( "id" ) + "/" + 213 getString( "jsp" ) ); 214 215 dispatcher.include( request, response ); 216 } 217 catch ( ServletException e ) { 218 out.println( ExceptionUtils.getStackTrace( e ) ); 219 } 220 } 221 222 225 public void destroy() { 226 deleteAll( 227 dynaClass_, "page_id=" + getInt( "page_id" ) + " AND panel=" + 228 getSqlLiteral( getString( "panel" ) ) ); 229 } 230 231 public void copyTo( 232 Page page ) { 233 234 load(); 238 239 IncludeModule includeModule = new IncludeModule(); 243 includeModule.init( page, getString( "panel" ) ); 244 245 try { 249 includeModule.load(); 250 251 includeModule.setString( "jsp", getString( "jsp" ) ); 255 includeModule.save(); 256 } 257 catch ( PersistableException e ) { 258 259 includeModule.setString( "jsp", getString( "jsp" ) ); 263 includeModule.saveNew(); 264 } 265 } 266 267 269 } 271 | Popular Tags |