1 package com.teamkonzept.webman.field; 2 3 import com.teamkonzept.webman.mainint.*; 4 import com.teamkonzept.webman.mainint.db.queries.content.SelectGroupContentChilds; 5 8 import com.teamkonzept.lib.*; 9 import com.teamkonzept.publishing.markups.*; 10 import com.teamkonzept.web.*; 11 import com.teamkonzept.field.db.queries.*; 12 import com.teamkonzept.field.db.*; 13 import com.teamkonzept.db.*; 14 import com.teamkonzept.field.*; 15 import de.webman.generator.*; 16 import de.webman.content.workflow.*; 17 import java.sql.*; 18 import java.util.*; 19 import com.teamkonzept.international.LanguageManager; 20 import org.w3c.dom.*; 21 import org.apache.log4j.Category; 22 import de.webman.content.Content; 23 import de.webman.content.workflow.ContentVersion; 24 import de.webman.util.log4j.WebmanCategory; 25 26 32 public class TKContentReferenceField 33 extends TKAtomField 34 { 35 36 39 private static WebmanCategory cat = (WebmanCategory)WebmanCategory.getInstance(TKContentReferenceField.class); 40 41 44 public static final String CLASS_ID = "CONTENTREF"; 45 46 49 public static final String NODE_KEY = "NODE"; 50 51 54 public static final String MULTIPLE_KEY = "MULTIPLE"; 55 56 57 public static final String NAME_ONLY_KEY = "NAME_ONLY"; 58 59 public static final String YES_VALUE = "YES"; 60 public static final String NO_VALUE = "NO"; 61 62 63 protected int contentNodeId; 64 protected boolean multipleSelection; 65 protected boolean deliverOnlyNames; 66 67 68 71 public TKContentReferenceField () 72 { 73 super(); 74 } 75 76 public TKContentReferenceField( String name, int contentNodeId ) 77 { 78 this( name, contentNodeId, null ); 79 } 80 81 public TKContentReferenceField( String name, int contentNodeId, String showName ) 82 { 83 this( name, contentNodeId, true, showName ); 84 } 85 86 public TKContentReferenceField( String name, int contentNodeId, boolean deliverOnlyNames, String showName ) 87 { 88 this( name, contentNodeId, deliverOnlyNames, false, showName ); 89 } 90 91 public TKContentReferenceField( 92 String name, 93 int contentNodeId, 94 boolean deliverOnlyNames, 95 boolean multipeSelection, 96 String showName 97 ) 98 { 99 initContentReferenceField( CLASS_ID, name, contentNodeId, deliverOnlyNames, multipeSelection, showName ); 100 } 101 102 111 public final void initContentReferenceField( 112 String type, 113 String name, 114 int contentNodeId, 115 boolean deliverOnlyNames, 116 boolean multipleSelection, 117 String showName 118 ) 119 { 120 initAtomField( type, name, showName ); 121 this.contentNodeId = contentNodeId; 122 this.deliverOnlyNames = deliverOnlyNames; 123 this.multipleSelection = multipleSelection; 124 } 125 126 129 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 130 131 TKVector multipleOptions = new TKVector(2); 132 multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "YES"), YES_VALUE ) ); 133 multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "NO"), NO_VALUE ) ); 134 135 TKBaseField [] inputArray = { 136 new TKInputField( NAME_KEY, TKContentSiteReferenceField.WIDTH_ONE, TKContentSiteReferenceField.WIDTH_TWO, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NAME"), TKInputField.CHECK_STRING), 137 new TKInputField( SHOW_NAME_KEY, TKContentSiteReferenceField.WIDTH_THREE, TKContentSiteReferenceField.WIDTH_FOUR, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_SHOWNAME"), TKInputField.CHECK_STRING), 138 new TKContentNodeTypeSelectorField( NODE_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NODE"), TKContentNodeTypeSelectorField.GROUP_NODE_TYPE ), 140 new TKCheckField( MULTIPLE_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_MULTIPLE"), multipleOptions, false ), 141 new TKCheckField( NAME_ONLY_KEY, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_REF_NAME_ONLY"), multipleOptions, false ) 142 }; 143 TKFieldGroup inputGroup = 144 new TKFieldGroup( TKContentReferenceField.CLASS_ID, new TKVector( inputArray ), LanguageManager.getText(LANGUAGE_CONTEXT, TKContentReferenceField.CLASS_ID) ); 145 146 return inputGroup; 147 } 148 149 152 public void init( String classId, Object initData ) 153 throws 154 TKUnregisteredClassException, 155 ClassNotFoundException , 156 InstantiationException , 157 IllegalAccessException 158 { 159 super.init( classId, initData ); 160 TKHashtable data = (TKHashtable) initData; 161 contentNodeId = Integer.parseInt( (String ) data.get(NODE_KEY) ); 162 multipleSelection = ((String )data.get(MULTIPLE_KEY)).equals( YES_VALUE ); 163 deliverOnlyNames = ((String )data.get(NAME_ONLY_KEY)).equals( YES_VALUE ); 164 } 165 166 171 public Object toData () 172 { 173 TKHashtable result = (TKHashtable) super.toData(); 174 result.put( NODE_KEY, String.valueOf( contentNodeId ) ); 175 result.put( MULTIPLE_KEY, ( multipleSelection ? YES_VALUE : NO_VALUE ) ); 176 result.put( NAME_ONLY_KEY, ( deliverOnlyNames ? YES_VALUE : NO_VALUE ) ); 177 return result; 178 } 179 180 187 public void fillIntoTemplate (TKHTMLTemplate template, 188 Object value, 189 String prefix) 190 { 191 try 192 { 193 super.fillIntoTemplate(template, value, prefix); 194 template.set(NODE_KEY, String.valueOf(this.contentNodeId)); 195 196 if (this.multipleSelection) 197 { 198 template.set(MULTIPLE_KEY, Boolean.TRUE); 199 } 200 201 VersionStatics statics = VersionStatics.setup(); 203 TKHashtable statusPool = VersionStatus.selectSingles(statics, false); 204 TKHashtable filter = VersionSelection.initContentFilter(statusPool); 205 TKVector filteredInstances = new TKVector(); 206 207 TKVector allInstances = VersionCache.getVersionInfo(new Integer (this.contentNodeId), false); 209 210 for (int i = 0; i < allInstances.size(); i++) 211 { 212 Content content = (Content) allInstances.elementAt(i); 214 215 if (content.filterTransitions(filter, "SRC")) 216 { 217 TKHashtable entry = new TKHashtable(); 218 entry.put("CONTENT_NODE_SHORTNAME", content.getShortName()); 219 entry.put("CONTENT_NODE_NAME", content.getName()); 220 221 filteredInstances.addElement(entry); 222 } 223 } 224 225 template.setListIterator(new TKStandardPluginIterator(prefix + getName(), 227 null, 228 filteredInstances, 229 false, 230 template.getListIterator())); 231 } 232 catch (Exception e) 233 { 234 cat.error(e); 235 } 236 } 237 238 243 public TKVector getSelectedContents( Object data, GeneratorContext context ) 244 { 245 if (data == null) 246 return new TKVector(); 247 TKVector searchNames; 248 if( data instanceof TKVector ) { 249 searchNames = (TKVector) data; 250 } 251 else { 252 searchNames = new TKVector(1); 253 searchNames.addElement( data ); 254 } 255 256 cat.debug("Got selected contents : " + searchNames); 257 TKVector contentNodes = new TKVector(); 258 259 SiteContentNode node = context.contentNodes.getContentNode(contentNodeId); 260 if (node == null) 261 { 262 cat.error("CONTENT_REFERENCE " + getName() + " points to invalid group node: " + contentNodeId); 263 return contentNodes; 264 } 265 TKVector contents = node.getContents(); 266 for ( int i= 0; i < contents.size(); i++) 267 { 268 SiteContent siteContent = (SiteContent)contents.elementAt(i); 269 String shortName = siteContent.getShortName(); 270 int idx = -1; 271 if (shortName != null) 272 idx = searchNames.indexOf(shortName); 273 274 if (idx != -1) 275 { 276 while( contentNodes.size() <= idx ) { 277 contentNodes.addElement( null ); 278 } 279 if (siteContent != null && siteContent.isGeneratableVersion()) { 280 cat.debug(" Added " + shortName + " at " + idx); 281 contentNodes.setElementAt( siteContent, idx ); 282 } 283 else if (siteContent == null) { 284 cat.error("Content: " + shortName + " is not referenced in the sitetree - " + 285 "content reference is not generated!"); 286 } 287 else if (!siteContent.isGeneratableVersion()) { 288 cat.warn("Content: " + shortName + " is not released - " + 289 "content reference is not generated!"); 290 } 291 } 292 } 293 294 for (int i = 0; i < contentNodes.size(); ) { 296 if (contentNodes.elementAt(i) == null) { 297 contentNodes.removeElementAt(i); 298 } 299 else { 300 i++; 301 } 302 } 303 304 return contentNodes; 305 } 306 307 public void fillIntoPresentation( TKHTMLTemplate t, Object data, String scope ) 308 { 309 GeneratorContext context = GeneratorContext.setup (); 310 311 context.push(); 312 try { 313 TKVector contents = getSelectedContents(data, context); 314 if (contents == null || contents.size() == 0) { 315 super.fillIntoPresentation(t, new TKVector(), scope); 316 return; 317 } 318 if( deliverOnlyNames && contents != null && contents.size() > 0) { 319 if (multipleSelection) { 320 t.set( scope, Integer.toString(contents.size())); 321 t.set(scope + ".SIZE", Integer.toString(contents.size())); 322 t.setListIterator( 323 new GenContentIterator( 324 context, contents, scope, 325 t.getListIterator(), 326 false, true, true) 327 ); 328 } 329 else { 330 SiteContent content = (SiteContent) contents.firstElement(); 331 332 if (content != null) { 333 content.fillNameIntoTemplate(t,scope + "." + getName()); 334 super.fillIntoPresentation(t, content.getShortName(), scope); 335 } 336 } 337 return; 338 } 339 scope += "." + getName(); 340 341 if (contents != null && contents.size() > 0) { 342 if( multipleSelection ) { 343 t.set( scope, Integer.toString(contents.size())); 344 345 351 t.set(scope + ".SIZE", Integer.toString(contents.size())); 352 353 t.setListIterator( 354 new GenContentIterator( 355 context, contents, scope, 356 t.getListIterator(), 357 false, true, false) 358 ); 359 } 360 else { 361 SiteContent content = (SiteContent) contents.firstElement(); 362 if (content != null) { 363 content.fillNameIntoTemplate(t, scope); 364 if (!ReferenceCycleDetector.isCycle(new Integer (content.getInstanceId()))) 365 content.fillIntoTemplate(t, scope); 366 } 367 } 368 return; 369 } 370 } 373 finally { 374 context.pop(); 375 } 376 } 377 378 381 public int insertDataIntoDB( TKContentDBData db, Object data, int contentId, int leftNr ) 382 { 383 return super.insertDataIntoDB(db, data, contentId, leftNr); 384 } 385 386 protected void fillDataIntoNode(Document doc, Element node, Object data) throws DOMException 387 { 388 if( deliverOnlyNames ) { 389 super.fillDataIntoNode( doc, node, data ); 390 return; 391 } 392 GeneratorContext context = GeneratorContext.setup (); 393 394 TKVector contents = getSelectedContents(data, context); 395 if (contents.size() > 1) { 396 node.setAttribute(MULTIPLE, Boolean.TRUE.toString()); 397 } 398 else { 399 node.setAttribute(MULTIPLE, Boolean.FALSE.toString()); 400 } 401 402 Element me = doc.createElement("content"); 404 node.appendChild(me); 405 406 if (multipleSelection) { 407 for (int i = 0; i < contents.size(); i++) { 408 try { 409 context.push(); 410 SiteContent content = (SiteContent) contents.elementAt(i); 411 if (!ReferenceCycleDetector.isCycle(new Integer (content.getInstanceId()))) { 412 content.fillBasicDOM(doc, me); 413 } 414 } 415 finally { 416 context.pop(); 417 } 418 } 419 } 420 else { 421 SiteContent content = (SiteContent) contents.firstElement(); 422 if (!ReferenceCycleDetector.isCycle(new Integer (content.getInstanceId()))) { 423 context.push(); 424 try { 425 content.fillBasicDOM(doc, me); 426 } 427 finally { 428 context.pop(); 429 } 430 return; 431 } 432 } 433 } 434 435 public void initFromDB( String classId, TKFormDBData db, TKVector otherFields ) 436 throws 437 TKUnregisteredClassException, 438 ClassNotFoundException , 439 InstantiationException , 440 IllegalAccessException 441 { 442 super.initFromDB( classId, db, otherFields ); 443 contentNodeId = Integer.parseInt( getFieldAttribute( db, NODE_KEY, 0 ) ); 445 multipleSelection = false; 446 multipleSelection = Boolean.valueOf(getFieldAttribute( db, MULTIPLE_KEY, 0)).booleanValue(); 447 deliverOnlyNames= Boolean.valueOf(getFieldAttribute( db, NAME_ONLY_KEY, 0)).booleanValue(); 448 } 449 450 public int realInsertIntoDB( TKFormDBData db, int formId ) 451 { 452 if( super.realInsertIntoDB( db, formId ) == -1 ) return -1; 453 insertNewFieldAttribute( db, formId, NODE_KEY, 0, String.valueOf( contentNodeId ) ); 454 insertNewFieldAttribute( db, formId, MULTIPLE_KEY, 0, String.valueOf( multipleSelection ) ); 455 insertNewFieldAttribute( db, formId, NAME_ONLY_KEY, 0, String.valueOf( deliverOnlyNames ) ); 456 return fieldId; 457 } 458 459 public Object compileData( String prefix, TKMarkupNode data, TKHashtable context ) 460 { 461 462 TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup; 463 if (markup == null) { return null;} 464 465 if (!markup.name.equals (getName())) { 466 return null; 467 } 468 469 TKXmlTree tree = (TKXmlTree) data.tree; 470 if (tree == null) { return null;} 471 472 String val = tree.getSingleText (); 473 474 if (val == null) return getDefault(); 475 else return val.trim(); 476 } 477 478 479 488 public boolean equals (Object object) 489 { 490 if (! super.equals(object)) 491 { 492 return false; 493 } 494 495 TKContentReferenceField field = (TKContentReferenceField) object; 496 497 return (this.contentNodeId == field.contentNodeId) && 498 (this.multipleSelection == field.multipleSelection) && 499 (this.deliverOnlyNames == field.deliverOnlyNames); 500 } 501 502 507 public int hashCode () 508 { 509 return super.hashCode(); 511 } 512 513 } 514 | Popular Tags |