1 19 package org.openharmonise.rm.commands; 20 21 import org.openharmonise.commons.cache.CacheException; 22 import org.openharmonise.rm.*; 23 import org.openharmonise.rm.factory.CacheHandler; 24 import org.openharmonise.rm.publishing.State; 25 import org.openharmonise.rm.publishing.StateException; 26 import org.openharmonise.rm.resources.*; 27 import org.openharmonise.rm.resources.lifecycle.EditException; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NodeList ; 30 31 32 40 public class CmdMove extends AbstractCmd { 41 42 45 static final String PARAM_NEWGROUP = "new_group"; 46 47 50 static final String PARAM_STATEGROUP = "state_group"; 51 52 56 public CmdMove() { 57 super(); 58 } 59 60 63 public Object execute(Context context) throws CommandException { 64 65 if ((m_commandObj instanceof AbstractChildObject) == false) { 66 throw new InvalidCommandException( 67 "Command is not valid for this object:" 68 + m_commandObj.getClass()); 69 } 70 71 if (isAvailable(context) == false) { 72 throw new InvalidCommandException("Command is not available for this object"); 73 } 74 75 AbstractChildObject obj = (AbstractChildObject) getCommandObject(context); 76 77 try { 78 AbstractParentObject groupCurrent = obj.getRealParent(); 79 String sGroupName = groupCurrent.getClass().getName(); 80 81 String sGroupStateId = this.getParameter(PARAM_STATEGROUP); 82 83 String sGroupIdentifier = null; 84 85 if(sGroupStateId != null && sGroupStateId.length()>0){ 86 87 String sGroupTagName = groupCurrent.getTagName(); 88 NodeList nodes = getState().getElementsByTagName(sGroupTagName); 89 90 for(int i=0;i<nodes.getLength();i++){ 91 Element el = (Element )nodes.item(i); 92 String sStateId = el.getAttribute(State.ATTRIB_STATE_ID); 93 if(sGroupStateId.equals(sStateId)){ 94 sGroupIdentifier = el.getAttribute(AbstractChildObject.ATTRIB_ID); 95 break; 96 } 97 } 98 } else { 99 sGroupIdentifier = this.getParameter(PARAM_NEWGROUP); 100 } 101 102 int nId = 0; 103 try { 104 nId = Integer.parseInt(sGroupIdentifier); 105 } catch (NumberFormatException e) { 106 nId = 0; 107 } 108 109 AbstractParentObject groupNew = null; 110 if (nId != 0) { 111 groupNew = 112 (AbstractParentObject) CacheHandler 113 .getInstance(getDataStoreInteface()) 114 .getObject(sGroupName, nId); 115 } else { 116 if (sGroupIdentifier.startsWith(".")) { 117 String sObjName = obj.getClass().getName(); 118 sObjName = 119 sObjName.substring(sObjName.lastIndexOf('.') + 1); 120 sGroupIdentifier = 121 getState().resolveRelativePath( 122 sObjName, 123 sGroupIdentifier); 124 } 125 groupNew = 126 (AbstractParentObject) CacheHandler 127 .getInstance(getDataStoreInteface()) 128 .getObjectFromPath(sGroupName, sGroupIdentifier); 129 } 130 131 groupCurrent.acquireEditWriteLock(); 132 try { 133 groupCurrent.removeChild(obj); 134 groupCurrent.save(); 135 } finally { 136 groupCurrent.releaseEditWriteLock(); 137 } 138 139 groupNew.acquireEditWriteLock(); 140 try { 141 groupNew.addChild(obj, false); 142 groupNew.save(); 143 } finally { 144 groupNew.releaseEditWriteLock(); 145 } 146 147 } catch (DataAccessException e) { 148 throw new CommandExecutionException("Data access exception", e); 149 } catch (PopulateException e) { 150 throw new CommandExecutionException("Populate exception", e); 151 } catch (InvalidChildException e) { 152 throw new CommandExecutionException("Invalid child exception", e); 153 } catch (CacheException e) { 154 throw new CommandExecutionException("Cache exception", e); 155 } catch (StateException e) { 156 throw new CommandExecutionException("State exception", e); 157 } catch (EditException e) { 158 throw new CommandExecutionException(e); 159 } 160 161 return obj; 162 } 163 164 167 public String getName() { 168 return "Move"; 169 } 170 171 174 public boolean isValidCommandObject(Object obj) { 175 return (obj instanceof AbstractChildObject); 176 } 177 178 } | Popular Tags |