KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.generator;
2
3 import com.teamkonzept.db.*;
4 import com.teamkonzept.lib.*;
5 import com.teamkonzept.webman.db.*;
6 import de.webman.generator.db.queries.*;
7
8 import java.io.*;
9 import java.util.*;
10 import java.sql.*;
11 import org.apache.log4j.Category;
12
13 /**
14  * @author $Author: alex $
15  * @version $Revision: 1.3 $
16 */

17 public class SiteNode implements Visitable
18 {
19
20     /**
21         Verweis zurück auf den aktuellen statischen Kontext des laufenden Threads
22     */

23     GeneratorContext context;
24
25     public final static int NODE_TYPE_SINGLE = 3;
26     public final static int NODE_TYPE_GROUP = 2;
27
28     public final static int DOC_TRAV_MODE_CONTENT = 1;
29     public final static int DOC_TRAV_MODE_REFERENCE = 2;
30     public final static int DOC_TRAV_MODE_STRUCT_CONTENT = 3;
31
32     /** Log4J Category */
33     private static Category cat = Category.getInstance(SiteNode.class.getName());
34     
35     /** Knotenid */
36     private int id;
37     
38     /** Vaterknoten */
39     private SiteNode parent;
40     
41     /** Einzel- oder Gruppen-Knoten*/
42     private int type;
43     
44     /** Knotenname (= directory name) */
45     private String JavaDoc shortName;
46     
47     /** Knotenbeschreibung */
48     private String JavaDoc name;
49     
50     /** Primaerer Inhalt (nur bei Gruppen-Knoten) */
51     private SiteContentNode primaryContentNode;
52     
53     /** Auswahl-Funktion des primären Gruppen-Inhalts */
54     private String JavaDoc primaryContentSelectionType;
55     
56     /** Daten für Auswahl-Funktion */
57     private String JavaDoc primaryContentSelectionData;
58     
59     /** Hash-Key für Auswahl-Funktionsergebnis */
60     private String JavaDoc primaryContentSelectionKey;
61
62     /** Vector von SiteNodes */
63     private TKVector childs; // Liste von Kinder-Knoten
64

65     /** Vector von SiteDocuments */
66     private TKVector documents; // Liste der definierten Dokumente des Knotens
67

68     /** Vector von SiteDocuments*/
69     private TKVector realDocuments; // Liste der wirklich zu generierenden Dokumente (Vererbung beruecksichtigt)
70

71     /** Key:shortName , Value: SiteDocument*/
72     private TKHashtable docNameHash; // Hash mit Shortname der Dokumente des Knotens
73

74     /** Key:formularId (Integer), Value: SiteContent*/
75     private TKHashtable structureContents; // Hash mit Struktur-Inhalten
76

77     /** Vector von GenNodes */
78     private TKVector genNodes; // Liste der Knoten, die für diesen Knoten
79
// generiert werden muessen
80

81     /** Ist der Knoten bereits vollständig aufgebaut ?
82         siehe SiteNode.reducedBuild
83     */

84     private boolean isComplete;
85     
86     private int level; // the hierarchy level, root is 0.
87

88     public SiteNode( GeneratorContext context, ResultSet rs, SiteNode lastInsertedNode )
89         throws SQLException
90     {
91         this.context = context != null ? context : GeneratorContext.setup ();
92
93         this.id = rs.getInt("SITE_NODE_ID");
94         this.type = rs.getInt("SITE_NODE_TYPE");
95         this.shortName = rs.getString( "SITE_NODE_SHORTNAME" );
96         name = rs.getString("SITE_NODE_NAME");
97         if( lastInsertedNode == null ) {
98             parent = null;
99             level = 0;
100         }
101         else {
102             int parentId = rs.getInt("SITE_NODE_PARENT");
103             while( lastInsertedNode.id != parentId ) {
104                 lastInsertedNode = lastInsertedNode.parent;
105             }
106             parent = lastInsertedNode;
107             parent.childs.addElement( this );
108             level = parent.level + 1;
109         }
110
111         if ( this.type == NODE_TYPE_GROUP ) {
112             int primaryContentNodeId = rs.getInt( "PG_CONTENT_NODE" );
113             if( rs.wasNull() ) throw new Error JavaDoc( "Primary Content-Node not set" );
114             this.primaryContentNode = this.context.contentNodes.getContentNode( primaryContentNodeId );
115             this.primaryContentSelectionType = rs.getString( "SELECTION_TYPE" );
116             this.primaryContentSelectionData = rs.getString( "SELECTION_DATA" );
117         }
118         else if( this.type != NODE_TYPE_SINGLE ) {
119             throw new Error JavaDoc( "Unexpected Node-Type:"+type );
120         }
121
122         this.childs = new TKVector();
123         this.documents = new TKVector();
124         realDocuments = new TKVector();
125         this.docNameHash = new TKHashtable();
126         this.structureContents = null;
127         this.isComplete = !this.context.siteNodes.isReducedBuild() && !this.context.siteNodes.isIndexBuild();
128
129         if (this.context.siteNodes.getSubtreeNode() == null)
130         {
131             if (this.context.siteNodes.getSubtreeId() == this.id)
132             {
133                 if (this.parent == null) this.context.siteNodes.setSubtreeId( -1);
134                 else
135                 {
136                     this.context.siteNodes.setSubtreeNode(this);
137                     this.context.siteNodes.setSubtreePath (getPath());
138                 }
139             } else if (this.context.siteNodes.getSubtreePath() != null)
140             {
141                 String JavaDoc thisPath = getPath();
142                 if ((thisPath != null) && thisPath.equals (this.context.siteNodes.getSubtreePath()))
143                 {
144                     if (this.parent == null) this.context.siteNodes.setSubtreePath (null);
145                     else
146                     {
147                         this.context.siteNodes.setSubtreeNode(this);
148                         this.context.siteNodes.setSubtreeId (this.id);
149                     }
150                 }
151             }
152         }
153     }
154
155     public int getLevel()
156     {
157         return level;
158     }
159     
160     public SiteContentNode getPrimaryContentNode()
161     {
162         return primaryContentNode;
163     }
164     
165     public String JavaDoc getPrimaryContentSelectionType()
166     {
167         return primaryContentSelectionType;
168     }
169     
170     public String JavaDoc getPrimaryContentSelectionData()
171     {
172         return primaryContentSelectionData;
173     }
174     
175     public String JavaDoc getPrimaryContentSelectionKey()
176     {
177         return primaryContentSelectionKey;
178     }
179     
180     public TKVector getChilds()
181     {
182         return childs;
183     }
184     
185     public String JavaDoc getShortName()
186     {
187         return shortName;
188     }
189     
190     public TKVector getGenNodes()
191     {
192         return genNodes;
193     }
194     
195     public TKHashtable getStructureContents()
196     {
197         return structureContents;
198     }
199     
200     public TKHashtable getDocumentNameHash()
201     {
202         return docNameHash;
203     }
204     
205     public TKVector getDocuments()
206     {
207         return documents;
208     }
209     
210     public String JavaDoc path()
211     {
212         if( parent == null ) return shortName;
213         return parent.path()+'.'+shortName;
214     }
215
216     public String JavaDoc toString()
217     {
218         String JavaDoc path = path();
219         String JavaDoc result =
220             "<sitenode '"+path+"'>"+
221             " id = "+id+
222             " type = " +(type==NODE_TYPE_SINGLE?"SINGLE":"GROUP")+
223             " ";
224
225         result += " sitenode documents:";
226         Enumeration e = documents.elements();
227         while( e.hasMoreElements() ) {
228             Object JavaDoc o = e.nextElement();
229             if( o != null )
230                 result += " "+o;
231         }
232
233         result += " sitenode childs:";
234         e = childs.elements();
235         while( e.hasMoreElements() ) {
236             result += " "+e.nextElement();
237         }
238         return result+" </sitenode '"+path+"'> ";
239     }
240
241     /**
242     Returns the path-string of this site node.
243     The root-node's name is omitted.
244     Example: "/PressReleases/".
245     */

246     public String JavaDoc getPath()
247     {
248         return ( parent == null ? "/" : parent.getPath() + shortName + "/" ) ;
249     }
250     
251     /**
252     Same as getPath(), but with File.separator instead of "/".
253     */

254     public String JavaDoc getDirPath()
255     {
256         return ( parent == null ? File.separator : parent.getDirPath() + shortName + File.separator) ;
257     }
258
259     /**
260     Same as getDirPath(), but without a trailing File.separator.
261     If parent is null, it returns an empty string.
262     */

263     public String JavaDoc getPathDir()
264     {
265         return ( parent == null ? "" : parent.getDirPath() + shortName) ;
266     }
267     
268     public SiteNode getParent()
269     {
270         return parent;
271     }
272     
273     public int getType()
274     {
275         return type;
276     }
277     
278     public int getId()
279     {
280         return id;
281     }
282
283     public int addDocuments( ResultSet rs, int docNodeId )
284         throws SQLException
285     {
286         // singh + 1999.06.25, nur teilweise eingelesene Sites beruecksichtigen ...
287
while (context.siteNodes.getNode (docNodeId) == null) {
288
289             if( !rs.next() ) return -1;
290             docNodeId = rs.getInt( "SITE_NODE_ID" );
291         }
292         // singh - 1999.06.25
293

294         while( docNodeId == id ) {
295             SiteDocument doc = new SiteDocument( context, rs, this );
296             documents.put( doc.getIndex(), doc );
297             if( !rs.next() ) return -1;
298             docNodeId = rs.getInt( "SITE_NODE_ID" );
299         }
300
301         Enumeration e = childs.elements();
302         while( e.hasMoreElements() ) {
303             SiteNode child = (SiteNode) e.nextElement();
304             docNodeId = child.addDocuments( rs, docNodeId );
305             if( docNodeId == -1 )
306                 return -1;
307         }
308         return docNodeId;
309     }
310
311     public void readDocuments()
312         throws SQLException
313     {
314         TKQuery q = TKWebmanDBManager.newQuery(GenSiteDocs.class);
315         q.execute();
316         ResultSet rs = q.fetchResultSet();
317
318         if( rs.next() ) {
319             addDocuments( rs, rs.getInt( "SITE_NODE_ID" ) );
320         }
321     }
322
323     public int traverseDocuments(
324         int mode,
325         String JavaDoc nodeColName,
326         String JavaDoc idxColName,
327         ResultSet rs,
328         int docNodeId,
329         int docIdx
330     )
331         throws SQLException
332     {
333         // singh + 1999.06.25, nur teilweise eingelesene Sites beruecksichtigen ...
334
while (context.siteNodes.getNode (docNodeId) == null) {
335
336             if( !rs.next() ) return 0;
337
338             docNodeId = rs.getInt(nodeColName);
339             docIdx = rs.getInt(idxColName);
340         }
341         // singh - 1999.06.25
342

343         int res;
344         // String nextIdxColumnName;
345
while( docNodeId == id ) {
346             SiteDocument doc =
347                 (SiteDocument) documents.get( docIdx );
348             res = doc.traverse( mode, nodeColName, idxColName, rs, docNodeId );
349             if( res == 0 )
350                 return 0;
351
352             if( res > 0 ) {
353                 docIdx = res;
354             }
355             else {
356                 docNodeId = -res;
357                 docIdx = rs.getInt( idxColName );
358             }
359         }
360
361         Enumeration e = childs.elements();
362         while( e.hasMoreElements() ) {
363             SiteNode child = (SiteNode) e.nextElement();
364             res = child.traverseDocuments( mode, nodeColName, idxColName, rs, docNodeId, docIdx );
365             if( res == 0 )
366                 return 0;
367
368             if( res > 0 ) {
369                 docIdx = res;
370             }
371             else {
372                 docNodeId = -res;
373                 docIdx = rs.getInt( idxColName );
374             }
375         }
376         return -docNodeId;
377     }
378
379     public void readReferences()
380         throws SQLException
381     {
382         TKQuery q = TKWebmanDBManager.newQuery(GenSiteRefs.class);
383         q.execute();
384         ResultSet rs = q.fetchResultSet();
385
386         if( rs.next() ) {
387             traverseDocuments(
388                 DOC_TRAV_MODE_REFERENCE,
389                 "SRC_SITE_NODE_ID",
390                 "SRC_SITE_NODE_DOC_IDX",
391                 rs,
392                 rs.getInt( "SRC_SITE_NODE_ID" ),
393                 rs.getInt( "SRC_SITE_NODE_DOC_IDX" )
394             );
395         }
396     }
397
398     public int addStructureContents( ResultSet rs, int docNodeId )
399         throws SQLException
400     {
401         structureContents = new TKHashtable();
402         if( parent != null ) {
403             Enumeration e = parent.structureContents.keys();
404             while( e.hasMoreElements() ) {
405                 Object JavaDoc o = e.nextElement();
406                 structureContents.put( o, parent.structureContents.get( o ) );
407             }
408         }
409
410         // singh + 1999.06.25, nur teilweise eingelesene Sites beruecksichtigen ...
411
if (docNodeId >= 0) {
412             while (context.siteNodes.getNode (docNodeId) == null)
413             {
414                 if( !rs.next() ) { docNodeId = -1; break; }
415                 docNodeId = rs.getInt("SITE_NODE_ID");
416             }
417         }
418         // singh - 1999.06.25
419

420         while( docNodeId == id ) {
421             int structuredContentId = rs.getInt( "CONTENT_ID" );
422             SiteContent newContent =
423                 context.siteContents.getContent( -structuredContentId );
424             if( newContent == null ) {
425                 
426                 newContent =
427                     new SiteContent(
428                         context,
429                         -structuredContentId,-1,
430                         structuredContentId,
431                         rs.getInt( "FORM_ID" ),
432                         null,
433                         null,
434                         true
435                     );
436             }
437             else
438             {
439                 cat.warn("KEIN CONTENT!" + newContent.getFormularId() + " , " + rs.getInt("FORM_ID") + ", " + structuredContentId);
440             }
441             structureContents.put( new Integer JavaDoc( newContent.getFormularId() ), newContent );
442             if( !rs.next() ) { docNodeId = -1; break; }
443             docNodeId = rs.getInt( "SITE_NODE_ID" );
444         }
445
446         Enumeration e = childs.elements();
447         while( e.hasMoreElements() ) {
448             SiteNode child = (SiteNode) e.nextElement();
449             docNodeId = child.addStructureContents( rs, docNodeId );
450         }
451         return docNodeId;
452     }
453
454     public void readStructureContents()
455         throws SQLException
456     {
457         TKQuery q = TKWebmanDBManager.newQuery(GenStructureConts.class);
458         q.execute();
459         ResultSet rs = q.fetchResultSet();
460
461         if( rs.next() ) {
462             addStructureContents( rs, rs.getInt( "SITE_NODE_ID" ) );
463         }
464         else {
465             addStructureContents( rs, -1 );
466         }
467
468         q = TKWebmanDBManager.newQuery(GenSiteDocStructConts.class);
469         q.execute();
470         rs = q.fetchResultSet();
471
472         if( rs.next() ) {
473             traverseDocuments(
474                 DOC_TRAV_MODE_STRUCT_CONTENT,
475                 "SITE_NODE_ID",
476                 "SITE_NODE_DOC_IDX",
477                 rs,
478                 rs.getInt( "SITE_NODE_ID" ),
479                 rs.getInt( "SITE_NODE_DOC_IDX" )
480             );
481         }
482     }
483
484     public void getContentIntegrations()
485         throws SQLException
486     {
487         TKQuery q = TKWebmanDBManager.newQuery(GenSiteContNodes.class);
488         q.execute();
489         ResultSet rs = q.fetchResultSet();
490
491         if( rs.next() ) {
492             traverseDocuments(
493                 DOC_TRAV_MODE_CONTENT,
494                 "SITE_NODE_ID",
495                 "SITE_NODE_DOC_IDX",
496                 rs,
497                 rs.getInt( "SITE_NODE_ID" ),
498                 rs.getInt( "SITE_NODE_DOC_IDX" )
499             );
500         }
501     }
502
503     public void expandReferences()
504     {
505         // real Documents ???
506
Enumeration e = realDocuments.elements();
507         while( e.hasMoreElements() ) {
508             SiteDocument doc = (SiteDocument) e.nextElement();
509             cat.debug("Expanding references for document : " + doc.getShortName());
510             if( (doc != null)/* && !doc.heretable*/ ) {
511                 doc.expandReferences();
512             }
513         }
514         e = childs.elements();
515         while( e.hasMoreElements() ) {
516             ((SiteNode) e.nextElement()).expandReferences();
517         }
518     }
519
520     public void findReferenceContentDocuments()
521     {
522         Enumeration e = realDocuments.elements();
523         while( e.hasMoreElements() ) {
524             SiteDocument doc = (SiteDocument) e.nextElement();
525             if( (doc != null) && doc.isRefered() ) {
526                 doc.findReferenceContentDocuments();
527             }
528         }
529         e = childs.elements();
530         while( e.hasMoreElements() ) {
531             ((SiteNode) e.nextElement()).findReferenceContentDocuments();
532         }
533     }
534
535     public void doContentSelections()
536     {
537         if( type == NODE_TYPE_GROUP && primaryContentSelectionType != null ) {
538             primaryContentSelectionKey = context.integrations.newSelection(
539                 primaryContentSelectionType,
540                 primaryContentSelectionData,
541                 primaryContentNode.getId()
542             );
543         }
544
545         // singh 1 1999.04.08
546
if (context.siteNodes.isReducedBuild()) return;
547
548         Enumeration e = realDocuments.elements();
549         while( e.hasMoreElements() ) {
550             SiteDocument doc = (SiteDocument) e.nextElement();
551             if( doc != null) {
552                 doc.doContentSelections();
553             }
554         }
555         e = childs.elements();
556         while( e.hasMoreElements() ) {
557             ((SiteNode) e.nextElement()).doContentSelections();
558         }
559     }
560
561     public int findDistance( SiteNode dest )
562     {
563         SiteNode node = this;
564         int dist = 0;
565         while( node != dest ) {
566             dist++;
567             node = node.parent;
568         }
569         return dist;
570     }
571
572     public SiteDocument getDocument( String JavaDoc shortName )
573     {
574         SiteDocument result = (SiteDocument) docNameHash.get( shortName );
575         if (result != null)
576             return result;
577             
578         if (parent != null)
579             return parent.getDocument(shortName);
580         
581         return null;
582     }
583         
584     public TKVector createGenNodes( GenNode parent )
585     {
586         if( type == NODE_TYPE_GROUP ) {
587             genNodes = getGroupGenNodes( parent );
588         }
589         else if( type == NODE_TYPE_SINGLE ) {
590             genNodes = getSingleGenNodes( parent );
591         }
592         else {
593             throw new Error JavaDoc( "unexpected node type "+type );
594         }
595         return genNodes;
596     }
597
598     // singh + 1999.03.25
599
public GenNode makeGenNode (SiteContent primContent)
600     {
601
602         String JavaDoc genId = "id_"+id+"_pid_"+(primContent == null ? -1 : primContent.getContentId());
603         GenNode node = context.genNodes.getGenNode(genId);
604         if (node != null) return node;
605
606         boolean old = context.genNodes.isRecursiveBuild();
607         context.genNodes.setRecursiveBuild(false);
608
609         node = parent != null ? parent.makeGenNode (null) : null;
610
611         if( type == NODE_TYPE_GROUP ) {
612
613             if( primContent == null ) return null;
614
615             String JavaDoc nodeName = primContent.getShortName();
616             if( nodeName == null ) { nodeName = "id_"+primContent.getContentId(); }
617             // alex
618
node = new GenNode(context,this,node,nodeName,primContent.getName(), realDocuments,primContent,0);
619             // node = new GenNode(context,this,node,nodeName,primContent.name, documents,primContent,0);
620
context.genNodes.putGenNode(genId,node);
621
622         } else if( type == NODE_TYPE_SINGLE )
623         {
624             // alex
625
node = new GenNode(context,this,node,shortName,name, realDocuments,null,0);
626             //node = new GenNode(context,this,node,shortName,name, documents,null,0);
627
context.genNodes.putGenNode(genId,node);
628
629         } else {
630             throw new Error JavaDoc( "unexpected node type "+type );
631         }
632
633         context.genNodes.setRecursiveBuild(old);
634         return node;
635     }
636
637     public TKVector getGroupGenNodes( GenNode parent )
638     {
639         TKVector primContents;
640         if( primaryContentSelectionKey != null ) {
641             primContents = context.integrations.getSelection( primaryContentSelectionKey );
642         }
643         else {
644             primContents = primaryContentNode.getContents();
645         }
646         int size = primContents.size();
647         if( size == 0 ) return new TKVector();
648
649         TKVector result = new TKVector( size );
650         Enumeration e = primContents.elements();
651         int i=0;
652         while( e.hasMoreElements() ) {
653             SiteContent primContent = (SiteContent) e.nextElement();
654             if( primContent == null ) {
655                 cat.warn( "got null as "+i+".th content of groupnode "+primaryContentNode.getName() );
656                 continue;
657             }
658             String JavaDoc nodeName = primContent.getShortName();
659             
660             if( nodeName == null ) {
661                 cat.warn("no shortname for groupcontent "+primContent.getContentId() );
662                 nodeName = "id_"+primContent.getContentId();
663             }
664             result.addElement(
665                 new GenNode(
666                     context,
667                     this,
668                     parent,
669                     nodeName,
670                     primContent.getName(),
671                     realDocuments,
672                     // documents, alex
673
primContent,
674                     i++
675                 )
676             );
677         }
678         return result;
679     }
680
681     public TKVector getSingleGenNodes( GenNode parent )
682     {
683         TKVector result = new TKVector();
684         result.addElement(
685             new GenNode(
686                 context,
687                 this,
688                 parent,
689                 shortName,
690                 name,
691                 realDocuments,
692                 // documents, alex
693
null,
694                 0
695             )
696         );
697         return result;
698     }
699
700     public TKVector getSubGenNodes( GenNode parent )
701     {
702         TKVector result = new TKVector();
703         Enumeration e = childs.elements();
704         while( e.hasMoreElements() ) {
705             result.concat( ((SiteNode) e.nextElement()).createGenNodes( parent ) );
706         }
707         return result;
708     }
709
710     // singh + 1999.04.08
711
public void complete () {
712
713         if (isComplete) return;
714         isComplete = true;
715
716         if (parent != null) parent.complete();
717
718         doContentSelections ();
719     }
720     // singh - 1999.04.08
721

722
723     public void accept(SiteTreeVisitor visitor) throws Exception JavaDoc
724     {
725         visitor.visitSiteNode(this);
726         for (int i = 0; i < documents.size(); ++i)
727         {
728             SiteDocument doc = (SiteDocument) documents.elementAt(i);
729             if (doc != null)
730             {
731                 doc.accept(visitor);
732             }
733         }
734         for (int i = 0; i < childs.size(); ++i)
735         {
736             ((SiteNode) childs.elementAt(i)).accept(visitor);
737         }
738     }
739     
740     /**
741     Assigns inheritable documents to their valid site nodes.
742     
743     Internally: fills the <code>docNameHash</doc> of all site nodes with the
744     appropriate inherited documents.
745     
746     Background: After initial construction of the site tree, inheritable documents
747     are only assigned to the site nodes of their first appearance (the
748     <code>documents</code> member variable).
749     By calling this method, these documents are assigned to all site nodes,
750     which actually fit ro the documents inheritance intervall.
751     */

752     public void assignInheritableDocuments()
753     {
754         InheritableDocumentsManager docMan = new InheritableDocumentsManager();
755         assignInheritableDocuments(docMan);
756     }
757     
758     /**
759     Recursively assigns inheritable documents to their valid site nodes.
760     @param docMan an instance of the InheritableDocumentsManager containing all
761     documents of the site nodes above <code>this</code>.
762     */

763     private void assignInheritableDocuments(InheritableDocumentsManager docMan)
764     {
765         docMan.descend();
766         Enumeration docs = documents.elements();
767         while (docs.hasMoreElements()) {
768             docMan.addDocument((SiteDocument) docs.nextElement());
769         }
770                 
771         Enumeration validDocs = docMan.getValidDocuments();
772         while (validDocs.hasMoreElements()) {
773             SiteDocument doc = (SiteDocument) validDocs.nextElement();
774             
775             if (doc.isInheritable()) {
776                 SiteDocument resultDoc = null;
777                 if (doc.getAnchor() == this) {
778                     // The document is already a document of this site node...
779
resultDoc = doc;
780                     if (doc.getInheritStartLevel() == 0)
781                     {
782                         realDocuments.addElement(resultDoc);
783                     }
784                 }
785                 else
786                 {
787                     int newInheritStartLevel = doc.getInheritStartLevel()
788                             + doc.getAnchor().getLevel() - getLevel();
789                     if (newInheritStartLevel < 0)
790                         newInheritStartLevel = 0;
791                             
792                     int newInheritStopLevel;
793                     if (doc.getInheritStopLevel() == -1) { // infinite inheritance
794
newInheritStopLevel = -1;
795                     }
796                     else {
797                         newInheritStopLevel = doc.getInheritStopLevel()
798                             + doc.getAnchor().getLevel() - getLevel();
799                     }
800                     resultDoc = new SiteDocument(context, doc, this, documents.size(),
801                         newInheritStartLevel , newInheritStopLevel);
802                     documents.addElement( resultDoc ); // ??? korrekt ??
803
if (resultDoc.getInheritStartLevel() == 0/*resultDoc.inheritStartLevel <= doc.anchor.getLevel() && resultDoc.inheritStopLevel <= doc.anchor.getLevel()*/)
804                     {
805                         realDocuments.addElement(resultDoc);
806                     }
807                     
808                 }
809                 docNameHash.put( resultDoc.getShortName(), resultDoc );
810                //realDocuments.addElement(resultDoc);
811
}
812             else
813             {
814                 docNameHash.put( doc.getShortName(), doc );
815                 realDocuments.addElement(doc);
816             }
817         }
818
819         //recursion:
820
Enumeration e = childs.elements();
821         while( e.hasMoreElements() ) {
822             SiteNode child = (SiteNode) e.nextElement();
823             child.assignInheritableDocuments(docMan);
824         }
825         docMan.ascend();
826     }
827 }
828
829 /**
830 Internal class used during site tree traversal to assign inheritable documents
831 to the site nodes coording to their inheritance intervall.
832
833 To use this class, create an instance of it in the root node of your site tree
834 and continously "update" it while traversing the site node tree:
835
836 If you enter a site node, call <code>descend</code>, then add all local
837 documents (as stored in the <code>documents</code> member variable via
838 <code>addDocument</code> of each site node.
839 Now you can call _valid_ documents of each site node by calling
840 <code>getValidDocuments</code>.
841 If you leave a site node, call <code>ascend</code>.
842  * @author $Author: alex $
843  * @version $Revision: 1.3 $
844
845 */

