1 package com.teamkonzept.field; 2 3 import java.util.*; 4 5 import com.teamkonzept.lib.*; 6 import com.teamkonzept.publishing.markups.*; 7 import com.teamkonzept.web.*; 8 import com.teamkonzept.field.db.*; 9 import org.w3c.dom.*; 10 import com.teamkonzept.international.LanguageManager; 11 12 18 public class TKFieldSwitch 19 extends TKBaseField 20 { 21 23 26 public static final String CLASS_ID = "FIELDSWITCH"; 27 public static final String ALTERNATIVES_KEY = "ALTERNATIVES"; 28 public static final String SELECT_CHECK_KEY = "SELECT_CHECK"; 29 30 public static final int CHECK_UNDEFINED = -1; 31 public static final int CHECK_OPTION = 0; 32 33 public static final String [] CHECK_NAMES = 34 { 35 "option" 36 }; 37 38 TKVector alternatives; 39 TKHashtable fieldHash; 40 41 protected int check = CHECK_UNDEFINED; 43 44 public TKFieldSwitch() {}; 45 46 public TKFieldSwitch( String name, TKVector alternatives ) 47 { 48 this( name, alternatives, null ); 49 } 50 51 public TKFieldSwitch( String name, TKVector alternatives, String showName ) 52 { 53 this(name, alternatives, showName, CHECK_UNDEFINED); 54 } 55 56 public TKFieldSwitch (String name, 57 TKVector alternatives, 58 String showName, 59 int check) 60 { 61 initFieldSwitch(CLASS_ID, name, alternatives, showName, check); 62 } 63 64 protected final void initFieldSwitch (String type, 65 String name, 66 TKVector alternatives, 67 String showName, 68 int check) 69 { 70 initBaseField(type, name, showName); 71 72 this.alternatives = alternatives; 73 this.fieldHash = getFieldHashFromList(alternatives); 74 this.check = check; 75 } 76 77 public void init (String fieldType, Object data) 78 throws 79 TKUnregisteredClassException, 80 ClassNotFoundException , 81 InstantiationException , 82 IllegalAccessException  83 { 84 super.init(fieldType, data); 85 86 TKHashtable switchData = (TKHashtable) data; 87 88 this.alternatives = getListOfFields((TKFieldSwitchListData) switchData.get(TKFieldGroup.SUB_LIST_KEY)); 89 this.fieldHash = getFieldHashFromList(alternatives); 90 91 String value = (String ) switchData.get(SELECT_CHECK_KEY); 92 this.check = value != null && value.length() > 0 93 ? Integer.parseInt(value) 94 : CHECK_UNDEFINED; 95 } 96 97 100 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 101 102 TKBaseField [] switchArray = { 103 new TKInputField( TKFieldSwitch.NAME_KEY, TKInputField.SMALL_DEFAULT_SIZE, TKInputField.SMALL_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDSWITCH_NAME"), TKInputField.CHECK_STRING), 104 new TKInputField( TKFieldSwitch.SHOW_NAME_KEY, TKInputField.LARGE_DEFAULT_SIZE, TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDSWITCH_SHOWNAME"), TKInputField.CHECK_STRING), 105 allSwitchList 106 }; 107 TKFieldGroup switchGroup = 108 new TKFieldGroup( TKFieldSwitch.CLASS_ID, new TKVector( switchArray ), LanguageManager.getText(LANGUAGE_CONTEXT, TKFieldSwitch.CLASS_ID) ); 109 110 return switchGroup; 111 } 112 113 public Object toData () 114 { 115 TKHashtable result = (TKHashtable) super.toData(); 116 117 result.put(SUB_LIST_KEY, getDataOfAlternatives(alternatives)); 118 119 if (this.check > CHECK_UNDEFINED) 120 { 121 result.put(SELECT_CHECK_KEY, String.valueOf(this.check)); 122 } 123 124 return result; 125 } 126 127 public void addAlternative (TKBaseField alternative) 128 { 129 alternatives.addElement(alternative); 130 fieldHash.put(alternative.getName(), alternative); 131 } 132 133 public void removeAlternative (TKBaseField alternative) 134 { 135 alternatives.removeElement(alternative); 136 fieldHash.remove(alternative.getName()); 137 } 138 139 public Object compileData( String prefix, TKHashtable data, TKHashtable context ) 140 { 141 prefix += fieldName+"."; 142 String currAlternative = (String ) data.get( prefix+"ALTERNATIVE" ); 143 String newAlternative = (String ) data.get( prefix+"NEWALTERNATIVE" ); 144 if( newAlternative == null ) newAlternative = ""; 145 146 if( (currAlternative != null) && (currAlternative.toString().length() > 0) ) { 147 TKBaseField field = (TKBaseField) fieldHash.get( currAlternative ); 148 return new TKFieldSwitchData( 149 currAlternative, 150 newAlternative, 151 field.compileData( prefix+currAlternative+".", data, context ) 152 ); 153 } 154 return new TKFieldSwitchData( "", newAlternative, null ); 155 } 156 157 public Object compileData( String prefix, TKMarkupNode data, TKHashtable context ) 158 { 159 TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup; 160 if (markup == null) { return null;} 161 162 if (!markup.name.equals (getName())) { 163 return null; 164 } 165 166 TKXmlTree tree = (TKXmlTree) data.tree; 167 if (tree == null) { return null;} 168 169 Object obj = tree.getSingleSub(); 170 if (obj == null) { return null;} 171 172 TKMarkupNode subNode = null; 173 TKXmlMarkup subMarkup = null; 174 175 if (obj instanceof TKXmlMarkup) { 177 subMarkup = (TKXmlMarkup) obj; 178 subNode = new TKMarkupNode (subMarkup,null); 179 } else { 180 subNode = (TKMarkupNode) obj; 181 subMarkup = (TKXmlMarkup) subNode.markup; 182 } 183 184 if (subMarkup == null) { return null; } 185 186 TKBaseField field = (TKBaseField) fieldHash.get( subMarkup.name ); 187 if (field == null) { 188 return null; 189 } 190 191 return new TKFieldSwitchData(subMarkup.name,"", 192 field.compileData( prefix+subMarkup.name+".", subNode, context )); 193 } 194 195 public Object getDefault() 196 { 197 return new TKFieldSwitchData( "", "", null ); 198 } 199 200 public Object modify( String action, String fieldPath, Object data, String prefix, StringBuffer destination ) 201 { 202 TKFieldSwitchData switchData = (TKFieldSwitchData) data; 203 int pLength = prefix.length()+fieldName.length(); 204 if ( fieldPath.length() == pLength ) { 205 destination.append( prefix+fieldName ); 206 if( action.equals( "SWITCH" ) ) { 208 if( ! switchData.alternative.equals( switchData.newAlternative ) ) { 209 switchData.alternative = switchData.newAlternative; 210 if( switchData.newAlternative.equals("") ) { 211 switchData.data = null; 212 } 213 else { 214 TKBaseField field = (TKBaseField) fieldHash.get( switchData.newAlternative ); 215 switchData.data = field.getDefault(); 216 } 217 } 218 } 219 } 220 else { 221 int subNameEnd = fieldPath.indexOf('.',pLength+1); 223 String subName = ( subNameEnd < 0 224 ? fieldPath.substring( pLength+1 ) 225 : fieldPath.substring( pLength+1, subNameEnd ) 226 ); 227 if( subName.equals( switchData.alternative ) ) { 229 TKBaseField field = (TKBaseField) fieldHash.get( subName ); 230 switchData.data = field.modify( 231 action, fieldPath, switchData.data, 232 prefix+fieldName+'.'+subName+'.', 233 destination 234 ); 235 } 236 } 237 return data; 238 } 239 240 public TKBaseField getTarget(String fieldPath, String prefix) 241 { 242 243 TKBaseField targetField = null; 244 int pLength = prefix.length()+fieldName.length(); 245 246 if ( fieldPath.length() != pLength ) { 248 int subNameEnd = fieldPath.indexOf('.',pLength+1); 249 String subName = ( subNameEnd < 0 250 ? fieldPath.substring( pLength+1 ) 251 : fieldPath.substring( pLength+1, subNameEnd ) 252 ); 253 TKBaseField field = (TKBaseField) fieldHash.get( subName ); 254 targetField = field.getTarget(fieldPath, prefix+fieldName+'.'+subName+'.'); 255 } 256 return targetField; 257 } 258 259 public TKVector sortAllSwitch() 260 { 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.elementAt(y); 275 if(showName.equals(field.getShowName()) ) 276 sortedFieldVector.addElement(field); 277 } 278 } 279 return sortedFieldVector; 280 } 281 282 public void fillIntoTemplate( TKHTMLTemplate t, Object data, String prefix ) 283 { 284 TKFieldSwitchData switchData = (TKFieldSwitchData) data; 285 TKVector sortedAlternatives = sortAllSwitch(); 286 287 super.fillIntoTemplate(t, data, prefix); 288 t.setListIterator(new TKFieldSwitchIterator(sortedAlternatives, 289 fieldName, 290 new TKFieldSwitchFakeIterator(switchData, 291 fieldHash, 292 prefix+fieldName+".", 293 t.getListIterator(), 294 "SWITCH_FAKELIST"), 295 "ALTERNATIVE_LIST")); 296 297 t.set("ALTERNATIVE", switchData.alternative); 298 t.set(prefix+fieldName + ".ALTERNATIVE", switchData.alternative); 299 300 if (this.check > CHECK_UNDEFINED) 301 { 302 t.set(SELECT_CHECK_KEY, CHECK_NAMES[this.check]); 303 } 304 } 305 306 public void fillIntoDOM(Document doc, Element node, Object data) throws DOMException 307 { 308 TKFieldSwitchData switchData = (TKFieldSwitchData) data; 309 Element me = doc.createElement(getInternationalName()); 310 node.appendChild(me); 311 fillAttributesIntoNode(me, data); 312 if( switchData.alternative.length() > 0 ) { 313 TKBaseField field = (TKBaseField) fieldHash.get( switchData.alternative ); 314 field.fillIntoDOM(doc, me, switchData.data); 315 } 316 } 317 318 public void fillIntoPresentation( TKHTMLTemplate t, Object data, String scope ) 319 { 320 TKFieldSwitchData switchData = (TKFieldSwitchData) data; 321 322 t.set( scope+"."+getName(), switchData.alternative ); 323 if( switchData.alternative.length() > 0 ) { 324 TKBaseField field = (TKBaseField) fieldHash.get( switchData.alternative ); 325 field.fillIntoPresentation( t, switchData.data, scope+"."+getName() ); 326 } 327 } 328 329 public int insertDataIntoDB( TKContentDBData db, Object data, int contentId, int leftNr ) 330 { 331 TKFieldSwitchData switchData = (TKFieldSwitchData) data; 332 333 TKContentNodeTableData node = insertNewContentNode( db, contentId, leftNr ); 334 int newNodeId = node.content_node_id; 335 336 insertNewContentValue( db, contentId, newNodeId, 0, switchData.alternative ); 337 338 if( switchData.alternative.length() > 0 ) { 339 TKBaseField field = (TKBaseField) fieldHash.get( switchData.alternative ); 340 node.right_nr = field.insertDataIntoDB( db, switchData.data, contentId, leftNr+1 )+1; 341 } 342 return node.right_nr; 343 } 344 345 public Object getDataFromDB( TKContentDBData db ) 346 { 347 TKContentNodeTableData node = getContentNodeFromDB( db ); 348 TKContentValueTableData value = getContentNodeValueFromDB( db, node ); 349 350 String alternative = value.value; 351 if( alternative.length() == 0 ) return getDefault(); 352 353 peekNextContentNode(db); 355 356 TKBaseField field = (TKBaseField) fieldHash.get( alternative ); 357 if( field == null ) { 358 removeNextContentNode( db ); 359 return getDefault(); 360 } 361 return new TKFieldSwitchData( alternative, alternative, field.getDataFromDB( db ) ); 362 } 363 364 public void clearId() 365 { 366 if( fieldId == -1 ) return; 367 368 fieldId = -1; 369 Enumeration e = alternatives.elements(); 370 while( e.hasMoreElements() ) { 371 ((TKBaseField) e.nextElement()).clearId(); 372 } 373 } 374 375 public int realInsertIntoDB( TKFormDBData db, int formId ) 376 { 377 if( super.realInsertIntoDB( db, formId ) == -1 ) return -1; 378 insertNewSubFieldList( db, formId, ALTERNATIVES_KEY, alternatives ); 379 return fieldId; 380 } 381 382 public void initFromDB( String classId, TKFormDBData db, TKVector otherFields ) 383 throws 384 TKUnregisteredClassException, 385 ClassNotFoundException , 386 InstantiationException , 387 IllegalAccessException  388 { 389 super.initFromDB( classId, db, otherFields ); 390 alternatives = getSubFieldList( db, ALTERNATIVES_KEY, otherFields ); 391 this.fieldHash = getFieldHashFromList( alternatives ); 392 } 393 394 403 public boolean equals (Object object) 404 { 405 if (! super.equals(object)) 406 { 407 return false; 408 } 409 410 TKFieldSwitch field = (TKFieldSwitch) object; 411 412 return (this.alternatives == null ? field.alternatives == null : this.alternatives.equals(field.alternatives)); 413 } 414 415 420 public int hashCode () 421 { 422 return super.hashCode(); 424 } 425 426 } 427
| Popular Tags
|