📄 copytree_engine.java
字号:
JahiaData jData = (JahiaData) engineMap.get("jData"); Integer maxLevels = (Integer) engineMap.get("maxLevels"); if(maxLevels == null) { toDebug(">> CopyTree_Engine :: executeCopy - 3"); errorMsg = "Levels to copy must be a number"; engineMap.put("errorMsg", errorMsg); return false; } else { engineMap.put("errorMsg", null); } // clone the pages // start with the toplevel page int newSourceParentID = targetPage.getID(); int newSourceParentAclID = targetPage.getAclID(); toDebug(">> CopyTree_Engine :: executeCopy - 6"); JahiaPage clonedPage = pSrv.clonePage( newSourceParentID, newSourceParentAclID, sourcePage, jParams, (maxLevels.intValue() > 1) ); clonedPage.setTitle("Copy of " + sourcePage.getTitle()); toDebug(">> CopyTree_Engine :: executeCopy - set cloned page title to ["+clonedPage.getTitle()+"]"); clonedPage.commitChanges(true); int newParentID = clonedPage.getID(); int newParentAclID = clonedPage.getAclID(); // store the ID of the page to move at the end of the operation engineMap.put("sourcePageTitle", clonedPage.getTitle()); engineMap.put("movedPageID", new Integer(newParentID)); //engineMap.put("sourcePageID", new Integer(newParentID)); Hashtable tslMap = new Hashtable(); Hashtable revTslMap = new Hashtable(); tslMap.put ( new Integer(newParentID), new Integer(sourcePage.getID()) ); revTslMap.put( new Integer(sourcePage.getID()), new Integer(newParentID) ); // clone its children recursively cloneChildren(maxLevels.intValue(), sourcePage, clonedPage, jParams, tslMap, revTslMap); Enumeration keys = tslMap.keys(); while(keys.hasMoreElements()) { Integer pageID = (Integer) keys.nextElement(); updateLinks(jParams, pageID.intValue(), revTslMap); // continue coding here for recursive copy... // continue coding here for recursive copy... // continue coding here for recursive copy... } return true; } // end executeCopy //--------------------------------------------------------------------------------------------- private void updateLinks(ParamBean jParams, int pageID, Hashtable revTslMap) throws JahiaException { // update all the page links pointing to a page inside the copied subtree Vector links = fldSrv.getPagefieldIDsInPage(pageID);System.out.println("CopyTree_Engine :: updateLinks - updating links in page ["+pageID+"]"); JahiaPage page = null; JahiaField field = null; int fieldID; int newLinkID; for (int i=0; i<links.size(); i++) { fieldID = ((Integer)links.get(i)).intValue(); field = fldSrv.loadField(fieldID, jParams); page = (JahiaPage)field.getObject();//////////////////////////////////////////////////////////////////////////////////// TODO : why is page NULL ?//////////////////////////////////////////////////////////////////////////////// if(page != null) { if (revTslMap.get(new Integer(page.getID())) != null) { newLinkID = ((Integer)revTslMap.get(new Integer(page.getID()))).intValue();System.out.println("CopyTree_Engine :: updateLinks - replacing link to ["+page.getID()+"] with link to ["+newLinkID+"]"); page = pSrv.lookupPage(newLinkID); field.setValue(Integer.toString(newLinkID)); field.setObject(page); fldSrv.saveField( field, 0, jParams ); } else if (page.getPageType() == JahiaPage.TYPE_DIRECT) {System.out.println("CopyTree_Engine :: updateLinks - deleting field with ID["+field.getID()+"]"); fldSrv.deleteField( field.getID(), jParams ); } } } } /*** * recursively clone all the pages in the subtree of a given page, until no levels are to be cloned. * * @param level countdown * @param origParentPage * @param clonedParentPAGe * @param jParams the ParamBean object * @param tslMap a <code>Hashtable</code> associating the ID of each page in the source subtree, * with the ID of its corresponding page copy * @param revTslMap a <code>Hashtable</code> with the reverse association as tslMap */ private void cloneChildren(int level, JahiaPage origParentPage, JahiaPage clonedParentPage, ParamBean jParams, Hashtable tslMap, Hashtable revTslMap ) throws JahiaException { if(level != 0) { Vector out = new Vector(); int newParentID = clonedParentPage.getID(); int newParentAclID = clonedParentPage.getAclID(); Vector children = pSrv.getPageChilds(origParentPage.getID(), PageLoadFlags.DIRECT, jParams); Enumeration childrenEnum = children.elements(); level--; while(childrenEnum.hasMoreElements()) { JahiaPage childPage = (JahiaPage) childrenEnum.nextElement(); JahiaPage clonedChildPage = pSrv.clonePage( newParentID, newParentAclID, childPage, jParams, (level > 1) ); clonedChildPage.setTitle(childPage.getTitle()); clonedChildPage.commitChanges(true); tslMap.put ( new Integer(clonedChildPage.getID()), new Integer(childPage.getID()) ); revTslMap.put( new Integer(childPage.getID()), new Integer(clonedChildPage.getID()) ); cloneChildren(level, childPage, clonedChildPage, jParams, tslMap, revTslMap); } } } // end cloneChildren /*** * recursive utility method to construct a Vector suitable for JSP processing into a tree view, * based on a double-column list of pageID/parentPageID * * @param treeList A Vector of Integer[] { int pageID, int parentPageID, int originalPageID } * representing the site structure after the proposed modifications. * @param originPageID The ID of the page where to start recursion from. * @param tmpTree A partially built output Vector; each loop appends a new line. * The initial call to this method should pass null object. * @param theSpacer Partially constructed spacer Vector. * @param engineMap a HashMap populated with the engine context * @param operation identifies the operation (move or copy) * @param theUser a reference to the user performing the operation * */ private void makeTree( Vector treeList, int originPageID, Vector tmpTree, Vector theSpacer, HashMap engineMap, String operation, JahiaUser theUser ) throws JahiaException { Boolean thePlug; Boolean theBoolean; Boolean isCopy = new Boolean(false); String theTitle; String origTitle; Vector nextSpacer; Vector treeVect = (tmpTree != null) ? tmpTree : new Vector(); HashMap tslMap = new HashMap(); HashMap revTslMap = new HashMap(); Integer targetPageIDObj = (Integer) engineMap.get("targetPageID"); int targetPageID = targetPageIDObj.intValue(); if(operation == COPY_OPERATION) { tslMap = (HashMap) engineMap.get("tslMap"); revTslMap = (HashMap) engineMap.get("revTslMap"); } Enumeration childrenEnum = simpleGetChildren( treeList, originPageID ); while (childrenEnum.hasMoreElements()) { Integer childPageID = (Integer) childrenEnum.nextElement(); if (childPageID != null) { isCopy = new Boolean(false); if(operation == COPY_OPERATION) { try { // separate new pages from existing pages if( tslMap.containsKey(childPageID) ) { Integer origPageID = ((Integer) tslMap.get(childPageID)); origTitle = getPageTitle(origPageID); theTitle = "Copy of " + origTitle; isCopy = new Boolean(true); } else { theTitle = getPageTitle(childPageID); } } catch(Exception e) { theTitle = "new page"; } } else { theTitle = getPageTitle(childPageID); } thePlug = new Boolean(childrenEnum.hasMoreElements()); HashMap lineMap = new HashMap(); if(!isCopy.booleanValue()) { JahiaPage childPage = pSrv.lookupPage(childPageID.intValue()); boolean isInTargetPath = isPageInPath( childPageID.intValue(), targetPageID ); boolean isTargetChild = (childPage.getParentID() == targetPageID); lineMap.put("isTargetChild", new Boolean(isTargetChild)); lineMap.put("isInTargetPath", new Boolean(isInTargetPath)); lineMap.put("hasWriteAccess", new Boolean(childPage.checkWriteAccess(theUser))); lineMap.put("hasReadAccess", new Boolean(childPage.checkReadAccess(theUser))); } lineMap.put("theSpacer", theSpacer); lineMap.put("thePlug", thePlug); lineMap.put("pageID", childPageID); lineMap.put("theTitle", theTitle); lineMap.put("isCopy", isCopy); treeVect.add(lineMap); theBoolean = new Boolean(childrenEnum.hasMoreElements()); nextSpacer = (Vector) theSpacer.clone(); nextSpacer.addElement(theBoolean); makeTree( treeList, childPageID.intValue(), treeVect, nextSpacer, engineMap, operation, theUser ); } } engineMap.put("tmpTreeData", treeVect); } // end makeTree /*** * given a list of ID/parentID pairs, get an Enumeration of all the children of a given parentID. * * @param pageList a Vector of int[2] { int ID, int parentID } * @param pageID the ID of which we want to get all children * */ private Enumeration simpleGetChildren( Vector pageList, int pageID ) { Vector vect = new Vector(); if(pageList != null) { Enumeration pageListEnum = pageList.elements(); while(pageListEnum.hasMoreElements()) { int[] i = (int[]) pageListEnum.nextElement(); if(i[1] == pageID) { vect.add(new Integer(i[0])); } } } return vect.elements(); } // end simpleGetChildren /*** * Check if destPage is in the path of srcPage * * @param destPageID check if this page is in the path * @param srcPageID reference page at the end of the path * */ private boolean isPageInPath( int destPageID, int srcPageID ) throws JahiaException { JahiaPage srcPage = pSrv.lookupPage(srcPageID); Enumeration thePath = srcPage.getPagePath(); while (thePath.hasMoreElements()) { JahiaPage aPage = (JahiaPage) thePath.nextElement(); if (aPage.getID() == destPageID) { return true; } } return false; } // end isPageInPath /*** * Get the number of parenting levels between destPage and srcPage * * @param destPageID some page ID * @param srcPageID some other page ID * @return the number of parenting levels, or -1 if not in path * */ private int getLevelsBetween( int destPageID, int srcPageID ) throws JahiaException { int levels = -1; JahiaPage srcPage = pSrv.lookupPage(srcPageID); Enumeration thePath = srcPage.getPagePath(); while (thePath.hasMoreElements()) { JahiaPage aPage = (JahiaPage) thePath.nextElement(); if (aPage.getID() == destPageID) { levels = 0; } if(levels >= 0) { levels++; if(aPage.getID() == srcPageID) { return levels; } } } return -1; } // end getLevelsBetween
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -