KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > transformation > WorkflowMenuTransformer


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: WorkflowMenuTransformer.java 109488 2004-12-02 10:12:22Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.transformation;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.environment.SourceResolver;
28 import org.apache.cocoon.transformation.AbstractSAXTransformer;
29 import org.apache.lenya.cms.cocoon.workflow.WorkflowHelper;
30 import org.apache.lenya.cms.publication.Document;
31 import org.apache.lenya.cms.publication.PageEnvelope;
32 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
33 import org.apache.lenya.cms.workflow.WorkflowFactory;
34 import org.apache.lenya.workflow.Event;
35 import org.apache.lenya.workflow.Situation;
36 import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
37 import org.apache.lenya.workflow.Workflow;
38 import org.apache.lenya.workflow.WorkflowException;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.helpers.AttributesImpl JavaDoc;
42
43 /**
44  * This transformer disables menu items (by removing the href attribute)
45  * which are not allowed with respect to the current workflow state.
46  */

47 public class WorkflowMenuTransformer extends AbstractSAXTransformer {
48     public static final String JavaDoc MENU_ELEMENT = "menu";
49     public static final String JavaDoc ITEM_ELEMENT = "item";
50     public static final String JavaDoc EVENT_ATTRIBUTE = "event";
51
52     /** (non-Javadoc)
53      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
54      */

55     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc raw, Attributes JavaDoc attr)
56         throws SAXException JavaDoc {
57         boolean passed = true;
58
59         if (hasWorkflow() && localName.equals(ITEM_ELEMENT)) {
60             String JavaDoc event = attr.getValue(Workflow.NAMESPACE, EVENT_ATTRIBUTE);
61             if (getLogger().isDebugEnabled()) {
62                 getLogger().debug("Event: [" + event + "]");
63             }
64
65             // filter item if command not allowed
66
if (event != null) {
67                 passed = false;
68
69                 AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc(attr);
70
71                 int hrefIndex = attributes.getIndex("href");
72                 if (hrefIndex > -1) {
73
74                     if (!containsEvent(event)) {
75                         if (getLogger().isDebugEnabled()) {
76                             getLogger().debug("Removing href attribute");
77                         }
78                         attributes.removeAttribute(hrefIndex);
79                     } else {
80                         if (getLogger().isDebugEnabled()) {
81                             getLogger().debug("Adding event to href attribute");
82                         }
83                         String JavaDoc href = attributes.getValue("href");
84                         attributes.setValue(hrefIndex, href + "&lenya.event=" + event);
85                     }
86
87                 }
88
89                 super.startElement(uri, localName, raw, attributes);
90             }
91         }
92
93         if (passed) {
94             super.startElement(uri, localName, raw, attr);
95         }
96
97     }
98
99     /**
100      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
101      */

102     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
103         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
104
105         super.setup(resolver, objectModel, src, parameters);
106         
107         PageEnvelope envelope = null;
108
109         try {
110             envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
111         } catch (Exception JavaDoc e) {
112             throw new ProcessingException(e);
113         }
114
115         Document document = envelope.getDocument();
116         WorkflowFactory factory = WorkflowFactory.newInstance();
117
118         setHasWorkflow(factory.hasWorkflow(document));
119
120         if (hasWorkflow()) {
121             Situation situation = null;
122
123             try {
124                 setInstance(factory.buildSynchronizedInstance(document));
125                 situation = WorkflowHelper.buildSituation(objectModel);
126             } catch (Exception JavaDoc e) {
127                 throw new ProcessingException(e);
128             }
129
130             try {
131                 this.events = getInstance().getExecutableEvents(situation);
132
133                 if (getLogger().isDebugEnabled()) {
134                     getLogger().debug("Executable events: ");
135                     for (int i = 0; i < events.length; i++) {
136                         getLogger().debug(" [" + events[i] + "]");
137                     }
138                 }
139
140             } catch (WorkflowException e) {
141                 throw new ProcessingException(e);
142             }
143         }
144     }
145
146     private boolean hasWorkflow;
147     private SynchronizedWorkflowInstances instance;
148
149     /**
150      * Get the workflow instance.
151      *
152      * @return a <code>WorkflowInstance</code>
153      */

154     protected SynchronizedWorkflowInstances getInstance() {
155         return instance;
156     }
157
158     private Event[] events;
159
160     /**
161      * Returns if the events contain a specific event.
162      * @param eventName The name of the event to check for.
163      * @return A boolean value.
164      */

165     protected boolean containsEvent(String JavaDoc eventName) {
166         boolean result = false;
167
168         for (int i = 0; i < events.length; i++) {
169             if (events[i].getName().equals(eventName)) {
170                 result = true;
171             }
172         }
173
174         return result;
175     }
176
177     /**
178      * Returns if the current document has a workflow.
179      * @return A boolean value.
180      */

181     protected boolean hasWorkflow() {
182         return hasWorkflow;
183     }
184
185     /**
186      * Sets if the current document has a workflow.
187      * @param hasWorkflow A boolean value.
188      */

189     public void setHasWorkflow(boolean hasWorkflow) {
190         this.hasWorkflow = hasWorkflow;
191     }
192
193     /**
194      * Sets the workflow instance for the current request.
195      * @param instance A workflow instance.
196      */

197     public void setInstance(SynchronizedWorkflowInstances instance) {
198         this.instance = instance;
199     }
200 }
201
Popular Tags