001: package com.teamkonzept.field;
002:
003: import java.util.Enumeration;
004: import com.teamkonzept.lib.*;
005: import com.teamkonzept.publishing.markups.*;
006: import com.teamkonzept.web.*;
007: import com.teamkonzept.field.db.*;
008: import org.w3c.dom.*;
009: import com.teamkonzept.international.LanguageManager;
010: import org.apache.log4j.Category;
011:
012: /**
013: * The field group control.
014: *
015: * @author $Author: uli $
016: * @version $Revision: 1.24 $
017: */
018: public class TKFieldGroup extends TKBaseField {
019: // $Id: TKFieldGroup.java,v 1.24 2002/02/27 11:07:04 uli Exp $
020:
021: /**
022: * The class identifier.
023: */
024: public static final String CLASS_ID = "FIELDGROUP";
025: public static final String SUB_FIELD_KEY = "SUBFIELD";
026: public static final String IGNORES_NAME_KEY = "IGNORESNAME";
027:
028: // hack fuer die Strukturanzeige - alex
029: private boolean noName = false;
030:
031: TKVector fieldList;
032: TKHashtable fields;
033: boolean ignoresOwnName;
034:
035: /**
036: * Creates an empty field group control.
037: */
038: public TKFieldGroup() {
039: };
040:
041: public TKFieldGroup(String name, TKVector fields) {
042: this (name, fields, null);
043: }
044:
045: public TKFieldGroup(String name, TKVector fields, String showName) {
046: this (name, fields, true, showName);
047: }
048:
049: public TKFieldGroup(String name, TKVector fields,
050: boolean ignoresOwnName, String showName) {
051: initFieldGroup("FIELDGROUP", name, fields, ignoresOwnName,
052: showName);
053: }
054:
055: /**
056: ueberschrieben, damit der NAME und SHOWNAME ausgegeben wird
057: */
058: public String getInternationalName() {
059: if (noName)
060: return TKUploadField.toFilename(LanguageManager.getText(
061: LANGUAGE_CONTEXT, fieldName));
062: else
063: return super .getInternationalName();
064: }
065:
066: public final void initFieldGroup(String fieldType, String name,
067: TKVector fields, boolean ignoresOwnName, String showName) {
068: initBaseField(fieldType, name, showName);
069: this .fieldList = fields;
070: this .fields = getFieldHashFromList(fields);
071: this .ignoresOwnName = ignoresOwnName;
072: noName = true;
073: }
074:
075: public void init(String fieldType, Object data)
076: throws TKUnregisteredClassException,
077: ClassNotFoundException, InstantiationException,
078: IllegalAccessException {
079: super .init(fieldType, data);
080: TKHashtable groupData = (TKHashtable) data;
081:
082: String sIgnores = ((String) groupData.get(IGNORES_NAME_KEY));
083: this .ignoresOwnName = sIgnores == null ? true : sIgnores
084: .equals("YES");
085:
086: this .fieldList = getListOfFields((TKFieldSwitchListData) groupData
087: .get(SUB_LIST_KEY));
088: this .fields = getFieldHashFromList(fieldList);
089: }
090:
091: /**
092: * Methode zur Definition von Gruppen von verschiedenen Eingabe-Objekte
093: */
094: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
095: TKFieldSwitchList allSwitchList) {
096: TKVector ignoresNameOptions = new TKVector(2);
097: ignoresNameOptions
098: .addElement(new TKOptionFieldEntry(LanguageManager
099: .getText(LanguageManager.GENERAL, "YES"), "YES"));
100: ignoresNameOptions.addElement(new TKOptionFieldEntry(
101: LanguageManager.getText(LanguageManager.GENERAL, "NO"),
102: "NO"));
103: TKBaseField[] groupArray = {
104: new TKInputField(TKFieldGroup.NAME_KEY,
105: TKInputField.SMALL_DEFAULT_SIZE,
106: TKInputField.SMALL_DEFAULT_LENGTH,
107: LanguageManager.getText(LANGUAGE_CONTEXT,
108: "GROUP_NAME"),
109: TKInputField.CHECK_STRING),
110: new TKInputField(TKFieldGroup.SHOW_NAME_KEY,
111: TKInputField.LARGE_DEFAULT_SIZE,
112: TKInputField.LARGE_DEFAULT_LENGTH,
113: LanguageManager.getText(LANGUAGE_CONTEXT,
114: "GROUP_SHOWNAME"),
115: TKInputField.CHECK_STRING),
116: new TKCheckField(TKFieldGroup.IGNORES_NAME_KEY,
117: LanguageManager.getText(LANGUAGE_CONTEXT,
118: "GROUP_IGNORES_NAME"),
119: ignoresNameOptions, false), allSwitchList };
120: TKFieldGroup groupGroup = new TKFieldGroup(
121: TKFieldGroup.CLASS_ID, new TKVector(groupArray),
122: LanguageManager.getText(LANGUAGE_CONTEXT,
123: TKFieldGroup.CLASS_ID));
124:
125: return groupGroup;
126: }
127:
128: public Object toData() {
129: TKHashtable result = (TKHashtable) super .toData();
130: result.put(TKFieldGroup.IGNORES_NAME_KEY,
131: (ignoresOwnName ? "YES" : "NO"));
132: result.put(SUB_LIST_KEY, getDataOfAlternatives(fieldList));
133: return result;
134: }
135:
136: public final String subPrefix(String prefix) {
137: return prefix + fieldName + ".";
138: }
139:
140: public Object compileData(String prefix, TKHashtable data,
141: TKHashtable context) {
142: int nameCount = fieldList.size();
143: if (nameCount == 0)
144: return new TKHashtable();
145:
146: TKHashtable result = new TKHashtable(nameCount);
147:
148: prefix = subPrefix(prefix);
149:
150: for (int i = 0; i < nameCount; i++) {
151: TKBaseField field = (TKBaseField) fieldList.get(i);
152: String name = field.getName();
153: result.put(name, field.compileData(prefix, data, context));
154: }
155:
156: return result;
157: }
158:
159: public Object compileData(String prefix, TKMarkupNode data,
160: TKHashtable context) {
161:
162: TKXmlMarkup markup = data == null ? null
163: : (TKXmlMarkup) data.markup;
164: if (markup == null) {
165: return null;
166: }
167:
168: if (!markup.name.equals(getName())) {
169: return null;
170: }
171:
172: TKXmlTree tree = (TKXmlTree) data.tree;
173: if (tree == null) {
174: return null;
175: }
176:
177: TKHashtable subs = new TKHashtable(tree.size());
178:
179: for (int i = 0; i < tree.size(); i++) {
180:
181: Object obj = tree.getSub(i);
182: if (obj == null)
183: continue;
184:
185: TKMarkupNode subNode = null;
186: TKXmlMarkup subMarkup = null;
187:
188: if (obj instanceof TKXmlMarkup) { // Atomares Markup
189:
190: subMarkup = (TKXmlMarkup) obj;
191: subNode = new TKMarkupNode(subMarkup, null);
192: } else {
193: subNode = (TKMarkupNode) obj;
194: subMarkup = (TKXmlMarkup) subNode.markup;
195: }
196:
197: subs.put(subMarkup.name, subNode);
198: }
199:
200: int nameCount = fieldList.size();
201: if (nameCount == 0)
202: return new TKHashtable();
203:
204: TKHashtable result = new TKHashtable(nameCount);
205: prefix = subPrefix(prefix);
206:
207: for (int i = 0; i < nameCount; i++) {
208:
209: TKBaseField field = (TKBaseField) fieldList.get(i);
210: String name = field.getName();
211:
212: TKMarkupNode subNode = (TKMarkupNode) subs.get(name);
213: if (subNode == null)
214: continue;
215:
216: Object obj = field.compileData(prefix, subNode, context);
217: if (obj != null)
218: result.put(name, obj);
219: }
220:
221: return result;
222: }
223:
224: public void fillIntoTemplate(TKHTMLTemplate t, Object data,
225: String prefix) {
226: TKHashtable dataHash = (TKHashtable) data;
227: super .fillIntoTemplate(t, data, prefix);
228:
229: t.setListIterator(new TKFieldGroupIterator(dataHash, fieldList,
230: subPrefix(prefix), t.getListIterator(), "FIELDGROUP"));
231: t.set("FIELDGROUP", Boolean.TRUE);
232: }
233:
234: public void fillAttributesIntoNode(Element node, Object data)
235: throws DOMException {
236: super .fillAttributesIntoNode(node, data);
237: node.setAttribute(IGNORES_NAME_KEY, new Boolean(ignoresOwnName)
238: .toString());
239: }
240:
241: /**
242: Feldklassen repraesentieren sich als DOM Baum
243: @param doc Documentroot zum Erzeugen weiterer Elemente
244: @param node Vaterknoten, an den weitere Knoten rangehaengt werden
245: */
246: public void fillIntoDOM(Document doc, Element node, Object data)
247: throws DOMException {
248: Element me = doc.createElement(getInternationalName());
249: node.appendChild(me);
250: fillAttributesIntoNode(me, data);
251: TKHashtable dataHash = (TKHashtable) data;
252: for (int i = 0; i < fieldList.size(); i++) {
253: TKBaseField field = (TKBaseField) fieldList.get(i);
254: Object data2 = dataHash.get(field.getName());
255: field.fillIntoDOM(doc, me, data2);
256: }
257: }
258:
259: public void fillIntoPresentation(TKHTMLTemplate t, Object value,
260: String scope) {
261: TKHashtable dataHash = (TKHashtable) value;
262:
263: if (!ignoresOwnName) {
264: scope += "." + getName();
265: }
266: Enumeration e = fieldList.elements();
267: while (e.hasMoreElements()) {
268: TKBaseField field = (TKBaseField) e.nextElement();
269: field.fillIntoPresentation(t,
270: dataHash.get(field.getName()), scope);
271: }
272: }
273:
274: public Object getDefault() {
275: int nameCount = fieldList.size();
276: if (nameCount == 0)
277: return new TKHashtable();
278:
279: TKHashtable result = new TKHashtable(nameCount);
280:
281: for (int i = 0; i < nameCount; i++) {
282: TKBaseField field = (TKBaseField) fieldList.get(i);
283: String name = field.getName();
284: result.put(name, field.getDefault());
285: }
286:
287: return result;
288: }
289:
290: public Object modify(String action, String fieldPath, Object data,
291: String prefix, StringBuffer destination) {
292: TKHashtable dataHash = (TKHashtable) data;
293: int pLength = prefix.length() + fieldName.length();
294:
295: if (fieldPath.length() != pLength) {
296: // action betrifft Sub-ELement
297: int subNameEnd = fieldPath.indexOf('.', pLength + 1);
298: String subName = (subNameEnd < 0 ? fieldPath
299: .substring(pLength + 1) : fieldPath.substring(
300: pLength + 1, subNameEnd));
301: TKBaseField field = (TKBaseField) fields.get(subName);
302: dataHash.put(subName, field.modify(action, fieldPath,
303: dataHash.get(subName), prefix + fieldName + '.',
304: destination));
305: }
306: return data;
307: }
308:
309: public TKBaseField getTarget(String fieldPath, String prefix) {
310:
311: TKBaseField field = null;
312: TKBaseField targetField = null;
313: int pLength = prefix.length() + fieldName.length();
314:
315: if (fieldPath.length() != pLength) {
316: // action betrifft Sub-ELement
317: int subNameEnd = fieldPath.indexOf('.', pLength + 1);
318: String subName = (subNameEnd < 0 ? fieldPath
319: .substring(pLength + 1) : fieldPath.substring(
320: pLength + 1, subNameEnd));
321: field = (TKBaseField) fields.get(subName);
322:
323: targetField = field.getTarget(fieldPath, prefix + fieldName
324: + '.');
325: }
326:
327: return targetField;
328: }
329:
330: public int insertDataIntoDB(TKContentDBData db, Object data,
331: int contentId, int leftNr) {
332: int nameCount = fieldList.size();
333: if (nameCount == 0)
334: return leftNr;
335:
336: TKContentNodeTableData node = insertNewContentNode(db,
337: contentId, leftNr);
338: //int newNodeId = node.content_node_id;
339: TKHashtable dataHash = (TKHashtable) data;
340:
341: leftNr++;
342: for (int i = 0; i < nameCount; i++) {
343: TKBaseField field = (TKBaseField) fieldList.get(i);
344: leftNr = field.insertDataIntoDB(db, dataHash.get(field
345: .getName()), contentId, leftNr + 1);
346: leftNr++;
347: }
348: node.right_nr = leftNr;
349: return leftNr;
350: }
351:
352: public Object getDataFromDB(TKContentDBData db) {
353: int nameCount = fieldList.size();
354: if (nameCount == 0)
355: return new TKHashtable();
356:
357: TKHashtable result = new TKHashtable(nameCount);
358: TKContentNodeTableData node = getContentNodeFromDB(db);
359: TKContentNodeTableData subNode = peekNextContentNode(db);
360: while (subNode != null && subNode.left_nr > node.left_nr
361: && subNode.right_nr < node.right_nr) {
362: TKBaseField field = (TKBaseField) fields.get(subNode.name);
363: if (field != null) {
364: result.put(subNode.name, field.getDataFromDB(db));
365: } else {
366: removeNextContentNode(db);
367: }
368: subNode = peekNextContentNode(db);
369: }
370:
371: for (int i = 0; i < nameCount; i++) {
372: TKBaseField field = (TKBaseField) fieldList.get(i);
373: if (!result.containsKey(field.getName())) {
374: result.put(field.getName(), field.getDefault());
375: }
376: }
377:
378: return result;
379: }
380:
381: public void clearId() {
382: if (fieldId == -1)
383: return;
384:
385: fieldId = -1;
386: Enumeration e = fieldList.elements();
387: while (e.hasMoreElements()) {
388: ((TKBaseField) e.nextElement()).clearId();
389: }
390: }
391:
392: public int realInsertIntoDB(TKFormDBData db, int formId) {
393: if (super .realInsertIntoDB(db, formId) == -1)
394: return -1;
395: insertNewFieldAttribute(db, formId, IGNORES_NAME_KEY, 0, String
396: .valueOf(ignoresOwnName));
397: insertNewSubFieldList(db, formId, SUB_FIELD_KEY, fieldList);
398: return fieldId;
399: }
400:
401: public void initFromDB(String classId, TKFormDBData db,
402: TKVector otherFields) throws TKUnregisteredClassException,
403: ClassNotFoundException, InstantiationException,
404: IllegalAccessException {
405: super .initFromDB(classId, db, otherFields);
406: if (hasFieldAttribute(db, IGNORES_NAME_KEY, 0)) {
407: ignoresOwnName = (new Boolean(getFieldAttribute(db,
408: IGNORES_NAME_KEY, 0))).booleanValue();
409: } else {
410: ignoresOwnName = true;
411: }
412: fieldList = getSubFieldList(db, SUB_FIELD_KEY, otherFields);
413: fields = getFieldHashFromList(fieldList);
414: }
415:
416: /**
417: * Checks wether this object and the specified object
418: * may be treated as equal.
419: *
420: * @param object the object to checked for equality.
421: * @return <CODE>true</CODE> if this object and the
422: * specified object may be treated as equal, otherwise
423: * <CODE>false</CODE>.
424: */
425: public boolean equals(Object object) {
426: if (!super .equals(object)) {
427: return false;
428: }
429:
430: TKFieldGroup field = (TKFieldGroup) object;
431:
432: return (this .ignoresOwnName == field.ignoresOwnName)
433: && (this .fieldList == null ? field.fieldList == null
434: : this .fieldList.equals(field.fieldList));
435: }
436:
437: /**
438: * Returns the hash code for this object.
439: *
440: * @return the hash code for this object.
441: */
442: public int hashCode() {
443: // Implementation for JTest only ;-(
444: return super.hashCode();
445: }
446:
447: }
|