1 package com.teamkonzept.field; 2 3 import com.teamkonzept.web.*; 4 import com.teamkonzept.publishing.markups.*; 5 import com.teamkonzept.lib.*; 6 import com.teamkonzept.international.LanguageManager; 7 import com.teamkonzept.field.db.*; 8 import org.w3c.dom.*; 9 10 29 public class TKFieldOption 30 extends TKBaseField 31 { 32 34 37 public static final String CLASS_ID = "FIELDOPTION"; 38 public static final String OPTION_ENTRY_KEY = "OPTIONENTRY"; 39 public static final String YES_NAME_KEY = "YES_NAME"; 40 public static final String NO_NAME_KEY = "NO_NAME"; 41 42 public static final String DEFAULT_YES_NAME = "Ja"; 43 44 public static final String DEFAULT_NO_NAME = "Nein"; 45 46 47 TKBaseField optionEntry; 48 49 52 protected String yesName = null; 53 54 57 protected String noName = null; 58 59 62 public TKFieldOption() {}; 63 64 65 71 public TKFieldOption( String name, TKBaseField optionEntry ) 72 { 73 this( name, optionEntry, null, null, null ); 75 } 76 77 78 85 public TKFieldOption( String name, TKBaseField optionEntry, String showName ) 86 { 87 this( name, optionEntry, showName, null, null ); 89 } 90 91 public TKFieldOption( String name, 92 TKBaseField optionEntry, 93 String showName, 94 String yesName, 95 String noName ) 96 { 97 initFieldOption( name, optionEntry, showName, yesName, noName ); 98 } 99 100 107 public final void initFieldOption( String name, 108 TKBaseField optionEntry, 109 String showName, 110 String yesName, 111 String noName ) 112 { 113 initBaseField( CLASS_ID, name, showName ); 114 this.optionEntry = optionEntry; 115 this.yesName = yesName; 116 this.noName = noName; 117 } 118 119 125 public void init( String fieldClass, Object data ) 126 throws 127 TKUnregisteredClassException, 128 ClassNotFoundException , 129 InstantiationException , 130 IllegalAccessException  131 { 132 super.init( fieldClass, data ); 134 135 TKHashtable jaNeinData = (TKHashtable) data; 136 TKFieldSwitchData subType = (TKFieldSwitchData) jaNeinData.get(SUB_TYPE_KEY); 137 138 139 if( subType.alternative.length() == 0 ) 140 throw new InstantiationException ( "no tabletype for "+fieldType+" "+fieldName ); 141 142 this.optionEntry = (TKBaseField) 143 TKFieldRegistry.registry.get( subType.alternative, subType.data ); 144 145 this.yesName = (String ) jaNeinData.get(YES_NAME_KEY); 146 this.noName = (String ) jaNeinData.get(NO_NAME_KEY); 147 } 148 149 159 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 160 TKBaseField [] optionArray = { 161 new TKInputField( TKFieldOption.NAME_KEY, 162 TKInputField.SMALL_DEFAULT_SIZE, 163 TKInputField.SMALL_DEFAULT_LENGTH, 164 LanguageManager.getText(LANGUAGE_CONTEXT, 165 "FIELDOPTION_NAME"), 166 TKInputField.CHECK_STRING), 167 new TKInputField( TKFieldOption.SHOW_NAME_KEY, 168 TKInputField.LARGE_DEFAULT_SIZE, 169 TKInputField.LARGE_DEFAULT_LENGTH, 170 LanguageManager.getText(LANGUAGE_CONTEXT, 171 "FIELDOPTION_SHOWNAME"), 172 TKInputField.CHECK_STRING), 173 new TKInputField( TKFieldOption.YES_NAME_KEY, 174 TKInputField.LARGE_DEFAULT_SIZE, 175 TKInputField.LARGE_DEFAULT_SIZE, 176 LanguageManager.getText(LANGUAGE_CONTEXT, 177 "FIELDOPTION_YES_NAME_KEY") ), 178 new TKInputField( TKFieldOption.NO_NAME_KEY, 179 TKInputField.LARGE_DEFAULT_SIZE, 180 TKInputField.LARGE_DEFAULT_SIZE, 181 LanguageManager.getText(LANGUAGE_CONTEXT, 182 "FIELDOPTION_NO_NAME_KEY") ), 183 allSwitch 184 }; 185 TKFieldGroup optionGroup = 186 new TKFieldGroup( TKFieldOption.CLASS_ID, 187 new TKVector( optionArray ), 188 LanguageManager.getText(LANGUAGE_CONTEXT, 189 TKFieldOption.CLASS_ID) ); 190 191 return optionGroup; 192 } 193 194 199 public Object toData () 200 { 201 TKHashtable result = (TKHashtable) super.toData(); 202 result.put( SUB_TYPE_KEY, getDataOfAlternative( optionEntry ) ); 203 204 if( yesName != null ) 205 { 206 result.put( YES_NAME_KEY, yesName ); 207 } 208 else 209 { 210 result.put( YES_NAME_KEY, DEFAULT_YES_NAME ); 211 } 212 if( noName != null ) 213 { 214 result.put( NO_NAME_KEY, noName ); 215 } 216 else 217 { 218 result.put( NO_NAME_KEY, DEFAULT_YES_NAME ); 219 } 220 return result; 221 } 222 223 235 public Object compileData( String prefix, TKHashtable data, TKHashtable context ) 236 { 237 prefix += fieldName+"."; 238 String currJaNein = (String ) data.get(prefix+"CURRYESNO"); 239 String newJaNein = (String ) data.get(prefix+"NEWYESNO"); 240 if(newJaNein == null) newJaNein = ""; 241 242 if( (currJaNein != null) && (currJaNein.toString().length() > 0) ){ 243 return new TKFieldOptionData( 244 currJaNein, 245 newJaNein, 246 ( currJaNein.equalsIgnoreCase("YES") 247 ? optionEntry.compileData(prefix+currJaNein+".", data, context) 248 : null 249 ) 250 ); 251 } 252 return new TKFieldOptionData("", newJaNein, null); 253 254 } 255 256 public Object compileData( String prefix, TKMarkupNode data, TKHashtable context ) 257 { 258 TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup; 259 if (markup == null) { return null;} 260 261 if (!markup.name.equals (getName())) { 262 return null; 263 } 264 265 TKXmlTree tree = (TKXmlTree) data.tree; 266 if (tree == null) { return null;} 267 268 Object obj = tree.getSingleSub(); 269 270 TKMarkupNode subNode = null; 271 TKXmlMarkup subMarkup = null; 272 273 if (obj != null) 274 { 275 if (obj instanceof TKXmlMarkup) { 277 subMarkup = (TKXmlMarkup) obj; 278 subNode = new TKMarkupNode (subMarkup,null); 279 } else { 280 subNode = (TKMarkupNode) obj; 281 subMarkup = (TKXmlMarkup) subNode.markup; 282 } 283 } 284 String currJaNein = TKMarkupParam.paramValue (markup.params,"VALUE"); 285 286 if( (currJaNein != null) && (currJaNein.toString().length() > 0) ){ 287 return new TKFieldOptionData( 288 currJaNein,"", 289 ( currJaNein.equalsIgnoreCase("YES") && (subNode != null) 290 ? optionEntry.compileData(prefix+currJaNein+".", subNode, context) 291 : null 292 ) 293 ); 294 } 295 296 return new TKFieldOptionData("", "", null); 297 } 298 299 306 public void fillIntoTemplate( TKHTMLTemplate t, Object data, String prefix ) 307 { 308 TKFieldOptionData optionData = (TKFieldOptionData) data; 309 super.fillIntoTemplate( t, data, prefix ); 310 if( (yesName == null) || (yesName.equals("")) ) 311 t.set( YES_NAME_KEY, DEFAULT_YES_NAME ); 312 else 313 t.set( YES_NAME_KEY, yesName ); 314 315 if( (noName == null) || (noName.equals("")) ) 316 t.set( NO_NAME_KEY, DEFAULT_NO_NAME ); 317 else 318 t.set( NO_NAME_KEY, noName ); 319 t.setListIterator( 320 new TKFieldOptionFakeIterator( 321 optionEntry, 322 optionData, 323 prefix+fieldName+".", 324 t.getListIterator(), 325 "SWITCH_FAKELIST" 326 )); 327 t.set("CURRYESNO", optionData.currJaNein); 328 t.set( prefix+fieldName+".CURRYESNO", optionData.currJaNein ); 329 } 330 331 public void fillIntoDOM(Document doc, Element node, Object data) throws DOMException 332 { 333 TKFieldOptionData optionData = (TKFieldOptionData) data; 335 Element me = doc.createElement(getInternationalName()); 336 node.appendChild(me); 337 fillAttributesIntoNode(me, data); 338 if( optionData.data != null ) 339 { 340 if( optionData.currJaNein.equalsIgnoreCase("YES")){ 341 optionEntry.fillIntoDOM( doc, me, optionData.data); 342 } 343 } 344 } 345 346 353 public void fillIntoPresentation( TKHTMLTemplate t, Object data, String scope ) 354 { 355 356 TKFieldOptionData optionData = (TKFieldOptionData) data; 357 358 t.set( scope+"."+getName(), optionData.currJaNein); 359 360 if(optionData.currJaNein.equalsIgnoreCase("YES")) 361 optionEntry.fillIntoPresentation( t, optionData.data, scope+"."+getName() ); 362 363 } 364 365 371 public Object getDefault() 372 { 373 return new TKFieldOptionData("","",null); 374 } 375 376 399 public Object modify( String action, String fieldPath, Object data, String prefix, StringBuffer destination ) 400 { 401 TKFieldOptionData optionData = (TKFieldOptionData) data; 402 403 int pLength = prefix.length()+fieldName.length(); 404 if( fieldPath.length() == pLength) { 406 destination.append( prefix+fieldName ); 407 if( action.equals("SWITCH") ) { 408 if(!optionData.currJaNein.equals(optionData.newJaNein)) { 409 optionData.currJaNein = optionData.newJaNein; 410 if(optionData.newJaNein.equals("")) { 411 optionData.data = null; 412 } 413 else { 414 optionData.data = optionEntry.getDefault(); 415 } 416 } 417 } 418 } 419 else { 421 int subNameEnd = fieldPath.indexOf('.', pLength+1); 422 String subName = ( subNameEnd < 0 423 ? fieldPath.substring( pLength+1 ) 424 : fieldPath.substring( pLength+1, subNameEnd ) 425 ); 426 if( subName.equals( optionData.currJaNein ) ) { 427 optionData.data = optionEntry.modify( 428 action, fieldPath, optionData.data, 429 prefix+fieldName+'.'+subName+'.' ,destination 430 ); 431 } 432 433 } 434 435 return data; 436 } 437 438 439 455 public TKBaseField getTarget(String fieldPath, String prefix) 456 { 457 TKBaseField targetField = null; 458 int pLength = prefix.length()+fieldName.length(); 459 460 if ( (fieldPath.length() != pLength) ) 464 { 465 int subNameEnd = fieldPath.indexOf('.', pLength+1); 466 String subName = ( subNameEnd < 0 467 ? fieldPath.substring( pLength+1 ) 468 : fieldPath.substring( pLength+1, subNameEnd ) 469 ); 470 targetField = optionEntry.getTarget(fieldPath, prefix+fieldName+"."+subName+"."); 471 } 472 return targetField; 473 } 474 475 490 public int insertDataIntoDB( TKContentDBData db, Object data, int contentId, int leftNr ) 491 { 492 TKFieldOptionData optionData = (TKFieldOptionData) data; 493 TKContentNodeTableData node = insertNewContentNode( db, contentId, leftNr ); 500 int newNodeId = node.content_node_id; 501 502 507 insertNewContentValue( db, contentId, newNodeId, 0, optionData.currJaNein ); 511 512 if( optionData.currJaNein.equalsIgnoreCase("YES") ) { 518 node.right_nr = optionEntry.insertDataIntoDB( db, optionData.data, contentId, leftNr+1 )+1; 519 } 520 521 return node.right_nr; 522 523 } 524 525 533 public Object getDataFromDB( TKContentDBData db ) 534 { 535 TKContentNodeTableData node = getContentNodeFromDB( db ); 539 540 TKContentValueTableData value = getContentNodeValueFromDB( db, node ); 544 String currJaNein = value.value; 545 if( !currJaNein.equalsIgnoreCase("YES") ) return getDefault(); 550 551 if( optionEntry == null ) return getDefault(); 557 return new TKFieldOptionData( currJaNein, currJaNein, optionEntry.getDataFromDB( db ) ); 558 559 560 561 } 562 563 570 public void clearId() 571 { 572 if( fieldId == -1 ) return; 573 574 fieldId = -1; 575 optionEntry.clearId(); 576 577 } 578 579 589 public int realInsertIntoDB( TKFormDBData db, int formId ) 590 { 591 if( super.realInsertIntoDB( db, formId ) == -1 ) return -1; 592 593 if(yesName != null && !yesName.equals("") ) 595 insertNewFieldAttribute( db, formId, YES_NAME_KEY, 0, yesName ); 596 else 597 insertNewFieldAttribute( db, formId, YES_NAME_KEY, 0, DEFAULT_YES_NAME ); 598 599 600 if(noName != null && !noName.equals("") ) 601 insertNewFieldAttribute( db, formId, NO_NAME_KEY, 0, noName ); 602 else 603 insertNewFieldAttribute( db, formId, NO_NAME_KEY, 0, DEFAULT_NO_NAME ); 604 605 TKSubFieldTableData subFieldDB = insertNewSubField( db, formId, OPTION_ENTRY_KEY, 0 ); 607 optionEntry.realInsertIntoDB( db, formId ); 608 subFieldDB.sub_field_id = optionEntry.fieldId; 609 610 611 612 return fieldId; 613 } 614 615 623 public void initFromDB( String classId, TKFormDBData db, TKVector otherFields ) 624 throws 625 TKUnregisteredClassException, 626 ClassNotFoundException , 627 InstantiationException , 628 IllegalAccessException  629 { 630 super.initFromDB( classId, db, otherFields ); 631 632 if(hasFieldAttribute( db, YES_NAME_KEY, 0 )) 634 this.yesName = getFieldAttribute( db, YES_NAME_KEY, 0 ); 635 636 if( hasFieldAttribute( db, NO_NAME_KEY, 0 )) 637 this.noName= getFieldAttribute( db, NO_NAME_KEY, 0 ); 638 639 optionEntry = getSubField( db, OPTION_ENTRY_KEY,0, otherFields ); 641 } 642 643 652 public boolean equals (Object object) 653 { 654 if (! super.equals(object)) 655 { 656 return false; 657 } 658 659 TKFieldOption field = (TKFieldOption) object; 660 661 return (this.yesName == null ? field.yesName == null : this.yesName.equals(field.yesName)) && 662 (this.noName == null ? field.noName == null : this.noName.equals(field.noName)) && 663 (this.optionEntry == null ? field.optionEntry == null : this.optionEntry.equals(field.optionEntry)); 664 } 665 666 671 public int hashCode () 672 { 673 return super.hashCode(); 675 } 676 677 } 678
| Popular Tags
|