1 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 43 public class CommandWrapper { 44 45 48 private static final Logger m_logger = Logger.getLogger(CommandWrapper.class.getName()); 49 50 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 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 executeCommand( 168 AbstractDataStoreInterface dsi, 169 AbstractChildObject child, 170 User usr, 171 AbstractCmd cmd) 172 throws WebDAVException { 173 Object 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 |