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.context;
021:
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.log4j.Logger;
029:
030: import de.schlund.pfixcore.workflow.PageRequest;
031: import de.schlund.pfixcore.workflow.VariantManager;
032: import de.schlund.pfixxml.Variant;
033: import de.schlund.pfixxml.config.ContextConfig;
034: import de.schlund.pfixxml.config.PageFlowConfig;
035: import de.schlund.pfixxml.config.PageFlowStepConfig;
036:
037: /**
038: * @author: jtl
039: *
040: */
041:
042: public class PageFlowManager {
043: private Map<String, Set<String>> pagetoflowmap = new HashMap<String, Set<String>>();
044: private Map<String, PageFlow> flowmap = new HashMap<String, PageFlow>();
045:
046: private VariantManager vmanager;
047: private ContextConfig config;
048:
049: private final static Logger LOG = Logger
050: .getLogger(PageFlowManager.class);
051:
052: public final static String PROP_PREFIX = "context.pageflow";
053:
054: public PageFlowManager(ContextConfig config,
055: VariantManager variantmanager) {
056: vmanager = variantmanager;
057: this .config = config;
058:
059: // Initialize map mapping each page name to a list of
060: // flows which contain this page in at least one variant
061: // and create PageFlow object for each flow
062: for (PageFlowConfig pageflowConfig : config
063: .getPageFlowConfigs()) {
064: PageFlow flow = new PageFlow(pageflowConfig);
065: flowmap.put(flow.getName(), flow);
066:
067: String rootname = flow.getRootName();
068: for (PageFlowStepConfig stepConfig : pageflowConfig
069: .getFlowSteps()) {
070: String pagename = stepConfig.getPage();
071: Set<String> names = pagetoflowmap.get(pagename);
072: if (names == null) {
073: names = new HashSet<String>();
074: pagetoflowmap.put(pagename, names);
075: }
076: if (!names.contains(rootname)) {
077: names.add(rootname);
078: }
079: }
080: }
081: }
082:
083: protected PageFlow pageFlowToPageRequest(PageFlow currentflow,
084: PageRequest page, Variant variant) {
085: LOG.debug("===> Testing pageflow: " + currentflow.getName()
086: + " / page: " + page);
087: if (!currentflow.containsPage(page.getRootName())) {
088: Set<String> rootflownames = pagetoflowmap.get(page
089: .getRootName());
090: if (rootflownames == null) {
091: LOG
092: .debug("===> Page "
093: + page
094: + " isn't a member of any pageflow: Reusing flow "
095: + currentflow.getName());
096: return currentflow;
097: }
098: String defaultFlowForRequest = this .config
099: .getPageRequestConfig(page.getName())
100: .getDefaultFlow();
101: if (defaultFlowForRequest != null) {
102: LOG.debug("===> Page " + page
103: + " has a default flow specified: Using flow "
104: + defaultFlowForRequest);
105: String pageflowname = vmanager
106: .getVariantMatchingPageFlowName(
107: defaultFlowForRequest, variant);
108: PageFlow pf = getPageFlowByName(pageflowname);
109: if (pf.containsPage(page.getRootName())) {
110: LOG.debug("===> Switching to pageflow: "
111: + pf.getName());
112: return pf;
113: }
114: }
115: for (Iterator<String> i = rootflownames.iterator(); i
116: .hasNext();) {
117: String pageflowname = vmanager
118: .getVariantMatchingPageFlowName(i.next(),
119: variant);
120: PageFlow pf = getPageFlowByName(pageflowname);
121: if (pf.containsPage(page.getRootName())) {
122: LOG.debug("===> Switching to pageflow: "
123: + pf.getName());
124: return pf;
125: }
126: }
127: LOG.debug("===> Page " + page
128: + " isn't a member of any pageflow: Reusing flow "
129: + currentflow.getName());
130: } else {
131: LOG.debug("===> Page " + page
132: + " is member of current pageflow: Reusing flow "
133: + currentflow.getName());
134: }
135: return currentflow;
136: }
137:
138: protected PageFlow getPageFlowByName(String rootname,
139: Variant variant) {
140: return getPageFlowByName(vmanager
141: .getVariantMatchingPageFlowName(rootname, variant));
142: }
143:
144: protected PageFlow getPageFlowByName(String fullname) {
145: return flowmap.get(fullname);
146: }
147: }
|