001: package de.webman.generator;
002:
003: import java.io.*;
004: import com.teamkonzept.web.*;
005: import com.teamkonzept.lib.*;
006: import com.teamkonzept.webman.mainint.events.*;
007: import org.apache.log4j.Category;
008:
009: /**
010: * @author $Author: alex $
011: * @version $Revision: 1.7 $
012: */
013: public class Preview implements UserCodes {
014:
015: private static Category cat = Category.getInstance(Preview.class
016: .getName());
017: private static final int NO_PRIMARY_CONTENT = -2;
018:
019: private TKEvent evt;
020: private TKHttpInterface httpInterface;
021: private TKParams params;
022:
023: private String ownUrl;
024: private String docRoot;
025: private PrintStream out;
026:
027: public Preview(String ownUrl, String docRoot, TKParams params,
028: PrintStream out) {
029: this .evt = null;
030: this .httpInterface = null;
031: this .params = params;
032:
033: this .ownUrl = ownUrl;
034: this .docRoot = docRoot;
035:
036: this .out = out;
037: }
038:
039: public Preview(TKEvent _evt) {
040: this .evt = _evt;
041: this .httpInterface = evt.getHttpInterface();
042: this .params = evt.getParams();
043:
044: this .ownUrl = httpInterface.getOwnURL();
045: this .docRoot = httpInterface.getDocumentRoot();
046:
047: this .out = evt.getOutputStream();
048: }
049:
050: public TKHTMLTemplate getPrepHTMLTemplate(String name)
051: throws Throwable {
052: TKHTMLTemplate t = new TKHTMLTemplate("file:" + this .docRoot
053: + File.separator
054: + TemplateUtils.getWebmanTemplateDirectory()
055: + File.separator + name);
056:
057: if (evt != null)
058: evt.prepTemplate(t);
059: else
060: t.set("SELF", this .ownUrl);
061:
062: return t;
063: }
064:
065: public void finishTemplate(TKHTMLTemplate t) throws Throwable {
066: if (evt != null)
067: evt.finishTemplate(t);
068: else {
069: t.doTagSubstitution();
070: t.printTemplate(out);
071: }
072: }
073:
074: public boolean doPreviewIndexPerContent() throws Throwable {
075: String instanceIdStr = params.get("PAR", "INSTANCE_ID");
076: String levelStr = params.get("PAR", "LEVEL");
077: String target = params.get("PAR", "TARGET");
078: String showIndexStr = params.get("PAR", "INDEX");
079:
080: if ((target == null) || (target.equals("")))
081: target = "_blank";
082:
083: int instanceId = instanceIdStr != null ? Integer
084: .parseInt(instanceIdStr) : -1;
085: int level = levelStr != null ? Integer.parseInt(levelStr) : -1;
086:
087: if (showIndexStr != null)
088: showIndexStr = showIndexStr.trim();
089: boolean showIndex = showIndexStr != null
090: && !showIndexStr.equalsIgnoreCase("FALSE");
091:
092: if (instanceId < 0)
093: throw new Error("Falsche Instance-Id: "
094: + (instanceIdStr != null ? instanceIdStr : "NULL"));
095: if (level < 0)
096: throw new Error("Falscher Level: "
097: + (levelStr != null ? levelStr : "NULL"));
098:
099: return doPreviewIndex(instanceId, level, target, showIndex);
100: }
101:
102: public boolean doPreviewIndex(int instanceId, int level,
103: String target, boolean showIndex) throws Throwable,
104: TKException {
105: GeneratorContext context = GeneratorContext.setup();
106: GeneratorContext.setPreviewMode(true);
107: GeneratorContext.setup().setWorkflowIgnored(true);
108: try {
109: cat.debug("start index of Content-Instance " + instanceId
110: + " with level " + level);
111:
112: // Achtung, das hier ist bei shared Preview NICHT thread-safe, falls mit
113: // unterschiedlichen leveln gearbeitet wird !!!
114: //
115: // Auf jeden Fall darf im shared-Modus kein indexBuild erfolgen, sonst bleibt
116: // die Site-Struktur im Fehlerfall unvollständig !!!
117: if (level == 0)
118: context.siteNodes.setIndexBuild(!GeneratorContext
119: .isShared());
120:
121: SiteNode tree = null;
122:
123: synchronized (context) {
124: tree = context.siteNodes.getRoot();
125: if (tree == null) {
126: tree = context.siteNodes.buildSiteTree();
127: //WebManGen.DumpSiteTree(tree, "previewSiteTree1.txt");//frank
128: if (GeneratorContext.isShared())
129: context.siteNodes.setRoot(tree);
130: cat.debug("finished buildSiteTree");
131: context.contentForms.prepareFormHash();
132: context.siteContents.resetCounters();
133: context.contentCalls.setup();
134: }
135: }
136:
137: SiteContent sc = null;
138:
139: sc = (SiteContent) context.siteContents
140: .getContent(instanceId);
141: if (sc == null) // kann eigentlich nicht mehr passieren !
142: throw new TKUserException(
143: "Content nicht im Sitetree referenziert",
144: CONTENT_NOT_REFERENCED, USER_SEVERITY, false,
145: null);
146:
147: TKVector list = context.contentCalls.getCalls(new Integer(
148: instanceId));
149: if (list == null) {
150: // ersetzt die obige Exception...
151: throw new TKUserException(
152: "Content nicht im Sitetree referenziert",
153: CONTENT_NOT_REFERENCED, USER_SEVERITY, false,
154: null);
155: }
156:
157: if ((list.size() == 1) && !showIndex)
158: return doImmedeatePreview(tree, (ContentCall) list
159: .get(0));
160:
161: cat.debug("finished index");
162:
163: TKHTMLTemplate calls = getPrepHTMLTemplate("ce_previewIndex.tmpl");
164: ContentCallsIterator iterator = new ContentCallsIterator(
165: list, null, "CE_CALLS_LIST");
166:
167: calls.set("RTARGET", target);
168:
169: calls.setListIterator(iterator);
170: finishTemplate(calls);
171:
172: } catch (Throwable th) {
173: throw th;
174: } finally {
175: context.cleanup();
176: }
177: return true;
178: }
179:
180: public boolean doPreviewIndexPerSiteNode() throws Throwable {
181: String siteNodeIdStr = params.get("PAR", "SNODE");
182: String shortName = params.get("PAR", "DOCNAME");
183: String target = params.get("PAR", "TARGET");
184: String showIndexStr = params.get("PAR", "INDEX");
185:
186: if ((target == null) || (target.equals("")))
187: target = "_blank";
188:
189: int siteNodeId = siteNodeIdStr != null ? Integer
190: .parseInt(siteNodeIdStr) : -1;
191:
192: if (showIndexStr != null)
193: showIndexStr = showIndexStr.trim();
194: boolean showIndex = showIndexStr != null
195: && !showIndexStr.equalsIgnoreCase("FALSE");
196:
197: if (siteNodeId < 0)
198: throw new Error("Falsche SiteNode-Id: "
199: + (siteNodeIdStr != null ? siteNodeIdStr : "NULL"));
200: if (shortName == null)
201: throw new Error("Document-ShortName fehlt");
202:
203: return doPreviewIndexPerSiteNode(siteNodeId, shortName, target,
204: showIndex);
205: }
206:
207: public boolean doPreviewIndexPerSiteNode(int siteNodeId,
208: String shortName, String target, boolean showIndex)
209: throws Throwable {
210:
211: GeneratorContext context = GeneratorContext.setup();
212: GeneratorContext.setPreviewMode(true);
213: GeneratorContext.setup().setWorkflowIgnored(true);
214:
215: try {
216: cat.debug("start index of " + "SiteNode " + siteNodeId);
217:
218: // Achtung, das hier ist bei shared Preview NICHT thread-safe, falls mit
219: // unterschiedlichen leveln gearbeitet wird !!!
220: //
221: // Auf jeden Fall darf im shared-Modus kein indexBuild erfolgen, sonst bleibt
222: // die Site-Struktur im Fehlerfall unvollständig !!!
223: context.siteNodes.setIndexBuild(!GeneratorContext
224: .isShared());
225:
226: SiteNode tree = null;
227:
228: synchronized (context) {
229: tree = context.siteNodes.getRoot();
230: if (tree == null) {
231: tree = context.siteNodes.buildSiteTree();
232: //WebManGen.DumpSiteTree(tree, "previewSiteTree2.txt");//frank
233: if (GeneratorContext.isShared())
234: context.siteNodes.setRoot(tree);
235: context.contentForms.prepareFormHash();
236: context.siteContents.resetCounters();
237: context.contentCalls.setup();
238: }
239: }
240:
241: SiteNode sn = context.siteNodes.getNode(siteNodeId);
242: if (sn == null)
243: throw new Error("SiteNode " + siteNodeId + " not there");
244:
245: TKVector primContents = null;
246: if (sn.getType() == SiteNode.NODE_TYPE_GROUP) {
247: if (sn.getPrimaryContentSelectionKey() != null)
248: primContents = context.integrations.getSelection(sn
249: .getPrimaryContentSelectionKey());
250: else
251: primContents = sn.getPrimaryContentNode()
252: .getContents();
253:
254: int size = primContents.size();
255: if (size == 0)
256: throw new Error("Group-SiteNode " + siteNodeId
257: + " leer");
258:
259: }
260:
261: if ((primContents == null) || (primContents.size() == 1)
262: && !showIndex)
263: return doImmedeatePreviewPerSiteNode(tree, sn,
264: shortName, (primContents == null ? null
265: : (SiteContent) primContents
266: .elementAt(0)));
267:
268: TKHTMLTemplate index = getPrepHTMLTemplate("st_previewIndex.tmpl");
269:
270: index.set("RTARGET", target);
271: index.set("PATH", sn.getPath());
272: index.set("SNODE", new Integer(siteNodeId));
273: index.set("DOCNAME", shortName);
274:
275: if (primContents != null) {
276:
277: index.set("PRIMARY_ID", new Integer(sn
278: .getPrimaryContentNode().getId()));
279: index.set("CONTENT_NAME", sn.getPrimaryContentNode()
280: .getShortName());
281:
282: PrimContentsIterator iterator = new PrimContentsIterator(
283: primContents, null, "ST_PRIMARY_CONTENTS");
284: index.setListIterator(iterator);
285: }
286:
287: finishTemplate(index);
288:
289: } catch (Throwable th) {
290: throw th;
291: } finally {
292: context.cleanup();
293: }
294: return true;
295: }
296:
297: public void generatePreview(GeneratorContext context, SiteNode sn,
298: String shortName, SiteContent primContent) throws Exception {
299: GenNode gn = null;
300: SiteDocument sd = null;
301: GenDocument gd = null;
302:
303: synchronized (context) {
304: gn = sn.makeGenNode(primContent);
305: if (gn == null)
306: throw new Error("making genNode from SiteNode "
307: + sn.getId() + " failed");
308:
309: sd = sn.getDocument(shortName);
310: if (sd == null)
311: throw new Error("getting Document for SiteNode "
312: + sn.getId() + " / Doc " + shortName
313: + " failed");
314:
315: cat.debug("generatePreview of " + sd.path());
316: sd.complete();
317:
318: gd = sd.getGenDocument(primContent, gn);
319:
320: if (gd == null)
321: throw new Error("making genDocument for SiteNode "
322: + sn.getId() + " / Doc " + shortName
323: + " failed");
324:
325: gd.makeReferences(ownUrl, primContent);
326: }
327:
328: gd.generatePreview(out, docRoot, "file:" + docRoot
329: + File.separator
330: + TemplateUtils.getGenerationDirectory(), ownUrl);
331: }
332:
333: public boolean doPreview() throws Throwable {
334: String siteNodeIdStr = params.get("PAR", "SNODE");
335: String primaryIdStr = params.get("PAR", "PID");
336: String shortName = params.get("PAR", "DOCNAME");
337:
338: int siteNodeId = siteNodeIdStr != null ? Integer
339: .parseInt(siteNodeIdStr) : -1;
340: int primaryId = primaryIdStr != null ? Integer
341: .parseInt(primaryIdStr) : NO_PRIMARY_CONTENT;
342:
343: if (siteNodeId < 0)
344: throw new Error("Falsche SiteNode-Id: "
345: + (siteNodeIdStr != null ? siteNodeIdStr : "NULL"));
346: if (primaryId < -1)
347: throw new Error("Falsche Primary-Id: "
348: + (primaryIdStr != null ? primaryIdStr : "NULL"));
349: if (shortName == null)
350: throw new Error("Document-ShortName fehlt");
351: GeneratorContext context = GeneratorContext.setup();
352: GeneratorContext.setPreviewMode(true);
353: GeneratorContext.setup().setWorkflowIgnored(true);
354:
355: try {
356: cat.debug(" start viewing of sn=" + siteNodeIdStr + " pid="
357: + primaryIdStr + " dn=" + shortName);
358:
359: context.siteNodes.setReducedBuild(true);
360: SiteNode tree = null;
361:
362: synchronized (context) {
363: tree = context.siteNodes.getRoot();
364: if (tree == null) {
365: tree = context.siteNodes.buildSiteTree();
366: //WebManGen.DumpSiteTree(tree, "previewSiteTree3.txt");//frank
367:
368: if (GeneratorContext.isShared())
369: context.siteNodes.setRoot(tree);
370: context.contentForms.prepareFormHash();
371: context.siteContents.resetCounters();
372: }
373: }
374: SiteNode sn = context.siteNodes.getNode(siteNodeId);
375: if (sn == null)
376: throw new Error("SiteNode " + siteNodeId + " not there");
377:
378: SiteContent primContent = primaryId == -1 ? null
379: : (SiteContent) context.siteContents
380: .getContent(primaryId);
381:
382: if ((primContent == null) && (primaryId >= 0))
383: throw new Error("Primary Content " + primaryId
384: + " not there");
385:
386: generatePreview(context, sn, shortName, primContent);
387:
388: } catch (Throwable th) {
389: throw th;
390: } finally {
391: context.cleanup();
392: }
393: return true;
394: }
395:
396: public boolean doImmedeatePreview(SiteNode tree, ContentCall cc)
397: throws Throwable {
398: if (cc == null)
399: return false;
400:
401: SiteDocument sd = cc.getDocument();
402: if (sd == null)
403: return false;
404:
405: SiteNode sn = sd.getAnchor();
406: if (sn == null)
407: return false;
408:
409: int siteNodeId = sn.getId();
410: int primaryId = cc.getPrimaryId();
411: String shortName = sd.getShortName();
412:
413: GeneratorContext context = GeneratorContext.setup();
414: GeneratorContext.setPreviewMode(true);
415: GeneratorContext.setup().setWorkflowIgnored(true);
416:
417: try {
418: cat.debug("start viewing of sn=" + siteNodeId + " pid="
419: + primaryId + " dn=" + shortName);
420: context.siteNodes.setReducedBuild(context.siteNodes
421: .isIndexBuild());
422: synchronized (context) {
423: if (context.siteNodes.getRoot() == null) {
424: if (GeneratorContext.isShared())
425: context.siteNodes.setRoot(tree);
426: context.siteNodes.completeSiteTree(tree);
427: context.contentForms.prepareFormHash();
428: context.siteContents.resetCounters();
429: }
430: }
431:
432: SiteContent primContent = primaryId == -1 ? null
433: : (SiteContent) context.siteContents
434: .getContent(primaryId);
435:
436: if ((primContent == null) && (primaryId >= 0))
437: throw new Error("Primary Content " + primaryId
438: + "not there");
439:
440: GenNode gn = null;
441: GenDocument gd = null;
442:
443: synchronized (context) {
444:
445: gn = sn.makeGenNode(primContent);
446: if (gn == null)
447: throw new Error("making genNode from SiteNode "
448: + siteNodeId + " failed");
449:
450: sd.complete();
451: gd = sd.getGenDocument(primContent, gn);
452:
453: if (gd == null)
454: throw new Error("making genDocument for SiteNode "
455: + siteNodeId + " / Doc " + shortName
456: + " failed");
457: gd.makeReferences(ownUrl, primContent);
458: }
459: gd.generatePreview(out, docRoot, "file:" + docRoot
460: + File.separator
461: + TemplateUtils.getGenerationDirectory(), ownUrl);
462: } catch (Throwable th) {
463: throw th;
464: } finally {
465: context.cleanup();
466: }
467: return true;
468: }
469:
470: public boolean doImmedeatePreviewPerSiteNode(SiteNode tree,
471: SiteNode sn, String shortName, SiteContent primContent)
472: throws Throwable {
473: if (sn == null)
474: return false;
475:
476: int siteNodeId = sn.getId();
477: int primaryId = primContent == null ? -1 : primContent
478: .getContentId();
479:
480: GeneratorContext context = GeneratorContext.setup();
481: GeneratorContext.setPreviewMode(true);
482: GeneratorContext.setup().setWorkflowIgnored(true);
483:
484: try {
485: cat.debug("start viewing of sn=" + siteNodeId + " pid="
486: + primaryId + " dn=" + shortName);
487:
488: context.siteNodes.setReducedBuild(context.siteNodes
489: .isIndexBuild());
490:
491: synchronized (context) {
492: if (context.siteNodes.getRoot() == null) {
493: if (GeneratorContext.isShared())
494: context.siteNodes.setRoot(tree);
495: context.siteNodes.completeSiteTree(tree);
496:
497: context.contentForms.prepareFormHash();
498: context.siteContents.resetCounters();
499: }
500: }
501:
502: generatePreview(context, sn, shortName, primContent);
503:
504: } catch (Throwable th) {
505: throw th;
506: } finally {
507: context.cleanup();
508: }
509: return true;
510: }
511: }
|