1 5 package com.teamkonzept.publishing.markups; 6 7 import java.sql.*; 8 import java.util.*; 9 10 import com.teamkonzept.db.*; 11 import com.teamkonzept.lib.*; 12 import com.teamkonzept.publishing.markups.db.*; 13 import com.teamkonzept.publishing.markups.db.queries.*; 14 import org.apache.log4j.Category; 15 16 public class TKMarkupParamDefinition implements TKSortable 17 { 18 private static final Category cat = Category.getInstance(TKMarkupParamDefinition.class); 19 20 public int id; 21 public String name; 22 public TKMarkupParamClass typeClass; 23 public boolean isCaseSensitive; 24 public TKVector optionList; 25 public TKHashtable optionLookup; 26 27 private String defaultValue; 28 private TKMarkupStatics statics = TKMarkupStatics.setup(); 29 public String data; 30 31 private TKMarkupParamDefinition (int id, String name, boolean isCaseSensitive, 33 String defaultValue, String data) throws Exception { 34 35 this.id = id; 36 this.name = name.toUpperCase(); 37 this.typeClass = defaultTypeClass(statics); 38 this.isCaseSensitive = isCaseSensitive; 39 this.optionList = null; 40 41 setData(data); 42 setDefaultValue(defaultValue); 43 44 this.optionLookup = null; 45 } 46 47 public TKMarkupParamDefinition (int id, String name, TKMarkupParamClass typeClass, 48 boolean isCaseSensitive, String defaultValue, String data) 49 throws Exception { 50 51 this (id,name,isCaseSensitive,defaultValue,data); 52 53 this.typeClass = typeClass; 54 55 if (this.typeClass == null) 56 throw new Exception ("Fehler in TKMarkupParamDefinition.init(): name="+name+ 57 " typeClass=null"); 58 59 this.optionList = this.typeClass == null ? null : this.typeClass.getOptions(data()); 60 61 defineOptions(); 62 } 63 64 public TKMarkupParamDefinition (int id, String name, String type, 65 boolean isCaseSensitive, String defaultValue, String data) throws Exception { 66 67 this (id,name,isCaseSensitive,defaultValue,data); 68 69 this.typeClass = TKMarkupParamClass.lookup(statics,type); 70 71 if (this.typeClass == null) 72 throw new Exception ("Fehler in TKMarkupParamDefinition.init(): name="+name+ 73 " typeClass=null"); 74 75 this.optionList = this.typeClass == null ? null : this.typeClass.getOptions(data()); 76 77 defineOptions(); 78 } 79 80 public TKMarkupParamDefinition (int id, String name, int typeClassId, 81 boolean isCaseSensitive, String defaultValue, String data) throws Exception { 82 83 this (id,name,isCaseSensitive,defaultValue,data); 84 85 this.typeClass = TKMarkupParamClass.lookup(statics,typeClassId); 86 87 if (this.typeClass == null) 88 throw new Exception ("Fehler in TKMarkupParamDefinition.init(): name="+name+ 89 " typeClass=null"); 90 91 this.optionList = this.typeClass == null ? null : this.typeClass.getOptions(data()); 92 93 defineOptions(); 94 } 95 96 private TKMarkupParamDefinition () { 97 98 this.id = 0; 99 this.name = null; 100 this.typeClass = defaultTypeClass(); 101 this.isCaseSensitive = true; 102 this.optionList = null; 103 this.optionLookup = null; 104 105 this.setData(null); 106 this.setDefaultValue(null); 107 } 108 109 public static TKMarkupParamDefinition makeDummy (String name) { 110 111 TKMarkupParamDefinition param = new TKMarkupParamDefinition (); 112 113 param.name = name.toUpperCase(); 114 return param; 115 } 116 117 public String defaultValue() { 118 119 if (defaultValue == null) return null; 120 if (defaultValue.length() == 0) defaultValue = null; 121 return defaultValue; 122 } 123 124 public void setDefaultValue (String value) { 125 126 if ((value == null) || (value.length() == 0)) defaultValue = null; 127 else defaultValue = isCaseSensitive ? value : value.toUpperCase(); 128 } 129 130 public String data() { 131 132 if (data == null) return null; 133 if (data.length() == 0) data = null; 134 return data; 135 } 136 137 public void setData (String data) { 138 139 this.data = (data == null) || (data.length() == 0) ? null : data; 140 } 141 142 public boolean isFlag () { 143 144 return (typeClass != null) && typeClass.isFlag(); 145 } 146 147 public void setOptions (TKVector optionList) { 148 149 this.optionList = optionList; 150 setData(this.typeClass == null ? null : this.typeClass.setOptions(optionList)); 151 152 defineOptions(); 153 } 154 155 public void defineOptions () { 156 157 this.optionLookup = null; 158 159 if (this.optionList == null) return; 160 161 Enumeration e = this.optionList.elements(); 162 while (e.hasMoreElements()) { 163 164 String option = (String ) e.nextElement(); 165 if (option != null) { 166 167 if (optionLookup == null) optionLookup = new TKHashtable (); 168 if (this.isCaseSensitive) optionLookup.put (option,option); 169 else optionLookup.put (option.toUpperCase(),option); 170 } 171 } 172 } 173 174 public String checkValue (String value, boolean editable) throws Exception { 175 176 if ((value == null) || (typeClass == null)) return value; 177 178 String checkedValue = typeClass.checkValue (value); 179 if (checkedValue == null) 180 throw new Exception ("Parameter "+name+": Wert ("+typeClass.name+") '"+value+"' ist falsch"); 181 182 String lookupValue = this.isCaseSensitive ? checkedValue : checkedValue.toUpperCase(); 183 184 if (!editable && (optionLookup != null) && (optionLookup.get (lookupValue) == null)) 185 throw new Exception ("Parameter "+name+": Wert ("+typeClass.name+") '"+ 186 checkedValue+"' ist keine der zul‰ssigen Optionen"); 187 188 return checkedValue; 189 } 190 191 public static String defaultType () { 193 194 return "Zeichenkette"; 195 } 196 197 public static TKMarkupParamClass defaultTypeClass () { 198 199 return defaultTypeClass(TKMarkupStatics.setup()); 200 } 201 202 public static TKMarkupParamClass defaultTypeClass (TKMarkupStatics statics) { 203 204 if (statics.defaultTypeClass == null) 205 statics.defaultTypeClass = TKMarkupParamClass.lookup(statics,defaultType()); 206 207 if (statics.defaultTypeClass == null) 208 cat.error ("Fehler in TKMarkupParamDefinition.init(): type="+ 209 defaultType()+" typeClass=null"); 210 211 return statics.defaultTypeClass; 212 } 213 214 public static Enumeration allParams () { 215 216 return TKMarkupStatics.setup().paramNames.elements(); 217 } 218 219 public static Enumeration allTypes () { 220 221 return TKMarkupParamClass.allNames(TKMarkupStatics.setup()); 222 } 223 224 public static void define (TKMarkupParamDefinition definition) { 225 226 define (TKMarkupStatics.setup(),definition); 227 } 228 229 public static synchronized void define (TKMarkupStatics statics, TKMarkupParamDefinition definition) { 230 231 if ((definition == null) || (definition.id < 0)) return; 232 233 statics.paramIds.put (new Integer (definition.id),definition); 234 statics.paramNames.put (definition.name.toUpperCase(),definition); 235 236 } 237 238 public static synchronized void unDefine (String name) { 239 240 if (name == null) return; 241 242 TKMarkupStatics statics = TKMarkupStatics.setup(); 243 TKMarkupParamDefinition definition = lookup (statics,name); 244 245 if (definition != null) statics.paramIds.remove (new Integer (definition.id)); 246 statics.paramNames.remove (name.toUpperCase()); 247 } 248 249 public static synchronized void unDefine (int id) { 250 251 if (id < 0) return; 252 253 TKMarkupStatics statics = TKMarkupStatics.setup(); 254 TKMarkupParamDefinition definition = lookup (statics,id); 255 256 if (definition != null) statics.paramNames.remove (definition.name.toUpperCase()); 257 statics.paramIds.remove (new Integer (id)); 258 } 259 260 public static TKMarkupParamDefinition lookup (String name) { 261 262 return lookup (TKMarkupStatics.setup(),name); 263 } 264 265 public static TKMarkupParamDefinition lookup (int id) { 266 267 return lookup (TKMarkupStatics.setup(),id); 268 } 269 270 public static synchronized TKMarkupParamDefinition lookup (TKMarkupStatics statics, String name) { 271 272 return name == null ? null : (TKMarkupParamDefinition) statics.paramNames.get(name.toUpperCase()); 273 } 274 275 public static synchronized TKMarkupParamDefinition lookup (TKMarkupStatics statics, int id) { 276 277 return id < 0 ? null : (TKMarkupParamDefinition) statics.paramIds.get(new Integer (id)); 278 } 279 280 public int cmp (TKSortable other) { 281 282 if (!(other instanceof TKMarkupParamDefinition)) 283 return toString().compareTo (other.toString()); 284 285 TKMarkupParamDefinition otherParam = (TKMarkupParamDefinition) other; 286 287 if ((name == null) && (otherParam.name != null)) return -1; 288 else if ((name != null) && (otherParam.name == null)) return 1; 289 else if ((name == null) && (otherParam.name == null)) return 0; 290 else return name.compareTo (otherParam.name); 291 }; 292 293 public String toString() { 294 295 StringBuffer buf = new StringBuffer (); 296 297 buf.append("id=").append(id) 298 .append(" name=").append(name) 299 .append(" type=").append(typeClass.name) 300 .append(" isCaseSensitive=").append(isCaseSensitive) 301 .append(" optionList=").append(optionList) 302 .append(" defaultValue=\"").append(defaultValue).append('"'); 303 304 return new String (buf); 305 } 306 307 public static void getDefinitions() throws SQLException { 308 309 TKMarkupStatics statics = TKMarkupStatics.setup(); 310 311 statics.paramNames = new TKHashtable (); 312 statics.paramIds = new TKHashtable (); 313 314 TKQuery q = TKDBManager.newQuery(TKMarkupParamGetDefinitions.class); 315 q.execute(); 316 ResultSet rs = q.fetchResultSet(); 317 318 while (rs.next()) { 319 320 TKMarkupParamDefinition param = 321 TKMarkupParamDefinitionDBData.newParamFromResultSet (rs); 322 323 if (param != null) define (statics,param); 324 } 325 } 326 327 public static synchronized void setup () { 328 329 try { getDefinitions(); } catch (Exception ex) 330 { 331 cat.error ("TKMarkupParamDefinition.setup() failed: ", ex); 332 } catch (TKSQLError er) { 333 cat.error ("TKMarkupParamDefinition.setup() failed: ", er); 334 } 335 } 336 337 public static void putDefinition(TKMarkupParamDefinition definition) throws SQLException, Exception { 338 339 if (definition == null) return; 340 341 StringBuffer diagnostics = new StringBuffer (); 342 TKMarkupParamDefinitionDBData dbData = new TKMarkupParamDefinitionDBData (definition,diagnostics); 343 344 if (definition.id < 0) TKMarkupParamDefinitionDBInterface.New (dbData); 345 else TKMarkupParamDefinitionDBInterface.Put (dbData); 346 347 if (diagnostics.length() != 0) throw new Exception (diagnostics.toString()); 348 349 unDefine (definition.id); 350 definition.id = dbData.param.id; 351 define (dbData.param); 352 353 Enumeration e = TKMarkupDefinition.allMarkups(); 354 while ((e != null) && e.hasMoreElements()) { 355 356 TKMarkupDefinition markup = (TKMarkupDefinition) e.nextElement(); 357 if (markup == null) continue; 358 359 Enumeration ee = markup.allParams(); 360 while ((ee != null) && ee.hasMoreElements()) { 361 362 TKMarkupParamCall param = (TKMarkupParamCall) ee.nextElement(); 363 364 if ((param != null) && param.param.name.equalsIgnoreCase(definition.name)) 365 param.param = dbData.param; 366 } 367 } 368 } 369 370 public synchronized void save () throws Exception { 371 372 define (this); 373 374 try { putDefinition(this); } catch (Exception ex) 375 { 376 cat.error ("TKMarkupParamDefinition.save() failed: definition="+this, ex); 377 throw new Exception ("save failed: "); 378 379 } catch (TKSQLError er) 380 { 381 cat.error ("TKMarkupParamDefinition.save() failed: definition="+this, er); 382 throw new Exception ("save failed: definition='"+this+"'"); 383 } 384 } 385 386 public static void deleteDefinition(TKMarkupParamDefinition definition) throws SQLException, Exception { 387 388 if (definition == null) return; 389 390 StringBuffer diagnostics = new StringBuffer (); 391 392 Enumeration e = TKMarkupDefinition.allMarkups(); 393 while ((e != null) && e.hasMoreElements()) { 394 395 TKMarkupDefinition markup = (TKMarkupDefinition) e.nextElement(); 396 if (markup == null) continue; 397 398 Enumeration ee = markup.allParams(); 399 while ((ee != null) && ee.hasMoreElements()) { 400 401 TKMarkupParamCall param = (TKMarkupParamCall) ee.nextElement(); 402 403 if ((param != null) && param.param.name.equalsIgnoreCase(definition.name)) { 404 405 if (diagnostics.length() == 0) 406 diagnostics.append ("Folgende Markups enthalten diesen Parameter: "); 407 else diagnostics.append (", "); 408 409 diagnostics.append (markup.name); 410 } 411 } 412 } 413 414 if (diagnostics.length() != 0) throw new Exception (diagnostics.toString()); 415 416 TKMarkupParamDefinitionDBData dbData = new TKMarkupParamDefinitionDBData (definition,diagnostics); 417 TKMarkupParamDefinitionDBInterface.Del (dbData); 418 419 if (diagnostics.length() != 0) throw new Exception (diagnostics.toString()); 420 421 unDefine (dbData.param.id); 422 } 423 424 public synchronized void delete () throws Exception { 425 426 try { deleteDefinition(this); } catch (Exception ex) 427 { 428 cat.error("TKMarkupParamDefinition.delete() failed: definition="+this, ex); 429 throw new Exception ("delete failed: "); 430 } catch (TKSQLError er) 431 { 432 cat.error("TKMarkupParamDefinition.delete() failed: definition="+this, er); 433 throw new Exception ("delete failed: definition='"+this+"'"); 434 } 435 } 436 } 437 438 | Popular Tags |