1 20 21 package com.methodhead.shim; 22 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.io.IOException ; 27 28 import com.methodhead.persistable.Persistable; 29 import com.methodhead.persistable.PersistableException; 30 31 import org.apache.commons.beanutils.DynaClass; 32 import org.apache.commons.beanutils.DynaProperty; 33 import org.apache.commons.beanutils.BasicDynaClass; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.jsp.JspWriter ; 37 import org.apache.struts.action.ActionMapping; 38 import org.apache.struts.action.DynaActionForm; 39 import org.apache.struts.action.ActionForward; 40 import javax.servlet.RequestDispatcher ; 41 import javax.servlet.ServletException ; 42 import com.methodhead.sitecontext.SiteContext; 43 import org.apache.commons.lang.StringUtils; 44 import org.apache.commons.lang.exception.ExceptionUtils; 45 46 66 public class NavModule 67 extends 68 Persistable 69 implements 70 Module { 71 72 protected static DynaClass dynaClass_ = null; 73 74 static { 75 DynaProperty[] dynaProperties = 76 new DynaProperty[] { 77 new DynaProperty( "sitecontext_id", Integer .class ), 78 new DynaProperty( "template", String .class ), 79 new DynaProperty( "panel", String .class ), 80 new DynaProperty( "hideroot", Boolean .class ), 81 new DynaProperty( "type", String .class ), 82 new DynaProperty( "separator", String .class ), 83 new DynaProperty( "jsp", String .class ), 84 new DynaProperty( "startlev", Integer .class ), 85 new DynaProperty( "contextlev", Integer .class ), 86 new DynaProperty( "depthlev", Integer .class ), 87 new DynaProperty( "toplev", Integer .class ), 88 new DynaProperty( "header", String .class ), 89 new DynaProperty( "link", String .class ), 90 new DynaProperty( "curlink", String .class ), 91 new DynaProperty( "footer", String .class ) 92 }; 93 94 dynaClass_ = 95 new BasicDynaClass( 96 "shim_nav", NavModule.class, dynaProperties ); 97 } 98 99 101 public NavModule() { 102 super( dynaClass_ ); 103 init(); 104 } 105 106 public NavModule( 107 DynaClass dynaClass ) { 108 super( dynaClass ); 109 init(); 110 } 111 112 114 public static final String TYPE_TEXTSEPARATED = "TEXTSEPARATED"; 115 public static final String TYPE_JSP = "JSP"; 116 public static final String TYPE_FOLDING = "FOLDING"; 117 118 public static final String DEFAULT_TYPE = TYPE_TEXTSEPARATED; 119 public static final String DEFAULT_SEPARATOR = " | "; 120 121 123 125 private void init() { 126 setInt( "sitecontext_id", 0 ); 127 setString( "template", "" ); 128 setString( "panel", "" ); 129 setBoolean( "hideroot", false ); 130 setString( "type", "" ); 131 setString( "separator", "" ); 132 setString( "jsp", "" ); 133 setInt( "startlev", 1 ); 134 setInt( "contextlev", 1 ); 135 setInt( "depthlev", 1 ); 136 setInt( "toplev", 1 ); 137 setString( "header", "" ); 138 setString( "link", "" ); 139 setString( "curlink", "" ); 140 setString( "footer", "" ); 141 } 142 143 147 public void load() { 148 load( 149 "sitecontext_id=" + getInt( "sitecontext_id" ) + 150 " AND template=" + getSqlLiteral( getString( "template" ) ) + 151 " AND panel=" + getSqlLiteral( getString( "panel" ) ) ); 152 } 153 154 158 public void save() { 159 save( 160 "sitecontext_id=" + getInt( "sitecontext_id" ) + 161 " AND template=" + getSqlLiteral( getString( "template" ) ) + 162 " AND panel=" + getSqlLiteral( getString( "panel" ) ) ); 163 } 164 165 public String getName() { 166 return "Navigation"; 167 } 168 169 public void init( 170 Page page, 171 String panel ) { 172 173 setInt( "sitecontext_id", page.getSiteContext().getInt( "id" ) ); 174 setString( "template", page.getString( "template" ) ); 175 setString( "panel", panel ); 176 177 siteContextPath_ = page.getSiteContext().getString( "path" ); 181 } 182 183 public void create() { 184 try { 188 load(); 189 } 190 catch ( PersistableException e ) { 191 setString( "type", DEFAULT_TYPE ); 195 setString( "separator", DEFAULT_SEPARATOR ); 196 saveNew(); 197 } 198 } 199 200 public void destroy() { 201 } 205 206 211 protected void renderTextSeparatedLink( 212 JspWriter out, 213 Link link, 214 Link cur, 215 boolean isEditMode ) 216 throws IOException { 217 218 String s = null; 219 220 if ( link.isRoot() ) { 221 if ( cur == link ) 222 s = getString( "curlink" ); 223 else 224 s = getString( "link" ); 225 } 226 else { 227 if ( cur.isNodeAncestor( link ) ) 228 s = getString( "curlink" ); 229 else 230 s = getString( "link" ); 231 } 232 233 String url = ShimUtils.getLinkUrl( link ); 234 235 if ( "".equals( s ) ) { 236 out.print( "<a HREF=\"" + url + "\">" + link.getTitle() + "</a>" ); 237 } 238 else { 239 s = StringUtils.replace( 240 s, FoldingNavRenderer.URL_KEY, url ); 241 s = StringUtils.replace( 242 s, FoldingNavRenderer.TITLE_KEY, link.getTitle() ); 243 } 244 245 out.print( s ); 246 } 247 248 public void display( 249 HttpServletRequest request, 250 HttpServletResponse response, 251 JspWriter out ) 252 throws 253 IOException { 254 255 load(); 259 260 Page curPage = ( Page )request.getAttribute( ShimGlobals.PAGE_KEY ); 264 SiteMap siteMap = ShimUtils.getSiteMap( request ); 265 Link root = ( Link )siteMap.getRoot(); 266 Link cur = siteMap.find( curPage.getInt( "id" ) ); 267 boolean isEditMode = 268 ShimGlobals.MODE_EDIT.equals( 269 request.getSession().getAttribute( ShimGlobals.MODE_KEY ) ); 270 271 if ( TYPE_TEXTSEPARATED.equals( getString( "type" ) ) ) { 275 try { 276 if ( root != null ) { 277 278 out.print( getString( "header" ) ); 279 280 boolean linkRendered = false; 285 286 if ( !getBoolean( "hideroot" ) && !root.getHidden() ) { 290 renderTextSeparatedLink( out, root, cur, isEditMode ); 291 linkRendered = true; 292 } 293 294 for ( int i = 0; i < root.getChildCount(); i++ ) { 298 Link link = ( Link )root.getChildAt( i ); 299 300 if ( !link.getHidden() ) { 301 302 if ( linkRendered ) { 303 out.print( getString( "separator" ) ); 304 } 305 306 renderTextSeparatedLink( out, link, cur, isEditMode ); 307 linkRendered = true; 308 } 309 } 310 311 out.print( getString( "footer" ) ); 312 } 313 } 314 catch ( IOException e ) { 315 throw new ShimException( "Unexpected IOException while printing nav." ); 316 } 317 } 318 319 else if ( TYPE_JSP.equals( getString( "type" ) ) ) { 323 try { 324 request.setAttribute( ShimGlobals.CURRENTLINK_KEY, cur ); 328 329 SiteContext siteContext = SiteContext.getContext( request ); 333 334 out.flush(); 335 336 RequestDispatcher dispatcher = 337 request.getSession().getServletContext().getRequestDispatcher( 338 "/WEB-INF/resources/" + siteContext.getInt( "id" ) + "/" + 339 getString( "jsp" ) ); 340 341 dispatcher.include( request, response ); 342 } 343 catch ( ServletException e ) { 344 out.println( ExceptionUtils.getStackTrace( e ) ); 345 } 346 } 347 348 else if ( TYPE_FOLDING.equals( getString( "type" ) ) ) { 352 try { 353 FoldingNavRenderer renderer = new FoldingNavRenderer(); 354 renderer.setRoot( root ); 355 renderer.setCur( cur ); 356 renderer.setStart( getInt( "startlev" ) ); 357 renderer.setContext( getInt( "contextlev" ) ); 358 renderer.setDepth( getInt( "depthlev" ) ); 359 renderer.setTop( getInt( "toplev" ) ); 360 renderer.setHeader( getString( "header" ) ); 361 renderer.setLink( getString( "link" ) ); 362 renderer.setCurLink( getString( "curlink" ) ); 363 renderer.setFooter( getString( "footer" ) ); 364 renderer.setIsEditMode( 365 ShimGlobals.MODE_EDIT.equals( 366 request.getSession().getAttribute( ShimGlobals.MODE_KEY ) ) ); 367 368 out.println( renderer.getNavHtml() ); 369 } 370 catch ( IOException e ) { 371 throw new ShimException( "Unexpected IOException while printing nav." ); 372 } 373 } 374 else { 375 throw new ShimException( 376 "Unexpected nav type \"" + getString( "type" ) + "\"." ); 377 } 378 } 379 380 public boolean isConfigurable() { 381 return true; 382 } 383 384 public ActionForward configure( 385 ActionMapping mapping, 386 DynaActionForm form, 387 HttpServletRequest request, 388 HttpServletResponse response ) { 389 390 return new ActionForward( 391 "/configureNavModuleForm.do?pageid=" + form.get( "pageid" ) + 392 "&panel=" + form.get( "panel" ) ); 393 } 394 395 public boolean isEditable() { 396 return false; 397 } 398 399 402 public void update( 403 String text ) { 404 } 405 406 public void copyTo( 407 Page page ) { 408 } 414 415 417 419 private String siteContextPath_ = null; 420 } 421 | Popular Tags |