001: package com.teamkonzept.field;
002:
003: import java.util.*;
004:
005: import com.teamkonzept.lib.*;
006: import com.teamkonzept.publishing.markups.*;
007: import com.teamkonzept.web.*;
008: import com.teamkonzept.field.db.*;
009: import org.w3c.dom.*;
010: import com.teamkonzept.international.LanguageManager;
011:
012: /**
013: * The field switch control.
014: *
015: * @author $Author: uli $
016: * @version $Revision: 1.29 $
017: */
018: public class TKFieldSwitch extends TKBaseField {
019: // $Id: TKFieldSwitch.java,v 1.29 2002/02/27 11:07:04 uli Exp $
020:
021: /**
022: * The class identifier.
023: */
024: public static final String CLASS_ID = "FIELDSWITCH";
025: public static final String ALTERNATIVES_KEY = "ALTERNATIVES";
026: public static final String SELECT_CHECK_KEY = "SELECT_CHECK";
027:
028: public static final int CHECK_UNDEFINED = -1;
029: public static final int CHECK_OPTION = 0;
030:
031: public static final String[] CHECK_NAMES = { "option" };
032:
033: TKVector alternatives;
034: TKHashtable fieldHash;
035:
036: // Initialize to default: no check at all.
037: protected int check = CHECK_UNDEFINED;
038:
039: public TKFieldSwitch() {
040: };
041:
042: public TKFieldSwitch(String name, TKVector alternatives) {
043: this (name, alternatives, null);
044: }
045:
046: public TKFieldSwitch(String name, TKVector alternatives,
047: String showName) {
048: this (name, alternatives, showName, CHECK_UNDEFINED);
049: }
050:
051: public TKFieldSwitch(String name, TKVector alternatives,
052: String showName, int check) {
053: initFieldSwitch(CLASS_ID, name, alternatives, showName, check);
054: }
055:
056: protected final void initFieldSwitch(String type, String name,
057: TKVector alternatives, String showName, int check) {
058: initBaseField(type, name, showName);
059:
060: this .alternatives = alternatives;
061: this .fieldHash = getFieldHashFromList(alternatives);
062: this .check = check;
063: }
064:
065: public void init(String fieldType, Object data)
066: throws TKUnregisteredClassException,
067: ClassNotFoundException, InstantiationException,
068: IllegalAccessException {
069: super .init(fieldType, data);
070:
071: TKHashtable switchData = (TKHashtable) data;
072:
073: this .alternatives = getListOfFields((TKFieldSwitchListData) switchData
074: .get(TKFieldGroup.SUB_LIST_KEY));
075: this .fieldHash = getFieldHashFromList(alternatives);
076:
077: String value = (String) switchData.get(SELECT_CHECK_KEY);
078: this .check = value != null && value.length() > 0 ? Integer
079: .parseInt(value) : CHECK_UNDEFINED;
080: }
081:
082: /**
083: * Methode zur Definition alternativer Eingabe-Objekte
084: */
085: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
086: TKFieldSwitchList allSwitchList) {
087:
088: TKBaseField[] switchArray = {
089: new TKInputField(TKFieldSwitch.NAME_KEY,
090: TKInputField.SMALL_DEFAULT_SIZE,
091: TKInputField.SMALL_DEFAULT_LENGTH,
092: LanguageManager.getText(LANGUAGE_CONTEXT,
093: "FIELDSWITCH_NAME"),
094: TKInputField.CHECK_STRING),
095: new TKInputField(TKFieldSwitch.SHOW_NAME_KEY,
096: TKInputField.LARGE_DEFAULT_SIZE,
097: TKInputField.LARGE_DEFAULT_LENGTH,
098: LanguageManager.getText(LANGUAGE_CONTEXT,
099: "FIELDSWITCH_SHOWNAME"),
100: TKInputField.CHECK_STRING), allSwitchList };
101: TKFieldGroup switchGroup = new TKFieldGroup(
102: TKFieldSwitch.CLASS_ID, new TKVector(switchArray),
103: LanguageManager.getText(LANGUAGE_CONTEXT,
104: TKFieldSwitch.CLASS_ID));
105:
106: return switchGroup;
107: }
108:
109: public Object toData() {
110: TKHashtable result = (TKHashtable) super .toData();
111:
112: result.put(SUB_LIST_KEY, getDataOfAlternatives(alternatives));
113:
114: if (this .check > CHECK_UNDEFINED) {
115: result.put(SELECT_CHECK_KEY, String.valueOf(this .check));
116: }
117:
118: return result;
119: }
120:
121: public void addAlternative(TKBaseField alternative) {
122: alternatives.addElement(alternative);
123: fieldHash.put(alternative.getName(), alternative);
124: }
125:
126: public void removeAlternative(TKBaseField alternative) {
127: alternatives.removeElement(alternative);
128: fieldHash.remove(alternative.getName());
129: }
130:
131: public Object compileData(String prefix, TKHashtable data,
132: TKHashtable context) {
133: prefix += fieldName + ".";
134: String currAlternative = (String) data.get(prefix
135: + "ALTERNATIVE");
136: String newAlternative = (String) data.get(prefix
137: + "NEWALTERNATIVE");
138: if (newAlternative == null)
139: newAlternative = "";
140:
141: if ((currAlternative != null)
142: && (currAlternative.toString().length() > 0)) {
143: TKBaseField field = (TKBaseField) fieldHash
144: .get(currAlternative);
145: return new TKFieldSwitchData(currAlternative,
146: newAlternative, field.compileData(prefix
147: + currAlternative + ".", data, context));
148: }
149: return new TKFieldSwitchData("", newAlternative, null);
150: }
151:
152: public Object compileData(String prefix, TKMarkupNode data,
153: TKHashtable context) {
154: TKXmlMarkup markup = data == null ? null
155: : (TKXmlMarkup) data.markup;
156: if (markup == null) {
157: return null;
158: }
159:
160: if (!markup.name.equals(getName())) {
161: return null;
162: }
163:
164: TKXmlTree tree = (TKXmlTree) data.tree;
165: if (tree == null) {
166: return null;
167: }
168:
169: Object obj = tree.getSingleSub();
170: if (obj == null) {
171: return null;
172: }
173:
174: TKMarkupNode subNode = null;
175: TKXmlMarkup subMarkup = null;
176:
177: if (obj instanceof TKXmlMarkup) { // Atomares Markup
178:
179: subMarkup = (TKXmlMarkup) obj;
180: subNode = new TKMarkupNode(subMarkup, null);
181: } else {
182: subNode = (TKMarkupNode) obj;
183: subMarkup = (TKXmlMarkup) subNode.markup;
184: }
185:
186: if (subMarkup == null) {
187: return null;
188: }
189:
190: TKBaseField field = (TKBaseField) fieldHash.get(subMarkup.name);
191: if (field == null) {
192: return null;
193: }
194:
195: return new TKFieldSwitchData(subMarkup.name, "", field
196: .compileData(prefix + subMarkup.name + ".", subNode,
197: context));
198: }
199:
200: public Object getDefault() {
201: return new TKFieldSwitchData("", "", null);
202: }
203:
204: public Object modify(String action, String fieldPath, Object data,
205: String prefix, StringBuffer destination) {
206: TKFieldSwitchData switchData = (TKFieldSwitchData) data;
207: int pLength = prefix.length() + fieldName.length();
208: if (fieldPath.length() == pLength) {
209: destination.append(prefix + fieldName);
210: // action betrifft Alternative selbst
211: if (action.equals("SWITCH")) {
212: if (!switchData.alternative
213: .equals(switchData.newAlternative)) {
214: switchData.alternative = switchData.newAlternative;
215: if (switchData.newAlternative.equals("")) {
216: switchData.data = null;
217: } else {
218: TKBaseField field = (TKBaseField) fieldHash
219: .get(switchData.newAlternative);
220: switchData.data = field.getDefault();
221: }
222: }
223: }
224: } else {
225: // action betrifft Sub-ELement
226: int subNameEnd = fieldPath.indexOf('.', pLength + 1);
227: String subName = (subNameEnd < 0 ? fieldPath
228: .substring(pLength + 1) : fieldPath.substring(
229: pLength + 1, subNameEnd));
230: //TKHttp.out().println( "subName="+subName+", switchData.alternative="+switchData.alternative+"<br>" );
231: if (subName.equals(switchData.alternative)) {
232: TKBaseField field = (TKBaseField) fieldHash
233: .get(subName);
234: switchData.data = field.modify(action, fieldPath,
235: switchData.data, prefix + fieldName + '.'
236: + subName + '.', destination);
237: }
238: }
239: return data;
240: }
241:
242: public TKBaseField getTarget(String fieldPath, String prefix) {
243:
244: TKBaseField targetField = null;
245: int pLength = prefix.length() + fieldName.length();
246:
247: // action betrifft Sub-ELement
248: if (fieldPath.length() != pLength) {
249: int subNameEnd = fieldPath.indexOf('.', pLength + 1);
250: String subName = (subNameEnd < 0 ? fieldPath
251: .substring(pLength + 1) : fieldPath.substring(
252: pLength + 1, subNameEnd));
253: TKBaseField field = (TKBaseField) fieldHash.get(subName);
254: targetField = field.getTarget(fieldPath, prefix + fieldName
255: + '.' + subName + '.');
256: }
257: return targetField;
258: }
259:
260: public TKVector sortAllSwitch() {
261: TKVector switchVector = alternatives;
262: TKVector showNameVector = new TKVector();
263:
264: for (int i = 0; i < switchVector.size(); i++) {
265: TKBaseField field = (TKBaseField) switchVector.elementAt(i);
266: showNameVector.addElement(field.getShowName());
267: }
268: TKVector sortedShowName = showNameVector.sort();
269: TKVector sortedFieldVector = new TKVector();
270:
271: for (int x = 0; x < sortedShowName.size(); x++) {
272: String showName = (String) sortedShowName.elementAt(x);
273: for (int y = 0; y < switchVector.size(); y++) {
274: TKBaseField field = (TKBaseField) switchVector
275: .elementAt(y);
276: if (showName.equals(field.getShowName()))
277: sortedFieldVector.addElement(field);
278: }
279: }
280: return sortedFieldVector;
281: }
282:
283: public void fillIntoTemplate(TKHTMLTemplate t, Object data,
284: String prefix) {
285: TKFieldSwitchData switchData = (TKFieldSwitchData) data;
286: TKVector sortedAlternatives = sortAllSwitch();
287:
288: super .fillIntoTemplate(t, data, prefix);
289: t.setListIterator(new TKFieldSwitchIterator(sortedAlternatives,
290: fieldName, new TKFieldSwitchFakeIterator(switchData,
291: fieldHash, prefix + fieldName + ".", t
292: .getListIterator(), "SWITCH_FAKELIST"),
293: "ALTERNATIVE_LIST"));
294:
295: t.set("ALTERNATIVE", switchData.alternative);
296: t.set(prefix + fieldName + ".ALTERNATIVE",
297: switchData.alternative);
298:
299: if (this .check > CHECK_UNDEFINED) {
300: t.set(SELECT_CHECK_KEY, CHECK_NAMES[this .check]);
301: }
302: }
303:
304: public void fillIntoDOM(Document doc, Element node, Object data)
305: throws DOMException {
306: TKFieldSwitchData switchData = (TKFieldSwitchData) data;
307: Element me = doc.createElement(getInternationalName());
308: node.appendChild(me);
309: fillAttributesIntoNode(me, data);
310: if (switchData.alternative.length() > 0) {
311: TKBaseField field = (TKBaseField) fieldHash
312: .get(switchData.alternative);
313: field.fillIntoDOM(doc, me, switchData.data);
314: }
315: }
316:
317: public void fillIntoPresentation(TKHTMLTemplate t, Object data,
318: String scope) {
319: TKFieldSwitchData switchData = (TKFieldSwitchData) data;
320:
321: t.set(scope + "." + getName(), switchData.alternative);
322: if (switchData.alternative.length() > 0) {
323: TKBaseField field = (TKBaseField) fieldHash
324: .get(switchData.alternative);
325: field.fillIntoPresentation(t, switchData.data, scope + "."
326: + getName());
327: }
328: }
329:
330: public int insertDataIntoDB(TKContentDBData db, Object data,
331: int contentId, int leftNr) {
332: TKFieldSwitchData switchData = (TKFieldSwitchData) data;
333:
334: TKContentNodeTableData node = insertNewContentNode(db,
335: contentId, leftNr);
336: int newNodeId = node.content_node_id;
337:
338: insertNewContentValue(db, contentId, newNodeId, 0,
339: switchData.alternative);
340:
341: if (switchData.alternative.length() > 0) {
342: TKBaseField field = (TKBaseField) fieldHash
343: .get(switchData.alternative);
344: node.right_nr = field.insertDataIntoDB(db, switchData.data,
345: contentId, leftNr + 1) + 1;
346: }
347: return node.right_nr;
348: }
349:
350: public Object getDataFromDB(TKContentDBData db) {
351: TKContentNodeTableData node = getContentNodeFromDB(db);
352: TKContentValueTableData value = getContentNodeValueFromDB(db,
353: node);
354:
355: String alternative = value.value;
356: if (alternative.length() == 0)
357: return getDefault();
358:
359: //TKContentNodeTableData subNode = peekNextContentNode( db );
360: peekNextContentNode(db);
361:
362: TKBaseField field = (TKBaseField) fieldHash.get(alternative);
363: if (field == null) {
364: removeNextContentNode(db);
365: return getDefault();
366: }
367: return new TKFieldSwitchData(alternative, alternative, field
368: .getDataFromDB(db));
369: }
370:
371: public void clearId() {
372: if (fieldId == -1)
373: return;
374:
375: fieldId = -1;
376: Enumeration e = alternatives.elements();
377: while (e.hasMoreElements()) {
378: ((TKBaseField) e.nextElement()).clearId();
379: }
380: }
381:
382: public int realInsertIntoDB(TKFormDBData db, int formId) {
383: if (super .realInsertIntoDB(db, formId) == -1)
384: return -1;
385: insertNewSubFieldList(db, formId, ALTERNATIVES_KEY,
386: alternatives);
387: return fieldId;
388: }
389:
390: public void initFromDB(String classId, TKFormDBData db,
391: TKVector otherFields) throws TKUnregisteredClassException,
392: ClassNotFoundException, InstantiationException,
393: IllegalAccessException {
394: super .initFromDB(classId, db, otherFields);
395: alternatives = getSubFieldList(db, ALTERNATIVES_KEY,
396: otherFields);
397: this .fieldHash = getFieldHashFromList(alternatives);
398: }
399:
400: /**
401: * Checks wether this object and the specified object
402: * may be treated as equal.
403: *
404: * @param object the object to checked for equality.
405: * @return <CODE>true</CODE> if this object and the
406: * specified object may be treated as equal, otherwise
407: * <CODE>false</CODE>.
408: */
409: public boolean equals(Object object) {
410: if (!super .equals(object)) {
411: return false;
412: }
413:
414: TKFieldSwitch field = (TKFieldSwitch) object;
415:
416: return (this .alternatives == null ? field.alternatives == null
417: : this .alternatives.equals(field.alternatives));
418: }
419:
420: /**
421: * Returns the hash code for this object.
422: *
423: * @return the hash code for this object.
424: */
425: public int hashCode() {
426: // Implementation for JTest only ;-(
427: return super.hashCode();
428: }
429:
430: }
|