001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.commands;
020:
021: import org.openharmonise.commons.cache.CacheException;
022: import org.openharmonise.rm.*;
023: import org.openharmonise.rm.factory.CacheHandler;
024: import org.openharmonise.rm.publishing.State;
025: import org.openharmonise.rm.publishing.StateException;
026: import org.openharmonise.rm.resources.*;
027: import org.openharmonise.rm.resources.lifecycle.EditException;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030:
031: /**
032: * Moves the command object to a new location, if it is an
033: * instance of <code>AbstractChildObect</code>.
034: *
035: * @author Michael Bell
036: * @version $Revision: 1.6 $
037: *
038: */
039: public class CmdMove extends AbstractCmd {
040:
041: /**
042: * Parameter name for the new parent object.
043: */
044: static final String PARAM_NEWGROUP = "new_group";
045:
046: /**
047: * Parameter name for the stateId of the new parent object in the state.
048: */
049: static final String PARAM_STATEGROUP = "state_group";
050:
051: /**
052: * Creates a new instance of the command.
053: *
054: */
055: public CmdMove() {
056: super ();
057: }
058:
059: /* (non-Javadoc)
060: * @see org.openharmonise.rm.commands.AbstractCmd#execute()
061: */
062: public Object execute(Context context) throws CommandException {
063:
064: if ((m_commandObj instanceof AbstractChildObject) == false) {
065: throw new InvalidCommandException(
066: "Command is not valid for this object:"
067: + m_commandObj.getClass());
068: }
069:
070: if (isAvailable(context) == false) {
071: throw new InvalidCommandException(
072: "Command is not available for this object");
073: }
074:
075: AbstractChildObject obj = (AbstractChildObject) getCommandObject(context);
076:
077: try {
078: AbstractParentObject groupCurrent = obj.getRealParent();
079: String sGroupName = groupCurrent.getClass().getName();
080:
081: String sGroupStateId = this .getParameter(PARAM_STATEGROUP);
082:
083: String sGroupIdentifier = null;
084:
085: if (sGroupStateId != null && sGroupStateId.length() > 0) {
086:
087: String sGroupTagName = groupCurrent.getTagName();
088: NodeList nodes = getState().getElementsByTagName(
089: sGroupTagName);
090:
091: for (int i = 0; i < nodes.getLength(); i++) {
092: Element el = (Element) nodes.item(i);
093: String sStateId = el
094: .getAttribute(State.ATTRIB_STATE_ID);
095: if (sGroupStateId.equals(sStateId)) {
096: sGroupIdentifier = el
097: .getAttribute(AbstractChildObject.ATTRIB_ID);
098: break;
099: }
100: }
101: } else {
102: sGroupIdentifier = this .getParameter(PARAM_NEWGROUP);
103: }
104:
105: int nId = 0;
106: try {
107: nId = Integer.parseInt(sGroupIdentifier);
108: } catch (NumberFormatException e) {
109: nId = 0;
110: }
111:
112: AbstractParentObject groupNew = null;
113: if (nId != 0) {
114: groupNew = (AbstractParentObject) CacheHandler
115: .getInstance(getDataStoreInteface()).getObject(
116: sGroupName, nId);
117: } else {
118: if (sGroupIdentifier.startsWith(".")) {
119: String sObjName = obj.getClass().getName();
120: sObjName = sObjName.substring(sObjName
121: .lastIndexOf('.') + 1);
122: sGroupIdentifier = getState().resolveRelativePath(
123: sObjName, sGroupIdentifier);
124: }
125: groupNew = (AbstractParentObject) CacheHandler
126: .getInstance(getDataStoreInteface())
127: .getObjectFromPath(sGroupName, sGroupIdentifier);
128: }
129:
130: groupCurrent.acquireEditWriteLock();
131: try {
132: groupCurrent.removeChild(obj);
133: groupCurrent.save();
134: } finally {
135: groupCurrent.releaseEditWriteLock();
136: }
137:
138: groupNew.acquireEditWriteLock();
139: try {
140: groupNew.addChild(obj, false);
141: groupNew.save();
142: } finally {
143: groupNew.releaseEditWriteLock();
144: }
145:
146: } catch (DataAccessException e) {
147: throw new CommandExecutionException(
148: "Data access exception", e);
149: } catch (PopulateException e) {
150: throw new CommandExecutionException("Populate exception", e);
151: } catch (InvalidChildException e) {
152: throw new CommandExecutionException(
153: "Invalid child exception", e);
154: } catch (CacheException e) {
155: throw new CommandExecutionException("Cache exception", e);
156: } catch (StateException e) {
157: throw new CommandExecutionException("State exception", e);
158: } catch (EditException e) {
159: throw new CommandExecutionException(e);
160: }
161:
162: return obj;
163: }
164:
165: /* (non-Javadoc)
166: * @see org.openharmonise.rm.commands.AbstractCmd#getName()
167: */
168: public String getName() {
169: return "Move";
170: }
171:
172: /* (non-Javadoc)
173: * @see org.openharmonise.rm.commands.AbstractCmd#isValidCommandObject(java.lang.Object)
174: */
175: public boolean isValidCommandObject(Object obj) {
176: return (obj instanceof AbstractChildObject);
177: }
178:
179: }
|