001: package com.teamkonzept.field;
002:
003: import com.teamkonzept.lib.*;
004: import com.teamkonzept.publishing.markups.*;
005: import com.teamkonzept.web.*;
006: import com.teamkonzept.field.db.*;
007: import java.util.*;
008: import org.w3c.dom.Element;
009: import org.w3c.dom.DOMException;
010: import org.w3c.dom.Document;
011: import com.teamkonzept.international.LanguageManager;
012:
013: /**
014: * The base field control.
015: *
016: * Die Klasse TKBaseField ist die Basisklasse aller Objekte die
017: * zum Aufbau dynamischer HTML-Formulare benoetigt werden.
018: *
019: * @author $Author: uli $
020: * @version $Revision: 1.29 $
021: */
022: public abstract class TKBaseField implements XMLAttributes {
023: // $Id: TKBaseField.java,v 1.29 2002/02/27 11:07:04 uli Exp $
024:
025: public static final String DIAGS_KEY = "### diagnostics";
026:
027: public static final String NAME_KEY = "NAME";
028: public static final String SHOW_NAME_KEY = "SHOWNAME";
029: public static final String SUB_LIST_KEY = "ALLLIST";
030: public static final String SUB_TYPE_KEY = "ALL";
031: public static final String BASEPATH_PAR = "UPBASE";
032: public static final String NEWFILENAME = "NEWFILENAME";
033: public static final String OLDFILENAME = "OLDFILENAME";
034:
035: /**
036: * The type of the field.
037: */
038: public String fieldType = null;
039:
040: /**
041: * The name of the field.
042: */
043: public String fieldName = null;
044:
045: /**
046: * The description of the field.
047: */
048: public String showName = null;
049:
050: public static final String LANGUAGE_CONTEXT = "field";
051:
052: protected int fieldId;
053:
054: public TKBaseField() {
055: };
056:
057: public TKBaseField(String fieldType, String fieldName,
058: String showName) {
059: initBaseField(fieldType, fieldName, showName);
060: }
061:
062: public final void initBaseField(String fieldType, String fieldName,
063: String showName) {
064: this .fieldName = fieldName;
065: this .fieldType = fieldType;
066: this .showName = showName;
067: }
068:
069: public void init(String fieldType, Object initData)
070: throws TKUnregisteredClassException,
071: ClassNotFoundException, InstantiationException,
072: IllegalAccessException {
073: TKHashtable data = (TKHashtable) initData;
074: initBaseField(fieldType, ((String) data.get(NAME_KEY))
075: .toUpperCase(), (String) data.get(SHOW_NAME_KEY));
076: }
077:
078: public void addToContext(Object value, String key,
079: TKHashtable context) {
080:
081: if (context == null || key == null || value == null)
082: return;
083:
084: TKVector vec;
085:
086: Object obj = context.get(key);
087: if (obj == null) {
088:
089: vec = new TKVector();
090: context.put(key, vec);
091:
092: } else if (!(obj instanceof TKVector))
093: return;
094: else
095: vec = (TKVector) obj;
096:
097: vec.addElement(value);
098: }
099:
100: public Object getFromContext(String key, TKHashtable context) {
101:
102: return context == null || key == null ? null : context.get(key);
103: }
104:
105: /**
106: * Methode zur Definition eines Fields
107: */
108: public abstract TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
109: TKFieldSwitchList allSwitchList);
110:
111: /**
112: * Setzt einen neuen showNae
113: */
114: public void setName(String newShowName, String newFieldName) {
115: showName = newShowName;
116: fieldName = newFieldName.toUpperCase();
117: }
118:
119: public Object toData() {
120: TKHashtable result = new TKHashtable(2);
121: result.put(NAME_KEY, fieldName);
122: if (showName != null)
123: result.put(SHOW_NAME_KEY, showName);
124: return result;
125: }
126:
127: public void fillIntoTemplate(TKHTMLTemplate t, Object value,
128: String prefix) {
129: t.set("NAME", fieldName);
130: t.set("PREFIX", prefix);
131: t.set("SHOWNAME", getShowName());
132: //t.addCase( prefix+fieldType );
133: t.set(prefix + "FIELDTYPE", fieldType);
134: }
135:
136: public abstract void fillIntoPresentation(TKHTMLTemplate t,
137: Object value, String prefix);
138:
139: /**
140: Feldklassen repraesentieren sich als DOM Baum
141: @param doc Documentroot zum Erzeugen weiterer Elemente
142: @param node Vaterknoten, an den weitere Knoten rangehaengt werden
143: @param value einzufuellende Daten
144: */
145: public abstract void fillIntoDOM(Document doc, Element node,
146: Object value) throws DOMException;
147:
148: /**
149: fuellt in den DOM Node die Attribute ein
150: */
151: public void fillAttributesIntoNode(Element node, Object data)
152: throws DOMException {
153: node.setAttribute(TYPE, this .getClass().getName());
154: node.setAttribute(NAME, getName());
155: }
156:
157: public final String getName() {
158: return fieldName;
159: }
160:
161: public String getInternationalName() {
162: String text = LanguageManager.getText(LANGUAGE_CONTEXT,
163: fieldType);
164: if (text == null || text.equals(""))
165: return fieldName;
166: return TKUploadField.toFilename(text);
167: }
168:
169: public final String getShowName() {
170: return (showName == null ? fieldName : showName);
171: }
172:
173: public final String getType() {
174: return fieldType;
175: }
176:
177: public abstract Object compileData(String prefix, TKHashtable data,
178: TKHashtable context);
179:
180: public abstract Object compileData(String prefix,
181: TKMarkupNode data, TKHashtable context);
182:
183: public TKBaseField getField(String fieldPath, String prefix) {
184: if (fieldPath.equals(prefix + fieldName)) {
185: return this ;
186: } else {
187: return null;
188: }
189: }
190:
191: public Object getDefault() {
192: return "";
193: }
194:
195: public Object modify(String action, String fieldPath, Object data,
196: String prefix, StringBuffer destination) {
197: return data;
198: }
199:
200: public String modify(String action, String fieldPath, Object data) {
201: StringBuffer dest = new StringBuffer();
202: modify(action, fieldPath, data, "", dest);
203: return dest.toString();
204: }
205:
206: public TKBaseField getTarget(String fieldPath, String prefix) {
207: return this ;
208: }
209:
210: public TKHashtable finishExtModify(String action, TKHashtable params) {
211: return params;
212: }
213:
214: /*
215: * Prueft, ob ein Attribut in der DB vorhanden
216: */
217: public boolean hasFieldAttribute(TKFormDBData db, String name,
218: int idx) {
219:
220: if (db.field_attribute.isEmpty())
221: return false;
222: TKFieldAttributeTableData attrib = (TKFieldAttributeTableData) db.field_attribute
223: .firstElement();
224:
225: if ((attrib.field_id != fieldId) || (attrib.idx != idx)
226: || (!attrib.name.equals(name))) {
227: return false;
228: } else {
229: return true;
230: }
231:
232: }
233:
234: public String getFieldAttribute(TKFormDBData db, String name,
235: int idx) {
236:
237: TKFieldAttributeTableData attrib = (TKFieldAttributeTableData) db.field_attribute
238: .firstElement();
239:
240: if ((attrib.field_id != fieldId) || (attrib.idx != idx)
241: || (!attrib.name.equals(name))) {
242: throw new TKFormDBError("FIELD_ATTRIBUTE (got "
243: + attrib.name + ")", name, idx);
244: }
245:
246: db.field_attribute.removeElementAt(0);
247: return attrib.value;
248: }
249:
250: public TKBaseField getSubField(TKFormDBData db, String name,
251: int idx, TKVector otherFields)
252: throws TKUnregisteredClassException,
253: ClassNotFoundException, InstantiationException,
254: IllegalAccessException {
255: TKSubFieldTableData subFieldDB = (TKSubFieldTableData) db.sub_field
256: .firstElement();
257:
258: if ((subFieldDB.field_id != fieldId) || (subFieldDB.idx != idx)
259: || (!subFieldDB.name.equals(name))) {
260: throw new TKFormDBError("SUB_FIELD (got " + subFieldDB.name
261: + ")", name, idx);
262: }
263:
264: db.sub_field.removeElementAt(0);
265:
266: int subId = subFieldDB.sub_field_id;
267: if (subId < otherFields.size())
268: return (TKBaseField) otherFields.get(subId);
269:
270: if (subId == fieldId)
271: return this ;
272:
273: return TKFieldRegistry.getFieldFromDB(db, otherFields);
274: }
275:
276: public TKVector getSubFieldList(TKFormDBData db, String name,
277: TKVector otherFields) throws TKUnregisteredClassException,
278: ClassNotFoundException, InstantiationException,
279: IllegalAccessException {
280: int size = Integer.parseInt(getFieldAttribute(db, name, 0));
281:
282: if (size == 0)
283: return new TKVector();
284:
285: TKVector fieldList = new TKVector(size);
286: for (int i = 0; i < size; i++) {
287: fieldList.addElement(getSubField(db, name, i, otherFields));
288: }
289: return fieldList;
290: }
291:
292: public void initFromDB(String classId, TKFormDBData db,
293: TKVector otherFields) throws TKUnregisteredClassException,
294: ClassNotFoundException, InstantiationException,
295: IllegalAccessException {
296: TKFieldTableData field = (TKFieldTableData) db.field
297: .firstElement();
298:
299: if (!field.field_type.equals(classId)) {
300: throw new TKFormDBError("FIELD (got " + field.field_type
301: + ")", classId, 0);
302: }
303:
304: db.field.removeElementAt(0);
305: initBaseField(field.field_type, field.field_name,
306: field.field_show_name);
307: fieldId = field.field_id;
308: otherFields.put(fieldId, this );
309: }
310:
311: public void clearId() {
312: fieldId = -1;
313: }
314:
315: public TKSubFieldTableData insertNewSubField(TKFormDBData db,
316: int formId, String name, int idx) {
317: TKSubFieldTableData result = new TKSubFieldTableData(formId,
318: fieldId, 0, name, idx);
319: db.sub_field.addElement(result);
320: return result;
321: }
322:
323: public void insertNewSubFieldList(TKFormDBData db, int formId,
324: String name, TKVector fieldList) {
325: insertNewFieldAttribute(db, formId, name, 0, String
326: .valueOf(fieldList.size()));
327:
328: Enumeration e = fieldList.elements();
329: int i = 0;
330: while (e.hasMoreElements()) {
331: TKSubFieldTableData subFieldDB = insertNewSubField(db,
332: formId, name, i++);
333: TKBaseField subField = (TKBaseField) e.nextElement();
334: subField.realInsertIntoDB(db, formId);
335: subFieldDB.sub_field_id = subField.fieldId;
336: }
337:
338: }
339:
340: public void insertNewFieldAttribute(TKFormDBData db, int formId,
341: String name, int idx, String value) {
342: db.field_attribute.addElement(new TKFieldAttributeTableData(
343: formId, fieldId, name, idx, value));
344: }
345:
346: public int realInsertIntoDB(TKFormDBData db, int formId) {
347: if (fieldId != -1)
348: return -1;
349:
350: fieldId = db.field.size();
351: TKFieldTableData field = new TKFieldTableData(formId, fieldId,
352: fieldType, fieldName, showName);
353: db.field.addElement(field);
354: return fieldId;
355: }
356:
357: public void insertIntoDB(TKFormDBData db) {
358: clearId();
359: realInsertIntoDB(db, db.form_id);
360: }
361:
362: public TKContentNodeTableData insertNewContentNode(
363: TKContentDBData db, int contentId, int leftNr) {
364: int newContentNodeId = db.content_node.size();
365: TKContentNodeTableData node = new TKContentNodeTableData(
366: contentId, newContentNodeId, leftNr, leftNr + 1,
367: getName());
368:
369: db.content_node.addElement(node);
370: return node;
371: }
372:
373: public void insertNewContentValue(TKContentDBData db,
374: int contentId, int contentNodeId, int idx, String value) {
375: db.content_value.addElement(new TKContentValueTableData(
376: contentId, contentNodeId, idx, value));
377: }
378:
379: /**
380: *@author Marwan
381: */
382: public void insertNewContentValue(TKContentDBData db,
383: int contentId, int contentNodeId, int idx, String value,
384: Integer mediaID) {
385: db.content_value.addElement(new TKContentValueTableData(
386: contentId, contentNodeId, idx, value, mediaID));
387: }
388:
389: public abstract int insertDataIntoDB(TKContentDBData db,
390: Object data, int contentId, int leftNr);
391:
392: public final void insertDataIntoDB(TKContentDBData db, Object data) {
393: insertDataIntoDB(db, data, db.content_id, 0);
394: }
395:
396: public final static TKContentNodeTableData peekNextContentNode(
397: TKContentDBData db) {
398: if (db.content_node.size() == 0)
399: return null;
400:
401: return (TKContentNodeTableData) db.content_node.elementAt(0);
402: }
403:
404: public final static void removeNextContentNode(TKContentDBData db) {
405: TKContentNodeTableData node = (TKContentNodeTableData) db.content_node
406: .elementAt(0);
407: db.content_node.removeElementAt(0);
408: boolean done;
409: do {
410: done = db.content_value.size() == 0;
411:
412: if (!done) {
413: TKContentValueTableData value = (TKContentValueTableData) db.content_value
414: .elementAt(0);
415: done = value == null
416: || value.content_node_id != node.content_node_id;
417:
418: if (!done) {
419: db.content_value.removeElementAt(0);
420: }
421: }
422: } while (!done);
423: }
424:
425: public TKContentNodeTableData getContentNodeFromDB(
426: TKContentDBData db) {
427: if (db.content_node.size() == 0)
428: return null;
429:
430: TKContentNodeTableData node = (TKContentNodeTableData) db.content_node
431: .elementAt(0);
432: if (!node.name.equals(getName())) {
433: throw new TKFieldDataDBError("CONTENT_NODE(got node "
434: + node.name + ")", getName(), node.content_node_id);
435: }
436: db.content_node.removeElementAt(0);
437: return node;
438: }
439:
440: public TKContentValueTableData getContentNodeValueFromDB(
441: TKContentDBData db, TKContentNodeTableData node) {
442: TKContentValueTableData value = (TKContentValueTableData) db.content_value
443: .elementAt(0);
444: if ((value.content_id == node.content_id)
445: && (value.content_node_id == node.content_node_id)) {
446: db.content_value.removeElementAt(0);
447: } else {
448: throw new TKFieldDataDBError("CONTENT_VALUE", getName(),
449: value.content_node_id);
450: }
451: return value;
452: }
453:
454: public abstract Object getDataFromDB(TKContentDBData db);
455:
456: public static final TKHashtable getFieldHashFromList(
457: TKVector fieldList) {
458: int fieldCount = fieldList.size();
459: if (fieldCount > 0) {
460: TKHashtable fields = new TKHashtable(fieldCount);
461: for (int i = 0; i < fieldCount; i++) {
462: TKBaseField field = (TKBaseField) fieldList.get(i);
463: String fieldName = field.getName();
464: fields.put(fieldName, field);
465: }
466: return fields;
467: } else {
468: return new TKHashtable();
469: }
470: }
471:
472: public static final TKVector getListOfFields(
473: TKFieldSwitchListData switchListData)
474: throws TKUnregisteredClassException,
475: ClassNotFoundException, InstantiationException,
476: IllegalAccessException {
477: TKVector listElements = new TKVector(switchListData.data.size());
478: Enumeration e = switchListData.data.elements();
479: while (e.hasMoreElements()) {
480: TKFieldSwitchData fieldDef = (TKFieldSwitchData) e
481: .nextElement();
482: if (fieldDef.alternative.length() > 0) {
483: listElements.addElement(TKFieldRegistry.registry.get(
484: fieldDef.alternative, fieldDef.data));
485: }
486: }
487: return listElements;
488: }
489:
490: public static final TKFieldSwitchData getDataOfAlternative(
491: TKBaseField field) {
492: return new TKFieldSwitchData(field.getType(), field.getType(),
493: field.toData());
494: }
495:
496: public static final TKFieldSwitchListData getDataOfAlternatives(
497: TKVector fieldList) {
498: TKVector listElements = new TKVector(fieldList.size());
499: Enumeration e = fieldList.elements();
500: while (e.hasMoreElements()) {
501: TKBaseField field = (TKBaseField) e.nextElement();
502: listElements.addElement(getDataOfAlternative(field));
503: }
504: return new TKFieldSwitchListData("", listElements);
505: }
506:
507: /**
508: * Checks wether this object and the specified object
509: * may be treated as equal.
510: *
511: * @param object the object to checked for equality.
512: * @return <CODE>true</CODE> if this object and the
513: * specified object may be treated as equal, otherwise
514: * <CODE>false</CODE>.
515: */
516: public boolean equals(Object object) {
517: if (object == null) {
518: return false;
519: }
520:
521: if (object == this ) {
522: return true;
523: }
524:
525: if (!this .getClass().equals(object.getClass())) {
526: return false;
527: }
528:
529: TKBaseField field = (TKBaseField) object;
530:
531: return (this .fieldType == null ? field.fieldType == null
532: : this .fieldType.equals(field.fieldType))
533: && (this .fieldName == null ? field.fieldName == null
534: : this .fieldName.equals(field.fieldName))
535: && (this .showName == null ? field.showName == null
536: : this .showName.equals(field.showName));
537: }
538:
539: /**
540: * Returns the hash code for this object.
541: *
542: * @return the hash code for this object.
543: */
544: public int hashCode() {
545: // Implementation for JTest only ;-(
546: return super.hashCode();
547: }
548:
549: }
|