KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > webman > field > TKContentReferenceField


1 package com.teamkonzept.webman.field;
2
3 import com.teamkonzept.webman.mainint.*;
4 import com.teamkonzept.webman.mainint.db.queries.content.SelectGroupContentChilds;
5 //import com.teamkonzept.webman.mainint.events.CEEventHandler;
6
// import com.teamkonzept.webman.mainint.events.contentedit.CEUtils;
7

8 import com.teamkonzept.lib.*;
9 import com.teamkonzept.publishing.markups.*;
10 import com.teamkonzept.web.*;
11 import com.teamkonzept.field.db.queries.*;
12 import com.teamkonzept.field.db.*;
13 import com.teamkonzept.db.*;
14 import com.teamkonzept.field.*;
15 import de.webman.generator.*;
16 import de.webman.content.workflow.*;
17 import java.sql.*;
18 import java.util.*;
19 import com.teamkonzept.international.LanguageManager;
20 import org.w3c.dom.*;
21 import org.apache.log4j.Category;
22 import de.webman.content.Content;
23 import de.webman.content.workflow.ContentVersion;
24 import de.webman.util.log4j.WebmanCategory;
25
26 /**
27  * The content reference field control.
28  *
29  * @author $Author: uli $
30  * @version $Revision: 1.57 $
31  */

