KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > utils > CommandWrapper


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
20 package org.openharmonise.dav.server.utils;
21
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
26 import org.openharmonise.rm.DataAccessException;
27 import org.openharmonise.rm.commands.*;
28 import org.openharmonise.rm.publishing.*;
29 import org.openharmonise.rm.resources.*;
30 import org.openharmonise.rm.resources.lifecycle.*;
31 import org.openharmonise.rm.resources.users.User;
32
33 import com.ibm.webdav.*;
34
35 /**
36  * This class gives a single point of access for the ***manager classes to access the
37  * command functionality within Harmonise.
38  *
39  * @author Michael Bell
40  * @version $Revision: 1.3 $
41  * @since November 19, 2003
42  */

43 public class CommandWrapper {
44     
45     /**
46      * Logger for this class
47      */

48     private static final Logger m_logger = Logger.getLogger(CommandWrapper.class.getName());
49
50     /**
51      *
52      */

53     private CommandWrapper() {
54         super();
55     }
56     
57     public static boolean isSaveAllowed(User usr,AbstractEditableObject obj) throws WebDAVException {
58         boolean bIsAllowed = false;
59         
60         CmdSave cmd = new CmdSave();
61         
62         try {
63             bIsAllowed = cmd.isAvailable(usr,obj);
64         } catch (InvalidCommandException e) {
65             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
66         }
67         
68         return bIsAllowed;
69     }
70     
71     public static boolean isSaveAllowed(User usr,Class JavaDoc objClass) throws WebDAVException {
72         boolean bIsAllowed = false;
73         
74         CmdSave cmd = new CmdSave();
75         
76         try {
77             bIsAllowed = cmd.isAvailable(usr,objClass);
78         } catch (InvalidCommandException e) {
79             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
80         }
81         
82         return bIsAllowed;
83     }
84
85     public static AbstractChildObject save(
86         AbstractDataStoreInterface dsi,
87         AbstractChildObject child,
88         User usr)
89         throws WebDAVException {
90         AbstractChildObject result = null;
91
92         CmdSave cmd = new CmdSave();
93
94         result = (AbstractChildObject) executeCommand(dsi, child, usr, cmd);
95
96         return result;
97     }
98
99     public static void archive(
100         AbstractDataStoreInterface dsi,
101         AbstractChildObject child,
102         User usr)
103         throws WebDAVException {
104
105         CmdArchive cmd = new CmdArchive();
106
107         executeCommand(dsi, child, usr, cmd);
108
109     }
110     
111     public static AbstractChildObject reactivate(
112             AbstractDataStoreInterface dsi,
113             AbstractChildObject child,
114             User usr)
115             throws WebDAVException {
116
117             CmdReactivate cmd = new CmdReactivate();
118
119             return (AbstractChildObject) executeCommand(dsi, child, usr, cmd);
120
121         }
122     
123     public static AbstractChildObject changeStatus(
124     AbstractDataStoreInterface dsi,
125     AbstractChildObject child,
126     User usr,Status status) throws WebDAVException {
127         AbstractChildObject result = null;
128
129         CmdChangeStatus cmd = new CmdChangeStatus();
130         
131         Map map = new Hashtable();
132         
133         List vec = new Vector();
134         vec.add(String.valueOf(status.getIntValue()));
135         
136         map.put(CmdChangeStatus.PARAM_STATUS,vec);
137
138         cmd.setParameters(map);
139
140         result = (AbstractChildObject) executeCommand(dsi, child, usr, cmd);
141
142         return result;
143     }
144     
145     public static void lock(AbstractDataStoreInterface dsi,
146     AbstractChildObject child,
147     User usr)
148     throws WebDAVException {
149
150         CmdLock cmd = new CmdLock();
151     
152         executeCommand(dsi, child, usr, cmd);
153     
154     }
155     
156     public static void unlock(AbstractDataStoreInterface dsi,
157         AbstractChildObject child,
158         User usr)
159         throws WebDAVException {
160
161         CmdUnlock cmd = new CmdUnlock();
162
163         executeCommand(dsi, child, usr, cmd);
164
165     }
166
167     private static Object JavaDoc executeCommand(
168         AbstractDataStoreInterface dsi,
169         AbstractChildObject child,
170         User usr,
171         AbstractCmd cmd)
172         throws WebDAVException {
173         Object JavaDoc result = null;
174
175         try {
176             
177             if(m_logger.isLoggable(Level.FINE)) {
178                 m_logger.logp(Level.FINE, CommandWrapper.class.getName(), "executeMethod", "Executing command " + cmd.getClass().getName() + " for user " + usr.getId() + " on " + child.getClass().getName() + " " + child.getId());
179             }
180             
181             State state = new State(dsi,usr);
182
183             cmd.setState(state);
184             cmd.setCommandObject(child);
185
186             result = cmd.execute(null);
187         } catch (StateException e) {
188             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
189             throw new WebDAVException(
190                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
191                 e.getLocalizedMessage());
192         } catch(InvalidCommandException e) {
193             if(m_logger.isLoggable(Level.INFO)) {
194                 try {
195                     m_logger.info(cmd.getName() + " not valid for user " + usr.getId() + " with " + child.getClass().getName() + "(key - " + child.getKey() + ")");
196                 } catch (DataAccessException le) {
197                     m_logger.log(Level.WARNING, "Logging error", le);
198                 }
199             }
200             throw new WebDAVException(
201                 WebDAVStatus.SC_FORBIDDEN,
202                 e.getLocalizedMessage());
203         } catch (CommandException e) {
204             
205             if(e.getCause() instanceof StatusChangeNotAllowedException) {
206                 throw new WebDAVException(
207                     WebDAVStatus.SC_CONFLICT,
208                     e.getLocalizedMessage());
209             } else {
210                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
211                 throw new WebDAVException(
212                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
213                     e.getLocalizedMessage());
214             }
215             
216         }
217
218         return result;
219     }
220
221 }
222
Popular Tags