1 package de.webman.generator; 2 3 import com.teamkonzept.db.*; 4 import com.teamkonzept.lib.*; 5 import de.webman.template.jsp.*; 6 import com.teamkonzept.lib.templates.TemplateTypes; 7 import com.teamkonzept.web.*; 8 import com.teamkonzept.field.*; 9 import com.teamkonzept.field.db.*; 10 import com.teamkonzept.international.LanguageManager; 11 import org.w3c.dom.*; 12 import java.sql.*; 13 import java.text.*; 14 import org.apache.log4j.Category; 15 import de.webman.content.Content; 16 import de.webman.content.workflow.ContentVersion; 17 import de.webman.util.legacy.Legacy; 18 import de.webman.util.log4j.WebmanCategory; 19 20 21 26 public class SiteContent implements TKSortable, TemplateTypes 27 { 28 29 30 private static final DateFormat DATE_FORMATTER = DateFormat.getDateInstance(DateFormat.SHORT, LanguageManager.getStandardLocale()); 31 32 33 private static final DateFormat TIME_FORMATTER = DateFormat.getTimeInstance(DateFormat.SHORT); 34 35 36 private static final DateFormat GNU_FORMATTER = new SimpleDateFormat ("yyyy.MM.dd"); 37 38 41 private GeneratorContext context; 42 43 44 private static WebmanCategory cat = (WebmanCategory)Category.getInstance(SiteContent.class.getName()); 45 46 47 50 private int instanceId; 51 52 55 private int versionId; 56 57 60 private int contentId; 61 62 63 private int content_node_id; 64 65 68 private String shortName; 69 70 73 private String name; 74 75 78 private boolean isStructureContent; 79 80 83 private int formId; 84 85 86 private String author; 87 88 89 private String initialAuthor; 90 91 92 private java.util.Date lastChange; 93 94 95 private java.util.Date firstDate; 96 97 98 private static boolean isCachingEnabled = true; 99 100 101 private int statusID; 102 103 110 private TKHashtable inDocs; 111 112 116 private Object contentData; 117 118 122 private boolean isPrimary; 123 124 125 private Integer generated = new Integer (0); 126 127 public SiteContent(GeneratorContext context, int formId, Content content, boolean isStructureContent ) 128 { 129 this(context, formId, content, isStructureContent, false); 130 } 131 134 public SiteContent(GeneratorContext context, int formId, Content content, boolean isStructureContent, boolean _isPrimary ) 135 { 136 this.context = context != null ? context : GeneratorContext.setup (); 137 this.formId = formId; 138 this.isStructureContent = isStructureContent; 139 isPrimary = _isPrimary; 140 this.instanceId = content.getInstanceId().intValue(); 141 ContentVersion genVersion = null; 142 if (context.isWorkflowIgnored() || isPrimary) 144 genVersion = content.getCurrentVersion(); 145 else 146 genVersion = content.getGeneratableVersion(); 147 149 if (genVersion == null) 150 { 151 this.versionId = -1; 152 this.contentId = -1; 153 author = ""; 154 statusID = -1; 155 lastChange = null; 156 157 } 158 else 159 { 160 this.versionId = genVersion.getId().intValue(); 161 this.contentId = genVersion.getContentId().intValue(); 162 author = genVersion.getAuthor(); 163 statusID = genVersion.getStatus().status_id; 164 lastChange = genVersion.getDate(); 165 } 166 this.content_node_id = content.getId().intValue(); 167 this.formId = formId; 168 this.shortName = content.getShortName(); 169 this.name = content.getName(); 170 ContentVersion firstVersion = content.getInitialVersion(); 171 if (firstVersion != null) 172 { 173 initialAuthor = firstVersion.getAuthor(); 174 firstDate = firstVersion.getDate(); 175 } 176 else 177 { 178 initialAuthor = "toDO"; 179 firstDate = new Date(System.currentTimeMillis()); 180 } 181 this.inDocs = new TKHashtable(); 182 this.contentData = null; 183 184 this.context.siteContents.putContent (instanceId, this); 185 186 } 187 188 191 public SiteContent( GeneratorContext context, int instanceId, int versionId, 192 int contentId, int formId, 193 String shortName, String name, boolean isStructureContent ) 194 { 195 this.context = context != null ? context : GeneratorContext.setup (); 196 197 this.instanceId = instanceId; 198 this.versionId = versionId; 199 this.contentId = contentId; 200 this.formId = formId; 201 this.shortName = shortName; 202 this.name = name; 203 this.isStructureContent = isStructureContent; 204 205 this.inDocs = new TKHashtable(); this.contentData = null; 207 208 this.context.siteContents.putContent (instanceId, this); 209 } 210 211 public Integer getGenerated() 212 { 213 return generated; 214 } 215 216 public void setGenerated(Integer generated) 217 { 218 this.generated = generated; 219 } 220 221 public void resetInDocs() 222 { 223 inDocs = new TKHashtable(); 224 } 225 226 public int getVersionId() 227 { 228 return versionId; 229 } 230 231 public boolean isStructureContent() 232 { 233 return isStructureContent; 234 } 235 236 public int getInstanceId() 237 { 238 return instanceId; 239 } 240 241 public String getName() 242 { 243 return name; 244 } 245 246 public int getContentId() 247 { 248 return contentId; 249 } 250 251 public int getFormularId() 252 { 253 return formId; 254 } 255 256 public String getShortName() 257 { 258 return shortName; 259 } 260 261 static 262 { 263 try 264 { 265 PropertyManager man = PropertyManager.getPropertyManager("CACHING"); 266 String enabled = man.getValue("SITE_CONTENT", null); 267 if (enabled != null && enabled.equalsIgnoreCase("false")) 268 isCachingEnabled = false; 269 } 270 catch (Throwable t) 271 { 272 cat.debug("Caching of Site Contents disabled - no property"); 274 } 275 } 276 277 278 public String toString() 279 { 280 String result = 281 "("+instanceId+", "+versionId+", "+contentId+", '"+shortName+"', "+ formId+")"; 282 return result; 283 } 284 285 286 290 private Object getData( TKBaseField form ) 291 { 292 if( contentData != null ) { 294 return contentData; 295 } 296 Object tempData = null; 297 cat.debug("+++ start getData "+contentId); 299 300 try 302 { 303 if (isStructureContent) { 306 if (contentId < 0 ) { 307 tempData = form.getDefault(); 308 } 309 else { 310 TKContentDBData cdata = new TKContentDBData( contentId, versionId ); 311 TKContentDBInterface tmp = TKContentDBInterface.self; 312 313 tmp.getEntry(cdata); 321 cdata.content_id = contentId; 322 tmp.getEntryTables (cdata); 323 324 tempData = form.getDataFromDB( cdata ); 325 } 326 } 327 else { 328 if ((contentId < 0) || (versionId < 0)) { 329 tempData = form.getDefault(); 330 } 331 else { 332 TKContentDBData cdata = new TKContentDBData( contentId, versionId ); 333 TKContentDBInterface.Get(cdata); 334 tempData = form.getDataFromDB( cdata ); 336 } 337 } 338 349 } 350 catch( SQLException e ) { 351 cat.warn( "could not read content "+contentId, e ); 352 throw new TKSQLError( e.getMessage(), e ); 353 } 354 355 if (isCachingEnabled) 356 contentData = tempData; 357 return tempData; 358 } 359 360 366 public void registerDoc( GenDocument doc, boolean onlyStructureContents ) 367 { 368 if (onlyStructureContents && !isStructureContent) return; 369 370 Integer oldVal = (Integer ) inDocs.get( doc ); 371 if( oldVal == null ) { 372 inDocs.put( doc, new Integer (1) ); 373 } 374 else { 375 inDocs.put( doc, new Integer ( oldVal.intValue()+1) ); 376 } 377 378 } 379 380 385 public void deregisterDoc( GenDocument doc, boolean onlyStructureContents ) 386 { 387 if (onlyStructureContents && !isStructureContent) return; 388 389 Integer oldVal = (Integer ) inDocs.get(doc); 390 if( oldVal == null ) 391 { 392 cat.error( "too many deregistrations of content "+contentId+ 393 " for document "+doc.getAnchor().getPath()+doc.getDocumentName() ); 394 return; 395 } 396 397 if( oldVal.intValue() == 1 ) { 398 inDocs.remove( doc ); 399 if( inDocs.size() == 0 ) { 400 contentData = null; 401 cat.debug("removed data for content "+contentId ); 402 } 403 } 404 else { 405 inDocs.put( doc, new Integer ( oldVal.intValue()-1 ) ); 406 } 407 } 408 409 412 public void fillNameIntoTemplate( TemplateBasic t, String integrationName ) 413 { 414 if ((name != null) && (name.length() > 0)) t.set (integrationName+".NAME",name); 415 if (author != null) t.set (integrationName+".AUTHOR", author); 416 if (initialAuthor != null) t.set (integrationName+".INITIAL_AUTHOR", initialAuthor); 417 418 if (lastChange != null) 420 { 421 t.set (integrationName+".MODIFIED", getDate(lastChange)); 422 t.set (integrationName+".MODIFIED_GNU", getGNUDate(lastChange)); 423 t.set (integrationName+".MODIFIED_TIME", getTime(lastChange)); 424 } 425 426 if (firstDate != null) 427 { 428 t.set (integrationName+".FIRST_DATE", getDate(firstDate)); 429 t.set (integrationName+".FIRST_DATE_GNU", getGNUDate(firstDate)); 430 t.set (integrationName+".FIRST_TIME", getTime(firstDate)); 431 } 432 if (contentId > 0) t.set(integrationName+".CONTENT_ID", Integer.toString(content_node_id)); 433 if (statusID > 0) t.set(integrationName+".WORKFLOW_ID", Integer.toString(statusID)); 434 if ((shortName != null) && (shortName.length() > 0)) t.set (integrationName+".SHORT_NAME",shortName); 435 } 436 437 440 public void fillIntoTemplate( TemplateBasic t, String integrationName ) 441 { 442 try 443 { 444 if (t.getType().equals(TK_TEMPLATE)) 445 { 446 TKBaseField form = context.contentForms.getContentForm( formId ); 447 Object data = getData( form ); 448 form.fillIntoPresentation( (TKHTMLTemplate)t, data, integrationName ); 449 } 450 else 451 { 452 DOMTemplateBasic domTemplate = (DOMTemplateBasic) t; 453 Element c = domTemplate.getContentElement(shortName); fillBasicDOM(domTemplate.getDocument(), c); 455 } 456 } 457 catch (TKFieldDataDBError e) 458 { 459 TKBaseField base = context.contentForms.getContentForm(formId); 461 if (GeneratorContext.isPreviewMode()) 462 { 463 cat.error("Incompatible form change of form : " + formId + " in Content : " + content_node_id); 464 throw new TKFieldDataDBError("Incompatible form change :" + formId, e.type, e.id, base.getShowName()); 465 } 466 else 467 { 468 cat.error("The form : " + base.getShowName() + " of content: " + content_node_id + " has an incompatible change. Content is not generated.Please contact your producer"); 469 } 470 } 471 } 472 473 474 477 public void fillBasicDOM(Document doc, Element node) 478 { 479 try 480 { 481 482 TKBaseField form = context.contentForms.getContentForm( formId ); 483 Object data = getData( form ); 484 fillIntoDOM(doc, node, data); 485 486 node.setAttribute ("NAME", name); 488 node.setAttribute ("SHORT_NAME", shortName); 489 node.setAttribute ("AUTHOR", author); 490 node.setAttribute ("INITIAL_AUTHOR", initialAuthor); 491 node.setAttribute ("MODIFIED", getDate(lastChange)); 492 node.setAttribute ("FIRST_DATE", getDate(firstDate)); 493 node.setAttribute ("MODIFIED_TIME", getTime(lastChange)); 494 node.setAttribute ("FIRST_TIME", getTime(firstDate)); 495 node.setAttribute ("CONTENT_ID" , Integer.toString(content_node_id)); 496 node.setAttribute ("WORKFLOW_ID", Integer.toString(statusID)); 497 } 498 catch (Throwable t) 499 { 500 cat.error("error getting dom content:"+ contentId, t); 501 } 502 } 503 506 public void fillIntoDOM(Document doc, Element node, Object data) 507 { 508 try 509 { 510 TKBaseField form = context.contentForms.getContentForm( formId ); 511 form.fillIntoDOM(doc, node, data); 512 } 513 catch (Throwable t) 514 { 515 cat.error("error getting dom content:"+ content_node_id, t); 516 } 517 518 } 519 523 public int cmpNames( SiteContent otherContent ) 524 { 525 if( name == null ) { 526 return ( otherContent.name == null ? 0 : 1 ); 527 } 528 else 529 if( otherContent.name == null ) return -1; 530 return name.compareTo( otherContent.name ); 531 } 532 533 546 public int cmp( TKSortable otherObject ) 547 { 548 SiteContent other = (SiteContent) otherObject; 549 return cmpNames(other); 550 572 } 573 574 577 public boolean isGeneratableVersion() 578 { 579 return (versionId != -1) ? true : false; 580 } 581 582 585 static String getDate(java.util.Date date) 586 { 587 if (date == null) 588 return ""; 589 return DATE_FORMATTER.format(date); 590 } 591 592 595 static String getGNUDate(java.util.Date date) 596 { 597 if (date == null) 598 return ""; 599 return GNU_FORMATTER.format(date); 600 } 601 602 static String getTime(java.util.Date date) 603 { 604 if (date == null) 605 return ""; 606 return TIME_FORMATTER.format(date); 607 } 608 } 609
| Popular Tags
|