32 public class TKContentReferenceField
33     extends TKAtomField
34 {
35
36     /**
37      * The logging category.
38      */

39     private static WebmanCategory cat = (WebmanCategory)WebmanCategory.getInstance(TKContentReferenceField.class);
40
41     /**
42      * The class identifier.
43      */

44     public static final String JavaDoc CLASS_ID = "CONTENTREF";
45
46     /**
47      * key for nodes
48      **/

49     public static final String JavaDoc NODE_KEY = "NODE";
50
51     /**
52      * key for multiple key
53      **/

54     public static final String JavaDoc MULTIPLE_KEY = "MULTIPLE";
55
56     /** Key fuer nur Namen holen */
57     public static final String JavaDoc NAME_ONLY_KEY = "NAME_ONLY";
58
59     public static final String JavaDoc YES_VALUE = "YES";
60     public static final String JavaDoc NO_VALUE = "NO";
61
62
63     protected int contentNodeId;
64     protected boolean multipleSelection;
65     protected boolean deliverOnlyNames;
66
67
68     /**
69      * Creates an empty content reference field control.
70      */

71     public TKContentReferenceField ()
72     {
73         super();
74     }
75
76     public TKContentReferenceField( String JavaDoc name, int contentNodeId )
77     {
78         this( name, contentNodeId, null );
79     }
80
81     public TKContentReferenceField( String JavaDoc name, int contentNodeId, String JavaDoc showName )
82     {
83         this( name, contentNodeId, true, showName );
84     }
85
86     public TKContentReferenceField( String JavaDoc name, int contentNodeId, boolean deliverOnlyNames, String JavaDoc showName )
87     {
88         this( name, contentNodeId, deliverOnlyNames, false, showName );
89     }
90
91     public TKContentReferenceField(
92         String JavaDoc name,
93         int contentNodeId,
94         boolean deliverOnlyNames,
95         boolean multipeSelection,
96         String JavaDoc showName
97     )
98     {
99         initContentReferenceField( CLASS_ID, name, contentNodeId, deliverOnlyNames, multipeSelection, showName );
100     }
101
102     /**
103      * tja, irgendne intitialisierung halt oder?
104      * @param type ...
105      * @param name ...
106      * @param contentNodeId ...
107      * @param deliverOnlyNames ...
108      * @param multipleSelection ...
109      * @param showName ...
110      **/

111     public final void initContentReferenceField(
112         String JavaDoc type,
113         String JavaDoc name,
114         int contentNodeId,
115         boolean deliverOnlyNames,
116         boolean multipleSelection,
117         String JavaDoc showName
118     )
119     {
120         initAtomField( type, name, showName );
121         this.contentNodeId = contentNodeId;
122         this.deliverOnlyNames = deliverOnlyNames;
123         this.multipleSelection = multipleSelection;
124     }
125
126     /**
127      * Methode zur Definition eines Eingabefeldes
128      */

129     public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) {
130
131         TKVector multipleOptions = new TKVector(2);
132         multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "YES"), YES_VALUE ) );
133         multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "NO"), NO_VALUE ) );
134
135         TKBaseField [] inputArray = {
136             new TKInputField( NAME_KEY, TKContentSiteReferenceField.WIDTH_ONE, TKContentSiteReferenceField.WIDTH_TWO, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NAME"), TKInputField.CHECK_STRING),
137             new TKInputField( SHOW_NAME_KEY, TKContentSiteReferenceField.WIDTH_THREE, TKContentSiteReferenceField.WIDTH_FOUR, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_SHOWNAME"), TKInputField.CHECK_STRING),
138             //new TKInputField( NODE_KEY, 7, 7, "NodeID des Gruppenknotens" ),
139
new TKContentNodeTypeSelectorField( NODE_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NODE"), TKContentNodeTypeSelectorField.GROUP_NODE_TYPE ),
140             new TKCheckField( MULTIPLE_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_MULTIPLE"), multipleOptions, false ),
141             new TKCheckField( NAME_ONLY_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NAME_ONLY"), multipleOptions, false )
142         };
143         TKFieldGroup inputGroup =
144             new TKFieldGroup( TKContentReferenceField.CLASS_ID, new TKVector( inputArray ), LanguageManager.getText(LANGUAGE_CONTEXT, TKContentReferenceField.CLASS_ID) );
145
146         return inputGroup;
147     }
148
149     /**
150         initialisiert das feld basierend auf den daten, die die feldgruppe von getDefGroup zurueckliefert
151     */

152     public void init( String JavaDoc classId, Object JavaDoc initData )
153         throws
154             TKUnregisteredClassException,
155             ClassNotFoundException JavaDoc,
156             InstantiationException JavaDoc,
157             IllegalAccessException JavaDoc
158     {
159         super.init( classId, initData );
160         TKHashtable data = (TKHashtable) initData;
161         contentNodeId = Integer.parseInt( (String JavaDoc) data.get(NODE_KEY) );
162         multipleSelection = ((String JavaDoc)data.get(MULTIPLE_KEY)).equals( YES_VALUE );
163         deliverOnlyNames = ((String JavaDoc)data.get(NAME_ONLY_KEY)).equals( YES_VALUE );
164     }
165
166     /**
167      * Returns the internal representation of this field.
168      *
169      * @return the internal representation of this field.
170      */

171     public Object JavaDoc toData ()
172     {
173         TKHashtable result = (TKHashtable) super.toData();
174         result.put( NODE_KEY, String.valueOf( contentNodeId ) );
175         result.put( MULTIPLE_KEY, ( multipleSelection ? YES_VALUE : NO_VALUE ) );
176         result.put( NAME_ONLY_KEY, ( deliverOnlyNames ? YES_VALUE : NO_VALUE ) );
177         return result;
178     }
179
180     /**
181      * Fills the data held by this field into the specified template.
182      *
183      * @param template the template.
184      * @param value a value ?
185      * @param prefix a prefix ?
186      */

187     public void fillIntoTemplate (TKHTMLTemplate template,
188                                   Object JavaDoc value,
189                                   String JavaDoc prefix)
190     {
191         try
192         {
193             super.fillIntoTemplate(template, value, prefix);
194             template.set(NODE_KEY, String.valueOf(this.contentNodeId));
195
196             if (this.multipleSelection)
197             {
198                 template.set(MULTIPLE_KEY, Boolean.TRUE);
199             }
200
201             // Initialize workflow.
202
VersionStatics statics = VersionStatics.setup();
203             TKHashtable statusPool = VersionStatus.selectSingles(statics, false);
204             TKHashtable filter = VersionSelection.initContentFilter(statusPool);
205             TKVector filteredInstances = new TKVector();
206
207             // Get all instances.
208
TKVector allInstances = VersionCache.getVersionInfo(new Integer JavaDoc(this.contentNodeId), false);
209
210             for (int i = 0; i < allInstances.size(); i++)
211             {
212                 // Apply filter.
213
Content content = (Content) allInstances.elementAt(i);
214
215                 if (content.filterTransitions(filter, "SRC"))
216                 {
217                     TKHashtable entry = new TKHashtable();
218                     entry.put("CONTENT_NODE_SHORTNAME", content.getShortName());
219                     entry.put("CONTENT_NODE_NAME", content.getName());
220
221                     filteredInstances.addElement(entry);
222                 }
223             }
224
225             // Publish list to template.
226
template.setListIterator(new TKStandardPluginIterator(prefix + getName(),
227                                                                   null,
228                                                                   filteredInstances,
229                                                                   false,
230                                                                   template.getListIterator()));
231         }
232         catch (Exception JavaDoc e)
233         {
234             cat.error(e);
235         }
236     }
237
238     /**
239      * fuehrt die eigentliche Ermittlung der ContentReferenzen bei der Generierung durch
240      * @param data entweder einzelner Shortname oder Liste von Namen
241      * @param context der Generierungscontext
242     */

243     public TKVector getSelectedContents( Object JavaDoc data, GeneratorContext context )
244     {
245         if (data == null)
246             return new TKVector();
247         TKVector searchNames;
248         if( data instanceof TKVector ) {
249             searchNames = (TKVector) data;
250         }
251         else {
252             searchNames = new TKVector(1);
253             searchNames.addElement( data );
254         }
255
256         cat.debug("Got selected contents : " + searchNames);
257         TKVector contentNodes = new TKVector();
258
259         SiteContentNode node = context.contentNodes.getContentNode(contentNodeId);
260         if (node == null)
261         {
262              cat.error("CONTENT_REFERENCE " + getName() + " points to invalid group node: " + contentNodeId);
263              return contentNodes;
264         }
265         TKVector contents = node.getContents();
266         for ( int i= 0; i < contents.size(); i++)
267         {
268             SiteContent siteContent = (SiteContent)contents.elementAt(i);
269             String JavaDoc shortName = siteContent.getShortName();
270             int idx = -1;
271             if (shortName != null)
272                 idx = searchNames.indexOf(shortName);
273
274             if (idx != -1)
275             {
276                 while( contentNodes.size() <= idx ) {
277                     contentNodes.addElement( null );
278                 }
279                 if (siteContent != null && siteContent.isGeneratableVersion()) {
280                     cat.debug(" Added " + shortName + " at " + idx);
281                     contentNodes.setElementAt( siteContent, idx );
282                 }
283                 else if (siteContent == null) {
284                     cat.error("Content: " + shortName + " is not referenced in the sitetree - " +
285                               "content reference is not generated!");
286                 }
287                 else if (!siteContent.isGeneratableVersion()) {
288                     cat.warn("Content: " + shortName + " is not released - " +
289                              "content reference is not generated!");
290                 }
291             }
292         }
293
294         // liste aufraeumen
295
for (int i = 0; i < contentNodes.size(); ) {
296             if (contentNodes.elementAt(i) == null) {
297                 contentNodes.removeElementAt(i);
298             }
299             else {
300                 i++;
301             }
302         }
303
304         return contentNodes;
305     }
306
307     public void fillIntoPresentation( TKHTMLTemplate t, Object JavaDoc data, String JavaDoc scope )
308     {
309         GeneratorContext context = GeneratorContext.setup ();
310
311         context.push();
312         try {
313             TKVector contents = getSelectedContents(data, context);
314             if (contents == null || contents.size() == 0) {
315                 super.fillIntoPresentation(t, new TKVector(), scope);
316                 return;
317             }
318             if( deliverOnlyNames && contents != null && contents.size() > 0) {
319                 if (multipleSelection) {
320                     t.set( scope, Integer.toString(contents.size()));
321                     t.set(scope + ".SIZE", Integer.toString(contents.size()));
322                     t.setListIterator(
323                                       new GenContentIterator(
324                                                              context, contents, scope,
325                                                              t.getListIterator(),
326                                                              false, true, true)
327                                           );
328                 }
329                 else {
330                     SiteContent content = (SiteContent) contents.firstElement();
331
332                     if (content != null) {
333                         content.fillNameIntoTemplate(t,scope + "." + getName());
334                         super.fillIntoPresentation(t, content.getShortName(), scope);
335                     }
336                 }
337                 return;
338             }
339             scope += "." + getName();
340
341             if (contents != null && contents.size() > 0) {
342                 if( multipleSelection ) {
343                     t.set( scope, Integer.toString(contents.size()));
344
345                     /* gck: hab das mal auch so eingefügt, daß man immer
346                        auf TK_SCOPE.xxx.SIZE abfragen kann. Ansonsten geht das
347                        mit ".SIZE" nämlich nur für ~leere~ content referenz
348                        auswahlen, bei content referenz auswahlen ~mit inhalt~
349                        beinhaltet nur der SCOPE.xxx die länge der Liste. Wer
350                        hat sich das ausgedacht?? */

351                     t.set(scope + ".SIZE", Integer.toString(contents.size()));
352
353                     t.setListIterator(
354                                       new GenContentIterator(
355                                                              context, contents, scope,
356                                                              t.getListIterator(),
357                                                              false, true, false)
358                                           );
359                 }
360                 else {
361                     SiteContent content = (SiteContent) contents.firstElement();
362                     if (content != null) {
363                         content.fillNameIntoTemplate(t, scope);
364                         if (!ReferenceCycleDetector.isCycle(new Integer JavaDoc(content.getInstanceId())))
365                             content.fillIntoTemplate(t, scope);
366                     }
367                 }
368                 return;
369             }
370             // Kein Content gefunden -> leerer Listeneintrag !
371
// TKVector names = new TKVector();
372
}
373         finally {
374             context.pop();
375         }
376     }
377
378     /**
379      * ueberschrieben, um ContentRelationen anlegen zu können
380      */

381     public int insertDataIntoDB( TKContentDBData db, Object JavaDoc data, int contentId, int leftNr )
382     {
383        return super.insertDataIntoDB(db, data, contentId, leftNr);
384     }
385
386     protected void fillDataIntoNode(Document doc, Element node, Object JavaDoc data) throws DOMException
387     {
388         if( deliverOnlyNames ) {
389             super.fillDataIntoNode( doc, node, data );
390             return;
391         }
392         GeneratorContext context = GeneratorContext.setup ();
393
394         TKVector contents = getSelectedContents(data, context);
395         if (contents.size() > 1) {
396             node.setAttribute(MULTIPLE, Boolean.TRUE.toString());
397         }
398         else {
399             node.setAttribute(MULTIPLE, Boolean.FALSE.toString());
400         }
401
402         // Toplevel Content Element
403
Element me = doc.createElement("content");
404         node.appendChild(me);
405
406         if (multipleSelection) {
407             for (int i = 0; i < contents.size(); i++) {
408                 try {
409                     context.push();
410                     SiteContent content = (SiteContent) contents.elementAt(i);
411                     if (!ReferenceCycleDetector.isCycle(new Integer JavaDoc(content.getInstanceId()))) {
412                         content.fillBasicDOM(doc, me);
413                     }
414                 }
415                 finally {
416                     context.pop();
417                 }
418             }
419         }
420         else {
421             SiteContent content = (SiteContent) contents.firstElement();
422             if (!ReferenceCycleDetector.isCycle(new Integer JavaDoc(content.getInstanceId()))) {
423                 context.push();
424                 try {
425                     content.fillBasicDOM(doc, me);
426                 }
427                 finally {
428                     context.pop();
429                 }
430                 return;
431             }
432         }
433     }
434
435     public void initFromDB( String JavaDoc classId, TKFormDBData db, TKVector otherFields )
436         throws
437             TKUnregisteredClassException,
438             ClassNotFoundException JavaDoc,
439             InstantiationException JavaDoc,
440             IllegalAccessException JavaDoc
441     {
442         super.initFromDB( classId, db, otherFields );
443         // contentNodeId = getIntFieldAttribute( db, NODE_KEY, 0 );
444
contentNodeId = Integer.parseInt( getFieldAttribute( db, NODE_KEY, 0 ) );
445         multipleSelection = false;
446         multipleSelection = Boolean.valueOf(getFieldAttribute( db, MULTIPLE_KEY, 0)).booleanValue();
447         deliverOnlyNames= Boolean.valueOf(getFieldAttribute( db, NAME_ONLY_KEY, 0)).booleanValue();
448     }
449
450     public int realInsertIntoDB( TKFormDBData db, int formId )
451     {
452         if( super.realInsertIntoDB( db, formId ) == -1 ) return -1;
453         insertNewFieldAttribute( db, formId, NODE_KEY, 0, String.valueOf( contentNodeId ) );
454         insertNewFieldAttribute( db, formId, MULTIPLE_KEY, 0, String.valueOf( multipleSelection ) );
455         insertNewFieldAttribute( db, formId, NAME_ONLY_KEY, 0, String.valueOf( deliverOnlyNames ) );
456         return fieldId;
457     }
458
459     public Object JavaDoc compileData( String JavaDoc prefix, TKMarkupNode data, TKHashtable context )
460     {
461
462         TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup;
463         if (markup == null) { return null;}
464
465         if (!markup.name.equals (getName())) {
466             return null;
467         }
468
469         TKXmlTree tree = (TKXmlTree) data.tree;
470         if (tree == null) { return null;}
471
472         String JavaDoc val = tree.getSingleText ();
473
474         if (val == null) return getDefault();
475         else return val.trim();
476     }
477
478
479     /**
480      * Checks wether this object and the specified object
481      * may be treated as equal.
482      *
483      * @param object the object to checked for equality.
484      * @return <CODE>true</CODE> if this object and the
485      * specified object may be treated as equal, otherwise
486      * <CODE>false</CODE>.
487      */

488     public boolean equals (Object JavaDoc object)
489     {
490         if (! super.equals(object))
491         {
492             return false;
493         }
494
495         TKContentReferenceField field = (TKContentReferenceField) object;
496
497         return (this.contentNodeId == field.contentNodeId) &&
498                (this.multipleSelection == field.multipleSelection) &&
499                (this.deliverOnlyNames == field.deliverOnlyNames);
500     }
501
502     /**
503      * Returns the hash code for this object.
504      *
505      * @return the hash code for this object.
506      */

507     public int hashCode ()
508     {
509         // Implementation for JTest only ;-(
510
return super.hashCode();
511     }
512
513 }
514
Popular Tags