1 13 package info.magnolia.module.workflow; 14 15 import info.magnolia.cms.security.MgnlUser; 16 import info.magnolia.cms.util.FactoryUtil; 17 import info.magnolia.context.Context; 18 import info.magnolia.context.MgnlContext; 19 import info.magnolia.module.workflow.flows.FlowDefinionException; 20 import info.magnolia.module.workflow.flows.FlowDefinitionManager; 21 import info.magnolia.module.workflow.jcr.JCRPersistedEngine; 22 import info.magnolia.module.workflow.jcr.JCRWorkItemAPI; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import openwfe.org.engine.expressions.FlowExpressionId; 29 import openwfe.org.engine.workitem.InFlowItem; 30 import openwfe.org.engine.workitem.InFlowWorkItem; 31 import openwfe.org.engine.workitem.LaunchItem; 32 import openwfe.org.engine.workitem.StringAttribute; 33 import openwfe.org.worklist.store.StoreException; 34 35 import org.apache.commons.lang.StringUtils; 36 import org.slf4j.Logger; 37 import org.slf4j.LoggerFactory; 38 39 40 44 public class WorkflowUtil { 45 46 final static public StringAttribute ATTRIBUTE_TRUE = new StringAttribute("true"); 47 48 final static public StringAttribute ATTRIBUTE_FALSE = new StringAttribute("false"); 49 50 private final static Logger log = LoggerFactory.getLogger(WorkflowUtil.class.getName()); 51 52 55 private static JCRWorkItemAPI storage; 56 57 static { 58 59 try { 60 storage = new JCRWorkItemAPI(); 61 } 62 catch (Exception e) { 63 log.error("can't initialize the workflow util", e); 64 } 65 } 66 67 70 private WorkflowUtil() { 71 } 72 73 public static void launchFlow(LaunchItem li) { 74 try { 75 JCRPersistedEngine engine = WorkflowModule.getEngine(); 76 engine.launch(li, true); 78 } 79 catch (Exception e) { 80 log.error("Launching flow failed", e); 81 } 82 } 83 84 87 public static void launchFlow(String repository, String path, String flowName) throws Exception { 88 try { 89 LaunchItem li = new LaunchItem(); 91 92 if (repository != null) { 94 li.addAttribute(Context.ATTRIBUTE_REPOSITORY, new StringAttribute(repository)); 95 } 96 if (path != null) { 97 li.addAttribute(Context.ATTRIBUTE_PATH, new StringAttribute(path)); 98 } 99 launchFlow(li, flowName); 100 } 102 catch (Exception e) { 103 log.error("Launching flow " + flowName + " failed", e); 104 } 105 } 106 107 113 public static void launchFlow(LaunchItem li, String flowName) throws FlowDefinionException { 114 FlowDefinitionManager configurator = WorkflowModule.getFlowDefinitionManager(); 115 116 configurator.configure(li, flowName); 117 118 launchFlow(li); 119 } 120 121 124 public static void proceed(String id) { 125 proceed(id, WorkflowConstants.ACTION_PROCEED); 126 } 127 128 public static void proceed(String id, String action) { 129 proceed(id, action, null); 130 } 131 132 public static void proceed(String id, String action, String comment) { 133 InFlowWorkItem wi = getWorkItem(id); 134 if (wi == null) { 135 log.error("can't proceed workitem [{}]", id); 136 return; 137 } 138 wi.touch(); 139 if(wi.containsAttribute(Context.ATTRIBUTE_EXCEPTION)){ 141 wi.removeAttribute(Context.ATTRIBUTE_EXCEPTION); 142 } 143 if(wi.containsAttribute(Context.ATTRIBUTE_MESSAGE)){ 144 wi.removeAttribute(Context.ATTRIBUTE_MESSAGE); 145 } 146 wi.getAttributes().puts(WorkflowConstants.ATTRIBUTE_ACTION, action); 147 wi.getAttributes().puts(WorkflowConstants.ATTRIBUTE_USERNAME, MgnlContext.getUser().getName()); 148 149 if (StringUtils.isNotEmpty(comment)) { 150 wi.getAttributes().puts(Context.ATTRIBUTE_COMMENT, comment); 151 } 152 proceed(wi); 153 } 154 155 public static void reject(String id, String comment) { 156 proceed(id, WorkflowConstants.ACTION_REJECT, comment); 157 } 158 159 public static void cancel(String id, String comment) { 160 proceed(id, WorkflowConstants.ACTION_CANCEL, comment); 161 } 162 163 167 public static void proceed(InFlowWorkItem wi) { 168 try { 169 WorkflowModule.getEngine().reply(wi); 170 } 171 catch (Exception e) { 172 log.error("Error while accessing the workflow engine", e); 173 } 174 finally { 175 removeWorkItem(wi); 176 } 177 } 178 179 183 public static InFlowWorkItem getWorkItem(String id) { 184 InFlowWorkItem wi = null; 185 try { 186 wi = storage.retrieveWorkItem(StringUtils.EMPTY, FlowExpressionId.fromParseableString(id)); 187 } 188 catch (StoreException e) { 189 log.error("can't get the workitem by expression [" + id + "]", e); 190 } 191 return wi; 192 } 193 194 public static String getPath(String id){ 195 return storage.createPathFromId(FlowExpressionId.fromParseableString(id)); 196 } 197 198 204 public static List getWorkItems(String userName) throws Exception { 205 if (log.isDebugEnabled()) { 206 log.debug("enter getWorkItems"); 207 log.debug("user name = " + userName); 208 } 209 210 long start = System.currentTimeMillis(); 211 212 MgnlUser user = (MgnlUser) MgnlContext.getUser(); 213 Collection groups = user.getGroups(); 214 Collection roles = user.getRoles(); 215 StringBuffer queryString = new StringBuffer (); 216 queryString.append("//*[(@assignTo=\""); 217 queryString.append(userName); 218 queryString.append("\") or (@participant=\"user-"); 219 queryString.append(userName); 220 queryString.append("\" and not(@assignTo))"); 221 for (Iterator iter = groups.iterator(); iter.hasNext();) { 222 Object group = iter.next(); 223 queryString.append(" or (@participant=\"group-"); 224 queryString.append(group); 225 queryString.append("\") "); 229 } 230 for (Iterator iter = roles.iterator(); iter.hasNext();) { 231 Object role = iter.next(); 232 queryString.append(" or (@participant=\"role-"); 234 queryString.append(role); 235 queryString.append("\") "); 238 } 239 queryString.append("]"); 240 241 if (log.isDebugEnabled()) { 242 log.info("xpath query string = " + queryString); 243 } 244 245 final List doQuery = storage.doQuery(queryString.toString()); 246 long end = System.currentTimeMillis(); 247 log.debug("Retrieving workitems done. (Took " + (end - start) + " ms)"); 248 return doQuery; 249 } 250 251 public static String getId(InFlowItem wi) { 252 return wi.getId().toParseableString(); 253 } 254 255 258 public static void assignWorkItemToUser(String id, String userName) { 259 if (id == null || id.length() == 0) { 260 log.error("can not assign work item, invalid express id " + id); 261 return; 262 } 263 264 if (userName == null) { 265 log.info("User name was null"); 266 return; 267 } 268 269 InFlowWorkItem wi = getWorkItem(id); 270 if (wi == null) { 271 log.error("can not assign work item, can not retrieve work tiem by express id " + id); 272 return; 273 } 274 assignWorkItemToUser(wi, userName); 275 } 276 277 280 public static void assignWorkItemToUser(InFlowWorkItem wi, String userName) { 281 if (userName == null) { 282 log.info("User name was null"); 283 return; 284 } 285 286 try { 287 wi.addAttribute(WorkflowConstants.ATTRIBUTE_ASSIGN_TO, new StringAttribute(userName)); 288 storage.storeWorkItem(StringUtils.EMPTY, wi); 289 } 290 catch (Exception e) { 291 log.error("assign work item to user " + userName + " failed.)", e); 292 } 293 294 } 295 296 299 public static List getUserInbox(String userName) throws Exception { 300 return getWorkItems(userName); 301 } 302 303 306 public static List getGroupInbox(String GroupName) throws Exception { 307 if (log.isDebugEnabled()) { 308 log.debug("enter getGroupInbox"); 309 log.debug("GroupName = " + GroupName); 310 } 311 312 StringBuffer queryString = new StringBuffer (); 313 queryString.append("//*[@participant=\"group-"); 314 queryString.append(GroupName); 315 queryString.append("\"]"); 316 317 if (log.isDebugEnabled()) { 318 log.debug("xpath query string = " + queryString); 319 } 320 return storage.doQuery(queryString.toString()); 321 322 } 323 324 327 public static List getRoleInbox(String roleName) throws Exception { 328 if (log.isDebugEnabled()) { 329 log.debug("enter getGroupInbox"); 330 log.debug("roleName = " + roleName); 331 } 332 333 StringBuffer queryString = new StringBuffer (); 334 queryString.append("//*[@participant=\"group-"); 335 queryString.append(roleName); 336 queryString.append("\"]"); 337 338 if (log.isDebugEnabled()) { 339 log.debug("xpath query string = " + queryString); 340 } 341 return storage.doQuery(queryString.toString()); 342 } 343 344 347 private static void removeWorkItem(InFlowWorkItem wi) { 348 try { 349 storage.removeWorkItem(wi.getId()); 350 } 351 catch (StoreException e) { 352 log.error("can't remove workitem", e); 353 } 354 } 355 356 } | Popular Tags |