1 13 package info.magnolia.module.workflow; 14 15 import info.magnolia.commands.CommandsManager; 16 import info.magnolia.context.Context; 17 import info.magnolia.context.MgnlContext; 18 import info.magnolia.module.workflow.jcr.JCRWorkItemAPI; 19 import openwfe.org.embed.impl.engine.AbstractEmbeddedParticipant; 20 import openwfe.org.engine.workitem.CancelItem; 21 import openwfe.org.engine.workitem.InFlowWorkItem; 22 import openwfe.org.engine.workitem.WorkItem; 23 24 import org.apache.commons.chain.Command; 25 import org.apache.commons.lang.StringUtils; 26 import org.slf4j.Logger; 27 import org.slf4j.LoggerFactory; 28 29 30 37 public class MgnlParticipant extends AbstractEmbeddedParticipant { 38 39 42 private static Logger log = LoggerFactory.getLogger(AbstractEmbeddedParticipant.class); 43 44 JCRWorkItemAPI storage = null; 45 46 public MgnlParticipant() throws Exception { 47 super(); 48 this.storage = new JCRWorkItemAPI(); 49 log.debug("storage = {}", this.storage); 50 } 51 52 public MgnlParticipant(String name) throws Exception { 53 super(name); 54 this.storage = new JCRWorkItemAPI(); 55 log.debug("storage = {}", this.storage); 56 } 57 58 public void cancel(CancelItem cancelItem) throws Exception { 59 if (log.isDebugEnabled()) { 60 if (cancelItem.getId() != null) { 61 log.debug("Cancelling {}", cancelItem.getId().toParseableString()); 62 } 63 64 } 65 String parName = cancelItem.getParticipantName(); 66 if (!parName.startsWith(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND)) { 67 this.storage.removeWorkItem(cancelItem.getId()); 70 } 71 } 75 76 79 public void consume(WorkItem wi) throws Exception { 80 81 log.debug("enter consume().."); 83 84 if (wi == null) { 85 log.error("work item is null"); 86 return; 87 } 88 String parName = ((InFlowWorkItem) (wi)).getParticipantName(); 89 90 log.debug("participant name = {}", parName); 91 92 if (parName.startsWith(WorkflowConstants.PARTICIPANT_PREFIX_COMMAND)) { 94 log.info("consume command {}...", parName); 95 96 Context originalContext = null; 97 if (MgnlContext.hasInstance()) { 98 originalContext = MgnlContext.getInstance(); 99 } 100 101 try { 102 String name = StringUtils.removeStart(parName, WorkflowConstants.PARTICIPANT_PREFIX_COMMAND); 103 Command c = CommandsManager.getInstance().getCommand(name); 104 if (c != null) { 105 log.info("Command has been found through the magnolia catalog: {}", c.getClass().getName()); 106 107 Context context = new WorkItemContext(MgnlContext.getSystemContext(), wi); 111 112 MgnlContext.setInstance(context); 114 115 c.execute(context); 117 118 WorkflowModule.getEngine().reply((InFlowWorkItem) wi); 119 120 } 121 else { 122 log.error("No command has been found through the magnolia catalog for name: {}", parName); 124 } 125 126 log.info("consume command {} end", parName); 127 } 128 catch (Exception e) { 129 log.error("consume command failed", e); 130 } 131 finally { 132 MgnlContext.setInstance(originalContext); 133 } 134 } 135 else { 136 log.debug("storage = {}", this.storage); 137 138 this.storage.storeWorkItem(StringUtils.EMPTY, (InFlowWorkItem) wi); 139 } 140 141 log.debug("leave consume().."); 142 143 } 144 145 } 146 | Popular Tags |