1 13 package info.magnolia.module.workflow.jcr; 14 15 import info.magnolia.beancoder.MgnlNode; 16 import info.magnolia.cms.beans.config.ContentRepository; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.core.ItemType; 20 import info.magnolia.cms.core.search.Query; 21 import info.magnolia.cms.core.search.QueryManager; 22 import info.magnolia.cms.core.search.QueryResult; 23 import info.magnolia.cms.security.AccessDeniedException; 24 import info.magnolia.cms.util.ContentUtil; 25 import info.magnolia.context.MgnlContext; 26 import info.magnolia.module.workflow.WorkflowConstants; 27 import info.magnolia.module.workflow.beancoder.OwfeJcrBeanCoder; 28 import openwfe.org.engine.expressions.FlowExpressionId; 29 import openwfe.org.engine.workitem.InFlowWorkItem; 30 import openwfe.org.engine.workitem.StringAttribute; 31 import openwfe.org.worklist.store.StoreException; 32 import org.apache.commons.lang.StringUtils; 33 import org.slf4j.Logger; 34 import org.slf4j.LoggerFactory; 35 36 import javax.jcr.RepositoryException; 37 import javax.jcr.ValueFactory; 38 import java.util.ArrayList ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 42 43 51 public class JCRWorkItemAPI { 52 53 public final static Logger log = LoggerFactory.getLogger(JCRWorkItemAPI.class.getName()); 54 55 private HierarchyManager hm; 56 57 public JCRWorkItemAPI() throws Exception { 58 this.hm = ContentRepository.getHierarchyManager(WorkflowConstants.WORKSPACE_STORE); 59 if (this.hm == null) { 60 Exception e = new Exception ("Can't get HierarchyManager Object for workitems repository"); 61 log.error(e.getMessage(), e); 62 throw e; 63 } 64 } 65 66 69 public void removeWorkItem(FlowExpressionId fei) throws StoreException { 70 synchronized (this.hm) { 71 try { 72 Content ct = getWorkItemById(fei); 73 if (ct != null) { 74 ct.delete(); 75 this.hm.save(); 76 if (log.isDebugEnabled()) { 77 log.debug("work item removed"); 78 } 79 } 80 81 } 82 catch (Exception e) { 83 log.error("exception:" + e); 84 } 85 } 86 } 87 88 95 public InFlowWorkItem retrieveWorkItem(final String storeName, final FlowExpressionId fei) throws StoreException { 96 if (log.isDebugEnabled()) { 97 log.debug("starting retrieve work item. this = " + this); 98 log.debug("retrieve work item for ID = " + fei.toParseableString()); 99 } 100 101 Content ct = getWorkItemById(fei); 102 103 if (ct == null) { 104 throw new StoreException("cannot find workitem " + fei); 105 } 106 107 try { 108 return loadWorkItem(ct); 109 } 110 catch (Exception e) { 111 throw new StoreException("load work item form xml failed", e); 112 } 113 } 114 115 121 public static InFlowWorkItem loadWorkItem(Content ct) throws Exception { 122 OwfeJcrBeanCoder coder = new OwfeJcrBeanCoder(null,new MgnlNode(ct.getContent(WorkflowConstants.NODEDATA_VALUE))); 123 return (InFlowWorkItem)coder.decode(); 124 } 125 126 130 public Content getWorkItemByParticipant(String participant) { 131 String queryString = "//*[@participant=\"" + participant + "\"]"; 132 if (log.isDebugEnabled()) { 133 log.debug("xpath query string = " + queryString); 134 } 135 List list = doQuery(queryString); 136 if (list != null && list.size() > 0) { 137 return (Content) list.get(0); 138 } 139 140 return null; 141 } 142 143 147 public Content getWorkItemById(FlowExpressionId fei) { 148 String path = createPathFromId(fei); 149 try { 150 return this.hm.getContent(path); 151 } 152 catch (Exception e) { 153 log.error("get work item by id failed, path = " + path, e); 154 } 155 return null; 156 } 157 158 163 public boolean hasWorkItem(FlowExpressionId fei) throws AccessDeniedException, RepositoryException { 164 String path = createPathFromId(fei); 165 if (StringUtils.isNotEmpty(path) && StringUtils.indexOf(path, "/") != 0) { 166 path = "/" + path; 167 } 168 return this.hm.isExist(path); 169 } 170 171 176 public boolean checkContentWithEID(Content ct, FlowExpressionId eid) { 177 String cid = ct.getNodeData(WorkflowConstants.NODEDATA_ID).getString(); 178 if (log.isDebugEnabled()) { 179 log.debug("checkContentWithEID: ID = " + cid); 180 } 181 FlowExpressionId id = FlowExpressionId.fromParseableString(cid); 182 return id.equals(eid); 183 } 184 185 189 public final String convertPath(String id) { 190 return StringUtils.replace( 191 StringUtils.replace(id, WorkflowConstants.BAR, StringUtils.EMPTY), 192 WorkflowConstants.COLON, 193 WorkflowConstants.DOT); 194 } 195 196 200 public String createPathFromId(FlowExpressionId eid) { 201 String wlInstId = eid.getWorkflowInstanceId(); 202 String groupString = StringUtils.right(StringUtils.substringBefore(wlInstId, "."), 3); 204 int groupNumber = Integer.parseInt(groupString) % 100; 205 StringBuffer buffer = new StringBuffer (eid.getWorkflowDefinitionName()); 206 buffer.append(WorkflowConstants.SLASH); 207 buffer.append(eid.getWorkflowDefinitionRevision()); 208 buffer.append(WorkflowConstants.SLASH); 209 buffer.append(groupNumber); 210 buffer.append(WorkflowConstants.SLASH); 211 buffer.append(eid.getWorkflowInstanceId()); 212 buffer.append(WorkflowConstants.SLASH); 213 buffer.append(eid.getExpressionName()); 214 buffer.append(WorkflowConstants.SLASH); 215 buffer.append(eid.getExpressionId()); 216 217 return convertPath(buffer.toString()); 218 } 219 220 226 public void storeWorkItem(String arg0, InFlowWorkItem wi) throws StoreException { 227 synchronized(this.hm) { 228 try { 229 230 if (hasWorkItem(wi.getId())) { 232 this.hm.delete(createPathFromId(wi.getId())); 234 } 235 236 String path = createPathFromId(wi.getId()); 238 if (log.isDebugEnabled()) { 239 log.debug("storing workitem with path = " + path); 240 } 241 242 Content newc = ContentUtil.createPath(this.hm, path, ItemType.WORKITEM); 243 244 ValueFactory vf = newc.getJCRNode().getSession().getValueFactory(); 245 String sId = wi.getLastExpressionId().toParseableString(); 246 247 newc.createNodeData(WorkflowConstants.NODEDATA_ID, vf.createValue(sId)); 248 newc.createNodeData(WorkflowConstants.NODEDATA_PARTICIPANT, vf.createValue(wi.getParticipantName())); 249 250 StringAttribute assignTo = (StringAttribute) wi.getAttribute(WorkflowConstants.ATTRIBUTE_ASSIGN_TO); 251 if (assignTo != null) { 252 String s = assignTo.toString(); 253 if (s.length() > 0) { 254 newc.createNodeData(WorkflowConstants.ATTRIBUTE_ASSIGN_TO, vf.createValue(s)); 255 } 256 } 257 258 OwfeJcrBeanCoder coder = new OwfeJcrBeanCoder(null,new MgnlNode(newc),WorkflowConstants.NODEDATA_VALUE); 260 coder.encode(wi); 261 hm.save(); 262 263 if (log.isDebugEnabled()) { 264 log.debug("store work item ok. "); 265 } 266 } 267 catch (Exception e) { 268 log.error("store work item failed", e); 269 throw new StoreException(e.toString()); 270 } 271 } 272 } 273 274 277 public List doQuery(String queryString) { 278 ArrayList list = new ArrayList (); 279 if (log.isDebugEnabled()) { 280 log.debug("xpath query string: " + queryString); 281 } 282 try { 283 final QueryManager queryManager = MgnlContext.getSystemContext().getQueryManager( 284 WorkflowConstants.WORKSPACE_STORE); 285 final Query q = queryManager.createQuery(queryString, Query.XPATH); 286 287 QueryResult result = q.execute(); 288 if (result == null) { 289 log.info("query result was null"); 290 return null; 291 } 292 293 Iterator it = result.getContent(WorkflowConstants.NODENAME_WORKITEM).iterator(); 294 while (it.hasNext()) { 295 Content ct = (Content) it.next(); 296 297 try { 299 if (!hm.isExist(ct.getHandle())) { 300 if (log.isDebugEnabled()) { 301 log.debug(ct.getHandle() + " does not exist anymore."); 302 } 303 continue; 304 } 305 } 306 catch (Exception e) { 307 log.error("SKipping strange node"); 308 } 309 310 String title = ct.getTitle(); 311 String sname = ct.getName(); 312 313 if (log.isDebugEnabled()) { 314 log.debug("title=" + title); 315 log.debug("name=" + sname); 316 } 317 318 InFlowWorkItem wi = loadWorkItem(ct); 319 if (wi == null) { 320 log.error("can not load found workitem"); 321 continue; 322 } 323 if (log.isDebugEnabled()) { 324 log.debug("added workitem to return list ok"); 325 } 326 list.add(wi); 327 } 328 } 329 catch (Exception e) { 330 log.error("query flow failed", e); 331 return null; 332 } 333 return list; 334 335 } 336 337 } 338 | Popular Tags |