1 package com.teamkonzept.field; 2 3 import com.teamkonzept.lib.*; 4 import com.teamkonzept.publishing.markups.*; 5 import com.teamkonzept.web.*; 6 import com.teamkonzept.field.db.*; 7 import java.util.*; 8 import org.w3c.dom.Element ; 9 import org.w3c.dom.DOMException ; 10 import org.w3c.dom.Document ; 11 import com.teamkonzept.international.LanguageManager; 12 13 22 public abstract class TKBaseField 23 implements XMLAttributes 24 { 25 27 public static final String DIAGS_KEY = "### diagnostics"; 28 29 public static final String NAME_KEY = "NAME"; 30 public static final String SHOW_NAME_KEY = "SHOWNAME"; 31 public static final String SUB_LIST_KEY = "ALLLIST"; 32 public static final String SUB_TYPE_KEY = "ALL"; 33 public static final String BASEPATH_PAR = "UPBASE"; 34 public static final String NEWFILENAME = "NEWFILENAME"; 35 public static final String OLDFILENAME = "OLDFILENAME"; 36 37 40 public String fieldType = null; 41 42 45 public String fieldName = null; 46 47 50 public String showName = null; 51 52 public static final String LANGUAGE_CONTEXT = "field"; 53 54 protected int fieldId; 55 56 57 public TKBaseField() {}; 58 59 public TKBaseField( String fieldType, String fieldName, String showName ) 60 { 61 initBaseField( fieldType, fieldName, showName ); 62 } 63 64 public final void initBaseField( String fieldType, String fieldName, String showName ) 65 { 66 this.fieldName = fieldName; 67 this.fieldType = fieldType; 68 this.showName = showName; 69 } 70 71 public void init( String fieldType, Object initData ) 72 throws 73 TKUnregisteredClassException, 74 ClassNotFoundException , 75 InstantiationException , 76 IllegalAccessException  77 { 78 TKHashtable data = (TKHashtable) initData; 79 initBaseField( fieldType, ((String ) data.get(NAME_KEY)).toUpperCase(), (String ) data.get(SHOW_NAME_KEY) ); 80 } 81 82 public void addToContext ( Object value, String key, TKHashtable context ) { 83 84 if (context == null || key == null || value == null) return; 85 86 TKVector vec; 87 88 Object obj = context.get (key); 89 if (obj == null) { 90 91 vec = new TKVector(); 92 context.put (key,vec); 93 94 } else if (!(obj instanceof TKVector)) return; 95 else vec = (TKVector) obj; 96 97 vec.addElement (value); 98 } 99 100 public Object getFromContext ( String key, TKHashtable context ) { 101 102 return context == null || key == null ? null : context.get (key); 103 } 104 105 108 public abstract TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList); 109 110 113 public void setName(String newShowName, String newFieldName) 114 { 115 showName = newShowName; 116 fieldName = newFieldName.toUpperCase(); 117 } 118 119 public Object toData() 120 { 121 TKHashtable result = new TKHashtable(2); 122 result.put( NAME_KEY, fieldName ); 123 if( showName != null ) 124 result.put( SHOW_NAME_KEY, showName ); 125 return result; 126 } 127 128 public void fillIntoTemplate( TKHTMLTemplate t, Object value, String prefix ) 129 { 130 t.set( "NAME", fieldName ); 131 t.set( "PREFIX", prefix ); 132 t.set( "SHOWNAME", getShowName() ); 133 t.set(prefix+"FIELDTYPE",fieldType); 135 } 136 137 public abstract void fillIntoPresentation( TKHTMLTemplate t, Object value, String prefix ); 138 139 145 public abstract void fillIntoDOM(Document doc, Element node, Object value) throws DOMException ; 146 147 150 public void fillAttributesIntoNode(Element node, Object data) throws DOMException  151 { 152 node.setAttribute (TYPE, this.getClass().getName()); 153 node.setAttribute (NAME, getName()); 154 } 155 156 public final String getName() 157 { 158 return fieldName; 159 } 160 161 public String getInternationalName() 162 { 163 String text = LanguageManager.getText(LANGUAGE_CONTEXT, fieldType); 164 if (text == null || text.equals("")) 165 return fieldName; 166 return TKUploadField.toFilename(text); 167 } 168 169 public final String getShowName() 170 { 171 return ( showName == null ? fieldName : showName ); 172 } 173 174 public final String getType() 175 { 176 return fieldType; 177 } 178 179 public abstract Object compileData( String prefix, TKHashtable data, TKHashtable context ); 180 181 public abstract Object compileData( String prefix, TKMarkupNode data, TKHashtable context ); 182 183 public TKBaseField getField( String fieldPath, String prefix ) 184 { 185 if( fieldPath.equals( prefix+fieldName ) ) { 186 return this; 187 } 188 else { 189 return null; 190 } 191 } 192 193 public Object getDefault() 194 { 195 return ""; 196 } 197 198 public Object modify( String action, String fieldPath, Object data, String prefix, StringBuffer destination ) 199 { 200 return data; 201 } 202 203 public String modify( String action, String fieldPath, Object data ) 204 { 205 StringBuffer dest = new StringBuffer (); 206 modify( action, fieldPath, data, "", dest ); 207 return dest.toString(); 208 } 209 210 211 public TKBaseField getTarget( String fieldPath, String prefix ) 212 { 213 return this; 214 } 215 216 public TKHashtable finishExtModify( String action, TKHashtable params) 217 { 218 return params; 219 } 220 221 224 public boolean hasFieldAttribute( TKFormDBData db, String name, int idx ) 225 { 226 227 if(db.field_attribute.isEmpty()) 228 return false; 229 TKFieldAttributeTableData attrib = (TKFieldAttributeTableData) db.field_attribute.firstElement(); 230 231 232 if( (attrib.field_id != fieldId) 233 || (attrib.idx != idx) 234 || (!attrib.name.equals( name )) 235 ) { 236 return false; 237 } 238 else { 239 return true; 240 } 241 242 } 243 244 public String getFieldAttribute( TKFormDBData db, String name, int idx ) 245 { 246 247 TKFieldAttributeTableData attrib = (TKFieldAttributeTableData) db.field_attribute.firstElement(); 248 249 if( (attrib.field_id != fieldId) 250 || (attrib.idx != idx) 251 || (!attrib.name.equals( name )) 252 ) { 253 throw new TKFormDBError( "FIELD_ATTRIBUTE (got "+attrib.name+")", name, idx ); 254 } 255 256 db.field_attribute.removeElementAt(0); 257 return attrib.value; 258 } 259 260 public TKBaseField getSubField( TKFormDBData db, String name, int idx, TKVector otherFields ) 261 throws 262 TKUnregisteredClassException, 263 ClassNotFoundException , 264 InstantiationException , 265 IllegalAccessException  266 { 267 TKSubFieldTableData subFieldDB = (TKSubFieldTableData) db.sub_field.firstElement(); 268 269 if( (subFieldDB.field_id != fieldId) 270 || (subFieldDB.idx != idx) 271 || (!subFieldDB.name.equals( name )) 272 ) { 273 throw new TKFormDBError( "SUB_FIELD (got "+subFieldDB.name+")", name, idx ); 274 } 275 276 db.sub_field.removeElementAt(0); 277 278 int subId = subFieldDB.sub_field_id; 279 if( subId < otherFields.size() ) return (TKBaseField) otherFields.get(subId); 280 281 if( subId == fieldId ) return this; 282 283 return TKFieldRegistry.getFieldFromDB( db, otherFields ); 284 } 285 286 public TKVector getSubFieldList( TKFormDBData db, String name, TKVector otherFields ) 287 throws 288 TKUnregisteredClassException, 289 ClassNotFoundException , 290 InstantiationException , 291 IllegalAccessException  292 { 293 int size = Integer.parseInt( getFieldAttribute( db, name, 0 ) ); 294 295 if( size == 0 ) return new TKVector(); 296 297 TKVector fieldList = new TKVector( size ); 298 for( int i=0; i<size; i++ ) { 299 fieldList.addElement( getSubField( db, name, i, otherFields ) ); 300 } 301 return fieldList; 302 } 303 304 public void initFromDB( String classId, TKFormDBData db, TKVector otherFields ) 305 throws 306 TKUnregisteredClassException, 307 ClassNotFoundException , 308 InstantiationException , 309 IllegalAccessException  310 { 311 TKFieldTableData field = (TKFieldTableData) db.field.firstElement(); 312 313 if( !field.field_type.equals( classId ) ) { 314 throw new TKFormDBError( "FIELD (got "+field.field_type+")", classId, 0 ); 315 } 316 317 db.field.removeElementAt(0); 318 initBaseField( field.field_type, field.field_name, field.field_show_name ); 319 fieldId = field.field_id; 320 otherFields.put( fieldId, this ); 321 } 322 323 public void clearId() 324 { 325 fieldId = -1; 326 } 327 328 public TKSubFieldTableData insertNewSubField( TKFormDBData db, int formId, String name, int idx ) 329 { 330 TKSubFieldTableData result = new TKSubFieldTableData( 331 formId, 332 fieldId, 333 0, 334 name, 335 idx 336 ); 337 db.sub_field.addElement( result ); 338 return result; 339 } 340 341 public void insertNewSubFieldList( TKFormDBData db, int formId, String name, TKVector fieldList ) 342 { 343 insertNewFieldAttribute( db, formId, name, 0, String.valueOf( fieldList.size() ) ); 344 345 Enumeration e = fieldList.elements(); 346 int i=0; 347 while( e.hasMoreElements() ) { 348 TKSubFieldTableData subFieldDB = insertNewSubField( db, formId, name, i++ ); 349 TKBaseField subField = (TKBaseField) e.nextElement(); 350 subField.realInsertIntoDB( db, formId ); 351 subFieldDB.sub_field_id = subField.fieldId; 352 } 353 354 } 355 public void insertNewFieldAttribute( TKFormDBData db, int formId, String name, int idx, String value ) 356 { 357 db.field_attribute.addElement( new TKFieldAttributeTableData( 358 formId, 359 fieldId, 360 name, 361 idx, 362 value 363 ) ); 364 } 365 366 public int realInsertIntoDB( TKFormDBData db, int formId ) 367 { 368 if( fieldId != -1 ) return -1; 369 370 fieldId = db.field.size(); 371 TKFieldTableData field = new TKFieldTableData( 372 formId, 373 fieldId, 374 fieldType, 375 fieldName, 376 showName 377 ); 378 db.field.addElement( field ); 379 return fieldId; 380 } 381 382 public void insertIntoDB( TKFormDBData db ) 383 { 384 clearId(); 385 realInsertIntoDB( db, db.form_id ); 386 } 387 388 public TKContentNodeTableData insertNewContentNode( TKContentDBData db, int contentId, int leftNr ) 389 { 390 int newContentNodeId = db.content_node.size(); 391 TKContentNodeTableData node = new TKContentNodeTableData( 392 contentId, 393 newContentNodeId, 394 leftNr, 395 leftNr+1, 396 getName() 397 ); 398 399 db.content_node.addElement( node ); 400 return node; 401 } 402 403 public void insertNewContentValue( TKContentDBData db, int contentId, int contentNodeId, int idx, String value ) 404 { 405 db.content_value.addElement( 406 new TKContentValueTableData( 407 contentId, 408 contentNodeId, 409 idx, 410 value 411 ) 412 ); 413 } 414 415 418 public void insertNewContentValue( TKContentDBData db, int contentId, int contentNodeId, int idx, String value, Integer mediaID ) 419 { 420 db.content_value.addElement( 421 new TKContentValueTableData( 422 contentId, 423 contentNodeId, 424 idx, 425 value, 426 mediaID 427 ) 428 ); 429 } 430 431 public abstract int insertDataIntoDB( TKContentDBData db, Object data, int contentId, int leftNr ); 432 433 public final void insertDataIntoDB( TKContentDBData db, Object data) 434 { 435 insertDataIntoDB( db, data, db.content_id, 0 ); 436 } 437 438 public final static TKContentNodeTableData peekNextContentNode( TKContentDBData db ) 439 { 440 if( db.content_node.size() == 0 ) return null; 441 442 return (TKContentNodeTableData) db.content_node.elementAt(0); 443 } 444 445 public final static void removeNextContentNode( TKContentDBData db ) 446 { 447 TKContentNodeTableData node = (TKContentNodeTableData) db.content_node.elementAt(0); 448 db.content_node.removeElementAt(0); 449 boolean done; 450 do { 451 done = db.content_value.size() == 0; 452 453 if (! done) 454 { 455 TKContentValueTableData value = (TKContentValueTableData) db.content_value.elementAt(0); 456 done = value == null || value.content_node_id != node.content_node_id; 457 458 if (! done) 459 { 460 db.content_value.removeElementAt(0); 461 } 462 } 463 } while( !done ); 464 } 465 466 public TKContentNodeTableData getContentNodeFromDB( TKContentDBData db ) 467 { 468 if( db.content_node.size() == 0 ) return null; 469 470 TKContentNodeTableData node = (TKContentNodeTableData) db.content_node.elementAt(0); 471 if( !node.name.equals( getName() ) ){ 472 throw new TKFieldDataDBError("CONTENT_NODE(got node "+node.name+")", getName(), node.content_node_id ); 473 } 474 db.content_node.removeElementAt(0); 475 return node; 476 } 477 478 public TKContentValueTableData getContentNodeValueFromDB( TKContentDBData db, TKContentNodeTableData node ) 479 { 480 TKContentValueTableData value = (TKContentValueTableData) db.content_value.elementAt(0); 481 if( (value.content_id == node.content_id) 482 && (value.content_node_id == node.content_node_id) 483 ) { 484 db.content_value.removeElementAt(0); 485 } 486 else { 487 throw new TKFieldDataDBError("CONTENT_VALUE", getName(), value.content_node_id ); 488 } 489 return value; 490 } 491 492 public abstract Object getDataFromDB( TKContentDBData db ); 493 494 public static final TKHashtable getFieldHashFromList( TKVector fieldList ) 495 { 496 int fieldCount = fieldList.size(); 497 if( fieldCount > 0 ) { 498 TKHashtable fields = new TKHashtable( fieldCount ); 499 for( int i=0; i<fieldCount; i++ ) { 500 TKBaseField field = (TKBaseField) fieldList.get(i); 501 String fieldName = field.getName(); 502 fields.put( fieldName, field ); 503 } 504 return fields; 505 } 506 else { 507 return new TKHashtable(); 508 } 509 } 510 511 public static final TKVector getListOfFields( TKFieldSwitchListData switchListData ) 512 throws 513 TKUnregisteredClassException, 514 ClassNotFoundException , 515 InstantiationException , 516 IllegalAccessException  517 { 518 TKVector listElements = new TKVector( switchListData.data.size() ); 519 Enumeration e = switchListData.data.elements(); 520 while( e.hasMoreElements() ) { 521 TKFieldSwitchData fieldDef = (TKFieldSwitchData) e.nextElement(); 522 if( fieldDef.alternative.length() > 0 ) { 523 listElements.addElement( 524 TKFieldRegistry.registry.get( fieldDef.alternative, fieldDef.data ) 525 ); 526 } 527 } 528 return listElements; 529 } 530 531 public static final TKFieldSwitchData getDataOfAlternative( TKBaseField field ) 532 { 533 return new TKFieldSwitchData( field.getType(), field.getType(), field.toData() ); 534 } 535 536 public static final TKFieldSwitchListData getDataOfAlternatives( TKVector fieldList ) 537 { 538 TKVector listElements = new TKVector( fieldList.size() ); 539 Enumeration e = fieldList.elements(); 540 while( e.hasMoreElements() ) { 541 TKBaseField field = (TKBaseField) e.nextElement(); 542 listElements.addElement( getDataOfAlternative( field ) ); 543 } 544 return new TKFieldSwitchListData( "", listElements ); 545 } 546 547 556 public boolean equals (Object object) 557 { 558 if (object == null) 559 { 560 return false; 561 } 562 563 if (object == this) 564 { 565 return true; 566 } 567 568 if (! this.getClass().equals(object.getClass())) 569 { 570 return false; 571 } 572 573 TKBaseField field = (TKBaseField) object; 574 575 return (this.fieldType == null ? field.fieldType == null : this.fieldType.equals(field.fieldType)) && 576 (this.fieldName == null ? field.fieldName == null : this.fieldName.equals(field.fieldName)) && 577 (this.showName == null ? field.showName == null : this.showName.equals(field.showName)); 578 } 579 580 585 public int hashCode () 586 { 587 return super.hashCode(); 589 } 590 591 } 592 | Popular Tags |