001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.workflow;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Properties;
027:
028: import javax.xml.transform.TransformerException;
029:
030: import org.apache.log4j.Logger;
031: import org.w3c.dom.Document;
032:
033: import de.schlund.pfixcore.exception.PustefixApplicationException;
034: import de.schlund.pfixcore.exception.PustefixCoreException;
035: import de.schlund.pfixcore.workflow.context.PageFlow;
036: import de.schlund.pfixxml.ResultDocument;
037: import de.schlund.pfixxml.config.PageFlowStepActionConditionConfig;
038: import de.schlund.pfixxml.config.PageFlowStepActionConfig;
039: import de.schlund.pfixxml.config.PageFlowStepConfig;
040: import de.schlund.pfixxml.util.XPath;
041:
042: /**
043: * @author: jtl@schlund.de
044: *
045: */
046:
047: public class FlowStep {
048:
049: private ArrayList<List<FlowStepAction>> actions_oncontinue = new ArrayList<List<FlowStepAction>>();
050: private ArrayList<String> tests_oncontinue = new ArrayList<String>();
051: private PageFlowStepConfig config;
052:
053: private final static Logger LOG = Logger.getLogger(PageFlow.class);
054:
055: public FlowStep(PageFlowStepConfig config) {
056: this .config = config;
057:
058: for (PageFlowStepActionConditionConfig condition : config
059: .getActionConditions()) {
060: ArrayList<FlowStepAction> actionList = new ArrayList<FlowStepAction>();
061: for (PageFlowStepActionConfig actionConfig : condition
062: .getActions()) {
063: FlowStepAction action = FlowStepActionFactory
064: .getInstance().createAction(
065: actionConfig.getActionType().getName());
066: HashMap<String, String> datamap = new HashMap<String, String>();
067: Properties params = actionConfig.getParams();
068: for (Iterator<?> k = params.keySet().iterator(); k
069: .hasNext();) {
070: String key = (String) k.next();
071: String value = params.getProperty(key);
072: datamap.put(key, value);
073: }
074: action.setData(datamap);
075: actionList.add(action);
076: }
077: actions_oncontinue.add(actionList);
078: tests_oncontinue.add(condition.getXPathExpression());
079: }
080:
081: }
082:
083: public void applyActionsOnContinue(Context context,
084: ResultDocument resdoc) throws PustefixApplicationException,
085: PustefixCoreException {
086: if (!actions_oncontinue.isEmpty()) {
087: for (int i = 0; i < actions_oncontinue.size(); i++) {
088: List<FlowStepAction> actionList = actions_oncontinue
089: .get(i);
090: String test = (String) tests_oncontinue.get(i);
091: LOG.debug("*** [" + this .config.getPage()
092: + "] Trying on-continue-action #" + i);
093: boolean check;
094: try {
095: check = checkAction(test, resdoc.getSPDocument()
096: .getDocument());
097: } catch (TransformerException e) {
098: throw new PustefixCoreException(
099: "Error while testing XPath expression \""
100: + test + "\" for page "
101: + this .getPageName(), e);
102: }
103: if (check) {
104: LOG
105: .debug(" ===> Action applies, calling doAction now...");
106: for (Iterator<FlowStepAction> j = actionList
107: .iterator(); j.hasNext();) {
108: FlowStepAction action = j.next();
109: try {
110: action.doAction(context, resdoc);
111: } catch (Exception e) {
112: throw new PustefixApplicationException(
113: "Exception while running doAction() on action "
114: + action.getClass()
115: + " for page "
116: + this .getPageName(), e);
117: }
118: }
119:
120: if (!this .config.isApplyAllConditions()) {
121: break;
122: }
123:
124: } else {
125: LOG.debug(" ===> Action didn't apply, continue");
126: }
127: }
128: }
129: }
130:
131: private boolean checkAction(String test, Document doc)
132: throws TransformerException {
133: if (test.equals("")) {
134: return true;
135: }
136: return XPath.test(doc.getDocumentElement(), test);
137: }
138:
139: public boolean hasOnContinueAction() {
140: if (actions_oncontinue.size() > 0) {
141: return true;
142: } else {
143: return false;
144: }
145: }
146:
147: public String getPageName() {
148: return this .config.getPage();
149: }
150:
151: public String getName() {
152: return this .config.getPage();
153: }
154:
155: public boolean wantsToStopHere() {
156: return this .config.isStopHere();
157: }
158:
159: @Override
160: public String toString() {
161: return ("[page=" + this .config.getPage() + " stophere="
162: + this .config.isStopHere() + " #cont_actions="
163: + actions_oncontinue.size() + "]");
164: }
165: }
|