1 20 21 package com.methodhead.sitecontext; 22 23 import java.sql.SQLException ; 24 import java.sql.ResultSet ; 25 import javax.servlet.http.HttpServletRequest ; 26 27 import com.methodhead.persistable.ConnectionSingleton; 28 import com.methodhead.persistable.Persistable; 29 import com.methodhead.persistable.PersistableException; 30 import com.methodhead.persistable.Key; 31 import com.methodhead.MhfException; 32 import com.methodhead.event.Event; 33 import com.methodhead.aikp.IntKey; 34 import com.methodhead.aikp.AutoIntKeyPersistable; 35 36 import java.util.List ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.Collections ; 40 41 import org.apache.commons.beanutils.DynaClass; 42 import org.apache.commons.beanutils.DynaProperty; 43 import org.apache.commons.beanutils.BasicDynaClass; 44 import org.apache.log4j.Logger; 45 import org.apache.commons.lang.exception.ExceptionUtils; 46 47 48 55 public class SiteContext 56 extends 57 AutoIntKeyPersistable 58 implements 59 Comparable { 60 61 private static DynaClass dynaClass_ = null; 62 private static DynaClass domainDynaClass_ = null; 63 64 static { 65 DynaProperty[] dynaProperties = 66 new DynaProperty[] { 67 new DynaProperty( "id", Integer .class ), 68 new DynaProperty( "path", String .class ) 69 }; 70 71 dynaClass_ = 72 new BasicDynaClass( 73 "mh_sitecontext", SiteContext.class, dynaProperties ); 74 75 dynaProperties = 76 new DynaProperty[] { 77 new DynaProperty( "sitecontext_id", Integer .class ), 78 new DynaProperty( "name", String .class ), 79 new DynaProperty( "rank", Integer .class ) 80 }; 81 82 domainDynaClass_ = 83 new BasicDynaClass( 84 "mh_domain", Persistable.class, dynaProperties ); 85 } 86 87 89 public SiteContext() { 90 super( dynaClass_ ); 91 init(); 92 } 93 94 public SiteContext( DynaClass dynaClass ) { 95 super( dynaClass ); 96 init(); 97 } 98 99 101 public static final String SITECONTEXT_KEY = 102 "com.methodhead.sitecontext.SITECONTEXT_KEY"; 103 104 public static final String DEFAULTDOMAIN = 105 "DEFAULT"; 106 107 109 111 114 public String toString() { 115 if ( domains_.isEmpty() ) 116 return "SiteContext (no domains)"; 117 118 if ( "".equals( getString( "path" ) ) ) 119 return ( String )domains_.get( 0 ); 120 121 return domains_.get( 0 ) + "/" + getString( "path" ); 122 } 123 124 128 public boolean equals( 129 Object o ) { 130 131 if ( ( o != null ) && ( o instanceof SiteContext ) ) 132 return ( ( SiteContext )o ).getInt( "id" ) == getInt( "id" ); 133 134 return false; 135 } 136 137 140 public int hashCode() { 141 return getInt( "id" ); 142 } 143 144 148 public int compareTo( 149 Object o ) { 150 151 if ( !( o instanceof SiteContext ) ) 152 return -1; 153 154 SiteContext sc = ( SiteContext )o; 155 156 if ( getDomains().size() == 0 ) { 157 if ( sc.getDomains().size() > 0 ) 158 return -1; 159 else 160 return getString( "path" ).compareTo( sc.getString( "path" ) ); 161 } 162 else { 163 if ( sc.getDomains().size() == 0 ) 164 return 1; 165 else { 166 if ( getDomains().get( 0 ).equals( sc.getDomains().get( 0 ) ) ) 167 return getString( "path" ).compareTo( sc.getString( "path" ) ); 168 else 169 return 170 ( ( String )getDomains().get( 0 ) ).compareTo( 171 sc.getDomains().get( 0 ) ); 172 } 173 } 174 } 175 176 179 protected void init() { 180 setInt( "id", 0 ); 181 setString( "path", "" ); 182 } 183 184 187 private void deleteDomains() { 188 Persistable.deleteAll( 189 domainDynaClass_, "sitecontext_id=" + getInt( "id" ) ); 190 } 191 192 195 private void saveDomains() { 196 Persistable p = new Persistable( domainDynaClass_ ); 197 p.setInt( "sitecontext_id", getInt( "id" ) ); 198 199 int i = 0; 200 for ( Iterator iter = domains_.iterator(); iter.hasNext(); ) { 201 p.set( "name", iter.next() ); 202 p.setInt( "rank", i++ ); 203 p.saveNew(); 204 } 205 } 206 207 210 private void loadDomains() { 211 domains_.clear(); 212 213 List l = 214 Persistable.loadAll( 215 domainDynaClass_, "sitecontext_id=" + getInt( "id" ), "rank" ); 216 217 for ( Iterator iter = l.iterator(); iter.hasNext(); ) { 218 Persistable p = ( Persistable )iter.next(); 219 domains_.add( p.get( "name" ) ); 220 } 221 } 222 223 public void saveNew() { 224 super.saveNew(); 225 saveDomains(); 226 } 227 228 public void save() { 229 super.save(); 230 deleteDomains(); 231 saveDomains(); 232 } 233 234 public void load( 235 Key key ) { 236 super.load( key ); 237 loadDomains(); 238 } 239 240 245 public boolean loadForDomainAndPath( 246 String domain, 247 String path ) { 248 249 String sql = 250 "SELECT " + 251 " sc.id AS id " + 252 "FROM " + 253 " mh_sitecontext AS sc " + 254 "LEFT JOIN " + 255 " mh_domain AS d ON " + 256 " d.sitecontext_id = sc.id " + 257 "WHERE " + 258 " sc.path=" + getSqlLiteral( path ) + " AND " + 259 " d.name=" + getSqlLiteral( domain ) + " "; 260 261 ResultSet rs = null; 262 try { 263 rs = ConnectionSingleton.runQuery( sql ); 264 265 if ( !rs.next() ) { 266 return false; 267 } 268 269 load( new IntKey( rs.getInt( "id" ) ) ); 270 271 return true; 272 } 273 catch ( SQLException e ) { 274 String msg = "Loading SiteContext for domain \"" + domain + "\" and path \"" + path + "\". " + ExceptionUtils.getStackTrace( e ); 275 logger_.error( msg ); 276 throw new RuntimeException ( msg ); 277 } 278 finally { 279 ConnectionSingleton.close( rs ); 280 } 281 } 282 283 286 public void delete() { 287 deleteDomains(); 288 Event event = new Event(); 289 event.setSiteContext( this ); 290 event.deleteAll(); 291 super.delete(); 292 } 293 294 300 public static SiteContext getDefaultContext() { 301 SiteContext siteContext = new SiteContext(); 302 303 try { 304 siteContext.load( new IntKey( 0 ) ); 305 } 306 catch ( PersistableException e ) { 307 String sql = 312 "INSERT INTO " + 313 " mh_sitecontext " + 314 "( " + 315 " id, " + 316 " path ) " + 317 "VALUES ( " + 318 " 0, " + 319 " '' " + 320 ")"; 321 322 try { 323 ConnectionSingleton.runUpdate( sql ); 324 } 325 catch ( SQLException se ) { 326 throw new PersistableException( 327 "Unexpected SQLException: " + se.getMessage() ); 328 } 329 330 siteContext.getDomains().add( DEFAULTDOMAIN ); 331 siteContext.save(); 332 } 333 334 return siteContext; 335 } 336 337 340 public static void setContext( 341 HttpServletRequest request, 342 SiteContext context ) { 343 344 request.getSession().setAttribute( SITECONTEXT_KEY, context ); 345 } 346 347 351 public static SiteContext getContext( 352 HttpServletRequest request ) { 353 354 SiteContext context = ( SiteContext )request.getSession().getAttribute( SITECONTEXT_KEY ); 355 356 if ( context == null ) { 357 context = ( SiteContext )request.getAttribute( SITECONTEXT_KEY ); 358 } 359 360 return context; 361 } 362 363 367 public static List loadAll() { 368 List list = loadAll( dynaClass_, null, null ); 369 370 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 371 SiteContext sc = ( SiteContext )iter.next(); 372 sc.load( new IntKey( sc.getInt( "id" ) ) ); 373 } 374 375 Collections.sort( list ); 376 377 return list; 378 } 379 380 382 385 public List getDomains() { 386 return domains_; 387 } 388 389 391 protected List domains_ = new ArrayList (); 392 393 private static Logger logger_ = Logger.getLogger( SiteContext.class ); 394 } 395 | Popular Tags |