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 com.methodhead.persistable.ConnectionSingleton; 32 import com.methodhead.aikp.IntKey; 33 import org.apache.commons.beanutils.DynaClass; 34 import org.apache.commons.beanutils.DynaProperty; 35 import org.apache.commons.beanutils.BasicDynaClass; 36 import org.apache.commons.lang.exception.ExceptionUtils; 37 import java.io.IOException ; 38 import java.util.List ; 39 import java.util.ArrayList ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 import java.util.regex.Pattern ; 43 import java.util.regex.Matcher ; 44 import org.apache.commons.lang.exception.ExceptionUtils; 45 import org.apache.log4j.Logger; 46 47 56 public class TextModule 57 extends 58 Persistable 59 implements 60 Module { 61 62 private static DynaClass dynaClass_ = null; 63 64 static { 65 DynaProperty[] dynaProperties = 66 new DynaProperty[] { 67 new DynaProperty( "page_id", Integer .class ), 68 new DynaProperty( "panel", String .class ), 69 new DynaProperty( "value", String .class ), 70 new DynaProperty( "htmlfragment_id", Integer .class ) 71 }; 72 73 dynaClass_ = 74 new BasicDynaClass( 75 "shim_text", TextModule.class, dynaProperties ); 76 } 77 78 80 public TextModule() { 81 super( dynaClass_ ); 82 init(); 83 } 84 85 public TextModule( 86 DynaClass dynaClass ) { 87 super( dynaClass ); 88 init(); 89 } 90 91 93 95 97 101 public void load() { 102 super.load( 103 "page_id=" + getInt( "page_id" ) + " AND panel=" + 104 getSqlLiteral( getString( "panel" ) ) ); 105 } 106 107 112 public void save() { 113 if ( getInt( "htmlfragment_id" ) == 0 ) 118 set( "htmlfragment_id", null ); 119 120 super.save( 121 "page_id=" + getInt( "page_id" ) + " AND panel=" + 122 getSqlLiteral( getString( "panel" ) ) ); 123 } 124 125 130 public void saveNew() { 131 if ( getInt( "htmlfragment_id" ) == 0 ) 136 set( "htmlfragment_id", null ); 137 138 super.saveNew(); 139 } 140 141 private void init() { 142 setInt( "page_id", 0 ); 143 setString( "panel", "" ); 144 setString( "value", "" ); 145 set( "htmlfragment_id", null ); 146 } 147 148 151 public String getName() { 152 return "Text"; 153 } 154 155 159 public boolean isConfigurable() { 160 return true; 161 } 162 163 167 public void init( 168 Page page, 169 String panel ) { 170 171 if ( ( page == null ) || ( panel == null ) ) 172 throw new ShimException( "Page and/or panel is null." ); 173 174 if ( page.getPanels().get( panel ) == null ) 175 throw new ShimException( "Page has no panel \"" + panel + "\"" ); 176 177 setInt( "page_id", page.getInt( "id" ) ); 178 setString( "panel", panel ); 179 180 if ( !"".equals( page.getSiteContext().getString( "path" ) ) ) 181 siteContextPath_ = page.getSiteContext().getString( "path" ); 182 } 183 184 189 public void create() { 190 try { 194 load(); 195 } 196 catch ( PersistableException e ) { 197 198 setString( "value", "Insert your text here..." ); 202 saveNew(); 203 } 204 } 205 206 209 public ActionForward configure( 210 ActionMapping mapping, 211 DynaActionForm form, 212 HttpServletRequest request, 213 HttpServletResponse response ) { 214 215 return new ActionForward( 216 "/configureTextModuleForm.do?pageid=" + form.get( "pageid" ) + 217 "&panel=" + form.get( "panel" ) ); 218 } 219 220 public boolean isEditable() { 221 return true; 222 } 223 224 public void update( 225 String text ) { 226 227 load( 228 "page_id=" + getInt( "page_id" ) + " AND panel=" + 229 getSqlLiteral( getString( "panel" ) ) ); 230 231 if ( getInt( "htmlfragment_id" ) != 0 ) { 235 HtmlFragment fragment = new HtmlFragment(); 236 fragment.load( new IntKey( getInt( "htmlfragment_id" ) ) ); 237 fragment.setString( "value", text ); 238 fragment.save(); 239 } 240 else { 241 setString( "value", text ); 242 save(); 243 } 244 } 245 246 249 public void display( 250 HttpServletRequest request, 251 HttpServletResponse response, 252 JspWriter out ) 253 throws 254 IOException { 255 256 load( 257 "page_id=" + getInt( "page_id" ) + " AND panel=" + 258 getSqlLiteral( getString( "panel" ) ) ); 259 260 if ( getInt( "htmlfragment_id" ) != 0 ) { 264 HtmlFragment fragment = new HtmlFragment(); 265 fragment.load( new IntKey( getInt( "htmlfragment_id" ) ) ); 266 out.println( fragment.getString( "value" ) ); 267 } 268 else { 269 out.println( getString( "value" ) ); 270 } 271 } 272 273 276 public void destroy() { 277 deleteAll( 278 dynaClass_, "page_id=" + getInt( "page_id" ) + " AND panel=" + 279 getSqlLiteral( getString( "panel" ) ) ); 280 } 281 282 public void copyTo( 283 Page page ) { 284 285 load(); 289 290 TextModule textModule = new TextModule(); 294 textModule.init( page, getString( "panel" ) ); 295 296 try { 300 textModule.load(); 301 302 textModule.setString( "value", getString( "value" ) ); 306 textModule.setInt( "htmlfragment_id", getInt( "htmlfragment_id" ) ); 307 textModule.save(); 308 } 309 catch ( PersistableException e ) { 310 311 textModule.setString( "value", getString( "value" ) ); 315 textModule.setInt( "htmlfragment_id", getInt( "htmlfragment_id" ) ); 316 textModule.saveNew(); 317 } 318 } 319 320 325 public List getDependentPages( 326 HtmlFragment fragment ) { 327 328 List pages = new ArrayList (); 329 330 String sql = 331 "SELECT " + 332 " shim_page.id AS id " + 333 "FROM " + 334 " shim_page " + 335 "LEFT JOIN " + 336 " shim_text ON " + 337 " shim_text.page_id=shim_page.id " + 338 "WHERE " + 339 " shim_text.htmlfragment_id=" + fragment.getInt( "id" ); 340 341 ResultSet rs = null; 342 try { 343 rs = ConnectionSingleton.runQuery( sql ); 344 345 if ( rs == null ) { 346 throw new SQLException ( "Null result set." ); 347 } 348 349 while ( rs.next() ) { 350 Page page = new Page(); 351 page.setSiteContext( fragment.getSiteContext() ); 352 page.load( new IntKey( rs.getInt( "id" ) ) ); 353 pages.add( page ); 354 } 355 } 356 catch ( SQLException e ) { 357 String msg = "Doing something. " + ExceptionUtils.getStackTrace( e ); 358 logger_.error( msg ); 359 throw new RuntimeException ( msg ); 360 } 361 finally { 362 ConnectionSingleton.close( rs ); 363 } 364 365 return pages; 366 } 367 368 370 372 private String siteContextPath_ = null; 373 374 private static Logger logger_ = Logger.getLogger( TextModule.class ); 375 } 376 | Popular Tags |