846 class InheritableDocumentsManager
847 {
848     private Vector docVectors; // the vector of all doc-vectors
849
private Vector curValidDocs; // the valid docs of the current level
850
private int curLevel; // the current absolute level (root is 0)
851

852     InheritableDocumentsManager()
853     {
854         setDocVectorSize(3);
855         curLevel = -1;
856         curValidDocs = new Vector();
857     }
858     
859     /**
860     Must be called when descending into a site node during site tree traversal
861     @param node the site node which is entered
862     */

863     void descend()
864     {
865         Vector prevDocs = getDocs(curLevel);
866         Vector nextDocs = getDocs(curLevel+1);
867         nextDocs.setSize(0);
868         if (prevDocs != null) {
869             for (int i = 0; i < prevDocs.size(); ++i) {
870                 SiteDocument doc = (SiteDocument) prevDocs.elementAt(i);
871                 int classification = doc.classifyByLevel(curLevel + 1);
872                 if (classification <= 0) {
873                     nextDocs.addElement(doc);
874                 }
875             }
876         }
877         ++curLevel;
878         
879         findValidDocs();
880     }
881     
882     /**
883     Must be called when ascending out of a site node during site tree traversal.
884     */

885     void ascend()
886     {
887         if (curLevel == 0)
888             return; //todo: error!!?
889

890         --curLevel;
891         
892         findValidDocs();
893     }
894     
895     /**
896     Adds the document <code>doc</code> to the documents of the current level.
897     No check is done, if the document is alread inserted!
898     If <code>doc</code> is also a _valid_ document of the current level,
899     it is also added to the valid documents.
900     */

901     void addDocument(SiteDocument doc)
902     {
903         if (doc == null)
904             return;
905             
906         int classification = doc.classifyByLevel(curLevel);
907         if (classification <= 0) {
908             Vector docs = getDocs(curLevel);
909             for (int i = 0; i < docs.size(); i++)
910             {
911                 SiteDocument curDoc = (SiteDocument)docs.elementAt(i);
912                 if (curDoc.getShortName().equals(doc.getShortName()))
913                     docs.removeElementAt(i);
914             }
915             for (int i = 0; i < curValidDocs.size(); i++)
916             {
917                 SiteDocument curDoc = (SiteDocument)curValidDocs.elementAt(i);
918                 if (curDoc.getShortName().equals(doc.getShortName()))
919                     curValidDocs.removeElementAt(i);
920             }
921             docs.addElement(doc);
922         }
923         if (classification == 0) {
924             
925             curValidDocs.addElement(doc);
926         }
927     }
928     
929     /**
930     Returns the valid docs of the current level.
931     */

932     Enumeration getValidDocuments()
933     {
934         return curValidDocs.elements();
935     }
936     
937     /**
938     @return the current absolute level
939     */

940     int getCurrentLevel()
941     {
942         return curLevel;
943     }
944         
945     private void findValidDocs()
946     {
947         Vector docs = getDocs(curLevel);
948
949         curValidDocs.setSize(0);
950         for (int i = 0; i < docs.size(); ++i) {
951             SiteDocument doc = (SiteDocument) docs.elementAt(i);
952             if (doc.classifyByLevel(curLevel) == 0) {
953                 curValidDocs.addElement(doc);
954             }
955         }
956     }
957
958     private Vector getDocs(int index)
959     {
960         if (index >= docVectors.size()) {
961             setDocVectorSize(index + 1);
962         }
963         
964         Vector docs;
965         try {
966             docs = (Vector) docVectors.elementAt(index);
967         }
968         catch (ArrayIndexOutOfBoundsException JavaDoc e) {
969             return null; //this should not happen!
970
}
971                 
972         return docs;
973     }
974     
975     
976     private void setDocVectorSize(int size)
977     {
978         int prevSize = (docVectors == null) ? 0 : docVectors.size();
979         if (size > prevSize) {
980             if (docVectors == null) {
981                 docVectors = new Vector(size);
982             }
983             docVectors.setSize(size);
984             for (int i = prevSize; i < size; ++i) {
985                 docVectors.setElementAt(new Vector(), i);
986             }
987         }
988     }
989     
990 }
991
Popular Tags