KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > components > modules > input > WorkflowModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: WorkflowModule.java 43222 2004-08-16 12:16:45Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.components.modules.input;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.cocoon.components.modules.input.AbstractInputModule;
29 import org.apache.lenya.cms.publication.Document;
30 import org.apache.lenya.cms.publication.PageEnvelope;
31 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
32 import org.apache.lenya.cms.workflow.CMSHistory;
33 import org.apache.lenya.cms.workflow.WorkflowFactory;
34 import org.apache.lenya.workflow.WorkflowInstance;
35
36 public class WorkflowModule extends AbstractInputModule {
37
38     public static final String JavaDoc STATE = "state";
39     public static final String JavaDoc VARIABLE_PREFIX = "variable.";
40     public static final String JavaDoc HISTORY_PATH = "history-path";
41
42     static final String JavaDoc[] PARAMETER_NAMES = { STATE, HISTORY_PATH };
43
44     /**
45      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
46      */

47     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
48         throws ConfigurationException {
49
50         Object JavaDoc value = null;
51
52         
53         try {
54             PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
55             Document document = envelope.getDocument();
56
57             WorkflowFactory factory = WorkflowFactory.newInstance();
58             if (factory.hasWorkflow(document)) {
59                 WorkflowInstance instance = factory.buildInstance(document);
60                 if (name.equals(STATE)) {
61                     value = instance.getCurrentState().toString();
62                 }
63                 else if (name.startsWith(VARIABLE_PREFIX)) {
64                     String JavaDoc variableName = name.substring(VARIABLE_PREFIX.length());
65                     String JavaDoc[] variableNames = instance.getWorkflow().getVariableNames();
66                     if (Arrays.asList(variableNames).contains(variableName)) {
67                         value = Boolean.valueOf(instance.getValue(variableName));
68                     }
69                 }
70                 else if (name.equals(HISTORY_PATH)) {
71                     value = ((CMSHistory) WorkflowFactory.getHistory(document)).getHistoryPath();
72                 }
73                 else {
74                     throw new ConfigurationException("The attribute [" + name + "] is not supported!");
75                 }
76             }
77         } catch (ConfigurationException e) {
78             throw e;
79         } catch (Exception JavaDoc e) {
80             throw new ConfigurationException("Resolving attribute failed: ", e);
81         }
82         return value;
83     }
84
85     /**
86      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
87      */

88     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
89         throws ConfigurationException {
90         return Arrays.asList(PARAMETER_NAMES).iterator();
91     }
92
93     /**
94      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
95      */

96     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
97         throws ConfigurationException {
98         Object JavaDoc[] objects = { getAttribute(name, modeConf, objectModel)};
99
100         return objects;
101     }
102     
103 }
104
Popular Tags