KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > generator > SiteContent


1 package de.webman.generator;
2
3 import com.teamkonzept.db.*;
4 import com.teamkonzept.lib.*;
5 import de.webman.template.jsp.*;
6 import com.teamkonzept.lib.templates.TemplateTypes;
7 import com.teamkonzept.web.*;
8 import com.teamkonzept.field.*;
9 import com.teamkonzept.field.db.*;
10 import com.teamkonzept.international.LanguageManager;
11 import org.w3c.dom.*;
12 import java.sql.*;
13 import java.text.*;
14 import org.apache.log4j.Category;
15 import de.webman.content.Content;
16 import de.webman.content.workflow.ContentVersion;
17 import de.webman.util.legacy.Legacy;
18 import de.webman.util.log4j.WebmanCategory;
19
20
21 /**
22  * Verwaltet einen einzelnen Inhaltsdatensatz
23  * @author $Author: alex $
24  * @version $Revision: 1.19 $
25 */

26 public class SiteContent implements TKSortable, TemplateTypes
27 {
28
29     /** Datumsformatierer */
30     private static final DateFormat DATE_FORMATTER = DateFormat.getDateInstance(DateFormat.SHORT, LanguageManager.getStandardLocale());
31     
32     /** Zeitformatierer */
33     private static final DateFormat TIME_FORMATTER = DateFormat.getTimeInstance(DateFormat.SHORT);
34     
35     /** GNU Formattierer */
36     private static final DateFormat GNU_FORMATTER = new SimpleDateFormat ("yyyy.MM.dd");
37     
38     /**
39         Verweis zurück auf den aktuellen statischen Kontext des laufenden Threads
40     */

41     private GeneratorContext context;
42
43     /** Logging Category */
44     private static WebmanCategory cat = (WebmanCategory)Category.getInstance(SiteContent.class.getName());
45
46     
47     /**
48         Id der Content-Instanz, die der Datensatz in der DB hat
49     */

50     private int instanceId;
51     
52     /**
53         Versionsnummer, die der Datensatz in der DB hat
54     */

55     private int versionId;
56
57     /**
58         Id des Contents ?, die der Datensatz in der DB hat
59     */

60     private int contentId;
61     
62     /** Content Node Id fuer Online Link */
63     private int content_node_id;
64     
65     /**
66         Kurzname des Inhalts (nur bei Gruppen-Inhalten)
67     */

68     private String JavaDoc shortName;
69     
70     /**
71         Langname des Inhalts (nur bei Gruppen-Inhalten)
72     */

73     private String JavaDoc name;
74     
75     /**
76         Gibt an, ob es sich um eine Structure Content handelt
77     */

78     private boolean isStructureContent;
79     
80     /**
81         Id des Formulars, welches dem Inhalt zugrunde liegt
82     */

83     private int formId;
84     
85     /** Autor des Contents (letzte Aenderung)*/
86     private String JavaDoc author;
87     
88     /** Autor des Contents (Einsteller)*/
89     private String JavaDoc initialAuthor;
90     
91     /** letzte Aenderung des Contents */
92     private java.util.Date JavaDoc lastChange;
93     
94     /** Einstellungsdatum */
95     private java.util.Date JavaDoc firstDate;
96     
97     /** sollen Contents gecached werden ?*/
98     private static boolean isCachingEnabled = true;
99     
100     /** StatusID - Workflow */
101     private int statusID;
102     
103     /**
104         Alle noch nicht generierten Dokumente, in denen dieser
105         Content referenziert wird
106         
107         Key := GenDocument
108         Value := wie oft wird der Inhalt von dem Dokument referenziert
109     */

110     private TKHashtable inDocs;
111     
112     /**
113         Die eigentlichen Inhalte werden solange gespeichert, bis
114         alle Dokumente, die ihn referenzieren generiert sind
115     */

116     private Object JavaDoc contentData;
117     
118     /** bedeutet, dass dieser Content der Main Content ist und bei diesem der Workflow
119         ignoriert wird, bei allen anderen nicht, wird für den Real Live Preview der
120         Redakteure benutzt
121     */

122     private boolean isPrimary;
123
124     /** Merker fuer die inkrementelle Generierung */
125     private Integer JavaDoc generated = new Integer JavaDoc(0);
126
127     public SiteContent(GeneratorContext context, int formId, Content content, boolean isStructureContent )
128     {
129         this(context, formId, content, isStructureContent, false);
130     }
131     /**
132         Konstruktor fuer redaktionellen Content
133     */

134     public SiteContent(GeneratorContext context, int formId, Content content, boolean isStructureContent, boolean _isPrimary )
135     {
136         this.context = context != null ? context : GeneratorContext.setup ();
137         this.formId = formId;
138         this.isStructureContent = isStructureContent;
139         isPrimary = _isPrimary;
140         this.instanceId = content.getInstanceId().intValue();
141         ContentVersion genVersion = null;
142         // Achtung hier fuer den Preview !!!
143
if (context.isWorkflowIgnored() || isPrimary)
144             genVersion = content.getCurrentVersion();
145         else
146             genVersion = content.getGeneratableVersion();
147         /*if (content.getGenerated() != null)
148             generated = content.getGenerated(); */

149         if (genVersion == null)
150         {
151             this.versionId = -1;
152             this.contentId = -1;
153             author = "";
154             statusID = -1;
155             lastChange = null;
156             
157         }
158         else
159         {
160             this.versionId = genVersion.getId().intValue();
161             this.contentId = genVersion.getContentId().intValue();
162             author = genVersion.getAuthor();
163             statusID = genVersion.getStatus().status_id;
164             lastChange = genVersion.getDate();
165         }
166         this.content_node_id = content.getId().intValue();
167         this.formId = formId;
168         this.shortName = content.getShortName();
169         this.name = content.getName();
170         ContentVersion firstVersion = content.getInitialVersion();
171         if (firstVersion != null)
172         {
173             initialAuthor = firstVersion.getAuthor();
174             firstDate = firstVersion.getDate();
175         }
176         else
177         {
178             initialAuthor = "toDO";
179             firstDate = new Date(System.currentTimeMillis());
180         }
181         this.inDocs = new TKHashtable();
182         this.contentData = null;
183         
184         this.context.siteContents.putContent (instanceId, this);
185         
186     }
187     
188     /**
189         Konstruktor fuer SiteStruktur Parameter
190     */

191     public SiteContent( GeneratorContext context, int instanceId, int versionId,
192         int contentId, int formId,
193         String JavaDoc shortName, String JavaDoc name, boolean isStructureContent )
194     {
195         this.context = context != null ? context : GeneratorContext.setup ();
196
197         this.instanceId = instanceId;
198         this.versionId = versionId;
199         this.contentId = contentId;
200         this.formId = formId;
201         this.shortName = shortName;
202         this.name = name;
203         this.isStructureContent = isStructureContent;
204         
205         this.inDocs = new TKHashtable();//null;
206
this.contentData = null;
207         
208         this.context.siteContents.putContent (instanceId, this);
209     }
210
211     public Integer JavaDoc getGenerated()
212     {
213         return generated;
214     }
215
216     public void setGenerated(Integer JavaDoc generated)
217     {
218         this.generated = generated;
219     }
220
221     public void resetInDocs()
222     {
223         inDocs = new TKHashtable();
224     }
225     
226     public int getVersionId()
227     {
228         return versionId;
229     }
230     
231     public boolean isStructureContent()
232     {
233         return isStructureContent;
234     }
235     
236     public int getInstanceId()
237     {
238         return instanceId;
239     }
240     
241     public String JavaDoc getName()
242     {
243         return name;
244     }
245     
246     public int getContentId()
247     {
248         return contentId;
249     }
250     
251     public int getFormularId()
252     {
253         return formId;
254     }
255     
256     public String JavaDoc getShortName()
257     {
258         return shortName;
259     }
260     
261     static
262     {
263         try
264         {
265             PropertyManager man = PropertyManager.getPropertyManager("CACHING");
266             String JavaDoc enabled = man.getValue("SITE_CONTENT", null);
267             if (enabled != null && enabled.equalsIgnoreCase("false"))
268                isCachingEnabled = false;
269         }
270         catch (Throwable JavaDoc t)
271         {
272             // wird ignoriert - Property Gruppe nicht angelegt
273
cat.debug("Caching of Site Contents disabled - no property");
274         }
275     }
276     
277     
278     public String JavaDoc toString()
279     {
280         String JavaDoc result =
281             "("+instanceId+", "+versionId+", "+contentId+", '"+shortName+"', "+ formId+")";
282         return result;
283     }
284     
285
286     /**
287         Holt den Inhalt mit Hilfe des Formulars aus der DB
288         bzw. aus dem Cache
289     */

290     private Object JavaDoc getData( TKBaseField form )
291     {
292         // Inhalt schon im Cache?
293
if( contentData != null ) {
294             return contentData;
295         }
296         Object JavaDoc tempData = null;
297         // singh 1 1999.04.07
298
cat.debug("+++ start getData "+contentId);
299         
300         // Inhalt aus der DB holen
301
try
302         {
303             //frank hack: solange structure contents noch nicht in der
304
// Versionsverwaltung ist, Somderbehandlung...
305
if (isStructureContent) {
306                 if (contentId < 0 /*|| (versionId < 0)*/) {
307                     tempData = form.getDefault();
308                 }
309                 else {
310                     TKContentDBData cdata = new TKContentDBData( contentId, versionId );
311                     TKContentDBInterface tmp = TKContentDBInterface.self;
312                     
313                     // die folgenden drei Zeilen ersetzen den Standardaufruf
314
// TKContentDBInterface.Get(cdata);
315
// Die einzige neue Zeile ist die Mittlere: die contentId
316
// muss per Hand gesetzt werden, da sie noch nicht ueber die
317
// Versionsverwaltung ermittelt werden kann.
318
// (Remark: contentId im SiteTree entspricht der INSTANCE_ID
319
// in der Datenbank!)
320
tmp.getEntry(cdata);
321                     cdata.content_id = contentId;
322                     tmp.getEntryTables (cdata);
323                     
324                     tempData = form.getDataFromDB( cdata );
325                 }
326             }
327             else {
328                 if ((contentId < 0) || (versionId < 0)) {
329                     tempData = form.getDefault();
330                 }
331                 else {
332                     TKContentDBData cdata = new TKContentDBData( contentId, versionId );
333                     TKContentDBInterface.Get(cdata);
334                     // und merken
335
tempData = form.getDataFromDB( cdata );
336                 }
337             }
338             /* old version bevor hack by frank
339             if ((contentId < 0) || (versionId < 0)) {
340                 contentData = form.getDefault();
341             }
342             else {
343                 TKContentDBData cdata = new TKContentDBData( contentId, versionId );
344                 TKContentDBInterface.Get(cdata);
345                 // und merken
346                 contentData = form.getDataFromDB( cdata );
347             }
348             */

349         }
350         catch( SQLException e ) {
351             cat.warn( "could not read content "+contentId, e );
352             throw new TKSQLError( e.getMessage(), e );
353         }
354
355         if (isCachingEnabled)
356             contentData = tempData;
357         return tempData;
358     }
359         
360     /**
361         Registriert das Dokument als weiteren Benutzer des Inhalts
362         
363         Da Dokumente Inhalte evtl. mehrmals benutzen koennen, wird
364         das Hash als Referenz-Zaehler verwendet
365     */

366     public void registerDoc( GenDocument doc, boolean onlyStructureContents )
367     {
368         if (onlyStructureContents && !isStructureContent) return;
369
370         Integer JavaDoc oldVal = (Integer JavaDoc) inDocs.get( doc );
371         if( oldVal == null ) {
372             inDocs.put( doc, new Integer JavaDoc(1) );
373         }
374         else {
375             inDocs.put( doc, new Integer JavaDoc( oldVal.intValue()+1) );
376         }
377             
378     }
379     
380     /**
381         Das Dokument wurde generiert und kann aus der
382         Benutzer-Liste entfernt werden, wenn alle Referenzen deregistriert
383         wurden.
384     */

385     public void deregisterDoc( GenDocument doc, boolean onlyStructureContents )
386     {
387         if (onlyStructureContents && !isStructureContent) return;
388
389         Integer JavaDoc oldVal = (Integer JavaDoc) inDocs.get(doc);
390         if( oldVal == null )
391         {
392             cat.error( "too many deregistrations of content "+contentId+
393                 " for document "+doc.getAnchor().getPath()+doc.getDocumentName() );
394             return;
395         }
396         
397         if( oldVal.intValue() == 1 ) {
398             inDocs.remove( doc );
399             if( inDocs.size() == 0 ) {
400                 contentData = null;
401                 cat.debug("removed data for content "+contentId );
402             }
403         }
404         else {
405             inDocs.put( doc, new Integer JavaDoc( oldVal.intValue()-1 ) );
406         }
407     }
408     
409     /**
410         traegt den internen und oeffentlichen Namen in das Template ein
411     */

412     public void fillNameIntoTemplate( TemplateBasic t, String JavaDoc integrationName )
413     {
414         if ((name != null) && (name.length() > 0)) t.set (integrationName+".NAME",name);
415         if (author != null) t.set (integrationName+".AUTHOR", author);
416         if (initialAuthor != null) t.set (integrationName+".INITIAL_AUTHOR", initialAuthor);
417         
418         // Datum eintragen
419
if (lastChange != null)
420         {
421             t.set (integrationName+".MODIFIED", getDate(lastChange));
422             t.set (integrationName+".MODIFIED_GNU", getGNUDate(lastChange));
423             t.set (integrationName+".MODIFIED_TIME", getTime(lastChange));
424         }
425         
426         if (firstDate != null)
427         {
428             t.set (integrationName+".FIRST_DATE", getDate(firstDate));
429             t.set (integrationName+".FIRST_DATE_GNU", getGNUDate(firstDate));
430             t.set (integrationName+".FIRST_TIME", getTime(firstDate));
431         }
432         if (contentId > 0) t.set(integrationName+".CONTENT_ID", Integer.toString(content_node_id));
433         if (statusID > 0) t.set(integrationName+".WORKFLOW_ID", Integer.toString(statusID));
434         if ((shortName != null) && (shortName.length() > 0)) t.set (integrationName+".SHORT_NAME",shortName);
435     }
436
437     /**
438         traegt den Inhalt in das Template ein
439     */

440     public void fillIntoTemplate( TemplateBasic t, String JavaDoc integrationName )
441     {
442         try
443         {
444             if (t.getType().equals(TK_TEMPLATE))
445             {
446                 TKBaseField form = context.contentForms.getContentForm( formId );
447                 Object JavaDoc data = getData( form );
448                 form.fillIntoPresentation( (TKHTMLTemplate)t, data, integrationName );
449             }
450             else
451             {
452                 DOMTemplateBasic domTemplate = (DOMTemplateBasic) t;
453                 Element c = domTemplate.getContentElement(shortName); // Attribute
454
fillBasicDOM(domTemplate.getDocument(), c);
455             }
456         }
457         catch (TKFieldDataDBError e)
458         {
459              // Formular hat sich incompatibel geaendert
460
TKBaseField base = context.contentForms.getContentForm(formId);
461             if (GeneratorContext.isPreviewMode())
462             {
463                 cat.error("Incompatible form change of form : " + formId + " in Content : " + content_node_id);
464                 throw new TKFieldDataDBError("Incompatible form change :" + formId, e.type, e.id, base.getShowName());
465             }
466             else
467             {
468                 cat.error("The form : " + base.getShowName() + " of content: " + content_node_id + " has an incompatible change. Content is not generated.Please contact your producer");
469             }
470         }
471     }
472
473
474     /**
475         noetig, da direkt hierein gesprungen wird
476     */

477     public void fillBasicDOM(Document doc, Element node)
478     {
479         try
480         {
481
482             TKBaseField form = context.contentForms.getContentForm( formId );
483             Object JavaDoc data = getData( form );
484             fillIntoDOM(doc, node, data);
485
486             // System.out.println("Filling : "+ shortName);
487
node.setAttribute ("NAME", name);
488             node.setAttribute ("SHORT_NAME", shortName);
489             node.setAttribute ("AUTHOR", author);
490             node.setAttribute ("INITIAL_AUTHOR", initialAuthor);
491             node.setAttribute ("MODIFIED", getDate(lastChange));
492             node.setAttribute ("FIRST_DATE", getDate(firstDate));
493             node.setAttribute ("MODIFIED_TIME", getTime(lastChange));
494             node.setAttribute ("FIRST_TIME", getTime(firstDate));
495             node.setAttribute ("CONTENT_ID" , Integer.toString(content_node_id));
496             node.setAttribute ("WORKFLOW_ID", Integer.toString(statusID));
497         }
498         catch (Throwable JavaDoc t)
499         {
500             cat.error("error getting dom content:"+ contentId, t);
501         }
502     }
503     /**
504         noetig, da ueber ContentReferenzen direkt hierein gesprungen wird
505     */

506     public void fillIntoDOM(Document doc, Element node, Object JavaDoc data)
507     {
508         try
509         {
510             TKBaseField form = context.contentForms.getContentForm( formId );
511             form.fillIntoDOM(doc, node, data);
512         }
513         catch (Throwable JavaDoc t)
514         {
515             cat.error("error getting dom content:"+ content_node_id, t);
516         }
517         
518     }
519     /**
520         Vergleicht die Namen zweier Inhalte.
521         Inhalte ohne Namen sind "groesser" als solche mit!
522     */

523     public int cmpNames( SiteContent otherContent )
524     {
525         if( name == null ) {
526             return ( otherContent.name == null ? 0 : 1 );
527         }
528         else
529             if( otherContent.name == null ) return -1;
530         return name.compareTo( otherContent.name );
531     }
532     
533     /**
534         Vergleicht zwei Inhalte fuer eine Sortierung
535         miteinander. Es gibt zwei Sortierkriterien: Datum und Name
536         Es ist nicht zwingend, dass ein Inhalt Daten für eines
537         der beiden Kriterien zur Verfügung stellt.
538         
539         Inhalte mit Datum erscheinen generell vor denen ohne Datum
540         Inhalte mit kleineren Datum erscheinen hinter denen mit
541         größerem Datum
542         Bei gleichem Datum wird die Ordnung ueber die alphabetische
543         Reihenfolge der Inhaltsnamen festgelegt.
544         Auch hier erscheinen Inhalte mit Namen vor denen ohne Namen.
545     */

546     public int cmp( TKSortable otherObject )
547     {
548         SiteContent other = (SiteContent) otherObject;
549         return cmpNames(other);
550         /*
551         TKHashtable myData = (TKHashtable)getData();
552         TKHashtable otherData = (TKHashtable)other.getData();
553         
554         if( myData == null )
555         {
556             return ( otherData == null ? cmpNames( other ) : 1 );
557         }
558         else if( otherData == null ) return -1;
559         
560         java.util.Date myDate = context.siteContents.getDateFromString( (String) myData.get("DATE") );
561         java.util.Date otherDate = context.siteContents.getDateFromString( (String) otherData.get("DATE") );
562         
563         if( myDate == null ) {
564             return ( otherDate == null ? cmpNames( other ) : 1 );
565         }
566         else if( otherDate == null ) return -1;
567         
568         if( myDate.getTime() < otherDate.getTime() ) return 1;
569         if( myDate.getTime() > otherDate.getTime() ) return -1;
570         return cmpNames( other );
571         */

572     }
573     
574     /**
575     @return true, if the content is a generatable version, else false.
576     */

577     public boolean isGeneratableVersion()
578     {
579         return (versionId != -1) ? true : false;
580     }
581     
582     /**
583         liefert nur Datum zurueck - keine Zeit
584     */

585     static String JavaDoc getDate(java.util.Date JavaDoc date)
586     {
587         if (date == null)
588             return "";
589         return DATE_FORMATTER.format(date);
590     }
591     
592     /**
593         fuer GNU Search Engines
594     */

595     static String JavaDoc getGNUDate(java.util.Date JavaDoc date)
596     {
597         if (date == null)
598             return "";
599         return GNU_FORMATTER.format(date);
600     }
601     
602     static String JavaDoc getTime(java.util.Date JavaDoc date)
603     {
604         if (date == null)
605             return "";
606         return TIME_FORMATTER.format(date);
607     }
608 }
609
Popular Tags