1 13 package info.magnolia.module.workflow.jcr; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.ItemType; 19 import info.magnolia.cms.core.search.Query; 20 import info.magnolia.cms.core.search.QueryManager; 21 import info.magnolia.cms.core.search.QueryResult; 22 import info.magnolia.cms.util.ContentUtil; 23 import info.magnolia.context.MgnlContext; 24 import info.magnolia.module.workflow.WorkflowConstants; 25 import openwfe.org.ApplicationContext; 26 import openwfe.org.ServiceException; 27 import openwfe.org.engine.expool.PoolException; 28 import openwfe.org.engine.expressions.FlowExpression; 29 import openwfe.org.engine.expressions.FlowExpressionId; 30 import openwfe.org.engine.impl.expool.AbstractExpressionStore; 31 import openwfe.org.engine.impl.expool.ExpoolUtils; 32 import openwfe.org.util.beancoder.XmlBeanCoder; 33 import openwfe.org.xml.XmlUtils; 34 import org.jdom.Document; 35 import org.slf4j.Logger; 36 import org.slf4j.LoggerFactory; 37 38 import javax.jcr.ValueFactory; 39 import java.io.InputStream ; 40 import java.util.Iterator ; 41 42 43 50 public class JCRExpressionStore extends AbstractExpressionStore { 51 52 55 private static final String ENGINE_ID = "ee"; 56 57 protected static Logger log = LoggerFactory.getLogger(JCRExpressionStore.class.getName()); 58 59 61 64 private HierarchyManager hierarchyManager = null; 65 66 69 public void init(final String serviceName, final ApplicationContext context, final java.util.Map serviceParams) throws ServiceException { 70 71 super.init(serviceName, context, serviceParams); 72 this.hierarchyManager = ContentRepository.getHierarchyManager(WorkflowConstants.WORKSPACE_EXPRESSION); 73 74 if (this.hierarchyManager == null) { 75 throw new ServiceException("Can't access HierarchyManager for workitems"); 76 } 77 } 78 79 82 85 88 public void storeExpression(final FlowExpression fe) throws PoolException { 89 90 try { 91 synchronized (this.hierarchyManager) { 92 93 Content cExpression = findExpression(fe); 94 95 if (log.isDebugEnabled()) { 96 97 log.debug 98 ("storeExpression() handle is "+ 99 cExpression.getHandle()); 100 } 101 102 104 ValueFactory vf = cExpression.getJCRNode().getSession().getValueFactory(); 105 String value = fe.getId().toParseableString(); 106 107 cExpression.createNodeData 108 (WorkflowConstants.NODEDATA_ID, vf.createValue(value)); 109 110 112 serializeExpressionAsXml(cExpression, fe); 113 114 this.hierarchyManager.save(); 115 } 116 } 117 catch (final Exception e) { 118 119 log.error 120 ("storeExpression() store exception failed", e); 121 122 throw new PoolException 123 ("storeExpression() store exception failed", e); 124 } 125 } 126 127 130 public void unstoreExpression(final FlowExpression fe) throws PoolException { 131 132 try { 133 Content cExpression = findExpression(fe); 134 135 if (cExpression != null) { 136 137 synchronized (this.hierarchyManager) { 138 139 cExpression.delete(); 140 141 this.hierarchyManager.save(); 142 } 143 } 144 else { 145 log.info 146 ("unstoreExpression() "+ 147 "didn't find content node for fe "+ 148 fe.getId().toParseableString()); 149 } 150 } 151 catch (final Exception e) { 152 153 log.error 154 ("unstoreExpression() unstore exception failed", e); 155 156 throw new PoolException 157 ("unstoreExpression() unstore exception failed", e); 158 } 159 } 160 161 164 public synchronized Iterator contentIterator(final Class assignClass) { 165 166 try { 167 return new StoreIterator(assignClass); 168 } 169 catch (final Throwable t) { 170 log.error("contentIterator() failed to set up an iterator", t); 171 } 172 173 return new java.util.ArrayList (0).iterator(); 175 } 176 177 180 public FlowExpression loadExpression(final FlowExpressionId fei) throws PoolException { 181 182 try { 183 184 Content cExpression = findExpression(fei); 185 186 if (cExpression != null) { 187 188 final FlowExpression expression = 189 deserializeExpressionAsXml(cExpression); 190 191 expression.setApplicationContext(getContext()); 192 193 return expression; 194 } 195 } 196 catch (final Exception e) { 197 198 log.debug 199 ("loadExpression() failed for "+fei.asStringId(), e); 200 201 throw new PoolException 202 ("loadExpression() failed for "+fei.asStringId(), e); 203 } 204 205 if (log.isDebugEnabled()) { 206 207 log.debug 208 ("loadExpression() "+ 209 "didn't find expression "+fei.asStringId()+ 210 " in the repository"); 211 } 212 213 throw new PoolException 214 ("loadExpression() "+ 215 "didn't find expression "+fei.asStringId()+" in the repository"); 216 217 } 218 219 222 public int size() { 223 224 try { 225 226 QueryManager qm = MgnlContext.getSystemContext().getQueryManager(WorkflowConstants.WORKSPACE_EXPRESSION); 227 228 Query q = qm.createQuery(WorkflowConstants.STORE_ITERATOR_QUERY, Query.SQL); 229 QueryResult qr = q.execute(); 230 231 return qr.getContent().size(); 232 } 233 catch (final Exception e) { 234 235 log.error("size() failed", e); 236 237 return -1; 238 } 239 } 240 241 244 private void serializeExpressionAsXml(Content c, FlowExpression fe) throws Exception { 245 246 final org.jdom.Document doc = XmlBeanCoder.xmlEncode(fe); 247 String s = XmlUtils.toString(doc, null); 248 ValueFactory vf = c.getJCRNode().getSession().getValueFactory(); 249 c.createNodeData(WorkflowConstants.NODEDATA_VALUE, vf.createValue(s)); 250 } 251 252 259 260 public final String toXPathFriendlyString(final FlowExpressionId fei) { 261 262 final StringBuffer buffer = new StringBuffer (); 263 final String engineId = fei.getEngineId(); 264 265 buffer.append(WorkflowConstants.SLASH); 266 buffer.append(engineId); 267 268 if (engineId.equals(ENGINE_ID)) { 270 return buffer.toString(); 271 } 272 273 buffer.append(WorkflowConstants.SLASH); 274 buffer.append(fei.getWorkflowDefinitionName()); 275 276 buffer.append(WorkflowConstants.SLASH); 277 buffer.append(fei.getWorkflowInstanceId()); 278 279 buffer 280 .append(WorkflowConstants.SLASH); 281 buffer 282 .append(fei.getExpressionId()) 283 .append("__") 284 .append(fei.getExpressionName()); 285 286 return buffer.toString(); 287 } 288 289 private Content findExpression(final FlowExpression fe) throws Exception { 290 291 return findExpression(fe.getId()); 292 } 293 294 private Content findExpression(final FlowExpressionId fei) throws Exception { 295 296 if (log.isDebugEnabled()) { 297 log.debug("findExpression() looking for "+fei.toParseableString()); 298 } 299 300 final String path = toXPathFriendlyString(fei); 301 302 if (this.hierarchyManager.isExist(path)) { 303 return this.hierarchyManager.getContent(path); 304 } 305 else { 306 return ContentUtil.createPath 307 (this.hierarchyManager, path, ItemType.EXPRESSION); 308 } 309 } 310 311 private FlowExpression deserializeExpressionAsXml(final Content c) throws Exception { 312 313 final InputStream is = c 314 .getNodeData(WorkflowConstants.NODEDATA_VALUE) 315 .getStream(); 316 317 if (is == null) return null; 318 321 final org.jdom.input.SAXBuilder builder = 322 new org.jdom.input.SAXBuilder(); 323 324 final Document doc = builder.build(is); 325 326 return (FlowExpression)XmlBeanCoder.xmlDecode(doc); 327 } 328 329 355 356 362 363 371 protected final class StoreIterator implements Iterator { 372 373 376 private Class assignClass; 377 378 private Iterator rootIterator = null; 379 380 private FlowExpression next = null; 381 382 385 public StoreIterator(final Class assignClass) throws Exception { 386 387 super(); 388 389 this.assignClass = assignClass; 390 391 final QueryManager qm = MgnlContext 392 .getSystemContext() 393 .getQueryManager(WorkflowConstants.WORKSPACE_EXPRESSION); 394 395 final Query query = qm.createQuery 396 (WorkflowConstants.STORE_ITERATOR_QUERY, Query.SQL); 397 398 final QueryResult qr = query.execute(); 399 400 if (log.isDebugEnabled()) { 401 log.debug 402 ("() query found "+ 403 qr.getContent("expression").size()+" elements"); 404 } 405 406 this.rootIterator = qr.getContent("expression").iterator(); 407 408 this.next = fetchNext(); 409 } 410 411 414 public boolean hasNext() { 415 416 return (this.next != null); 417 } 418 419 public FlowExpression fetchNext() { 420 421 if ( ! this.rootIterator.hasNext()) { 422 return null; 423 } 424 425 final Content content = (Content) this.rootIterator.next(); 426 try { 427 428 final FlowExpression fe = deserializeExpressionAsXml(content); 429 430 if (fe == null) { 431 return fetchNext(); 432 } 433 434 fe.setApplicationContext(JCRExpressionStore.this.getContext()); 435 436 if ( ! ExpoolUtils.isAssignableFromClass(fe, this.assignClass)) { 437 return fetchNext(); 438 } 439 440 return fe; 441 } 442 catch (final Exception e) { 443 444 log.error("fetchNext() problem", e); 445 return null; 446 } 447 } 448 449 public Object next() throws java.util.NoSuchElementException { 450 451 final FlowExpression current = this.next; 452 453 if (current == null) { 454 throw new java.util.NoSuchElementException (); 455 } 456 457 this.next = fetchNext(); 458 459 if (log.isDebugEnabled()) { 460 461 if (this.next == null) { 462 log.debug("next() is 'null'"); 463 } 464 else { 465 log.debug("next() is "+this.next.getId()); 466 } 467 } 468 469 return current; 470 } 471 472 public void remove() { 473 } 475 } 476 477 } 478 | Popular Tags |