001: /*
002: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/publishing/markups/TKMarkupParamDefinition.java,v 1.7 2001/06/11 09:14:10 alex Exp $
003: *
004: */
005: package com.teamkonzept.publishing.markups;
006:
007: import java.sql.*;
008: import java.util.*;
009:
010: import com.teamkonzept.db.*;
011: import com.teamkonzept.lib.*;
012: import com.teamkonzept.publishing.markups.db.*;
013: import com.teamkonzept.publishing.markups.db.queries.*;
014: import org.apache.log4j.Category;
015:
016: public class TKMarkupParamDefinition implements TKSortable {
017: private static final Category cat = Category
018: .getInstance(TKMarkupParamDefinition.class);
019:
020: public int id;
021: public String name;
022: public TKMarkupParamClass typeClass;
023: public boolean isCaseSensitive;
024: public TKVector optionList;
025: public TKHashtable optionLookup;
026:
027: private String defaultValue;
028: private TKMarkupStatics statics = TKMarkupStatics.setup();
029: public String data;
030:
031: // bitte NICHT ˆffentlich machen (w¸rde Integrit‰t verletzen)
032: private TKMarkupParamDefinition(int id, String name,
033: boolean isCaseSensitive, String defaultValue, String data)
034: throws Exception {
035:
036: this .id = id;
037: this .name = name.toUpperCase();
038: this .typeClass = defaultTypeClass(statics);
039: this .isCaseSensitive = isCaseSensitive;
040: this .optionList = null;
041:
042: setData(data);
043: setDefaultValue(defaultValue);
044:
045: this .optionLookup = null;
046: }
047:
048: public TKMarkupParamDefinition(int id, String name,
049: TKMarkupParamClass typeClass, boolean isCaseSensitive,
050: String defaultValue, String data) throws Exception {
051:
052: this (id, name, isCaseSensitive, defaultValue, data);
053:
054: this .typeClass = typeClass;
055:
056: if (this .typeClass == null)
057: throw new Exception(
058: "Fehler in TKMarkupParamDefinition.init(): name="
059: + name + " typeClass=null");
060:
061: this .optionList = this .typeClass == null ? null
062: : this .typeClass.getOptions(data());
063:
064: defineOptions();
065: }
066:
067: public TKMarkupParamDefinition(int id, String name, String type,
068: boolean isCaseSensitive, String defaultValue, String data)
069: throws Exception {
070:
071: this (id, name, isCaseSensitive, defaultValue, data);
072:
073: this .typeClass = TKMarkupParamClass.lookup(statics, type);
074:
075: if (this .typeClass == null)
076: throw new Exception(
077: "Fehler in TKMarkupParamDefinition.init(): name="
078: + name + " typeClass=null");
079:
080: this .optionList = this .typeClass == null ? null
081: : this .typeClass.getOptions(data());
082:
083: defineOptions();
084: }
085:
086: public TKMarkupParamDefinition(int id, String name,
087: int typeClassId, boolean isCaseSensitive,
088: String defaultValue, String data) throws Exception {
089:
090: this (id, name, isCaseSensitive, defaultValue, data);
091:
092: this .typeClass = TKMarkupParamClass
093: .lookup(statics, typeClassId);
094:
095: if (this .typeClass == null)
096: throw new Exception(
097: "Fehler in TKMarkupParamDefinition.init(): name="
098: + name + " typeClass=null");
099:
100: this .optionList = this .typeClass == null ? null
101: : this .typeClass.getOptions(data());
102:
103: defineOptions();
104: }
105:
106: private TKMarkupParamDefinition() {
107:
108: this .id = 0;
109: this .name = null;
110: this .typeClass = defaultTypeClass();
111: this .isCaseSensitive = true;
112: this .optionList = null;
113: this .optionLookup = null;
114:
115: this .setData(null);
116: this .setDefaultValue(null);
117: }
118:
119: public static TKMarkupParamDefinition makeDummy(String name) {
120:
121: TKMarkupParamDefinition param = new TKMarkupParamDefinition();
122:
123: param.name = name.toUpperCase();
124: return param;
125: }
126:
127: public String defaultValue() {
128:
129: if (defaultValue == null)
130: return null;
131: if (defaultValue.length() == 0)
132: defaultValue = null;
133: return defaultValue;
134: }
135:
136: public void setDefaultValue(String value) {
137:
138: if ((value == null) || (value.length() == 0))
139: defaultValue = null;
140: else
141: defaultValue = isCaseSensitive ? value : value
142: .toUpperCase();
143: }
144:
145: public String data() {
146:
147: if (data == null)
148: return null;
149: if (data.length() == 0)
150: data = null;
151: return data;
152: }
153:
154: public void setData(String data) {
155:
156: this .data = (data == null) || (data.length() == 0) ? null
157: : data;
158: }
159:
160: public boolean isFlag() {
161:
162: return (typeClass != null) && typeClass.isFlag();
163: }
164:
165: public void setOptions(TKVector optionList) {
166:
167: this .optionList = optionList;
168: setData(this .typeClass == null ? null : this .typeClass
169: .setOptions(optionList));
170:
171: defineOptions();
172: }
173:
174: public void defineOptions() {
175:
176: this .optionLookup = null;
177:
178: if (this .optionList == null)
179: return;
180:
181: Enumeration e = this .optionList.elements();
182: while (e.hasMoreElements()) {
183:
184: String option = (String) e.nextElement();
185: if (option != null) {
186:
187: if (optionLookup == null)
188: optionLookup = new TKHashtable();
189: if (this .isCaseSensitive)
190: optionLookup.put(option, option);
191: else
192: optionLookup.put(option.toUpperCase(), option);
193: }
194: }
195: }
196:
197: public String checkValue(String value, boolean editable)
198: throws Exception {
199:
200: if ((value == null) || (typeClass == null))
201: return value;
202:
203: String checkedValue = typeClass.checkValue(value);
204: if (checkedValue == null)
205: throw new Exception("Parameter " + name + ": Wert ("
206: + typeClass.name + ") '" + value + "' ist falsch");
207:
208: String lookupValue = this .isCaseSensitive ? checkedValue
209: : checkedValue.toUpperCase();
210:
211: if (!editable && (optionLookup != null)
212: && (optionLookup.get(lookupValue) == null))
213: throw new Exception("Parameter " + name + ": Wert ("
214: + typeClass.name + ") '" + checkedValue
215: + "' ist keine der zul‰ssigen Optionen");
216:
217: return checkedValue;
218: }
219:
220: // Bitte nicht ˆffentlich machen !
221: public static String defaultType() {
222:
223: return "Zeichenkette";
224: }
225:
226: public static TKMarkupParamClass defaultTypeClass() {
227:
228: return defaultTypeClass(TKMarkupStatics.setup());
229: }
230:
231: public static TKMarkupParamClass defaultTypeClass(
232: TKMarkupStatics statics) {
233:
234: if (statics.defaultTypeClass == null)
235: statics.defaultTypeClass = TKMarkupParamClass.lookup(
236: statics, defaultType());
237:
238: if (statics.defaultTypeClass == null)
239: cat.error("Fehler in TKMarkupParamDefinition.init(): type="
240: + defaultType() + " typeClass=null");
241:
242: return statics.defaultTypeClass;
243: }
244:
245: public static Enumeration allParams() {
246:
247: return TKMarkupStatics.setup().paramNames.elements();
248: }
249:
250: public static Enumeration allTypes() {
251:
252: return TKMarkupParamClass.allNames(TKMarkupStatics.setup());
253: }
254:
255: public static void define(TKMarkupParamDefinition definition) {
256:
257: define(TKMarkupStatics.setup(), definition);
258: }
259:
260: public static synchronized void define(TKMarkupStatics statics,
261: TKMarkupParamDefinition definition) {
262:
263: if ((definition == null) || (definition.id < 0))
264: return;
265:
266: statics.paramIds.put(new Integer(definition.id), definition);
267: statics.paramNames.put(definition.name.toUpperCase(),
268: definition);
269:
270: }
271:
272: public static synchronized void unDefine(String name) {
273:
274: if (name == null)
275: return;
276:
277: TKMarkupStatics statics = TKMarkupStatics.setup();
278: TKMarkupParamDefinition definition = lookup(statics, name);
279:
280: if (definition != null)
281: statics.paramIds.remove(new Integer(definition.id));
282: statics.paramNames.remove(name.toUpperCase());
283: }
284:
285: public static synchronized void unDefine(int id) {
286:
287: if (id < 0)
288: return;
289:
290: TKMarkupStatics statics = TKMarkupStatics.setup();
291: TKMarkupParamDefinition definition = lookup(statics, id);
292:
293: if (definition != null)
294: statics.paramNames.remove(definition.name.toUpperCase());
295: statics.paramIds.remove(new Integer(id));
296: }
297:
298: public static TKMarkupParamDefinition lookup(String name) {
299:
300: return lookup(TKMarkupStatics.setup(), name);
301: }
302:
303: public static TKMarkupParamDefinition lookup(int id) {
304:
305: return lookup(TKMarkupStatics.setup(), id);
306: }
307:
308: public static synchronized TKMarkupParamDefinition lookup(
309: TKMarkupStatics statics, String name) {
310:
311: return name == null ? null
312: : (TKMarkupParamDefinition) statics.paramNames.get(name
313: .toUpperCase());
314: }
315:
316: public static synchronized TKMarkupParamDefinition lookup(
317: TKMarkupStatics statics, int id) {
318:
319: return id < 0 ? null
320: : (TKMarkupParamDefinition) statics.paramIds
321: .get(new Integer(id));
322: }
323:
324: public int cmp(TKSortable other) {
325:
326: if (!(other instanceof TKMarkupParamDefinition))
327: return toString().compareTo(other.toString());
328:
329: TKMarkupParamDefinition otherParam = (TKMarkupParamDefinition) other;
330:
331: if ((name == null) && (otherParam.name != null))
332: return -1;
333: else if ((name != null) && (otherParam.name == null))
334: return 1;
335: else if ((name == null) && (otherParam.name == null))
336: return 0;
337: else
338: return name.compareTo(otherParam.name);
339: };
340:
341: public String toString() {
342:
343: StringBuffer buf = new StringBuffer();
344:
345: buf.append("id=").append(id).append(" name=").append(name)
346: .append(" type=").append(typeClass.name).append(
347: " isCaseSensitive=").append(isCaseSensitive)
348: .append(" optionList=").append(optionList).append(
349: " defaultValue=\"").append(defaultValue)
350: .append('"');
351:
352: return new String(buf);
353: }
354:
355: public static void getDefinitions() throws SQLException {
356:
357: TKMarkupStatics statics = TKMarkupStatics.setup();
358:
359: statics.paramNames = new TKHashtable();
360: statics.paramIds = new TKHashtable();
361:
362: TKQuery q = TKDBManager
363: .newQuery(TKMarkupParamGetDefinitions.class);
364: q.execute();
365: ResultSet rs = q.fetchResultSet();
366:
367: while (rs.next()) {
368:
369: TKMarkupParamDefinition param = TKMarkupParamDefinitionDBData
370: .newParamFromResultSet(rs);
371:
372: if (param != null)
373: define(statics, param);
374: }
375: }
376:
377: public static synchronized void setup() {
378:
379: try {
380: getDefinitions();
381: } catch (Exception ex) {
382: cat.error("TKMarkupParamDefinition.setup() failed: ", ex);
383: } catch (TKSQLError er) {
384: cat.error("TKMarkupParamDefinition.setup() failed: ", er);
385: }
386: }
387:
388: public static void putDefinition(TKMarkupParamDefinition definition)
389: throws SQLException, Exception {
390:
391: if (definition == null)
392: return;
393:
394: StringBuffer diagnostics = new StringBuffer();
395: TKMarkupParamDefinitionDBData dbData = new TKMarkupParamDefinitionDBData(
396: definition, diagnostics);
397:
398: if (definition.id < 0)
399: TKMarkupParamDefinitionDBInterface.New(dbData);
400: else
401: TKMarkupParamDefinitionDBInterface.Put(dbData);
402:
403: if (diagnostics.length() != 0)
404: throw new Exception(diagnostics.toString());
405:
406: unDefine(definition.id);
407: definition.id = dbData.param.id;
408: define(dbData.param);
409:
410: Enumeration e = TKMarkupDefinition.allMarkups();
411: while ((e != null) && e.hasMoreElements()) {
412:
413: TKMarkupDefinition markup = (TKMarkupDefinition) e
414: .nextElement();
415: if (markup == null)
416: continue;
417:
418: Enumeration ee = markup.allParams();
419: while ((ee != null) && ee.hasMoreElements()) {
420:
421: TKMarkupParamCall param = (TKMarkupParamCall) ee
422: .nextElement();
423:
424: if ((param != null)
425: && param.param.name
426: .equalsIgnoreCase(definition.name))
427: param.param = dbData.param;
428: }
429: }
430: }
431:
432: public synchronized void save() throws Exception {
433:
434: define(this );
435:
436: try {
437: putDefinition(this );
438: } catch (Exception ex) {
439: cat.error(
440: "TKMarkupParamDefinition.save() failed: definition="
441: + this , ex);
442: throw new Exception("save failed: ");
443:
444: } catch (TKSQLError er) {
445: cat.error(
446: "TKMarkupParamDefinition.save() failed: definition="
447: + this , er);
448: throw new Exception("save failed: definition='" + this
449: + "'");
450: }
451: }
452:
453: public static void deleteDefinition(
454: TKMarkupParamDefinition definition) throws SQLException,
455: Exception {
456:
457: if (definition == null)
458: return;
459:
460: StringBuffer diagnostics = new StringBuffer();
461:
462: Enumeration e = TKMarkupDefinition.allMarkups();
463: while ((e != null) && e.hasMoreElements()) {
464:
465: TKMarkupDefinition markup = (TKMarkupDefinition) e
466: .nextElement();
467: if (markup == null)
468: continue;
469:
470: Enumeration ee = markup.allParams();
471: while ((ee != null) && ee.hasMoreElements()) {
472:
473: TKMarkupParamCall param = (TKMarkupParamCall) ee
474: .nextElement();
475:
476: if ((param != null)
477: && param.param.name
478: .equalsIgnoreCase(definition.name)) {
479:
480: if (diagnostics.length() == 0)
481: diagnostics
482: .append("Folgende Markups enthalten diesen Parameter: ");
483: else
484: diagnostics.append(", ");
485:
486: diagnostics.append(markup.name);
487: }
488: }
489: }
490:
491: if (diagnostics.length() != 0)
492: throw new Exception(diagnostics.toString());
493:
494: TKMarkupParamDefinitionDBData dbData = new TKMarkupParamDefinitionDBData(
495: definition, diagnostics);
496: TKMarkupParamDefinitionDBInterface.Del(dbData);
497:
498: if (diagnostics.length() != 0)
499: throw new Exception(diagnostics.toString());
500:
501: unDefine(dbData.param.id);
502: }
503:
504: public synchronized void delete() throws Exception {
505:
506: try {
507: deleteDefinition(this );
508: } catch (Exception ex) {
509: cat.error(
510: "TKMarkupParamDefinition.delete() failed: definition="
511: + this , ex);
512: throw new Exception("delete failed: ");
513: } catch (TKSQLError er) {
514: cat.error(
515: "TKMarkupParamDefinition.delete() failed: definition="
516: + this , er);
517: throw new Exception("delete failed: definition='" + this
518: + "'");
519: }
520: }
521: }
|