1 package de.webman.generator; 2 3 import java.io.*; 4 import com.teamkonzept.web.*; 5 import com.teamkonzept.lib.*; 6 import com.teamkonzept.webman.mainint.events.*; 7 import org.apache.log4j.Category; 8 9 13 public class Preview implements UserCodes 14 { 15 16 private static Category cat = Category.getInstance(Preview.class.getName()); 17 private static final int NO_PRIMARY_CONTENT = -2; 18 19 private TKEvent evt; 20 private TKHttpInterface httpInterface; 21 private TKParams params; 22 23 private String ownUrl; 24 private String docRoot; 25 private PrintStream out; 26 27 public Preview( String ownUrl, String docRoot, TKParams params, PrintStream out ) 28 { 29 this.evt = null; 30 this.httpInterface = null; 31 this.params = params; 32 33 this.ownUrl = ownUrl; 34 this.docRoot = docRoot; 35 36 this.out = out; 37 } 38 39 public Preview( TKEvent _evt ) 40 { 41 this.evt = _evt; 42 this.httpInterface = evt.getHttpInterface(); 43 this.params = evt.getParams(); 44 45 this.ownUrl = httpInterface.getOwnURL(); 46 this.docRoot = httpInterface.getDocumentRoot(); 47 48 this.out = evt.getOutputStream(); 49 } 50 51 public TKHTMLTemplate getPrepHTMLTemplate( String name ) throws Throwable 52 { 53 TKHTMLTemplate t = new TKHTMLTemplate( "file:" + this.docRoot + File.separator + TemplateUtils.getWebmanTemplateDirectory() + File.separator +name ); 54 55 if (evt != null) evt.prepTemplate(t); 56 else t.set("SELF", this.ownUrl); 57 58 return t; 59 } 60 61 public void finishTemplate( TKHTMLTemplate t ) throws Throwable 62 { 63 if (evt != null) evt.finishTemplate(t); 64 else 65 { 66 t.doTagSubstitution(); 67 t.printTemplate(out); 68 } 69 } 70 71 public boolean doPreviewIndexPerContent() throws Throwable 72 { 73 String instanceIdStr = params.get("PAR","INSTANCE_ID"); 74 String levelStr = params.get("PAR","LEVEL"); 75 String target = params.get("PAR","TARGET"); 76 String showIndexStr = params.get("PAR","INDEX"); 77 78 if ((target == null) || (target.equals(""))) target = "_blank"; 79 80 int instanceId = instanceIdStr != null ? Integer.parseInt (instanceIdStr) : -1; 81 int level = levelStr != null ? Integer.parseInt (levelStr) : -1; 82 83 if (showIndexStr != null) showIndexStr = showIndexStr.trim(); 84 boolean showIndex = showIndexStr != null && !showIndexStr.equalsIgnoreCase ("FALSE"); 85 86 if (instanceId < 0) 87 throw new Error ("Falsche Instance-Id: "+(instanceIdStr != null ? instanceIdStr : "NULL")); 88 if (level < 0) 89 throw new Error ("Falscher Level: "+(levelStr != null ? levelStr : "NULL")); 90 91 return doPreviewIndex (instanceId, level, target, showIndex); 92 } 93 94 public boolean doPreviewIndex(int instanceId, int level, String target, 95 boolean showIndex) throws Throwable , TKException 96 { 97 GeneratorContext context = GeneratorContext.setup (); 98 GeneratorContext.setPreviewMode(true); 99 GeneratorContext.setup().setWorkflowIgnored(true); 100 try 101 { 102 cat.debug("start index of Content-Instance " + instanceId+" with level "+level); 103 104 if (level == 0) context.siteNodes.setIndexBuild (!GeneratorContext.isShared()); 110 111 SiteNode tree = null; 112 113 synchronized (context) 114 { 115 tree = context.siteNodes.getRoot(); 116 if (tree == null) 117 { 118 tree = context.siteNodes.buildSiteTree(); 119 if (GeneratorContext.isShared()) context.siteNodes.setRoot(tree); 121 cat.debug("finished buildSiteTree"); 122 context.contentForms.prepareFormHash(); 123 context.siteContents.resetCounters(); 124 context.contentCalls.setup(); 125 } 126 } 127 128 SiteContent sc = null; 129 130 sc = (SiteContent) context.siteContents.getContent(instanceId); 131 if (sc == null) throw new TKUserException("Content nicht im Sitetree referenziert", CONTENT_NOT_REFERENCED, USER_SEVERITY, false, null); 133 134 TKVector list = context.contentCalls.getCalls(new Integer (instanceId)); 135 if (list == null) 136 { 137 throw new TKUserException("Content nicht im Sitetree referenziert", CONTENT_NOT_REFERENCED, USER_SEVERITY, false, null); 139 } 140 141 if ((list.size() == 1) && !showIndex) return doImmedeatePreview (tree, (ContentCall) list.get(0)); 142 143 cat.debug("finished index"); 144 145 TKHTMLTemplate calls = getPrepHTMLTemplate( "ce_previewIndex.tmpl" ); 146 ContentCallsIterator iterator = new ContentCallsIterator (list,null,"CE_CALLS_LIST"); 147 148 calls.set( "RTARGET", target ); 149 150 calls.setListIterator( iterator ); 151 finishTemplate(calls); 152 153 } catch (Throwable th) 154 { 155 throw th; 156 } 157 finally 158 { 159 context.cleanup(); 160 } 161 return true; 162 } 163 164 public boolean doPreviewIndexPerSiteNode() throws Throwable 165 { 166 String siteNodeIdStr = params.get("PAR","SNODE"); 167 String shortName = params.get("PAR","DOCNAME"); 168 String target = params.get("PAR","TARGET"); 169 String showIndexStr = params.get("PAR","INDEX"); 170 171 if ((target == null) || (target.equals(""))) target = "_blank"; 172 173 int siteNodeId = siteNodeIdStr != null ? Integer.parseInt (siteNodeIdStr) : -1; 174 175 if (showIndexStr != null) showIndexStr = showIndexStr.trim(); 176 boolean showIndex = showIndexStr != null && !showIndexStr.equalsIgnoreCase ("FALSE"); 177 178 if (siteNodeId < 0) 179 throw new Error ("Falsche SiteNode-Id: "+(siteNodeIdStr != null ? siteNodeIdStr : "NULL")); 180 if (shortName == null) 181 throw new Error ("Document-ShortName fehlt"); 182 183 return doPreviewIndexPerSiteNode (siteNodeId,shortName,target,showIndex); 184 } 185 186 public boolean doPreviewIndexPerSiteNode(int siteNodeId, String shortName, 187 String target, boolean showIndex) throws Throwable 188 { 189 190 GeneratorContext context = GeneratorContext.setup (); 191 GeneratorContext.setPreviewMode(true); 192 GeneratorContext.setup().setWorkflowIgnored(true); 193 194 try 195 { 196 cat.debug("start index of "+"SiteNode " + siteNodeId); 197 198 context.siteNodes.setIndexBuild(!GeneratorContext.isShared()); 204 205 SiteNode tree = null; 206 207 synchronized (context) 208 { 209 tree = context.siteNodes.getRoot(); 210 if (tree == null) 211 { 212 tree = context.siteNodes.buildSiteTree(); 213 if (GeneratorContext.isShared()) context.siteNodes.setRoot( tree); 215 context.contentForms.prepareFormHash(); 216 context.siteContents.resetCounters(); 217 context.contentCalls.setup(); 218 } 219 } 220 221 SiteNode sn = context.siteNodes.getNode (siteNodeId); 222 if (sn == null) throw new Error ("SiteNode "+siteNodeId+" not there"); 223 224 TKVector primContents = null; 225 if (sn.getType() == SiteNode.NODE_TYPE_GROUP) 226 { 227 if( sn.getPrimaryContentSelectionKey() != null ) 228 primContents = context.integrations.getSelection( sn.getPrimaryContentSelectionKey() ); 229 else primContents = sn.getPrimaryContentNode().getContents(); 230 231 int size = primContents.size(); 232 if (size == 0) throw new Error ("Group-SiteNode "+siteNodeId+" leer"); 233 234 } 235 236 if ((primContents == null) || (primContents.size() == 1) && !showIndex) 237 return doImmedeatePreviewPerSiteNode (tree, sn, shortName, 238 (primContents == null ? null : (SiteContent) primContents.elementAt(0))); 239 240 TKHTMLTemplate index = getPrepHTMLTemplate( "st_previewIndex.tmpl" ); 241 242 index.set( "RTARGET", target ); 243 index.set( "PATH", sn.getPath() ); 244 index.set( "SNODE", new Integer (siteNodeId) ); 245 index.set( "DOCNAME", shortName ); 246 247 if (primContents != null) 248 { 249 250 index.set ( "PRIMARY_ID",new Integer (sn.getPrimaryContentNode().getId())); 251 index.set ( "CONTENT_NAME",sn.getPrimaryContentNode().getShortName()); 252 253 PrimContentsIterator iterator = 254 new PrimContentsIterator (primContents,null,"ST_PRIMARY_CONTENTS"); 255 index.setListIterator( iterator ); 256 } 257 258 finishTemplate(index); 259 260 } catch (Throwable th) 261 { 262 throw th; 263 } 264 finally 265 { 266 context.cleanup(); 267 } 268 return true; 269 } 270 271 public void generatePreview (GeneratorContext context, SiteNode sn, 272 String shortName, SiteContent primContent) throws Exception 273 { 274 GenNode gn = null; 275 SiteDocument sd = null; 276 GenDocument gd = null; 277 278 synchronized (context) 279 { 280 gn = sn.makeGenNode (primContent); 281 if (gn == null) throw new Error ("making genNode from SiteNode "+sn.getId()+" failed"); 282 283 sd = sn.getDocument(shortName); 284 if (sd == null) throw new Error ("getting Document for SiteNode "+sn.getId()+" / Doc "+shortName+" failed"); 285 286 cat.debug("generatePreview of "+sd.path()); 287 sd.complete(); 288 289 gd = sd.getGenDocument( primContent, gn ); 290 291 if (gd == null) throw new Error ("making genDocument for SiteNode "+sn.getId()+" / Doc "+shortName+" failed"); 292 293 gd.makeReferences (ownUrl,primContent); 294 } 295 296 gd.generatePreview (out,docRoot, 297 "file:" + docRoot + File.separator + TemplateUtils.getGenerationDirectory(), ownUrl); 298 } 299 300 public boolean doPreview() throws Throwable 301 { 302 String siteNodeIdStr = params.get("PAR","SNODE"); 303 String primaryIdStr = params.get("PAR","PID"); 304 String shortName = params.get("PAR","DOCNAME"); 305 306 int siteNodeId = siteNodeIdStr != null ? Integer.parseInt (siteNodeIdStr) : -1; 307 int primaryId = primaryIdStr != null ? Integer.parseInt (primaryIdStr) : NO_PRIMARY_CONTENT; 308 309 if (siteNodeId < 0) 310 throw new Error ("Falsche SiteNode-Id: "+(siteNodeIdStr != null ? siteNodeIdStr : "NULL")); 311 if (primaryId < -1) 312 throw new Error ("Falsche Primary-Id: "+(primaryIdStr != null ? primaryIdStr : "NULL")); 313 if (shortName == null) 314 throw new Error ("Document-ShortName fehlt"); 315 GeneratorContext context = GeneratorContext.setup (); 316 GeneratorContext.setPreviewMode(true); 317 GeneratorContext.setup().setWorkflowIgnored(true); 318 319 try 320 { 321 cat.debug(" start viewing of sn="+siteNodeIdStr+" pid="+primaryIdStr+" dn="+shortName); 322 323 context.siteNodes.setReducedBuild(true); 324 SiteNode tree = null; 325 326 synchronized (context) 327 { 328 tree = context.siteNodes.getRoot(); 329 if (tree == null) 330 { 331 tree = context.siteNodes.buildSiteTree(); 332 334 if (GeneratorContext.isShared()) context.siteNodes.setRoot( tree); 335 context.contentForms.prepareFormHash(); 336 context.siteContents.resetCounters(); 337 } 338 } 339 SiteNode sn = context.siteNodes.getNode (siteNodeId); 340 if (sn == null) throw new Error ("SiteNode "+siteNodeId+" not there"); 341 342 SiteContent primContent = primaryId == -1 ? null : 343 (SiteContent) context.siteContents.getContent(primaryId); 344 345 if ((primContent == null) && (primaryId >= 0)) throw new Error ("Primary Content "+primaryId+" not there"); 346 347 generatePreview (context,sn,shortName,primContent); 348 349 } catch( Throwable th ) 350 { 351 throw th; 352 } 353 finally 354 { 355 context.cleanup(); 356 } 357 return true; 358 } 359 360 public boolean doImmedeatePreview(SiteNode tree, ContentCall cc) throws Throwable 361 { 362 if (cc == null) return false; 363 364 SiteDocument sd = cc.getDocument(); 365 if (sd == null) return false; 366 367 SiteNode sn = sd.getAnchor(); 368 if (sn == null) return false; 369 370 int siteNodeId = sn.getId(); 371 int primaryId = cc.getPrimaryId(); 372 String shortName = sd.getShortName(); 373 374 GeneratorContext context = GeneratorContext.setup (); 375 GeneratorContext.setPreviewMode(true); 376 GeneratorContext.setup().setWorkflowIgnored(true); 377 378 try 379 { 380 cat.debug("start viewing of sn="+siteNodeId+" pid="+primaryId+" dn="+shortName); 381 context.siteNodes.setReducedBuild (context.siteNodes.isIndexBuild()); 382 synchronized (context) 383 { 384 if (context.siteNodes.getRoot() == null) 385 { 386 if (GeneratorContext.isShared()) context.siteNodes.setRoot( tree); 387 context.siteNodes.completeSiteTree(tree); 388 context.contentForms.prepareFormHash(); 389 context.siteContents.resetCounters(); 390 } 391 } 392 393 SiteContent primContent = primaryId == -1 ? null : 394 (SiteContent) context.siteContents.getContent(primaryId); 395 396 if ((primContent == null) && (primaryId >= 0)) throw new Error ("Primary Content "+primaryId+"not there"); 397 398 GenNode gn = null; 399 GenDocument gd = null; 400 401 synchronized (context) 402 { 403 404 gn = sn.makeGenNode (primContent); 405 if (gn == null) throw new Error ("making genNode from SiteNode "+siteNodeId+" failed"); 406 407 sd.complete(); 408 gd = sd.getGenDocument( primContent, gn ); 409 410 if (gd == null) throw new Error ("making genDocument for SiteNode "+siteNodeId+" / Doc "+shortName+" failed"); 411 gd.makeReferences (ownUrl,primContent); 412 } 413 gd.generatePreview (out,docRoot, 414 "file:" + docRoot + File.separator + TemplateUtils.getGenerationDirectory(),ownUrl); 415 } catch( Throwable th ) 416 { 417 throw th; 418 } 419 finally 420 { 421 context.cleanup(); 422 } 423 return true; 424 } 425 426 public boolean doImmedeatePreviewPerSiteNode (SiteNode tree, SiteNode sn, 427 String shortName, SiteContent primContent) throws Throwable 428 { 429 if (sn == null) return false; 430 431 int siteNodeId = sn.getId(); 432 int primaryId = primContent == null ? -1 : primContent.getContentId(); 433 434 GeneratorContext context = GeneratorContext.setup (); 435 GeneratorContext.setPreviewMode(true); 436 GeneratorContext.setup().setWorkflowIgnored(true); 437 438 try 439 { 440 cat.debug("start viewing of sn="+ 441 siteNodeId+" pid="+primaryId+" dn="+shortName); 442 443 context.siteNodes.setReducedBuild(context.siteNodes.isIndexBuild()); 444 445 synchronized (context) 446 { 447 if (context.siteNodes.getRoot() == null) 448 { 449 if (GeneratorContext.isShared()) context.siteNodes.setRoot( tree); 450 context.siteNodes.completeSiteTree(tree); 451 452 context.contentForms.prepareFormHash(); 453 context.siteContents.resetCounters(); 454 } 455 } 456 457 generatePreview (context,sn,shortName,primContent); 458 459 } catch( Throwable th ) 460 { 461 throw th; 462 } 463 finally 464 { 465 context.cleanup(); 466 } 467 return true; 468 } 469 } 470 | Popular Tags |