1 19 package org.openharmonise.rm.resources.content; 20 21 import java.util.Vector ; 22 import java.util.logging.*; 23 24 import org.openharmonise.commons.dsi.AbstractDataStoreInterface; 25 import org.openharmonise.commons.xml.*; 26 import org.openharmonise.commons.xml.namespace.NamespaceType; 27 import org.openharmonise.rm.*; 28 import org.openharmonise.rm.dsi.*; 29 import org.openharmonise.rm.factory.*; 30 import org.openharmonise.rm.metadata.Profile; 31 import org.openharmonise.rm.publishing.*; 32 import org.openharmonise.rm.resources.lifecycle.Editable; 33 import org.openharmonise.rm.resources.publishing.*; 34 import org.openharmonise.rm.resources.xml.XMLResource; 35 import org.w3c.dom.*; 36 37 38 46 public class Document 47 extends XMLResource 48 implements DataStoreObject, Publishable, Editable, Cloneable , Comparable { 49 50 54 private static final String TBL_DOCUMENT = "document"; 55 56 60 public static final String ATTRIB_SELECTIONS_AND_INDEX = 61 "selectionsAndIndex"; 62 65 public static final String TAG_DOCUMENT = "Document"; 66 67 70 public static final String FLAG_INDEX = "index"; 71 72 75 public static final String TAG_CHAPTER = "Chapter"; 76 77 private static final Logger m_logger = Logger.getLogger(Document.class.getName()); 78 79 static { 81 DatabaseInfo.getInstance().registerTableName( 82 Document.class.getName(), 83 TBL_DOCUMENT); 84 } 85 86 90 public Document() { 91 super(); 92 93 } 94 95 100 public Document(AbstractDataStoreInterface dbintrf) { 101 super(dbintrf); 102 103 } 104 105 111 public Document(AbstractDataStoreInterface dbintrf, int nId) { 112 super(dbintrf, nId); 113 } 114 115 123 public Document( 124 AbstractDataStoreInterface dbintrf, 125 int nId, 126 int nKey, 127 boolean bIsHist) { 128 super(dbintrf, nId, nKey, bIsHist); 129 130 } 131 132 136 public void setContent(String sContent) throws PopulateException { 137 super.setContent(sContent); 138 139 try { 140 XMLDocument doc = getDocument(); 141 142 if(doc.getDocumentElement().getLocalName().equals(TAG_CONTENT) == false) { 143 String sMainContent = sContent; 144 145 if(sContent.startsWith("<?xml") == true) { 146 int nIndex = sContent.indexOf("?>"); 147 if(nIndex > 0) { 148 sMainContent = sContent.substring(nIndex+2); 149 } 150 } 151 152 StringBuffer sBuf = new StringBuffer (); 153 154 sBuf.append("<").append(TAG_CONTENT).append(">"); 155 sBuf.append(sMainContent); 156 sBuf.append("</").append(TAG_CONTENT).append(">"); 157 158 super.setContent(sBuf.toString()); 159 } 160 } catch (DataAccessException e) { 161 throw new PopulateException(e); 162 } 163 164 165 } 166 167 170 public String toString() { 171 StringBuffer strBuff = new StringBuffer (); 172 173 strBuff 174 .append("Document Title:[" + m_sName + "] ") 175 .append("Document Summary:[" + m_sSummary + "] ") 176 .append("Document ID:[" + m_nId + "] "); 177 178 179 try { 180 Profile prof = getProfile(); 181 182 if(prof != null) { 183 strBuff.append( 184 "Document profile:[" + prof.toString() + "]"); 185 } 186 187 } catch (DataAccessException e) { 188 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 189 } 190 191 192 return strBuff.toString(); 193 } 194 195 198 public Element publish(Element topEl, HarmoniseOutput xmlDoc, State state) 199 throws PublishException { 200 201 Element docEl = null; 202 NodeList nodes = null; 203 Text txt = null; 204 String sTagName = XMLUtils.getElementName(topEl); 205 206 if (sTagName.equals(TAG_CONTENT)) { 207 org.w3c.dom.Document txtDoc; 208 try { 209 txtDoc = getDocument(); 210 } catch (DataAccessException e) { 211 throw new PublishException(e); 212 } 213 214 if (txtDoc != null) { 215 216 Element docRoot = txtDoc.getDocumentElement(); 217 218 if (docRoot == null) { 219 throw new PublishException( 220 "Parsed document contents return as null :" 221 + " for document id " 222 + m_nId 223 + " contents:" 224 + m_sContent); 225 } 226 227 docEl = publishContents(docRoot, topEl, xmlDoc, state); 228 } else { 229 docEl = (Element) xmlDoc.copyNode(topEl); 230 } 231 } else { 232 docEl = super.publish(topEl, xmlDoc, state); 234 } 235 236 return docEl; 237 } 238 239 240 249 public void populate(Element xmlElement, String sDocContent) 250 throws PopulateException { 251 252 try { 253 populate(xmlElement, new State(this.m_dsi)); 254 } catch (StateException e) { 255 throw new PopulateException("Error creating new State", e); 256 } 257 258 m_sContent = sDocContent; 259 } 260 261 264 public String getDBTableName() { 265 266 return TBL_DOCUMENT; 267 } 268 269 272 public String getParentObjectClassName() { 273 return Section.class.getName(); 274 } 275 276 279 public String getTagName() { 280 return TAG_DOCUMENT; 281 } 282 283 286 287 297 private Element publishContents( 298 Element contentsEl, 299 Element templateEl, 300 HarmoniseOutput xmlDoc, 301 State state) 302 throws PublishException { 303 304 String sTagName = XMLUtils.getElementName(contentsEl); 305 306 if (sTagName.equals(TAG_CONTENT) == false) { 307 throw new InvalidXMLElementException(TAG_CONTENT + " tag needed"); 308 } 309 310 316 Element elReturn = null; 317 String sFlag = templateEl.getAttribute(ATTRIB_SELECTIONS_AND_INDEX); 318 319 if ((sFlag != null) && (sFlag.length() > 0)) { 320 elReturn = 322 publishSelectionsAndIndex( 323 contentsEl, 324 templateEl, 325 xmlDoc, 326 state, 327 sFlag); 328 } else { 329 NodeList nlTest = templateEl.getChildNodes(); 330 331 if (nlTest.getLength() > 0) { 332 elReturn = 334 publishSelectionsOnly( 335 contentsEl, 336 templateEl, 337 xmlDoc, 338 state); 339 } else { 340 elReturn = publishFullContents(contentsEl, xmlDoc, state); 342 } 343 } 344 345 return elReturn; 346 } 347 348 359 private Element publishSelectionsAndIndex( 360 Element contentsEl, 361 Element templateEl, 362 HarmoniseOutput xmlDoc, 363 State state, 364 String sFlag) 365 throws PublishException { 366 370 Element elReturn = null; 371 Element elStateDoc = 372 state.findElement(state.createElement(TAG_DOCUMENT)); 373 Vector chapters = new Vector (); 374 NodeList nodeChapters = null; 375 376 if (sFlag.equals(FLAG_INDEX) == false) { 378 boolean bFound = false; 379 380 if (elStateDoc != null) { 381 String sId = elStateDoc.getAttribute(ATTRIB_ID); 382 383 if (sId != null) { 384 if (m_nId == Integer.parseInt(sId)) { 385 bFound = true; 386 } 387 } else { 388 Element elPath = 389 XMLUtils.getFirstNamedChild(elStateDoc, TAG_PATH); 390 391 if (elPath != null) { 392 String sPath = elPath.getFirstChild().getNodeValue(); 393 394 try { 395 if (sPath.equals(getPath() + "/" + getName())) { 396 bFound = true; 397 } 398 } catch (DataAccessException e) { 399 throw new PublishException( 400 "Error occured getting name or path",e); 401 } 402 } 403 } 404 405 if (bFound) { 406 nodeChapters = elStateDoc.getElementsByTagName(TAG_CHAPTER); 407 408 for (int i = 0; i < nodeChapters.getLength(); i++) { 409 Element el = (Element) nodeChapters.item(i); 410 chapters.add(el.getAttribute(ATTRIB_ID)); 411 } 412 } 413 } 414 415 if (chapters.size() == 0) { 416 nodeChapters = contentsEl.getElementsByTagNameNS(NamespaceType.OHRM_DOC.getURI(),TAG_CHAPTER); 417 418 if (nodeChapters.getLength() > 0) { 419 Element el = (Element) nodeChapters.item(0); 420 chapters.add(el.getAttribute(ATTRIB_ID)); 421 } 422 423 } 424 } 425 426 elReturn = xmlDoc.createElement(TAG_CONTENT); 427 428 NodeList nodes = contentsEl.getChildNodes(); 429 Element elLinkTemplate = 430 (Element) templateEl.getElementsByTagName( 431 Template.TAG_TEMPLATE).item( 432 0); 433 434 Template template = null; 435 436 try { 437 template = (Template) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, Template.class.getName(), Integer.parseInt(elLinkTemplate.getAttribute(ATTRIB_ID))); 438 } catch (NumberFormatException e) { 439 throw new PublishException(e); 440 } catch (HarmoniseFactoryException e) { 441 throw new PublishException(e); 442 } 443 444 int nPageId = 0; 445 NodeList nodesPage = 446 elLinkTemplate.getElementsByTagName(WebPage.TAG_PAGE); 447 448 if (nodesPage.getLength() > 0) { 449 Element elPage = (Element) nodesPage.item(0); 450 nPageId = Integer.parseInt(elPage.getAttribute(ATTRIB_ID)); 451 } 452 453 for (int i = 0; i < nodes.getLength(); i++) { 455 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 456 continue; 457 } 458 459 Element el = (Element) nodes.item(i); 460 461 if (XMLUtils.getElementName(el).equals(TAG_CHAPTER)) { 462 String sId = el.getAttribute(ATTRIB_ID); 463 464 if (chapters.contains(sId)) { 465 elReturn.appendChild( 467 publishFullContents(el, xmlDoc, state)); 468 } else { 469 try { 472 State stateNew = null; 473 474 stateNew = 475 new State((org.w3c.dom.Document ) state, m_dsi); 476 477 Element elNewStateDoc = 478 stateNew.findElement( 479 state.createElement(TAG_DOCUMENT)); 480 481 if (elNewStateDoc != null) { 482 NodeList children = elNewStateDoc.getChildNodes(); 483 484 for (int j = 0; j < children.getLength(); j++) { 485 elNewStateDoc.removeChild(children.item(j)); 486 } 487 } else { 488 elNewStateDoc = 489 stateNew.createElement(TAG_DOCUMENT); 490 elNewStateDoc.setAttribute( 491 ATTRIB_ID, 492 Integer.toString(getId())); 493 stateNew.getDocumentElement().appendChild( 494 elNewStateDoc); 495 } 496 497 Element elThisChapter = 498 stateNew.createElement(TAG_CHAPTER); 499 elThisChapter.setAttribute(ATTRIB_ID, sId); 500 elNewStateDoc.appendChild(elThisChapter); 501 502 Element elPublish = 504 template.publishObjectToElement( 505 this, 506 xmlDoc, 507 stateNew); 508 509 if (nPageId != 0) { 510 xmlDoc.addPageIdToLinkNode( 511 state.getLoggedInUser(), 512 elPublish, 513 nPageId); 514 } 515 516 elReturn.appendChild(elPublish); 517 } catch (StateException e) { 518 throw new PublishException( 519 "Error creating new State", 520 e); 521 } 522 } 523 } else { 524 elReturn.appendChild(publishFullContents(el, xmlDoc, state)); 526 } 527 } 528 529 return elReturn; 530 } 531 532 543 private Element publishSelectionsOnly( 544 Element contentsEl, 545 Element templateEl, 546 HarmoniseOutput xmlDoc, 547 State state) 548 throws PublishException { 549 NodeList nodesTemplate = templateEl.getChildNodes(); 551 Element elStateDoc = 552 state.findElement(state.createElement(TAG_DOCUMENT)); 553 554 Element elReturn = null; 558 559 String sTagname = XMLUtils.getElementName(contentsEl); 560 561 if(sTagname.equals(TAG_CONTENT)) { 562 elReturn = xmlDoc.createElement(TAG_CONTENT); 563 } else { 564 elReturn = (Element) xmlDoc.importNode(contentsEl.cloneNode(false), false); 565 } 566 567 boolean bFound = false; 568 569 for (int i = 0; i < nodesTemplate.getLength(); i++) { 572 if (nodesTemplate.item(i).getNodeType() != Node.ELEMENT_NODE) { 573 continue; 574 } 575 576 bFound = true; 577 578 Element elNext = (Element) nodesTemplate.item(i); 579 String sTagName = XMLUtils.getElementName(elNext); 580 581 NodeList nodesContents = contentsEl.getElementsByTagNameNS(NamespaceType.OHRM_DOC.getURI(),sTagName); 583 584 String sId = elNext.getAttribute(ATTRIB_ID); 586 587 if ((sId == null || sId.length() == 0) && (elStateDoc != null)) { 589 NodeList nodes = elStateDoc.getElementsByTagName(sTagName); 590 591 if (nodes.getLength() > 0) { 592 sId = ((Element) nodes.item(0)).getAttribute(ATTRIB_ID); 593 } 594 } 595 596 for (int j = 0; j < nodesContents.getLength(); j++) { 598 Element el = (Element) nodesContents.item(j); 599 String sNextId = null; 600 601 if ((sId != null) && (sId.length() > 0)) { 602 sNextId = el.getAttribute(ATTRIB_ID); 603 } 604 605 if ((sId == null || sId.length() == 0) 609 || (sNextId != null && sNextId.equals(sId))) { 610 elReturn.appendChild( 611 publishSelectionsOnly(el, elNext, xmlDoc, state)); 612 } 613 } 614 } 615 616 if (bFound == false) { 619 xmlDoc.copyChildren(elReturn, contentsEl); 620 } 621 622 return elReturn; 623 } 624 625 634 private Element publishFullContents( 635 Element contentEl, 636 HarmoniseOutput xmlDoc, 637 State state) 638 throws PublishException { 639 Element returnEl = null; 641 642 NodeList nlContents = null; 643 String sTagName; 644 int nTemplateId; 645 Template template = null; 646 647 sTagName = XMLUtils.getElementName(contentEl); 648 649 if (sTagName.equals(Template.TAG_TEMPLATE)) { 651 nTemplateId = Integer.parseInt(contentEl.getAttribute(ATTRIB_ID)); 652 Element templRoot = null; 653 654 try { 655 template = (Template) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, Template.class.getName(), nTemplateId); 656 657 templRoot = template.getTemplateRootElement(); 658 } catch (DataAccessException e) { 659 throw new PublishException( 660 "Error occured getting root element of template",e); 661 } catch (HarmoniseFactoryException e) { 662 throw new PublishException( 663 "Error occured getting root element of template",e); 664 } 665 666 NodeList children = contentEl.getChildNodes(); 667 668 for (int j = 0; j < children.getLength(); j++) { 669 if (children.item(j).getNodeType() == Node.ELEMENT_NODE) { 670 Element child = (Element) children.item(j); 671 672 if (XMLUtils.getElementName(templRoot) 673 .equalsIgnoreCase(XMLUtils.getElementName(child))) { 674 675 try { 676 Publishable pubObj = 677 HarmoniseObjectFactory.instantiatePublishableObject( 678 m_dsi, 679 child, 680 state); 681 682 if (pubObj != null) { 683 returnEl = 684 template.publishObjectToElement( 685 pubObj, 686 xmlDoc, 687 state); 688 } 689 } catch (HarmoniseFactoryException e) { 690 throw new PublishException( 691 "Error occured getting object from factory",e); 692 } 693 694 } 695 } 696 } 697 } else { 698 700 if(sTagName.equals(TAG_CONTENT)) { 701 returnEl = xmlDoc.createElement(TAG_CONTENT); 702 } else { 703 returnEl = (Element) xmlDoc.importNode(contentEl.cloneNode(false), false); 704 } 705 706 nlContents = contentEl.getChildNodes(); 707 } 708 709 if (nlContents != null) { 710 for (int i = 0; i < nlContents.getLength(); i++) { 711 if (nlContents.item(i).getNodeType() == Node.TEXT_NODE) { 713 returnEl.appendChild( 714 xmlDoc.createTextNode( 715 nlContents.item(i).getNodeValue())); 716 } 717 else if ( 719 nlContents.item(i).getNodeType() == Node.ELEMENT_NODE) { 720 contentEl = (Element) nlContents.item(i); 721 Element publishEl = 722 publishFullContents(contentEl, xmlDoc, state); 723 724 if (publishEl != null) { 725 returnEl.appendChild(publishEl); 726 } 727 } 728 } 729 } 730 731 return returnEl; 732 } 733 734 } | Popular Tags |