KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > commands > CmdMove


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

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 JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31
32 /**
33  * Moves the command object to a new location, if it is an
34  * instance of <code>AbstractChildObect</code>.
35  *
36  * @author Michael Bell
37  * @version $Revision: 1.6 $
38  *
39  */

40 public class CmdMove extends AbstractCmd {
41     
42     /**
43      * Parameter name for the new parent object.
44      */

45     static final String JavaDoc PARAM_NEWGROUP = "new_group";
46     
47     /**
48      * Parameter name for the stateId of the new parent object in the state.
49      */

50     static final String JavaDoc PARAM_STATEGROUP = "state_group";
51
52     /**
53      * Creates a new instance of the command.
54      *
55      */

56     public CmdMove() {
57         super();
58     }
59
60     /* (non-Javadoc)
61      * @see org.openharmonise.rm.commands.AbstractCmd#execute()
62      */

63     public Object JavaDoc 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 JavaDoc sGroupName = groupCurrent.getClass().getName();
80             
81             String JavaDoc sGroupStateId = this.getParameter(PARAM_STATEGROUP);
82             
83             String JavaDoc sGroupIdentifier = null;
84             
85             if(sGroupStateId != null && sGroupStateId.length()>0){
86                 
87                 String JavaDoc sGroupTagName = groupCurrent.getTagName();
88                 NodeList JavaDoc nodes = getState().getElementsByTagName(sGroupTagName);
89                 
90                 for(int i=0;i<nodes.getLength();i++){
91                     Element JavaDoc el = (Element JavaDoc)nodes.item(i);
92                     String JavaDoc 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 JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
165      * @see org.openharmonise.rm.commands.AbstractCmd#getName()
166      */

167     public String JavaDoc getName() {
168         return "Move";
169     }
170
171     /* (non-Javadoc)
172      * @see org.openharmonise.rm.commands.AbstractCmd#isValidCommandObject(java.lang.Object)
173      */

174     public boolean isValidCommandObject(Object JavaDoc obj) {
175         return (obj instanceof AbstractChildObject);
176     }
177
178 }
Popular Tags