KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > workflow > jcr > JCRWorkItemAPI


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42
43 /**
44  * The Magnolia-specific workflow participant.
45  *
46  * @author Jackie Ju
47  * @author Philipp Bracher
48  * @author Nicolas Modrzyk
49  * @author John Mettraux
50  */

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 JavaDoc {
58         this.hm = ContentRepository.getHierarchyManager(WorkflowConstants.WORKSPACE_STORE);
59         if (this.hm == null) {
60             Exception JavaDoc e = new Exception JavaDoc("Can't get HierarchyManager Object for workitems repository");
61             log.error(e.getMessage(), e);
62             throw e;
63         }
64     }
65
66     /**
67      * remove one workItem by its ID
68      */

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 JavaDoc e) {
83                 log.error("exception:" + e);
84             }
85         }
86     }
87
88     /**
89      * retrieve work item by
90      * @param storeName
91      * @param fei
92      * @return
93      * @throws StoreException
94      */

95     public InFlowWorkItem retrieveWorkItem(final String JavaDoc 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 JavaDoc e) {
111             throw new StoreException("load work item form xml failed", e);
112         }
113     }
114
115     /**
116      * load a work item from a JCR content
117      * @param ct the content node
118      * @return
119      * @throws Exception
120      */

121     public static InFlowWorkItem loadWorkItem(Content ct) throws Exception JavaDoc {
122         OwfeJcrBeanCoder coder = new OwfeJcrBeanCoder(null,new MgnlNode(ct.getContent(WorkflowConstants.NODEDATA_VALUE)));
123         return (InFlowWorkItem)coder.decode();
124     }
125
126     /**
127      * retrieve a work item by participant name
128      * @param participant the full participant name (for example, user-superuser)
129      */

130     public Content getWorkItemByParticipant(String JavaDoc participant) {
131         String JavaDoc queryString = "//*[@participant=\"" + participant + "\"]";
132         if (log.isDebugEnabled()) {
133             log.debug("xpath query string = " + queryString);
134         }
135         List JavaDoc list = doQuery(queryString);
136         if (list != null && list.size() > 0) {
137             return (Content) list.get(0);
138         }
139
140         return null;
141     }
142
143     /**
144      * get work item by id
145      * @param fei
146      */

147     public Content getWorkItemById(FlowExpressionId fei) {
148         String JavaDoc path = createPathFromId(fei);
149         try {
150             return this.hm.getContent(path);
151         }
152         catch (Exception JavaDoc e) {
153             log.error("get work item by id failed, path = " + path, e);
154         }
155         return null;
156     }
157
158     /**
159      * check whether the specified work item exists
160      * @param fei expression id of work item
161      * @return true if exist, false if not
162      */

163     public boolean hasWorkItem(FlowExpressionId fei) throws AccessDeniedException, RepositoryException {
164         String JavaDoc 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     /**
172      * check if the content contains the right work Item with same id
173      * @param ct JCR content
174      * @param eid id of work item
175      */

176     public boolean checkContentWithEID(Content ct, FlowExpressionId eid) {
177         String JavaDoc 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     /**
186      * convert the name to valid path
187      * @param id
188      */

189     public final String JavaDoc convertPath(String JavaDoc id) {
190         return StringUtils.replace(
191                 StringUtils.replace(id, WorkflowConstants.BAR, StringUtils.EMPTY),
192                 WorkflowConstants.COLON,
193                 WorkflowConstants.DOT);
194     }
195
196     /**
197      * create the jcr node path for work Item by its id
198      * @param eid
199      */

200     public String JavaDoc createPathFromId(FlowExpressionId eid) {
201         String JavaDoc wlInstId = eid.getWorkflowInstanceId();
202         // TODO someone who knows the code better should have a look
203
String JavaDoc groupString = StringUtils.right(StringUtils.substringBefore(wlInstId, "."), 3);
204         int groupNumber = Integer.parseInt(groupString) % 100;
205         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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     /**
221      * store work Item
222      * @param arg0
223      * @param wi the work item intends to be stored
224      * @throws StoreException
225      */

226     public void storeWorkItem(String JavaDoc arg0, InFlowWorkItem wi) throws StoreException {
227         synchronized(this.hm) {
228             try {
229
230                 // delete it if already exist
231
if (hasWorkItem(wi.getId())) {
232                     // do not use removeWorkItem() since it persist changes immedietely
233
this.hm.delete(createPathFromId(wi.getId()));
234                 }
235
236                 // create path from work item id
237
String JavaDoc 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 JavaDoc 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 JavaDoc s = assignTo.toString();
253                     if (s.length() > 0) {
254                         newc.createNodeData(WorkflowConstants.ATTRIBUTE_ASSIGN_TO, vf.createValue(s));
255                     }
256                 }
257
258                 // convert to xml string
259
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 JavaDoc e) {
268                 log.error("store work item failed", e);
269                 throw new StoreException(e.toString());
270             }
271         }
272     }
273
274     /**
275      * execute the xPath Query
276      */

277     public List JavaDoc doQuery(String JavaDoc queryString) {
278         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc it = result.getContent(WorkflowConstants.NODENAME_WORKITEM).iterator();
294             while (it.hasNext()) {
295                 Content ct = (Content) it.next();
296
297                 // check for stale data.
298
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 JavaDoc e) {
307                     log.error("SKipping strange node");
308                 }
309
310                 String JavaDoc title = ct.getTitle();
311                 String JavaDoc 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 JavaDoc e) {
330             log.error("query flow failed", e);
331             return null;
332         }
333         return list;
334
335     }
336
337 }
338
Popular Tags