001: package com.teamkonzept.webman.field;
002:
003: import com.teamkonzept.webman.mainint.*; // import com.teamkonzept.webman.mainint.events.CEEventHandler;
004: import de.webman.content.eventhandler.CEUtils;
005: import de.webman.content.workflow.*;
006: import de.webman.content.*;
007: import com.teamkonzept.lib.*;
008: import com.teamkonzept.publishing.markups.*;
009: import com.teamkonzept.web.*;
010: import com.teamkonzept.field.db.queries.*;
011: import com.teamkonzept.field.db.*;
012: import com.teamkonzept.db.*;
013: import com.teamkonzept.field.*;
014: import de.webman.generator.*;
015: import de.webman.util.log4j.WebmanCategory;
016: import com.teamkonzept.webman.mainint.db.queries.*;
017: import com.teamkonzept.webman.mainint.DatabaseDefaults;
018: import java.sql.*;
019: import java.util.*;
020: import com.teamkonzept.international.LanguageManager;
021: import org.w3c.dom.*;
022: import org.apache.log4j.Category;
023:
024: /**
025: * The content site reference field control.
026: *
027: * @author $Author: alex $
028: * @version $Revision: 1.43.2.1 $
029: */
030: public class TKContentSiteReferenceField extends TKAtomField {
031:
032: /**
033: * The class identifier.
034: */
035: public static final String CLASS_ID = "SITE_CONTENT_REF";
036:
037: /**
038: * key for forms
039: **/
040: public static final String FORM_KEY = "FORM";
041: public static final String PRESENTATION_KEY = "PRESENTATION";
042: public static final String NAME_ONLY_KEY = "NAME_ONLY";
043: public static final String CONTENT_NODE = "CONTENT_NODE";
044:
045: /** default YES Wert */
046: public static final String YES_VALUE = "YES";
047: public static final String NO_VALUE = "NO";
048:
049: /**
050: * A specific width constant.
051: */
052: public static final int WIDTH_ONE = 16;
053:
054: /**
055: * A specific width constant.
056: */
057: public static final int WIDTH_TWO = 80;
058:
059: /**
060: * A specific width constant.
061: */
062: public static final int WIDTH_THREE = 50;
063:
064: /**
065: * A specific width constant.
066: */
067: public static final int WIDTH_FOUR = 254;
068:
069: /**
070: * The logging category.
071: */
072: private static WebmanCategory cat = (WebmanCategory) WebmanCategory
073: .getInstance(TKContentSiteReferenceField.class);
074:
075: /** zugrundeliegendes Formular */
076: protected int formularId;
077:
078: /** zugrundeliegender Documenttyp */
079: protected int presentationId;
080:
081: /** vom benutzer gewählter Knoten, ist eigentlich die INSTANZ ID !!!! */
082: protected int instanceId;
083:
084: /** sollen nur Namen geholt werden ? */
085: protected boolean deliverOnlyNames = true;
086:
087: /**
088: * Creates an empty field.
089: */
090: public TKContentSiteReferenceField() {
091: super ();
092: }
093:
094: public TKContentSiteReferenceField(String name, int formId,
095: int presId, String showName) {
096: initField(CLASS_ID, name, formId, presId, showName);
097: }
098:
099: public final void initField(String type, String name, int formId,
100: int presID, String showName) {
101: initAtomField(type, name, showName);
102: this .formularId = formId;
103: this .presentationId = presID;
104: }
105:
106: /**
107: * Methode zur Definition eines Eingabefeldes
108: */
109: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
110: TKFieldSwitchList allSwitchList) {
111: TKVector multipleOptions = new TKVector(2);
112: multipleOptions
113: .addElement(new TKOptionFieldEntry(LanguageManager
114: .getText(LanguageManager.GENERAL, "YES"),
115: YES_VALUE));
116: multipleOptions.addElement(new TKOptionFieldEntry(
117: LanguageManager.getText(LanguageManager.GENERAL, "NO"),
118: NO_VALUE));
119:
120: TKBaseField[] inputArray = {
121: new TKInputField(NAME_KEY, WIDTH_ONE, WIDTH_TWO,
122: LanguageManager.getText(LANGUAGE_CONTEXT,
123: "CONTENT_SITE_REF_NAME"),
124: TKInputField.CHECK_STRING),
125: new TKInputField(SHOW_NAME_KEY, WIDTH_THREE,
126: WIDTH_FOUR, LanguageManager.getText(
127: LANGUAGE_CONTEXT,
128: "CONTENT_SITE_REF_SHOWNAME"),
129: TKInputField.CHECK_STRING),
130: new TKFormTypeSelectorField(FORM_KEY, LanguageManager
131: .getText(LANGUAGE_CONTEXT, "CONTENT_REF_FORM"),
132: TKFormTypeSelectorField.CONTENT_FORM_TYPE),
133: new TKDocumentTypeSelectorField(PRESENTATION_KEY,
134: LanguageManager.getText(LANGUAGE_CONTEXT,
135: "CONTENT_REF_PRES")),
136: new TKCheckField(NAME_ONLY_KEY, LanguageManager
137: .getText(LANGUAGE_CONTEXT,
138: "CONTENT_REF_NAME_ONLY"),
139: multipleOptions, false) };
140: TKFieldGroup inputGroup = new TKFieldGroup(CLASS_ID,
141: new TKVector(inputArray), LanguageManager.getText(
142: LANGUAGE_CONTEXT, CLASS_ID));
143:
144: return inputGroup;
145: }
146:
147: /**
148: initialisiert das feld basierend auf den daten, die die feldgruppe von getDefGroup zurueckliefert
149: */
150: public void init(String classId, Object initData)
151: throws TKUnregisteredClassException,
152: ClassNotFoundException, InstantiationException,
153: IllegalAccessException {
154: super .init(classId, initData);
155: TKHashtable data = (TKHashtable) initData;
156: formularId = Integer.parseInt((String) data.get(FORM_KEY));
157: presentationId = Integer.parseInt((String) data
158: .get(PRESENTATION_KEY));
159: deliverOnlyNames = ((String) data.get(NAME_ONLY_KEY))
160: .equals(YES_VALUE);
161: }
162:
163: /**
164: * Returns the internal representation of this field.
165: *
166: * @return the internal representation of this field.
167: */
168: public Object toData() {
169: TKHashtable result = (TKHashtable) super .toData();
170: result.put(FORM_KEY, String.valueOf(formularId));
171: result.put(PRESENTATION_KEY, String.valueOf(presentationId));
172: result.put(NAME_ONLY_KEY, (deliverOnlyNames ? YES_VALUE
173: : NO_VALUE));
174: return result;
175: }
176:
177: /**
178: * Fills the data held by this field into the specified template.
179: *
180: * @param template the template.
181: * @param value a value ?
182: * @param prefix a prefix ?
183: */
184: public void fillIntoTemplate(TKHTMLTemplate template, Object value,
185: String prefix) {
186: try {
187: super .fillIntoTemplate(template, value, prefix);
188:
189: // Initialize workflow.
190: VersionStatics statics = VersionStatics.setup();
191: TKHashtable statusPool = VersionStatus.selectSingles(
192: statics, false);
193: TKHashtable filter = VersionSelection
194: .initContentFilter(statusPool);
195: TKVector filteredInstances = new TKVector();
196:
197: // Select referenced contents.
198: TKQuery query = TKDBManager
199: .newQuery(TKDBContentTreeGetFormType2.class);
200: query.setQueryParams("CONTENT_NODE_TYPE",
201: DatabaseDefaults.SINGLE_INTEGER);
202: query.setQueryParams("CONTENT_FORM", new Integer(
203: this .formularId));
204: query.execute();
205:
206: ResultSet result = query.fetchResultSet();
207:
208: while (result.next()) {
209: // Apply filter.
210: Content content = VersionCache
211: .getContentInfo(new Integer(result
212: .getInt("CONTENT_NODE_ID")));
213:
214: if (content != null
215: && content.filterTransitions(filter, "SRC")) {
216: TKHashtable entry = new TKHashtable();
217: entry.put("INSTANCE_ID", content.getInstanceId());
218: entry.put("CONTENT_NODE_NAME", content.getName());
219:
220: filteredInstances.addElement(entry);
221: }
222: }
223:
224: // Publish list to template.
225: template.setListIterator(new TKStandardPluginIterator(
226: prefix + getName(), null, filteredInstances, false,
227: template.getListIterator()));
228: } catch (Exception e) {
229: cat.error(e);
230: }
231: }
232:
233: public void fillIntoPresentation(TKHTMLTemplate t, Object data,
234: String scope) {
235: String scopedName = scope + "." + getName();
236: Vector daten = getInformations(data);
237: if (daten != null) {
238: t.set(scopedName + ".REF", daten.elementAt(0));
239: t.set(scopedName + ".CONTENT_NAME", daten.elementAt(1));
240: t.set(scopedName + ".DOC", daten.elementAt(2));
241: t.set(scopedName + ".REF_NODE_PATH", daten.elementAt(4));
242: if (!deliverOnlyNames) {
243: GeneratorContext context = GeneratorContext.setup();
244: try {
245: context.push();
246:
247: SiteContent sc = (SiteContent) daten.elementAt(3);
248: if (!ReferenceCycleDetector.isCycle(new Integer(sc
249: .getInstanceId()))) {
250: sc.fillNameIntoTemplate(t, scopedName);
251: sc.fillIntoTemplate(t, scopedName);
252: }
253: } finally {
254: context.pop();
255: }
256: }
257: }
258: }
259:
260: /**
261: liefert die entsprechenden Info's in einem Vector zurueck
262: null falls nichts gefunden
263: sonst:
264: 1. Eintrag: Link auf das Dokument
265: 2. Eintrag: Interner Name des Contents
266: 3. Eintrag: Name des Documents
267: 4. Eintrag: der eigentliche Content
268: 5. Eintrag: REF_NODE_PATH
269: */
270: private Vector getInformations(Object data) {
271: if (data == null || data.equals(""))
272: return null;
273: Integer instanceID = new Integer((String) 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
283: .intValue());
284: if (sc == null) {
285: cat
286: .error("Content of site reference "
287: + getName()
288: + " is not referenced in the Site-structure - content site reference not generated!");
289: return null; //Content nicht im Sitetree referenziert
290: }
291: Vector back = new Vector();
292:
293: TKVector list = context.contentCalls.getCalls(instanceID);
294: if (list == null) {
295: cat.error("No Contents of site reference " + getName()
296: + " are released - site reference not generated!",
297: WebmanCategory.ALL);
298: return null; // keine Calls ?
299: }
300: ContentCall match = null;
301:
302: // Liste durchgehen und passendes ContentCall heraussuchen
303: for (int i = 0; i < list.size(); i++) {
304: ContentCall dummy = (ContentCall) list.elementAt(i);
305: if (dummy.getDocument().getPresentationId() == presentationId
306: && !dummy.isPerReference()) {
307: match = dummy;
308: break;
309: }
310: }
311:
312: if (match != null) {
313: String 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 docName = match.getDocument().getShortName();
320: String contentName = sc.getShortName();
321: path = node.getPath();
322: String 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: } else {
333: if (match.isPrimaryContent()) {
334: for (int i = 0; i < genNodes.size(); i++) {
335: destNode = (GenNode) genNodes.elementAt(i);
336: if (destNode.getPrimaryContent()
337: .getInstanceId() == instanceID
338: .intValue()) {
339: break;
340: }
341: destNode = null;
342: }
343: } else {
344: destNode = (GenNode) genNodes.elementAt(0);
345: }
346: }
347: if (GeneratorContext.isPreviewMode()) {
348: TKEvent evt = TKEvent.getEventForThread();
349: String 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 "
377: + getName() + " - site reference not generated!");
378:
379: return null;
380: }
381:
382: /**
383: * ueberschrieben, um ContentRelationen anlegen zu können
384: */
385: public int insertDataIntoDB(TKContentDBData db, Object data,
386: int contentId, int leftNr) {
387: return super .insertDataIntoDB(db, data, contentId, leftNr);
388: }
389:
390: protected void fillDataIntoNode(Document doc, Element node,
391: Object data) throws DOMException {
392: Vector daten = getInformations(data);
393: if (daten != null) {
394: node.setAttribute("REF", (String) daten.elementAt(0));
395: node.setAttribute("CONTENT_NAME", (String) daten
396: .elementAt(1));
397: node.setAttribute("DOC", (String) daten.elementAt(2));
398:
399: if (!deliverOnlyNames) {
400: Element me = doc.createElement("content");
401: node.appendChild(me);
402:
403: GeneratorContext context = GeneratorContext.setup();
404: try {
405: context.push();
406: SiteContent sc = (SiteContent) daten.elementAt(3);
407: if (!ReferenceCycleDetector.isCycle(new Integer(sc
408: .getInstanceId()))) {
409: sc.fillBasicDOM(doc, me);
410: }
411: } finally {
412: context.pop();
413: }
414: }
415: node.setAttribute("REF_NODE_PATH", (String) daten
416: .elementAt(4));
417: }
418: }
419:
420: public void initFromDB(String classId, TKFormDBData db,
421: TKVector otherFields) throws TKUnregisteredClassException,
422: ClassNotFoundException, InstantiationException,
423: IllegalAccessException {
424: super .initFromDB(classId, db, otherFields);
425: formularId = Integer
426: .parseInt(getFieldAttribute(db, FORM_KEY, 0));
427: presentationId = Integer.parseInt(getFieldAttribute(db,
428: PRESENTATION_KEY, 0));
429: if (hasFieldAttribute(db, NAME_ONLY_KEY, 0))
430: deliverOnlyNames = Boolean.valueOf(
431: getFieldAttribute(db, NAME_ONLY_KEY, 0))
432: .booleanValue();
433: }
434:
435: public int realInsertIntoDB(TKFormDBData db, int formId) {
436: if (super .realInsertIntoDB(db, formId) == -1)
437: return -1;
438: insertNewFieldAttribute(db, formId, FORM_KEY, 0, String
439: .valueOf(formularId));
440: insertNewFieldAttribute(db, formId, PRESENTATION_KEY, 0, String
441: .valueOf(presentationId));
442: insertNewFieldAttribute(db, formId, NAME_ONLY_KEY, 0, String
443: .valueOf(deliverOnlyNames));
444:
445: return fieldId;
446: }
447:
448: /**
449: * Checks wether this object and the specified object
450: * may be treated as equal.
451: *
452: * @param object the object to checked for equality.
453: * @return <CODE>true</CODE> if this object and the
454: * specified object may be treated as equal, otherwise
455: * <CODE>false</CODE>.
456: */
457: public boolean equals(Object object) {
458: if (!super .equals(object)) {
459: return false;
460: }
461:
462: TKContentSiteReferenceField field = (TKContentSiteReferenceField) object;
463:
464: return (this .formularId == field.formularId)
465: && (this .presentationId == field.presentationId)
466: && (this .instanceId == field.instanceId)
467: && (this .deliverOnlyNames == field.deliverOnlyNames);
468: }
469:
470: /**
471: * Returns the hash code for this object.
472: *
473: * @return the hash code for this object.
474: */
475: public int hashCode() {
476: // Implementation for JTest only ;-(
477: return super.hashCode();
478: }
479:
480: }
|