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 org.w3c.dom.*; 8 import com.teamkonzept.international.LanguageManager; 9 10 16 public class TKFieldList 17 extends TKBaseField 18 { 19 20 23 public static final String CLASS_ID = "FIELDLIST"; 24 public static final String LIST_ENTRY_KEY = "LISTENTRY"; 25 public static final String ENTRY_DESCRIPTION_KEY = "ENTRY_DESCRIPTION"; 26 27 TKBaseField listEntry; 28 29 32 public TKFieldList () 33 { 34 } 36 37 43 public TKFieldList (String name, 44 TKBaseField listEntry) 45 { 46 this(name, listEntry, null); 47 } 48 49 56 public TKFieldList (String name, 57 TKBaseField listEntry, 58 String showName) 59 { 60 initFieldList(name, listEntry, showName); 61 } 62 63 public final void initFieldList(String name, TKBaseField listEntry, String showName) 64 { 65 initBaseField(CLASS_ID, name, showName); 66 this.listEntry = listEntry; 67 } 68 69 public void init(String fieldClass, Object data) 70 throws 71 TKUnregisteredClassException, 72 ClassNotFoundException , 73 InstantiationException , 74 IllegalAccessException  75 { 76 super.init(fieldClass, data); 77 78 TKHashtable listData = (TKHashtable) data; 79 TKFieldSwitchData subType = (TKFieldSwitchData) listData.get(SUB_TYPE_KEY); 80 81 if(subType.alternative.length() == 0) 82 throw new InstantiationException ("no listtype for "+fieldType+" "+fieldName); 83 84 this.listEntry = (TKBaseField) 85 TKFieldRegistry.registry.get(subType.alternative, subType.data); 86 } 87 88 89 93 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 94 TKBaseField [] listArray = { 95 new TKInputField(TKFieldList.NAME_KEY, TKInputField.SMALL_DEFAULT_SIZE, TKInputField.SMALL_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDLIST_NAME"), TKInputField.CHECK_STRING), 96 new TKInputField(TKFieldList.SHOW_NAME_KEY, TKInputField.LARGE_DEFAULT_SIZE, TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDLIST_SHOWNAME"), TKInputField.CHECK_STRING), 97 allSwitch 98 }; 99 TKFieldGroup listGroup = 100 new TKFieldGroup(TKFieldList.CLASS_ID, new TKVector(listArray), LanguageManager.getText(LANGUAGE_CONTEXT, TKFieldList.CLASS_ID)); 101 102 return listGroup; 103 } 104 105 106 public Object toData() 107 { 108 TKHashtable result = (TKHashtable) super.toData(); 109 result.put(SUB_TYPE_KEY, getDataOfAlternative(listEntry)); 110 return result; 111 } 112 113 public Object compileData(String prefix, TKHashtable data, TKHashtable context) 114 { 115 prefix += fieldName+"."; 116 117 int listEntries = Integer.parseInt((String )data.get(prefix+"SIZE")); 118 TKVector result = new TKVector(listEntries); 119 for(int i=0; i< listEntries; i++) { 120 result.addElement( 121 listEntry.compileData(prefix+i+".", data, context) 122 ); 123 } 124 return result; 125 } 126 127 public Object compileData(String prefix, TKMarkupNode data, TKHashtable context) 128 { 129 130 TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup; 131 if (markup == null) { return null;} 132 133 if (!markup.name.equals (getName())) { 134 return null; 135 } 136 137 TKXmlTree tree = (TKXmlTree) data.tree; 138 if (tree == null) { return null;} 139 140 TKVector result = new TKVector(); 141 prefix += fieldName+"."; 142 143 int currentIndex = 0; 144 for (int i = 0; i < tree.size(); i++) { 145 146 Object obj = tree.getSub(i); 147 if (obj == null) continue; 148 149 TKMarkupNode subNode = null; 150 TKXmlMarkup subMarkup = null; 151 152 if (obj instanceof TKXmlMarkup) { 154 subMarkup = (TKXmlMarkup) obj; 155 subNode = new TKMarkupNode (subMarkup,null); 156 } else { 157 subNode = (TKMarkupNode) obj; 158 subMarkup = (TKXmlMarkup) subNode.markup; 159 } 160 result.addElement(listEntry.compileData(prefix+i+".", subNode, context)); 161 currentIndex++; 162 } 163 164 return result; 165 } 166 167 public void fillIntoTemplate(TKHTMLTemplate t, Object data, String prefix) 168 { 169 TKVector dataVector = (TKVector) data; 170 super.fillIntoTemplate(t, data, prefix); 171 t.set("SIZE", String.valueOf(dataVector.size())); 172 t.setListIterator(new TKFieldListIterator( 173 dataVector, 174 listEntry, 175 prefix+fieldName+".", 176 t.getListIterator(), 177 "FIELDLIST" 178 )); 179 180 t.set(ENTRY_DESCRIPTION_KEY, this.listEntry.getShowName()); 181 } 182 183 184 public void fillIntoDOM(Document doc, Element node, Object data) throws DOMException 185 { 186 Element me = doc.createElement(getInternationalName()); 187 node.appendChild(me); 188 fillAttributesIntoNode(me, data); 189 TKVector dataVector = (TKVector) data; 190 for (int i =0; i < dataVector.size(); i++) 191 { 192 Object fieldData = dataVector.get(i); 193 listEntry.fillIntoDOM(doc, me, fieldData); 194 } 196 } 197 198 public void fillIntoPresentation(TKHTMLTemplate t, Object data, String scope) 199 { 200 TKVector dataVector = (TKVector) data; 201 t.set(scope+"."+getName(), String.valueOf(dataVector.size())); 202 t.setListIterator(new TKListShowIterator( 203 dataVector, 204 listEntry, 205 scope, 206 getName(), 207 t.getListIterator() 208 )); 209 210 } 211 212 public Object getDefault() 213 { 214 return new TKVector(); 215 } 216 217 public Object modify(String action, String fieldPath, Object data, String prefix, StringBuffer destination) 218 { 219 TKVector dataVector = (TKVector) data; 220 int pLength = prefix.length()+fieldName.length(); 221 int fpLength = fieldPath.length(); 222 if (fpLength == pLength) { 223 if(action.equals("ADD")) { 225 dataVector.addElement(listEntry.getDefault()); 226 destination.append(prefix+fieldName+"."+(dataVector.size()-1)+"."); 227 } 228 } 229 else if(fieldPath.lastIndexOf('.', fpLength-2) == pLength) { 230 String idxStr = fieldPath.substring(pLength+1, fpLength-1); 232 int idx = Integer.parseInt(idxStr); 233 if(action.equals("DELETE")) { 234 dataVector.removeElementAt(idx); 235 destination.append(prefix+fieldName); 236 if(dataVector.size() > 0) 237 destination.append("."+(idx>=dataVector.size()?dataVector.size()-1:idx)+"."); 238 } 239 if(action.equals("INSERT")) { 240 dataVector.insertElementAt(listEntry.getDefault(), idx); 241 destination.append(prefix+fieldName+"."+idxStr+"."); 242 } 243 else if(action.startsWith("MOVE:")){ 244 int newPos = idx; 245 int total = dataVector.size()-1; 246 switch(action.charAt(5)) { 247 case '0' : 248 newPos = 0; break; 249 case '+' : 250 newPos = (idx==total?total:idx+1); break; 251 case '-' : 252 newPos = (idx==0?0:idx-1); break; 253 case '$' : 254 newPos = total; break; 255 default: 256 break; 257 } 258 if(newPos != idx) { 259 Object tmp = dataVector.get(idx); 260 dataVector.removeElementAt(idx); 261 if(newPos == total) { 262 dataVector.addElement(tmp); 263 } 264 else { 265 dataVector.insertElementAt(tmp, newPos); 266 } 267 } 268 destination.append(prefix+fieldName+"."+newPos+"."); 269 } 270 } 271 else { 272 int idxEnd = fieldPath.indexOf('.',pLength+1); 274 String idxStr = fieldPath.substring(pLength+1, idxEnd); 275 int idx = Integer.parseInt(idxStr); 276 dataVector.put(idx, listEntry.modify( 277 action, fieldPath, dataVector.get(idx), 278 prefix+fieldName+'.'+idx+'.', destination 279 )); 280 } 281 return data; 282 } 283 284 public TKBaseField getTarget(String fieldPath, String prefix) 285 { 286 287 TKBaseField targetField = null; 288 int pLength = prefix.length()+fieldName.length(); 289 int fpLength = fieldPath.length(); 290 291 if ((fpLength != pLength) && (fieldPath.lastIndexOf('.', fpLength-2) != pLength)) { 293 int idxEnd = fieldPath.indexOf('.',pLength+1); 294 String idxStr = fieldPath.substring(pLength+1, idxEnd); 295 int idx = Integer.parseInt(idxStr); 296 297 targetField = listEntry.getTarget(fieldPath, prefix+fieldName+'.'+idx+'.'); 298 } 299 300 return targetField; 301 } 302 303 304 public int insertDataIntoDB(TKContentDBData db, Object data, int contentId, int leftNr) 305 { 306 TKVector dataVector = (TKVector) data; 307 int listSize = dataVector.size(); 308 309 TKContentNodeTableData node = insertNewContentNode(db, contentId, leftNr); 310 int newNodeId = node.content_node_id; 311 312 insertNewContentValue(db, contentId, newNodeId, 0, String.valueOf(listSize)); 313 314 leftNr++; 315 for(int i=0; i<listSize; i++) { 316 leftNr = listEntry.insertDataIntoDB(db, dataVector.get(i), contentId, leftNr)+1; 317 } 318 node.right_nr = leftNr; 319 return leftNr; 320 } 321 322 public Object getDataFromDB(TKContentDBData db) 323 { 324 TKContentNodeTableData node = getContentNodeFromDB(db); 325 TKContentValueTableData value = getContentNodeValueFromDB(db, node); 326 int size = Integer.parseInt(value.value); 327 328 if(size == 0) return new TKVector(); 329 330 TKVector result = new TKVector(size); 331 for(int i=0; i<size; i++) { 332 result.addElement(listEntry.getDataFromDB(db)); 333 } 334 return result; 335 } 336 337 public void clearId() 338 { 339 if(fieldId == -1) return; 340 341 fieldId = -1; 342 listEntry.clearId(); 343 } 344 345 public int realInsertIntoDB(TKFormDBData db, int formId) 346 { 347 if(super.realInsertIntoDB(db, formId) == -1) return -1; 348 349 TKSubFieldTableData subFieldDB = insertNewSubField(db, formId, LIST_ENTRY_KEY, 0); 350 listEntry.realInsertIntoDB(db, formId); 351 subFieldDB.sub_field_id = listEntry.fieldId; 352 353 return fieldId; 354 } 355 356 public void initFromDB(String classId, TKFormDBData db, TKVector otherFields) 357 throws 358 TKUnregisteredClassException, 359 ClassNotFoundException , 360 InstantiationException , 361 IllegalAccessException  362 { 363 super.initFromDB(classId, db, otherFields); 364 listEntry = getSubField(db, LIST_ENTRY_KEY, 0, otherFields); 365 } 366 367 376 public boolean equals (Object object) 377 { 378 if (! super.equals(object)) 379 { 380 return false; 381 } 382 383 TKFieldList field = (TKFieldList) object; 384 385 return (this.listEntry == null ? field.listEntry == null : this.listEntry.equals(field.listEntry)); 386 } 387 388 393 public int hashCode () 394 { 395 return super.hashCode(); 397 } 398 399 } 400 | Popular Tags |