KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.teamkonzept.webman.field;
2
3 import com.teamkonzept.webman.mainint.*;
4 // import com.teamkonzept.webman.mainint.events.CEEventHandler;
5
import de.webman.content.eventhandler.CEUtils;
6 import de.webman.content.workflow.*;
7 import de.webman.content.*;
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.util.log4j.WebmanCategory;
17 import com.teamkonzept.webman.mainint.db.queries.*;
18 import com.teamkonzept.webman.mainint.DatabaseDefaults;
19 import java.sql.*;
20 import java.util.*;
21 import com.teamkonzept.international.LanguageManager;
22 import org.w3c.dom.*;
23 import org.apache.log4j.Category;
24
25 /**
26  * The content site reference field control.
27  *
28  * @author $Author: alex $
29  * @version $Revision: 1.43.2.1 $
30  */

31 public class TKContentSiteReferenceField
32     extends TKAtomField
33 {
34
35     /**
36      * The class identifier.
37      */

38     public static final String JavaDoc CLASS_ID = "SITE_CONTENT_REF";
39
40     /**
41      * key for forms
42      **/

43     public static final String JavaDoc FORM_KEY = "FORM";
44     public static final String JavaDoc PRESENTATION_KEY = "PRESENTATION";
45     public static final String JavaDoc NAME_ONLY_KEY = "NAME_ONLY";
46     public static final String JavaDoc CONTENT_NODE = "CONTENT_NODE";
47
48     /** default YES Wert */
49     public static final String JavaDoc YES_VALUE = "YES";
50     public static final String JavaDoc NO_VALUE = "NO";
51
52     /**
53      * A specific width constant.
54      */

55     public static final int WIDTH_ONE = 16;
56
57     /**
58      * A specific width constant.
59      */

60     public static final int WIDTH_TWO = 80;
61
62     /**
63      * A specific width constant.
64      */

65     public static final int WIDTH_THREE = 50;
66
67     /**
68      * A specific width constant.
69      */

70     public static final int WIDTH_FOUR = 254;
71
72     /**
73      * The logging category.
74      */

75     private static WebmanCategory cat = (WebmanCategory)WebmanCategory.getInstance(TKContentSiteReferenceField.class);
76
77     /** zugrundeliegendes Formular */
78     protected int formularId;
79
80     /** zugrundeliegender Documenttyp */
81     protected int presentationId;
82
83     /** vom benutzer gewählter Knoten, ist eigentlich die INSTANZ ID !!!! */
84     protected int instanceId;
85
86     /** sollen nur Namen geholt werden ? */
87     protected boolean deliverOnlyNames = true;
88
89     /**
90      * Creates an empty field.
91      */

92     public TKContentSiteReferenceField ()
93     {
94         super();
95     }
96
97     public TKContentSiteReferenceField(
98         String JavaDoc name,
99         int formId,
100         int presId,
101         String JavaDoc showName
102     )
103     {
104         initField( CLASS_ID, name, formId, presId, showName );
105     }
106
107     public final void initField(
108         String JavaDoc type,
109         String JavaDoc name,
110         int formId,
111         int presID,
112         String JavaDoc showName
113     )
114     {
115         initAtomField( type, name, showName );
116         this.formularId = formId;
117         this.presentationId = presID;
118     }
119
120     /**
121      * Methode zur Definition eines Eingabefeldes
122      */

123     public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList)
124     {
125         TKVector multipleOptions = new TKVector(2);
126         multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "YES"), YES_VALUE ) );
127         multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "NO"), NO_VALUE ) );
128
129         TKBaseField [] inputArray = {
130             new TKInputField( NAME_KEY, WIDTH_ONE, WIDTH_TWO, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_SITE_REF_NAME"), TKInputField.CHECK_STRING),
131             new TKInputField( SHOW_NAME_KEY, WIDTH_THREE, WIDTH_FOUR, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_SITE_REF_SHOWNAME"), TKInputField.CHECK_STRING),
132             new TKFormTypeSelectorField( FORM_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_FORM"), TKFormTypeSelectorField.CONTENT_FORM_TYPE),
133             new TKDocumentTypeSelectorField( PRESENTATION_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_PRES") ),
134             new TKCheckField( NAME_ONLY_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NAME_ONLY"), multipleOptions, false )
135         };
136         TKFieldGroup inputGroup =
137             new TKFieldGroup( CLASS_ID, new TKVector( inputArray ), LanguageManager.getText(LANGUAGE_CONTEXT, CLASS_ID) );
138
139         return inputGroup;
140     }
141
142     /**
143         initialisiert das feld basierend auf den daten, die die feldgruppe von getDefGroup zurueckliefert
144     */

145     public void init( String JavaDoc classId, Object JavaDoc initData )
146         throws
147             TKUnregisteredClassException,
148             ClassNotFoundException JavaDoc,
149             InstantiationException JavaDoc,
150             IllegalAccessException JavaDoc
151     {
152         super.init( classId, initData );
153         TKHashtable data = (TKHashtable) initData;
154         formularId = Integer.parseInt( (String JavaDoc) data.get(FORM_KEY) );
155         presentationId = Integer.parseInt( (String JavaDoc) data.get(PRESENTATION_KEY) );
156         deliverOnlyNames = ((String JavaDoc)data.get(NAME_ONLY_KEY)).equals( YES_VALUE );
157     }
158
159     /**
160      * Returns the internal representation of this field.
161      *
162      * @return the internal representation of this field.
163      */

164     public Object JavaDoc toData ()
165     {
166         TKHashtable result = (TKHashtable) super.toData();
167         result.put( FORM_KEY, String.valueOf( formularId ) );
168         result.put( PRESENTATION_KEY, String.valueOf( presentationId ) );
169         result.put( NAME_ONLY_KEY, ( deliverOnlyNames ? YES_VALUE : NO_VALUE ) );
170         return result;
171     }
172
173     /**
174      * Fills the data held by this field into the specified template.
175      *
176      * @param template the template.
177      * @param value a value ?
178      * @param prefix a prefix ?
179      */

180     public void fillIntoTemplate (TKHTMLTemplate template,
181                                   Object JavaDoc value,
182                                   String JavaDoc prefix)
183     {
184         try
185         {
186             super.fillIntoTemplate(template, value, prefix);
187
188             // Initialize workflow.
189
VersionStatics statics = VersionStatics.setup();
190             TKHashtable statusPool = VersionStatus.selectSingles(statics, false);
191             TKHashtable filter = VersionSelection.initContentFilter(statusPool);
192             TKVector filteredInstances = new TKVector();
193
194             // Select referenced contents.
195
TKQuery query = TKDBManager.newQuery(TKDBContentTreeGetFormType2.class);
196             query.setQueryParams("CONTENT_NODE_TYPE", DatabaseDefaults.SINGLE_INTEGER);
197             query.setQueryParams("CONTENT_FORM", new Integer JavaDoc(this.formularId));
198             query.execute();
199
200             ResultSet result = query.fetchResultSet();
201
202             while (result.next())
203             {
204                 // Apply filter.
205
Content content = VersionCache.getContentInfo(new Integer JavaDoc(result.getInt("CONTENT_NODE_ID")));
206
207                 if (content != null && content.filterTransitions(filter, "SRC"))
208                 {
209                     TKHashtable entry = new TKHashtable();
210                     entry.put("INSTANCE_ID", content.getInstanceId());
211                     entry.put("CONTENT_NODE_NAME", content.getName());
212
213                     filteredInstances.addElement(entry);
214                 }
215             }
216
217             // Publish list to template.
218
template.setListIterator(new TKStandardPluginIterator(prefix + getName(),
219                                                                   null,
220                                                                   filteredInstances,
221                                                                   false,
222                                                                   template.getListIterator()));
223         }
224         catch (Exception JavaDoc e)
225         {
226             cat.error(e);
227         }
228     }
229
230
231     public void fillIntoPresentation( TKHTMLTemplate t, Object JavaDoc data, String JavaDoc scope )
232     {
233         String JavaDoc scopedName = scope+"."+getName();
234         Vector daten = getInformations(data);
235         if (daten != null)
236         {
237             t.set(scopedName + ".REF", daten.elementAt(0));
238             t.set(scopedName + ".CONTENT_NAME", daten.elementAt(1));
239             t.set(scopedName + ".DOC", daten.elementAt(2));
240             t.set(scopedName + ".REF_NODE_PATH", daten.elementAt(4));
241             if (!deliverOnlyNames) {
242                 GeneratorContext context = GeneratorContext.setup ();
243                 try {
244                     context.push();
245
246                     SiteContent sc = (SiteContent)daten.elementAt(3);
247                     if (!ReferenceCycleDetector.isCycle(new Integer JavaDoc(sc.getInstanceId()))) {
248                         sc.fillNameIntoTemplate(t, scopedName);
249                         sc.fillIntoTemplate(t, scopedName);
250                     }
251                 }
252                 finally {
253                     context.pop();
254                 }
255             }
256         }
257     }
258
259     /**
260         liefert die entsprechenden Info's in einem Vector zurueck
261         null falls nichts gefunden
262         sonst:
263         1. Eintrag: Link auf das Dokument
264         2. Eintrag: Interner Name des Contents
265         3. Eintrag: Name des Documents
266         4. Eintrag: der eigentliche Content
267         5. Eintrag: REF_NODE_PATH
268     */

269     private Vector getInformations(Object JavaDoc data)
270     {
271         if (data == null || data.equals(""))
272             return null;
273         Integer JavaDoc instanceID = new Integer JavaDoc((String JavaDoc)data);
274         // falls ID= 0 ist nichts ausgewaehlt !
275
if (instanceID.intValue() == 0)
276             return null;
277         // Setup des Sitetrees
278
GeneratorContext context = GeneratorContext.setup ();
279         context.contentCalls.setup();
280
281         SiteContent sc = null;
282         sc = (SiteContent) context.siteContents.getContent(instanceID.intValue());
283         if (sc == null)
284         {
285             cat.error("Content of site reference " + getName() + " is not referenced in the Site-structure - content site reference not generated!");
286             return null; //Content nicht im Sitetree referenziert
287
}
288         Vector back = new Vector();
289
290
291         TKVector list = context.contentCalls.getCalls(instanceID);
292         if (list == null)
293         {
294             cat.error("No Contents of site reference " + getName() +
295                       " are released - site reference not generated!", WebmanCategory.ALL);
296             return null; // keine Calls ?
297
}
298         ContentCall match = null;
299
300         // Liste durchgehen und passendes ContentCall heraussuchen
301
for (int i = 0; i < list.size(); i++)
302         {
303             ContentCall dummy = (ContentCall)list.elementAt(i);
304             if (dummy.getDocument().getPresentationId() == presentationId && !dummy.isPerReference())
305             {
306                 match = dummy;
307                 break;
308             }
309         }
310
311         if (match != null)
312         {
313             String JavaDoc path = match.getDocument().path();
314             SiteNode node = match.getDocument().getAnchor();
315
316             TKVector genNodes = node.getGenNodes();
317             if (GeneratorContext.isPreviewMode())
318                 match.getDocument().complete();
319             String JavaDoc docName = match.getDocument().getShortName();
320             String JavaDoc contentName = sc.getShortName();
321             path = node.getPath();
322             String JavaDoc fullLink;
323             GenNode destNode = null;
324             /* Integration GROUP oder SINGLE ???
325             if (match.integration.integrationType == 2)
326                 fullLink = path.replace('.', '/') + contentName + "/" + docName;
327             else
328                 fullLink = path.replace('.', '/')+ docName;
329             */

330             if (GeneratorContext.isPreviewMode()) {
331                 destNode = node.makeGenNode(sc);
332             }
333             else {
334                 if (match.isPrimaryContent()) {
335                     for (int i = 0; i < genNodes.size(); i++) {
336                         destNode = (GenNode)genNodes.elementAt(i);
337                         if (destNode.getPrimaryContent().getInstanceId() == instanceID.intValue()) {
338                             break;
339                         }
340                         destNode = null;
341                     }
342                 }
343                 else {
344                     destNode = (GenNode)genNodes.elementAt(0);
345                 }
346             }
347             if (GeneratorContext.isPreviewMode()) {
348                 TKEvent evt = TKEvent.getEventForThread();
349                 String JavaDoc ownUrl = evt.getHttpInterface().getOwnURL();
350                 // PREVIEW!! Noch die URL erzeugen !!!!
351
fullLink = ownUrl+"?TK_EV[CE_PREVIEW]=1"+
352                         "&TK_PAR[SNODE]="+node.getId()+
353                         "&TK_PAR[PID]="+instanceID+
354                         "&TK_PAR[DOCNAME]="+ docName +
355                         "&TK_PAR[CHECKER]=1";
356                 back.addElement(fullLink);
357                 back.addElement(contentName);
358                 back.addElement(docName); // ist das der Docname von oben ?
359
back.addElement(sc);
360                 back.addElement(""); // kein Pfad da !
361
return back;
362             }
363
364             if (destNode == null)
365                 return null;
366
367             // TKWMGenDocument genDocument = destNode.getDocument(docName);
368
fullLink = destNode.getPath() + docName;
369             back.addElement(fullLink);
370             back.addElement(contentName);
371             back.addElement(docName);
372             back.addElement(sc);
373             back.addElement(destNode.getPath());
374             return back;
375         }
376         cat.error("Did not found matching document type for " + getName() + " - site reference not generated!");
377
378         return null;
379     }
380     /**
381      * ueberschrieben, um ContentRelationen anlegen zu können
382      */

383     public int insertDataIntoDB( TKContentDBData db, Object JavaDoc data, int contentId, int leftNr )
384     {
385         return super.insertDataIntoDB(db, data, contentId, leftNr);
386     }
387
388     protected void fillDataIntoNode(Document doc, Element node, Object JavaDoc data) throws DOMException
389     {
390         Vector daten = getInformations(data);
391         if (daten != null) {
392             node.setAttribute("REF", (String JavaDoc)daten.elementAt(0));
393             node.setAttribute("CONTENT_NAME", (String JavaDoc)daten.elementAt(1));
394             node.setAttribute("DOC", (String JavaDoc)daten.elementAt(2));
395
396             if (!deliverOnlyNames) {
397                 Element me = doc.createElement("content");
398                 node.appendChild(me);
399
400                 GeneratorContext context = GeneratorContext.setup ();
401                 try {
402                     context.push();
403                     SiteContent sc = (SiteContent)daten.elementAt(3);
404                     if (!ReferenceCycleDetector.isCycle(new Integer JavaDoc(sc.getInstanceId()))) {
405                         sc.fillBasicDOM(doc, me);
406                     }
407                 }
408                 finally {
409                     context.pop();
410                 }
411             }
412             node.setAttribute("REF_NODE_PATH", (String JavaDoc)daten.elementAt(4));
413         }
414     }
415
416     public void initFromDB( String JavaDoc classId, TKFormDBData db, TKVector otherFields )
417         throws
418             TKUnregisteredClassException,
419             ClassNotFoundException JavaDoc,
420             InstantiationException JavaDoc,
421             IllegalAccessException JavaDoc
422     {
423         super.initFromDB( classId, db, otherFields );
424         formularId = Integer.parseInt( getFieldAttribute( db, FORM_KEY, 0 ) );
425         presentationId = Integer.parseInt( getFieldAttribute( db, PRESENTATION_KEY, 0 ) );
426         if (hasFieldAttribute(db, NAME_ONLY_KEY, 0))
427             deliverOnlyNames= Boolean.valueOf(getFieldAttribute( db, NAME_ONLY_KEY, 0)).booleanValue();
428     }
429
430     public int realInsertIntoDB( TKFormDBData db, int formId )
431     {
432         if( super.realInsertIntoDB( db, formId ) == -1 ) return -1;
433         insertNewFieldAttribute( db, formId, FORM_KEY, 0, String.valueOf( formularId ) );
434         insertNewFieldAttribute( db, formId, PRESENTATION_KEY, 0, String.valueOf( presentationId ) );
435         insertNewFieldAttribute( db, formId, NAME_ONLY_KEY, 0, String.valueOf( deliverOnlyNames ) );
436
437         return fieldId;
438     }
439
440     /**
441      * Checks wether this object and the specified object
442      * may be treated as equal.
443      *
444      * @param object the object to checked for equality.
445      * @return <CODE>true</CODE> if this object and the
446      * specified object may be treated as equal, otherwise
447      * <CODE>false</CODE>.
448      */

449     public boolean equals (Object JavaDoc object)
450     {
451         if (! super.equals(object))
452         {
453             return false;
454         }
455
456         TKContentSiteReferenceField field = (TKContentSiteReferenceField) object;
457
458         return (this.formularId == field.formularId) &&
459                (this.presentationId == field.presentationId) &&
460                (this.instanceId == field.instanceId) &&
461                (this.deliverOnlyNames == field.deliverOnlyNames);
462     }
463
464     /**
465      * Returns the hash code for this object.
466      *
467      * @return the hash code for this object.
468      */

469     public int hashCode ()
470     {
471         // Implementation for JTest only ;-(
472
return super.hashCode();
473     }
474
475 }
476
Popular Tags