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:
020: package org.openharmonise.dav.server.utils;
021:
022: import java.util.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
026: import org.openharmonise.rm.DataAccessException;
027: import org.openharmonise.rm.commands.*;
028: import org.openharmonise.rm.publishing.*;
029: import org.openharmonise.rm.resources.*;
030: import org.openharmonise.rm.resources.lifecycle.*;
031: import org.openharmonise.rm.resources.users.User;
032:
033: import com.ibm.webdav.*;
034:
035: /**
036: * This class gives a single point of access for the ***manager classes to access the
037: * command functionality within Harmonise.
038: *
039: * @author Michael Bell
040: * @version $Revision: 1.3 $
041: * @since November 19, 2003
042: */
043: public class CommandWrapper {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger m_logger = Logger
049: .getLogger(CommandWrapper.class.getName());
050:
051: /**
052: *
053: */
054: private CommandWrapper() {
055: super ();
056: }
057:
058: public static boolean isSaveAllowed(User usr,
059: AbstractEditableObject obj) throws WebDAVException {
060: boolean bIsAllowed = false;
061:
062: CmdSave cmd = new CmdSave();
063:
064: try {
065: bIsAllowed = cmd.isAvailable(usr, obj);
066: } catch (InvalidCommandException e) {
067: throw new WebDAVException(
068: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
069: .getLocalizedMessage());
070: }
071:
072: return bIsAllowed;
073: }
074:
075: public static boolean isSaveAllowed(User usr, Class objClass)
076: throws WebDAVException {
077: boolean bIsAllowed = false;
078:
079: CmdSave cmd = new CmdSave();
080:
081: try {
082: bIsAllowed = cmd.isAvailable(usr, objClass);
083: } catch (InvalidCommandException e) {
084: throw new WebDAVException(
085: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
086: .getLocalizedMessage());
087: }
088:
089: return bIsAllowed;
090: }
091:
092: public static AbstractChildObject save(
093: AbstractDataStoreInterface dsi, AbstractChildObject child,
094: User usr) throws WebDAVException {
095: AbstractChildObject result = null;
096:
097: CmdSave cmd = new CmdSave();
098:
099: result = (AbstractChildObject) executeCommand(dsi, child, usr,
100: cmd);
101:
102: return result;
103: }
104:
105: public static void archive(AbstractDataStoreInterface dsi,
106: AbstractChildObject child, User usr) throws WebDAVException {
107:
108: CmdArchive cmd = new CmdArchive();
109:
110: executeCommand(dsi, child, usr, cmd);
111:
112: }
113:
114: public static AbstractChildObject reactivate(
115: AbstractDataStoreInterface dsi, AbstractChildObject child,
116: User usr) throws WebDAVException {
117:
118: CmdReactivate cmd = new CmdReactivate();
119:
120: return (AbstractChildObject) executeCommand(dsi, child, usr,
121: cmd);
122:
123: }
124:
125: public static AbstractChildObject changeStatus(
126: AbstractDataStoreInterface dsi, AbstractChildObject child,
127: User usr, Status status) throws WebDAVException {
128: AbstractChildObject result = null;
129:
130: CmdChangeStatus cmd = new CmdChangeStatus();
131:
132: Map map = new Hashtable();
133:
134: List vec = new Vector();
135: vec.add(String.valueOf(status.getIntValue()));
136:
137: map.put(CmdChangeStatus.PARAM_STATUS, vec);
138:
139: cmd.setParameters(map);
140:
141: result = (AbstractChildObject) executeCommand(dsi, child, usr,
142: cmd);
143:
144: return result;
145: }
146:
147: public static void lock(AbstractDataStoreInterface dsi,
148: AbstractChildObject child, User usr) 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, User usr) throws WebDAVException {
158:
159: CmdUnlock cmd = new CmdUnlock();
160:
161: executeCommand(dsi, child, usr, cmd);
162:
163: }
164:
165: private static Object executeCommand(
166: AbstractDataStoreInterface dsi, AbstractChildObject child,
167: User usr, AbstractCmd cmd) throws WebDAVException {
168: Object result = null;
169:
170: try {
171:
172: if (m_logger.isLoggable(Level.FINE)) {
173: m_logger.logp(Level.FINE, CommandWrapper.class
174: .getName(), "executeMethod",
175: "Executing command " + cmd.getClass().getName()
176: + " for user " + usr.getId() + " on "
177: + child.getClass().getName() + " "
178: + 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, e
191: .getLocalizedMessage());
192: } catch (InvalidCommandException e) {
193: if (m_logger.isLoggable(Level.INFO)) {
194: try {
195: m_logger.info(cmd.getName()
196: + " not valid for user " + usr.getId()
197: + " with " + child.getClass().getName()
198: + "(key - " + child.getKey() + ")");
199: } catch (DataAccessException le) {
200: m_logger.log(Level.WARNING, "Logging error", le);
201: }
202: }
203: throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, e
204: .getLocalizedMessage());
205: } catch (CommandException e) {
206:
207: if (e.getCause() instanceof StatusChangeNotAllowedException) {
208: throw new WebDAVException(WebDAVStatus.SC_CONFLICT, e
209: .getLocalizedMessage());
210: } else {
211: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
212: throw new WebDAVException(
213: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, e
214: .getLocalizedMessage());
215: }
216:
217: }
218:
219: return result;
220: }
221:
222: }
|