1 19 package org.openharmonise.rm.resources; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.sql.*; 23 import java.text.SimpleDateFormat ; 24 import java.util.*; 25 import java.util.Date ; 26 import java.util.logging.*; 27 28 import org.openharmonise.commons.cache.*; 29 import org.openharmonise.commons.dsi.*; 30 import org.openharmonise.commons.dsi.dml.*; 31 import org.openharmonise.rm.*; 32 import org.openharmonise.rm.commands.InvalidCommandException; 33 import org.openharmonise.rm.dsi.*; 34 import org.openharmonise.rm.factory.*; 35 import org.openharmonise.rm.publishing.*; 36 import org.openharmonise.rm.resources.lifecycle.*; 37 import org.openharmonise.rm.resources.publishing.*; 38 import org.openharmonise.rm.resources.users.User; 39 import org.openharmonise.rm.search.Search; 40 import org.openharmonise.rm.security.authorization.AuthorizationValidator; 41 import org.w3c.dom.*; 42 43 44 53 public abstract class AbstractChildObject 54 extends AbstractProfiledObject 55 implements Editable, Publishable, DataStoreObject, Cloneable { 56 57 59 63 public static final String ATTRIB_DEFAULT = "default"; 64 65 69 public static final String ATTRIB_SHOWALL = "showAll"; 70 71 74 public static final String TAG_PATH = "Path"; 75 76 79 public static final String TAG_GROUP = "Group"; 80 81 85 protected static final String CLMN_DEFAULT_LINK = "default_link"; 86 87 90 protected static final String CLMN_PATH = "path"; 91 92 95 protected static final String CLMN_PARENT_KEY = "parent_key"; 96 97 100 protected static final String CLMN_CHILD_KEY = "child_key"; 101 102 105 protected static final String CLMN_DATE = "date"; 106 107 113 public static final String JOIN_TO = "_2_"; 114 115 119 public static final String ARCHIVE_TIMESTAMP_FORMAT = "yyyy-MM-dd_HH-mm"; 120 121 126 protected static final String IS_DEFAULT_GROUP = "1"; 127 128 131 protected static Map m_child_parent_mappings = new Hashtable(); 132 133 136 protected Map m_groups = null; 137 138 142 protected List m_groups_to_save; 143 144 147 protected int m_defaultGroup = -1; 148 149 152 protected String m_sGroupClass = null; 153 154 158 protected boolean m_bIsGroupsPopulated = false; 159 160 166 protected String m_sPath = null; 167 168 172 public static final String separator = "/"; 173 174 177 private static final Logger m_logger = Logger.getLogger(AbstractChildObject.class.getName()); 178 179 { 181 m_sGroupClass = getParentObjectClassName(); 182 m_groups = new HashMap(); 183 m_groups_to_save = new Vector(); 184 } 185 186 190 public AbstractChildObject() { 191 super(); 192 } 193 194 199 public AbstractChildObject(AbstractDataStoreInterface dbintrf) { 200 super(dbintrf); 201 } 202 203 211 public AbstractChildObject(AbstractDataStoreInterface dbintrf, int nId) { 212 super(dbintrf, nId); 213 } 214 215 223 public AbstractChildObject( 224 AbstractDataStoreInterface dbintrf, 225 int nId, 226 int nKey, 227 boolean bIsHist) { 228 super(dbintrf, nId,nKey, bIsHist); 229 } 230 231 234 public void markAsNew() throws PopulateException { 235 super.markAsNew(); 236 m_groups_to_save = new Vector(m_groups.values()); 237 } 238 239 242 public Object clone() { 243 try { 244 if (m_bIsPopulated == false) { 245 populateFromDatabase(); 246 } 247 248 AbstractChildObject other = (AbstractChildObject) super.clone(); 249 250 if (m_groups != null) { 251 other.m_groups = new HashMap(m_groups); 252 } 253 254 return other; 255 } catch (PopulateException e) { 256 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 257 258 return null; 259 } 260 } 261 262 265 public void clear() { 266 Collection parent_groups = m_groups.values(); 268 269 Iterator iter = parent_groups.iterator(); 270 271 while (iter.hasNext() == true) { 272 CachePointer ptr = (CachePointer) iter.next(); 273 274 try { 275 if (this instanceof AbstractParentObject) { 276 ((AbstractParentObject) ptr.getObject()).clearChildren(); 277 } else { 278 ((AbstractParentObject) ptr.getObject()).clearChildren(); 279 } 280 } catch (CacheException e) { 281 throw new IllegalStateException ( 282 "Problem occured getting object from the cache:" 283 + e.getLocalizedMessage()); 284 } 285 } 286 287 super.clear(); 288 m_groups.clear(); 289 m_groups_to_save.clear(); 290 m_defaultGroup = -1; 291 m_bIsGroupsPopulated = false; 292 } 293 294 304 public AbstractParentObject getRealParent() throws DataAccessException { 305 AbstractParentObject defGroup = null; 306 307 if(isLiveVersion() == true || (getLiveVersion() == null && isHistorical() == false)) { 308 309 if (m_bIsGroupsPopulated == false) { 310 311 try { 312 populateGroupsFromDatabase(); 313 } catch (PopulateException e) { 314 throw new DataAccessException( 315 "Error occured accessing parents",e); 316 } 317 } 318 319 if(m_defaultGroup > 0) { 320 CachePointer ptr = 321 (CachePointer) m_groups.get(String.valueOf(m_defaultGroup)); 322 323 if (ptr != null) { 324 try { 325 defGroup = (AbstractParentObject) ptr.getObject(); 326 } catch (CacheException e) { 327 throw new DataAccessException( 328 "Error occured getting parent",e); 329 } 330 } 331 332 if ((defGroup == null) && (m_groups.size() > 0)) { 333 defGroup = (AbstractParentObject) getParents().get(0); 334 } 335 } 336 } else if(isHistorical() == true && getLiveVersion() == null) { 337 338 343 String sTmpPath = m_sPath; 344 Stack pathStack = new Stack(); 345 AbstractParentObject tmpParent = null; 346 347 while(defGroup == null && sTmpPath != null && sTmpPath.length() > 1) { 348 349 if(tmpParent == null) { 350 try { 351 tmpParent = (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, getParentObjectClassName(), sTmpPath); 352 } catch (HarmoniseFactoryException e) { 353 throw new DataAccessException(e); 354 } 355 } 356 357 if(tmpParent == null) { 358 int nLastSlashIndex = sTmpPath.lastIndexOf(separator); 359 sTmpPath = sTmpPath.substring(0, nLastSlashIndex); 360 nLastSlashIndex = sTmpPath.lastIndexOf(separator); 361 String sName = sTmpPath.substring(nLastSlashIndex + 1,sTmpPath.length()); 362 pathStack.push(sName); 363 } else if(pathStack.size() == 0) { 364 defGroup = tmpParent; 365 } else { 366 String sName = (String ) pathStack.pop(); 367 tmpParent = (AbstractParentObject) tmpParent.getArchivedChildByName(sName); 368 } 369 } 370 371 } else { 372 defGroup = ((AbstractChildObject)getLiveVersion()).getRealParent(); 373 } 374 375 376 return defGroup; 377 } 378 379 388 public boolean isRealParent(AbstractParentObject obj) 389 throws DataAccessException { 390 boolean isDefault = false; 391 AbstractParentObject defObj = getRealParent(); 392 393 if ((defObj != null) && (defObj.getId() == obj.getId())) { 394 isDefault = true; 395 } 396 397 return isDefault; 398 } 399 400 408 public String getPath() throws DataAccessException { 409 String sPath = null; 410 411 if (isPopulated() == false) { 412 try { 413 populateFromDatabase(); 414 } catch (PopulateException e) { 415 throw new DataAccessException("Error populating object:", e); 416 } 417 } 418 419 if (isHistorical() == true) { 420 sPath = m_sPath; 421 } else { 422 AbstractParentObject parent = getRealParent(); 423 424 if (parent != null) { 425 sPath = parent.getFullPath(); 426 } 427 } 428 429 return sPath; 430 } 431 432 441 public String getFullPath() throws DataAccessException { 442 StringBuffer sFullPath = new StringBuffer (); 443 444 sFullPath.append(getPath()); 445 446 if(sFullPath.length() > 1) { 448 sFullPath.append(separator); 449 } 450 451 sFullPath.append(getName()); 452 453 return sFullPath.toString(); 454 } 455 456 463 public List getAllPaths() throws DataAccessException { 464 ArrayList paths = new ArrayList(); 465 466 List parents = getParents(); 467 468 if(isHistorical() == false && parents.size() > 0) { 469 Iterator iter = parents.iterator(); 470 471 while (iter.hasNext()) { 472 AbstractChildObject child = (AbstractChildObject) iter.next(); 473 paths.addAll(child.getAllFullPaths()); 474 } 475 } else { 476 paths.add(getPath()); 477 } 478 479 480 return paths; 481 } 482 483 490 public List getAllFullPaths() throws DataAccessException { 491 ArrayList paths = new ArrayList(); 492 493 List parents = getParents(); 494 495 if(isHistorical() == false && parents.size() > 0) { 496 Iterator iter = parents.iterator(); 497 498 while (iter.hasNext()) { 499 AbstractChildObject child = (AbstractChildObject) iter.next(); 500 501 List childPaths = child.getAllFullPaths(); 502 503 Iterator pathIter = childPaths.iterator(); 504 505 while (pathIter.hasNext()) { 506 String path = (String ) pathIter.next(); 507 StringBuffer sFullPath = new StringBuffer (); 508 sFullPath.append(path).append(separator).append(m_sName); 509 paths.add(sFullPath.toString()); 510 } 511 } 512 513 } else { 514 paths.add(getFullPath()); 515 } 516 517 return paths; 518 } 519 520 527 public List getParents() throws DataAccessException { 528 List groups = new Vector(); 529 if(isLiveVersion() == true || getLiveVersion() == null) { 530 if (this.m_bIsGroupsPopulated == false) { 531 try { 532 this.populateGroupsFromDatabase(); 533 } catch (PopulateException e) { 534 throw new DataAccessException(e.getLocalizedMessage(),e); 535 } 536 } 537 538 Iterator iter = m_groups.values().iterator(); 539 540 while (iter.hasNext()) { 541 CachePointer ptr = (CachePointer) iter.next(); 542 543 try { 544 groups.add(ptr.getObject()); 545 } catch (CacheException e) { 546 throw new DataAccessException( 547 "Error occured getting object from cache",e); 548 } 549 } 550 } else { 551 AbstractChildObject live = (AbstractChildObject)getLiveVersion(); 552 553 if(live != null && live != this) { 554 groups = live.getParents(); 555 } 556 557 } 558 559 return groups; 560 } 561 562 565 public void populate(Element xmlElement, State state) 566 throws PopulateException { 567 String sTagName = xmlElement.getTagName(); 568 569 if (sTagName.equals(TAG_GROUP)) { 570 NodeList nodes = xmlElement.getChildNodes(); 571 572 for (int i = 0; i < nodes.getLength(); i++) { 573 if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) { 574 Element node = (Element) nodes.item(i); 575 576 try { 577 578 AbstractParentObject grpObj = 579 ( 580 AbstractParentObject) HarmoniseObjectFactory 581 .instantiatePublishableObject( 582 this.m_dsi, 583 node, 584 state); 585 try{ 586 if(this.isLiveVersion()==false){ 587 acquireEditWriteLock(); 588 User user = state.getLoggedInUser(); 589 if(user == null){ 590 throw new PopulateException("Unable to process object without without specifying a user"); 591 } 592 try { 593 boolean bIsAvailable = AuthorizationValidator.isCommandAvailable(user, (AbstractEditableObject) this, "Save"); 594 if(bIsAvailable == false){ 595 throw new PopulateException("Save is not authorised for this object"); 596 } 597 } catch (org.openharmonise.rm.security.authorization.AuthorizationException e) { 598 throw new PopulateException("There was an authorisation problem" + this.getClass(),e); 599 } 600 save(); 601 releaseEditWriteLock(); 602 } 603 } catch (Exception e){ 604 throw new PopulateException( 605 "There was a problem saving this object",e); 606 } 607 if (node.getAttribute(ATTRIB_DEFAULT).equals(IS_DEFAULT_GROUP)) { 608 AbstractParentObject parent = getRealParent(); 609 if(parent != null){ 610 parent.acquireEditWriteLock(); 611 parent.removeChild(this); 612 parent.saveNonCoreData(); 613 parent.releaseEditWriteLock(); 614 } 615 grpObj.acquireEditWriteLock(); 616 grpObj.addChild(this); 617 grpObj.saveNonCoreData(); 618 grpObj.releaseEditWriteLock(); 619 } else { 620 grpObj.acquireEditWriteLock(); 621 grpObj.addChild(this, true); 622 grpObj.saveNonCoreData(); 623 grpObj.releaseEditWriteLock(); 624 } 625 } catch (InvalidChildException ig_e) { 626 throw new PopulateException( 627 "Invalid child", ig_e); 628 } catch (HarmoniseFactoryException f_e) { 629 throw new PopulateException( 630 "Invalid parent for object",f_e); 631 } catch (DataAccessException e) { 632 throw new PopulateException( 633 "There was a problem populating parent",e); 634 } catch (EditException e) { 635 throw new PopulateException( 636 "There was a problem editing",e); 637 } 638 } 639 } 640 } else { 641 super.populate(xmlElement, state); 642 } 643 } 644 645 648 public static ColumnRef getColumnRef( 649 String sClassname, 650 String sColumn, 651 boolean bHist) 652 throws DataStoreException { 653 ColumnRef returnColRef = null; 654 String sTable = getTableName(sClassname,bHist); 655 656 return AbstractChildObject.getObjectColumnRef(sTable, sColumn); 657 } 658 659 662 public ColumnRef getInstanceColumnRef(String sColumn, boolean bIsHist) 663 throws DataStoreException { 664 String sDBTable = getTableName(bIsHist); 665 666 if(bIsHist == false && (sColumn.equals(CLMN_PATH) == true || sColumn.equals(TAG_PATH) == true)) { 667 throw new InvalidColumnReferenceException("Path only valid for historical objects"); 668 } 669 670 return getObjectColumnRef(sDBTable, sColumn); 671 } 672 673 676 public static ColumnRef getObjectColumnRef(String sTable, String sColumn) 677 throws DataStoreException { 678 ColumnRef returnColRef = null; 679 680 if (sColumn.equals(ATTRIB_DEFAULT) == true 681 || sColumn.equals(CLMN_DEFAULT_LINK) == true) { 682 returnColRef = new ColumnRef(sTable, CLMN_DEFAULT_LINK, ColumnRef.NUMBER); 683 } else if ( 684 sColumn.equals(CLMN_PATH) == true || sColumn.equals(TAG_PATH) == true) { 685 returnColRef = new ColumnRef(sTable, CLMN_PATH, ColumnRef.TEXT); 686 } 687 688 if (returnColRef != null) { 689 return returnColRef; 690 } else { 691 return AbstractEditableObject.getObjectColumnRef(sTable,sColumn); 692 } 693 } 694 695 698 public org.w3c.dom.Element publish( 699 Element topEl, 700 HarmoniseOutput xmlDoc, 701 State state) 702 throws PublishException { 703 Element docEl = null; 704 NodeList nodes = null; 705 Text txt = null; 706 String sTagName = topEl.getTagName(); 707 708 if (sTagName.equals(TAG_GROUP)) { 709 docEl = xmlDoc.createElement(TAG_GROUP); 710 711 NodeList nlChildren = topEl.getChildNodes(); 712 Node nTemp = null; 713 714 for (int j = 0; j < nlChildren.getLength(); j++) { 715 nTemp = nlChildren.item(j); 716 717 if (nTemp.getNodeType() != Node.ELEMENT_NODE) { 718 continue; 719 } else if (nTemp.getNodeName().equals(Template.TAG_TEMPLATE)) { 720 if (m_bIsGroupsPopulated == false) { 721 try { 722 723 populateGroupsFromDatabase(); 724 } catch (PopulateException pop_e) { 725 throw new PublishException( 726 "Error occured populating parents:" 727 + pop_e.getLocalizedMessage()); 728 } 729 } 730 731 if (m_groups.isEmpty() == false) { 732 Template grpTemplate = null; 733 try { 734 grpTemplate = (Template) HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi, Template.class.getName(), Integer.parseInt( 735 ((Element) nTemp).getAttribute(AbstractObject.ATTRIB_ID))); 736 } catch (NumberFormatException e) { 737 throw new PublishException(e); 738 } catch (HarmoniseFactoryException e) { 739 throw new PublishException(e); 740 } 741 int nPageId = -1; 742 743 NodeList pageNodes = 744 ((Element) nTemp).getElementsByTagName(WebPage.TAG_PAGE); 745 746 if (pageNodes.getLength() > 0) { 747 nPageId = 748 Integer.parseInt( 749 ((Element) pageNodes.item(0)).getAttribute( 750 AbstractObject.ATTRIB_ID)); 751 } 752 753 if (grpTemplate == null) { 754 throw new PublishException("No template supplied."); 755 } 756 757 if (topEl.hasAttribute(AbstractChildObject.ATTRIB_SHOWALL)) { 758 try { 759 760 List groups = getParents(); 761 762 for (int i = 0; i < groups.size(); i++) { 763 Publishable pObj = (Publishable) groups.get(i); 764 Element grpEl = pObj.publish(grpTemplate, xmlDoc, state); 765 766 if (((AbstractObject) pObj).getId() == this.m_defaultGroup) { 767 grpEl.setAttribute( 768 AbstractChildObject.ATTRIB_DEFAULT, 769 "true"); 770 } 771 772 if (nPageId > 0) { 773 xmlDoc.addPageIdToLinkNode( 774 state.getLoggedInUser(), 775 grpEl, 776 nPageId); 777 } 778 779 docEl.appendChild(grpEl); 780 } 781 } catch (DataAccessException da_e) { 782 throw new PublishException( 783 "Error occured accessing parents:" 784 + da_e.getLocalizedMessage()); 785 } 786 } else { 787 try { 788 789 Element grpEl = 790 ((Publishable) getRealParent()).publish( 791 grpTemplate, 792 xmlDoc, 793 state); 794 795 if (nPageId > 0) { 796 xmlDoc.addPageIdToLinkNode( 797 state.getLoggedInUser(), 798 grpEl, 799 nPageId); 800 } 801 802 docEl.appendChild(grpEl); 803 } catch (DataAccessException da_e) { 804 throw new PublishException( 805 "Error occured getting parent of object:" 806 + da_e.getLocalizedMessage()); 807 } 808 } 809 } 810 } else if (nTemp.getNodeName().equals(TAG_AVAILABLEOPTIONS)) { 811 Element elAvOpt = xmlDoc.createElement(TAG_AVAILABLEOPTIONS); 812 813 NodeList nlList = 814 ((Element) nTemp).getElementsByTagName(Search.TAG_LIST); 815 816 if (nlList.getLength() > 0) { 817 Element elList = (Element) nlList.item(0); 818 try { 819 820 Publishable list = 821 HarmoniseObjectFactory.instantiatePublishableObject( 822 this.m_dsi, 823 elList, 824 state); 825 826 elAvOpt.appendChild( 827 ((Search) list).publish(elList, xmlDoc, state)); 828 } catch (HarmoniseFactoryException e) { 829 throw new PublishException( 830 "Error instantiating list:" + e.getLocalizedMessage()); 831 } 832 } else { 833 throw new PublishException("No other implementation for available options"); 834 } 835 836 docEl.appendChild(elAvOpt); 837 } 838 } 839 } else if (sTagName.equals(TAG_PATH)) { 840 try { 841 842 docEl = xmlDoc.createElement(TAG_PATH); 843 txt = xmlDoc.createTextNode(getPath()); 844 docEl.appendChild(txt); 845 } catch (DataAccessException da_e) { 846 throw new PublishException( 847 "Error occured getting path:" + da_e.getLocalizedMessage()); 848 } 849 } else { 850 docEl = super.publish(topEl, xmlDoc, state); 852 } 853 854 Element formEl; 856 Element el; 857 858 if (nodes != null) { 859 for (int i = 0; i < nodes.getLength(); i++) { 860 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 861 continue; 862 } 863 864 formEl = (Element) nodes.item(i); 865 el = publish(formEl, xmlDoc, state); 866 867 if (el != null) { 868 try { 869 docEl.appendChild(el); 870 } catch (org.w3c.dom.DOMException e) { 871 throw new PublishException(el.getTagName() + ":" + e.getMessage()); 872 } 873 } 874 } 875 } 876 877 return docEl; 878 } 879 880 883 public boolean isChanged() throws DataAccessException { 884 boolean bReturn = super.isChanged(); 885 886 if (m_bIsGroupsPopulated == false && bReturn == false) { 887 888 try { 889 populateGroupsFromDatabase(); 890 } catch (PopulateException e) { 891 throw new DataAccessException( 892 "Error occured while populating parents:" + e.getLocalizedMessage()); 893 } 894 895 bReturn = (m_groups_to_save.size() > 0); 896 } 897 898 return bReturn; 899 } 900 901 904 905 908 protected void delete(boolean bDeleteHist) 909 throws DataStoreException, DataAccessException, EditException, PopulateException { 910 if (m_bIsGroupsPopulated == false) { 912 populateGroupsFromDatabase(); 913 } 914 915 Iterator iter = m_groups.values().iterator(); 917 918 while (iter.hasNext()) { 919 try { 920 AbstractParentObject parent = (AbstractParentObject) ((CachePointer)iter.next()).getObject(); 921 922 parent.removeChild(this); 923 924 parent.saveNonCoreData(); 925 } catch (CacheException e) { 926 throw new EditException("Error occurred getting object from cache:", e); 927 } 928 } 929 930 super.delete(bDeleteHist); 931 } 932 933 936 protected void saveCoreData() throws EditException { 937 938 updateParents(); 939 940 super.saveCoreData(); 941 } 942 943 946 protected void update() throws EditException, DataStoreException { 947 super.update(); 948 949 updateParents(); 950 951 } 952 953 960 protected void updateParents() throws EditException { 961 if (m_groups_to_save.isEmpty() == false) { 962 Iterator iter = m_groups_to_save.iterator(); 963 964 while (iter.hasNext()) { 965 try { 966 AbstractParentObject parent = (AbstractParentObject)((CachePointer) iter.next()).getObject(); 967 968 parent.saveNonCoreData(); 969 } catch (CacheException e) { 970 throw new EditException("Error occurred getting object from cache:", e); 971 } 972 } 973 974 m_groups_to_save.clear(); 975 } 976 } 977 978 981 protected void fullPopulate() throws PopulateException { 982 if (m_bIsGroupsPopulated == false) { 983 populateGroupsFromDatabase(); 984 } 985 986 super.fullPopulate(); 987 } 988 989 994 protected void populateGroupsFromDatabase() throws PopulateException { 995 if (m_bIsPopulated == false) { 996 populateFromDatabase(); 997 } 998 999 if (this.m_bIsGroupsPopulated == true) { 1000 return; 1001 } 1002 1003 if (m_groups == null) { 1004 m_groups = new HashMap(); 1005 } 1006 1007 if (this.m_nId != AbstractObject.NOTDBSAVED_ID) { 1008 SelectStatement select = new SelectStatement(); 1009 1010 ColumnRef typeColref; 1011 ColumnRef idColref; 1012 ColumnRef defaultColref; 1013 try { 1014 idColref = 1015 AbstractObject.getColumnRef(getParentObjectClassName(), ATTRIB_ID); 1016 defaultColref = getParentChildJoinColumnRef(ATTRIB_DEFAULT); 1017 getInstanceColumnRef(AbstractChildObject.ATTRIB_DEFAULT, false); 1018 typeColref = 1019 AbstractObject.getColumnRef(getParentObjectClassName(), ATTRIB_TYPE); 1020 1021 select.addSelectColumn(idColref); select.addSelectColumn(defaultColref); select.addSelectColumn(typeColref); 1025 Collection coreRefs = 1026 AbstractParentObject.getCoreColumnRefs( 1027 this.getParentObjectClassName()); 1028 Iterator iter = coreRefs.iterator(); 1029 1030 while (iter.hasNext() == true) { 1031 select.addSelectColumn((ColumnRef) iter.next()); 1032 } 1033 1034 ColumnRef joinColumn1 = 1036 AbstractObject.getColumnRef( 1037 getParentObjectClassName(), 1038 AbstractObject.ATTRIB_KEY); 1039 1040 ColumnRef joinColumn2 = getParentChildJoinColumnRef(TAG_GROUP); 1042 select.addJoinCondition(joinColumn1, joinColumn2); 1043 1044 ColumnRef whereColumn = 1045 getParentChildJoinColumnRef(AbstractParentObject.TAG_CHILDREN); 1046 1047 select.addWhereCondition(whereColumn, "=", m_nObjectKey); 1048 } catch (DataStoreException ds_e) { 1049 throw new PopulateException( 1050 "Error occured building query:",ds_e); 1051 } 1052 1053 ResultSet rs = null; 1054 1055 try { 1056 1057 rs = m_dsi.execute(select); 1058 1059 while (rs.next()) { 1060 int nId = rs.getInt(idColref.getColumn()); 1061 1062 AbstractParentObject tmpDSO = 1063 ( 1064 AbstractParentObject) HarmoniseObjectFactory 1065 .instantiatePublishableObject( 1066 this.m_dsi, 1067 rs.getString(typeColref.getColumn()), 1068 nId); 1069 1070 addEditEventListener(tmpDSO); 1071 1072 try { 1073 CachePointer pointer = 1074 CacheHandler.getInstance(m_dsi).getCachePointer(tmpDSO); 1075 1076 if (tmpDSO.isPopulated() == false) { 1077 tmpDSO.populateFromResultSetRow(rs, select); 1078 } 1079 1080 m_groups.put(String.valueOf(nId), pointer); 1081 1082 if (rs.getBoolean(defaultColref.getColumn()) == true) { 1083 this.m_defaultGroup = nId; 1084 } 1085 } catch (CacheException e1) { 1086 throw new PopulateException( 1087 "Had problems getting pointer from cache",e1); 1088 } 1089 } 1090 1091 m_bIsGroupsPopulated = true; 1092 } catch (SQLException e) { 1093 throw new PopulateException( 1094 "Error occured processing query",e); 1095 } catch (HarmoniseFactoryException e) { 1096 throw new PopulateException( 1097 "Error instantiatiing new parent object",e); 1098 } catch (DataStoreException e) { 1099 throw new PopulateException( 1100 "Error occured processing query",e); 1101 } finally { 1102 if (rs != null) { 1103 try { 1104 rs.close(); 1105 } catch (SQLException sql_e) { 1106 throw new PopulateException( 1107 "Error occured closing result set",sql_e); 1108 } 1109 } 1110 } 1111 } 1112 } 1113 1114 1121 protected AbstractParentObject createGroupFromPath(String sFullPath) 1122 throws EditException { 1123 AbstractParentObject group = null; 1124 ResultSet rs = null; 1125 1126 String sGroupName = sFullPath.substring(sFullPath.lastIndexOf('/') + 1); 1127 String sPath = sFullPath.substring(0, sFullPath.lastIndexOf('/')); 1128 1129 try { 1131 group = 1132 (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject( 1133 m_dsi, 1134 this.getParentObjectClassName(), 1135 sFullPath); 1136 } catch (HarmoniseFactoryException e) { 1137 throw new EditException( 1138 "Error occured getting object from factory",e); 1139 } 1140 1141 if (group == null) { 1143 Class groupClass; 1144 try { 1145 groupClass = Class.forName(getParentObjectClassName()); 1147 Class [] paramTypesHistory = 1148 { AbstractDataStoreInterface.class, boolean.class }; 1149 java.lang.reflect.Constructor ctorHistory = 1150 groupClass.getConstructor(paramTypesHistory); 1151 Object [] paramsHistory = { m_dsi, new Boolean (true)}; 1152 AbstractParentObject archivedGroupObject = 1153 (AbstractParentObject) ctorHistory.newInstance(paramsHistory); 1154 1155 Class [] paramTypes = 1157 { AbstractDataStoreInterface.class, int.class, boolean.class }; 1158 java.lang.reflect.Constructor ctorExists = 1159 groupClass.getConstructor(paramTypes); 1160 1161 SelectStatement select = new SelectStatement(); 1163 select.addSelectColumn( 1164 archivedGroupObject.getInstanceColumnRef( 1165 AbstractObject.ATTRIB_ID, 1166 false)); 1167 select.addSelectColumn( 1168 archivedGroupObject.getInstanceColumnRef( 1169 AbstractObject.ATTRIB_KEY, 1170 false)); 1171 select.addWhereCondition( 1172 archivedGroupObject.getInstanceColumnRef(CLMN_PATH, false), 1173 "=", 1174 sPath); 1175 select.addWhereCondition( 1176 archivedGroupObject.getInstanceColumnRef(TAG_NAME, false), 1177 "=", 1178 sGroupName); 1179 1180 rs = m_dsi.execute(select); 1181 1182 if (rs.next()) { 1183 Object [] params = 1184 { m_dsi, new Integer (rs.getInt(1)), new Boolean (true)}; 1185 group = (AbstractParentObject) ctorExists.newInstance(params); 1186 group.setKey(rs.getInt(2)); 1187 1188 AbstractChildObject reactivated = 1189 (AbstractChildObject) group.reactivate(); 1190 1191 AbstractParentObject parent = createGroupFromPath(sPath); 1192 1193 parent.acquireEditWriteLock(); 1194 try { 1195 parent.addChild(reactivated); 1196 1197 parent.saveNonCoreData(); 1198 } finally { 1199 parent.releaseEditWriteLock(); 1200 } 1201 } 1202 1203 rs.close(); 1204 } catch (SecurityException e1) { 1205 throw new EditException( 1206 "Security exception",e1); 1207 } catch (IllegalArgumentException e1) { 1208 throw new EditException( 1209 "IllegalArgumentException",e1); 1210 } catch (InvalidChildException e1) { 1211 throw new EditException( 1212 "InvalidGroupException",e1); 1213 } catch (PopulateException e1) { 1214 throw new EditException( 1215 "PopulateException",e1); 1216 } catch (ClassNotFoundException e1) { 1217 throw new EditException( 1218 "ClassNotFoundException",e1); 1219 } catch (NoSuchMethodException e1) { 1220 throw new EditException( 1221 "NoSuchMethodException",e1); 1222 } catch (InstantiationException e1) { 1223 throw new EditException( 1224 "InstantiationException",e1); 1225 } catch (IllegalAccessException e1) { 1226 throw new EditException( 1227 "IllegalAccessException",e1); 1228 } catch (InvocationTargetException e1) { 1229 throw new EditException( 1230 "InvocationTargetException",e1); 1231 } catch (DataStoreException e1) { 1232 throw new EditException( 1233 "DataStoreException",e1); 1234 } catch (SQLException e1) { 1235 throw new EditException("SQLException",e1); 1236 } 1237 1238 if (group == null) { 1241 try { 1242 Class [] dsiParamTypes = { AbstractDataStoreInterface.class }; 1243 java.lang.reflect.Constructor ctorNew = 1244 groupClass.getConstructor(dsiParamTypes); 1245 Object [] params = { m_dsi }; 1246 group = (AbstractParentObject) ctorNew.newInstance(params); 1247 1248 group.setName(sGroupName); 1249 1250 group.save(); 1252 1253 AbstractParentObject parent = createGroupFromPath(sPath); 1254 1255 parent.acquireEditWriteLock(); 1256 try { 1257 parent.addChild(group); 1258 1259 parent.save(); 1260 } finally { 1261 parent.releaseEditWriteLock(); 1262 } 1263 1264 group.changeStatus(Status.APPROVED); 1265 } catch (SecurityException e2) { 1266 throw new EditException( 1267 "Security exception:" + e2.getLocalizedMessage()); 1268 } catch (IllegalArgumentException e2) { 1269 throw new EditException( 1270 "IllegalArgumentException:" + e2.getLocalizedMessage()); 1271 } catch (InvalidChildException e2) { 1272 throw new EditException( 1273 "InvalidGroupException:" + e2.getLocalizedMessage()); 1274 } catch (PopulateException e2) { 1275 throw new EditException( 1276 "PopulateException:" + e2.getLocalizedMessage()); 1277 } catch (NoSuchMethodException e2) { 1278 throw new EditException( 1279 "NoSuchMethodException:" + e2.getLocalizedMessage()); 1280 } catch (InstantiationException e2) { 1281 throw new EditException( 1282 "InstantiationException:" + e2.getLocalizedMessage()); 1283 } catch (IllegalAccessException e2) { 1284 throw new EditException( 1285 "IllegalAccessException:" + e2.getLocalizedMessage()); 1286 } catch (InvocationTargetException e2) { 1287 throw new EditException( 1288 "InvocationTargetException:" + e2.getLocalizedMessage()); 1289 } catch (InvalidNameException e) { 1290 throw new EditException( 1291 "InvocationTargetException:" + e.getLocalizedMessage()); 1292 } 1293 } 1294 } 1295 1296 return group; 1297 } 1298 1299 1302 protected void addDataToSave(InsertStatement insert) 1303 throws DataStoreException { 1304 1305 if (isHistorical() == true) { 1306 try { 1307 insert.addColumnValue( 1308 this.getInstanceColumnRef(TAG_PATH, true), 1309 getPath()); 1310 } catch (DataAccessException e) { 1311 throw new DataStoreException( 1312 "Error occured getting path",e); 1313 } 1314 } 1315 1316 super.addDataToSave(insert); 1317 } 1318 1319 1329 protected ColumnRef getParentChildJoinColumnRef(String sCol) 1330 throws DataStoreException { 1331 ColumnRef colref = null; 1332 1333 String sGroupTableName = null; 1334 1335 sGroupTableName = 1336 DatabaseInfo.getInstance().getTableName(getParentObjectClassName()); 1337 1338 StringBuffer sbuf = 1339 new StringBuffer (sGroupTableName).append(JOIN_TO).append( 1340 getDBTableName()); 1341 1342 String sTable = sbuf.toString(); 1343 1344 if (sCol.equals(CLMN_CHILD_KEY) == true 1345 || sCol.equals(AbstractParentObject.TAG_CHILDREN)) { 1346 colref = new ColumnRef(sTable, CLMN_CHILD_KEY, ColumnRef.NUMBER); 1347 } else if ( 1348 sCol.equals(CLMN_PARENT_KEY) == true || sCol.equals(TAG_GROUP) == true) { 1349 colref = new ColumnRef(sTable, CLMN_PARENT_KEY, ColumnRef.NUMBER); 1350 } else if (sCol.equals(CLMN_DATE) == true) { 1351 colref = new ColumnRef(sTable, CLMN_DATE, ColumnRef.DATE); 1352 } else if ( 1353 sCol.equals(CLMN_DEFAULT_LINK) == true 1354 || sCol.equals(ATTRIB_DEFAULT) == true) { 1355 colref = new ColumnRef(sTable, CLMN_DEFAULT_LINK, ColumnRef.NUMBER); 1356 } 1357 1358 if (colref == null) { 1359 throw new InvalidColumnReferenceException(); 1360 } 1361 1362 return colref; 1363 } 1364 1365 1372 protected void addParent(AbstractParentObject group) 1373 throws PopulateException { 1374 if (this.m_bIsGroupsPopulated == false) { 1375 populateGroupsFromDatabase(); 1376 } 1377 1378 try { 1379 CachePointer ptr = CacheHandler.getInstance(m_dsi).getCachePointer(group); 1380 1381 1382 if (m_groups.containsValue(group) == false) { 1383 m_groups.put(String.valueOf(group.getId()), ptr); 1384 } 1385 } catch (CacheException e) { 1386 throw new PopulateException("cache pointer problem",e); 1387 } 1388 } 1389 1390 1397 protected void removeParent(AbstractParentObject group) 1398 throws PopulateException { 1399 if (this.m_bIsGroupsPopulated == false) { 1400 this.populateGroupsFromDatabase(); 1401 } 1402 1403 String sKey = String.valueOf(group.getId()); 1404 1405 if (m_groups.containsKey(sKey) == true) { 1406 m_groups.remove(m_groups.get(sKey)); 1407 } 1408 } 1409 1410 1415 abstract public String getParentObjectClassName(); 1416 1417 1425 protected void setRealParent(AbstractParentObject group) 1426 throws PopulateException { 1427 1428 int nId = group.getId(); 1429 String sId = String.valueOf(nId); 1430 1431 addParent(group); 1432 1433 m_defaultGroup = nId; 1434 } 1435 1436 1439 public Editable changeStatus(Status status) throws EditException { 1440 1441 Editable result = null; 1442 1443 try { 1444 if(getStatus() == Status.UNAPPROVED && status == Status.APPROVED) { 1445 AbstractChildObject live = (AbstractChildObject) this.getLiveVersion(); 1446 1447 if(live != null) { 1448 List parents = live.getParents(); 1449 AbstractParentObject realParent = live.getRealParent(); 1450 1451 result = super.changeStatus(status); 1452 1453 Iterator iter = parents.iterator(); 1454 1455 while (iter.hasNext()) { 1456 AbstractParentObject tmpParent = (AbstractParentObject) iter.next(); 1457 int nPos = tmpParent.indexOf(live); 1458 1459 tmpParent.acquireEditWriteLock(); 1460 try { 1461 if(tmpParent.equals(realParent) == true) { 1462 tmpParent.addChild(nPos,this); 1463 } else { 1464 tmpParent.addChild(nPos,this, true); 1465 } 1466 1467 tmpParent.saveNonCoreData(); 1468 } finally { 1469 tmpParent.releaseEditWriteLock(); 1470 } 1471 } 1472 } else { 1473 result = super.changeStatus(status); 1474 } 1475 1476 } else { 1477 result = super.changeStatus(status); 1478 } 1479 } catch (DataAccessException e) { 1480 throw new EditException(e.getLocalizedMessage(),e); 1481 } catch (InvalidChildException e) { 1482 throw new EditException(e.getLocalizedMessage(),e); 1483 } catch (PopulateException e) { 1484 throw new EditException(e.getLocalizedMessage(),e); 1485 } 1486 1487 return result; 1488 } 1489 1490 1493 public Editable archive() throws EditException { 1494 try { 1495 AbstractParentObject parent = getRealParent(); 1496 1497 if(parent != null && parent.getArchivedChildByName(m_sName) != null) { 1498 1499 Date now = new Date (); 1500 SimpleDateFormat format = new SimpleDateFormat (ARCHIVE_TIMESTAMP_FORMAT); 1501 1502 StringBuffer sbuf = new StringBuffer (); 1503 1504 sbuf.append(m_sName).append("_").append(format.format(now)); 1505 1506 m_sName = sbuf.toString(); 1507 } 1508 1509 m_sPath = getPath(); 1510 } catch (DataAccessException e) { 1511 throw new EditException(e.getLocalizedMessage(),e); 1512 } 1513 1514 return super.archive(); 1515 } 1516 1517 1520 protected void addColumnsToPopulateQuery( 1521 SelectStatement select, 1522 boolean bIsHist) 1523 throws DataStoreException { 1524 if(isHistorical() == true) { 1525 select.addSelectColumn(getInstanceColumnRef(TAG_PATH, true)); 1526 } 1527 super.addColumnsToPopulateQuery(select, bIsHist); 1528 } 1529 1530 1533 protected void populateFromResultSetRow( 1534 ResultSet rs, 1535 SelectStatement select) 1536 throws PopulateException { 1537 if(isHistorical() == true) { 1538 try { 1539 ColumnRef pathRef = getInstanceColumnRef(TAG_PATH, true); 1540 1541 if(select.containsSelectColumn(pathRef) == true) { 1542 m_sPath = rs.getString(select.getResultSetIndex(pathRef)); 1543 } 1544 } catch (DataStoreException e) { 1545 throw new PopulateException(e.getLocalizedMessage(),e); 1546 } catch (SQLException e) { 1547 throw new PopulateException(e.getLocalizedMessage(),e); 1548 } 1549 } 1550 super.populateFromResultSetRow(rs, select); 1551 } 1552 1553 1556 public Editable reactivate() throws EditException { 1557 1558 AbstractParentObject parent = null; 1559 1560 try { 1561 if(getLiveVersion() == null) { 1562 parent = getRealParent(); 1563 } 1564 } catch (DataAccessException e) { 1565 throw new EditException(e.getLocalizedMessage(),e); 1566 } 1567 1568 AbstractChildObject child = (AbstractChildObject) super.reactivate(); 1569 1570 try { 1571 if(parent != null) { 1572 parent.acquireEditWriteLock(); 1573 try { 1574 parent.addChild(child); 1575 parent.saveNonCoreData(); 1576 } finally { 1577 parent.releaseEditWriteLock(); 1578 } 1579 } 1580 } catch (InvalidChildException e) { 1581 throw new EditException(e.getLocalizedMessage(),e); 1582 } catch (PopulateException e) { 1583 throw new EditException(e.getLocalizedMessage(),e); 1584 } 1585 1586 return child; 1587 } 1588 1589 1597 static public String getParentObjectClassName(String sChildClassname) throws DataAccessException { 1598 1599 String sParentClassName = (String ) m_child_parent_mappings.get(sChildClassname); 1600 1601 try { 1602 if(sParentClassName == null) { 1603 Class clss = Class.forName(sChildClassname); 1604 1605 if(AbstractChildObject.class.isAssignableFrom(clss)) { 1606 1607 AbstractChildObject child = 1608 (AbstractChildObject)clss.newInstance(); 1609 1610 sParentClassName = child.getParentObjectClassName(); 1611 m_child_parent_mappings.put(sChildClassname, sParentClassName); 1612 } 1613 } 1614 } catch (InstantiationException e) { 1615 throw new DataAccessException(e); 1616 } catch (IllegalAccessException e) { 1617 throw new DataAccessException(e); 1618 } catch (ClassNotFoundException e) { 1619 throw new DataAccessException(e); 1620 } 1621 1622 return sParentClassName; 1623 } 1624 1625} | Popular Tags |