001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.workflow.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.SortedSet;
027: import java.util.TreeSet;
028:
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.lenya.cms.publication.Area;
032: import org.apache.lenya.cms.publication.Document;
033: import org.apache.lenya.cms.publication.DocumentBuildException;
034: import org.apache.lenya.cms.publication.DocumentException;
035: import org.apache.lenya.cms.publication.DocumentFactory;
036: import org.apache.lenya.cms.publication.Publication;
037: import org.apache.lenya.cms.publication.PublicationException;
038: import org.apache.lenya.cms.publication.URLInformation;
039: import org.apache.lenya.cms.site.SiteException;
040: import org.apache.lenya.cms.site.SiteNode;
041: import org.apache.lenya.cms.site.SiteStructure;
042: import org.apache.lenya.cms.usecase.AbstractUsecase;
043:
044: /**
045: * Manage the workflow of multiple documents.
046: */
047: public class MultiWorkflow extends AbstractUsecase {
048:
049: protected void initParameters() {
050: super .initParameters();
051:
052: try {
053: List preOrder = getNodes();
054: List wrappers = new ArrayList();
055: SortedSet states = new TreeSet();
056: for (Iterator i = preOrder.iterator(); i.hasNext();) {
057: Document doc = (Document) i.next();
058: WorkflowableWrapper wrapper = new WorkflowableWrapper(
059: this , this .manager, getDocumentFactory()
060: .getSession(), doc, getLogger());
061: wrappers.add(wrapper);
062: states.addAll(Arrays.asList(wrapper.getStates()));
063: }
064: setParameter("documents", wrappers);
065: setParameter("states", states);
066:
067: } catch (Exception e) {
068: throw new RuntimeException(e);
069: }
070: }
071:
072: protected List getNodes() throws DocumentBuildException,
073: DocumentException, PublicationException {
074: List preOrder;
075: String sourceUrl = getSourceURL();
076: DocumentFactory factory = getDocumentFactory();
077: if (getDocumentFactory().isDocument(sourceUrl)) {
078: Document doc = factory.getFromURL(sourceUrl);
079: preOrder = getPreOrder(doc.getLink().getNode());
080: } else {
081: preOrder = new ArrayList();
082: URLInformation info = new URLInformation(getSourceURL());
083: Publication pub = factory.getPublication(info
084: .getPublicationId());
085: Area area = pub.getArea(info.getArea());
086: SiteStructure site = area.getSite();
087: SiteNode[] topLevelNodes = site.getTopLevelNodes();
088: for (int i = 0; i < topLevelNodes.length; i++) {
089: preOrder.addAll(getPreOrder(topLevelNodes[i]));
090: }
091: }
092: return preOrder;
093: }
094:
095: protected List getPreOrder(SiteNode node) throws SiteException {
096: List preOrder = new ArrayList();
097: String[] langs = node.getLanguages();
098: Arrays.sort(langs);
099: for (int i = 0; i < langs.length; i++) {
100: preOrder.add(node.getLink(langs[i]).getDocument());
101: }
102: SiteNode[] children = node.getChildren();
103: for (int i = 0; i < children.length; i++) {
104: preOrder.addAll(getPreOrder(children[i]));
105: }
106: return preOrder;
107: }
108:
109: private Map usecase2event = new HashMap();
110:
111: public void configure(Configuration config)
112: throws ConfigurationException {
113: super .configure(config);
114:
115: Configuration[] usecaseConfigs = config.getChildren("usecase");
116: for (int i = 0; i < usecaseConfigs.length; i++) {
117: String usecase = usecaseConfigs[i].getAttribute("name");
118: String event = usecaseConfigs[i].getAttribute("event");
119: this .usecase2event.put(usecase, event);
120: }
121: }
122:
123: /**
124: * @param event An event.
125: * @return All usecases associated with this event.
126: */
127: public String[] getUsecases(String event) {
128: SortedSet usecases = new TreeSet();
129: for (Iterator i = this .usecase2event.keySet().iterator(); i
130: .hasNext();) {
131: String usecase = (String) i.next();
132: if (this .usecase2event.get(usecase).equals(event)) {
133: usecases.add(usecase);
134: }
135: }
136: return (String[]) usecases.toArray(new String[usecases.size()]);
137: }
138:
139: }
|