KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > multiplesitesdocument > RequestPublicationInputFormGenerator


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package nl.hippo.cms.workflows.multiplesitesdocument;
17
18 import java.io.IOException JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import nl.hippo.cms.sitesdirectory.SiteInformation;
22 import nl.hippo.cms.sitesdirectory.SitesDirectory;
23 import nl.hippo.cms.workflows.shared.ResourceGeneratorComponent;
24 import nl.hippo.componentcontainers.AvalonSpringBridge;
25 import nl.hippo.servermanager.Project;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.commons.httpclient.HttpState;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 public class RequestPublicationInputFormGenerator extends ResourceGeneratorComponent
33 {
34
35     public RequestPublicationInputFormGenerator()
36     {
37         super();
38     }
39
40     public void generateImpl(String JavaDoc uri, Project project, DefaultHandler JavaDoc handler)
41             throws SAXException JavaDoc
42     {
43         try
44         {
45             SitesDirectory sitesDir = (SitesDirectory) m_manager.lookup(SitesDirectory.ROLE);
46             try
47             {
48                 handler.startDocument();
49
50                 sendForm(uri, project, handler, sitesDir);
51
52                 handler.endDocument();
53             }
54             finally
55             {
56                 m_manager.release(sitesDir);
57             }
58         }
59         catch (ServiceException e)
60         {
61             throw new SAXException JavaDoc(e);
62         }
63     }
64
65     private void sendForm(String JavaDoc uri, Project project, DefaultHandler JavaDoc handler,
66             SitesDirectory sitesDir) throws SAXException JavaDoc
67     {
68         handler.startElement(null, "form", "form", new AttributesImpl JavaDoc());
69
70         boolean outputCheckboxes = sitesDir.getNumberOfSites() > 1;
71         Iterator JavaDoc siteIdsIter = sitesDir.siteIdsIterator();
72         while (siteIdsIter.hasNext())
73         {
74             String JavaDoc siteId = (String JavaDoc) siteIdsIter.next();
75             SiteInformation siteInfo = sitesDir.getSite(siteId);
76             sendSiteWidgets(uri, project, handler, siteInfo, outputCheckboxes);
77         }
78
79         handler.endElement(null, "form", "form");
80     }
81
82     private void sendSiteWidgets(String JavaDoc uri, Project project, DefaultHandler JavaDoc handler,
83             SiteInformation siteInfo, boolean outputCheckboxes) throws SAXException JavaDoc
84     {
85         try
86         {
87             AvalonSpringBridge avalonSpringBridge = (AvalonSpringBridge) m_manager
88                     .lookup(AvalonSpringBridge.ROLE);
89             try
90             {
91                 HttpState httpState = (HttpState) avalonSpringBridge.getBean("httpState");
92                 boolean isPublished = MultipleSitesDocumentUtil.isPublishedOnSite(uri, siteInfo
93                         .getId(), project, httpState);
94
95                 if (outputCheckboxes)
96                 {
97                     AttributesImpl JavaDoc selectionAttr = new AttributesImpl JavaDoc();
98                     selectionAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
99                             + "-selection");
100                     selectionAttr.addAttribute(null, "parameter-box-id", "parameter-box-id", "CDATA", siteInfo.getId()
101                             + "-parameters");
102                     handler.startElement(null, "checkbox", "checkbox", selectionAttr);
103                     handler.startElement(null, "output", "output", new AttributesImpl JavaDoc());
104                     // TODO: where to get locale?
105
String JavaDoc siteName = siteInfo.getName("en");
106                     handler.characters(siteName.toCharArray(), 0, siteName.length());
107                     handler.endElement(null, "output", "output");
108                     sendPublicationInfoAndParameters(uri, project, handler, siteInfo, isPublished);
109                     handler.endElement(null, "checkbox", "checkbox");
110                 }
111                 else
112                 {
113                     sendPublicationInfoAndParameters(uri, project, handler, siteInfo, isPublished);
114                 }
115             }
116             finally
117             {
118                 m_manager.release(avalonSpringBridge);
119             }
120         }
121         catch (IOException JavaDoc e)
122         {
123             throw new SAXException JavaDoc(e);
124         }
125         catch (ServiceException e)
126         {
127             throw new SAXException JavaDoc(e);
128         }
129     }
130
131     private void sendPublicationInfoAndParameters(String JavaDoc uri, Project project, DefaultHandler JavaDoc handler, SiteInformation siteInfo,
132             boolean isPublished) throws SAXException JavaDoc, ServiceException, IOException JavaDoc
133     {
134         handler.startElement(null, "output", "output", new AttributesImpl JavaDoc());
135         if (isPublished)
136         {
137             VersionInformation versionInfo = MultipleSitesDocumentUtil
138                     .getPublishedVersionInformation(uri, project, siteInfo.getId(), m_manager);
139             // TODO: where to get locale?
140
MessageFormat JavaDoc mf = new MessageFormat JavaDoc("Version {0}. {1}");
141             StringBuffer JavaDoc versionInfoTextBuffer = mf.format(new Object JavaDoc[]
142             {
143                     new Integer JavaDoc(versionInfo.getVersionNumber()), versionInfo.getComment(),
144             }, new StringBuffer JavaDoc(), null);
145             String JavaDoc versionInfoText = versionInfoTextBuffer.toString();
146             handler.characters(versionInfoText.toCharArray(), 0, versionInfoText.length());
147         }
148         else
149         {
150             // TODO: where to get locale?
151
String JavaDoc notPublishedText = "Not published.";
152             handler.characters(notPublishedText.toCharArray(), 0, notPublishedText.length());
153         }
154         handler.endElement(null, "output", "output");
155
156         AttributesImpl JavaDoc groupAttributes = new AttributesImpl JavaDoc();
157         groupAttributes.addAttribute(null, "id", "id", "CDATA", siteInfo.getId() + "-parameters");
158         groupAttributes.addAttribute(null, "visible", "visible", "CDATA", isPublished
159                 ? "true"
160                 : "false");
161         handler.startElement(null, "group", "group", groupAttributes);
162
163         // Publication-date mode label
164
AttributesImpl JavaDoc pubDateModeLabelAttr = new AttributesImpl JavaDoc();
165         pubDateModeLabelAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
166                 + "-publicationDateMode");
167         handler.startElement(null, "label", "label", pubDateModeLabelAttr);
168         handler.endElement(null, "label", "label");
169
170         // Publication date mode drop-down
171
AttributesImpl JavaDoc pubDateModeAttr = new AttributesImpl JavaDoc();
172         pubDateModeAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
173                 + "-publicationDateMode");
174         pubDateModeAttr.addAttribute(null, "appearance", "appearance", "CDATA", "full");
175         handler.startElement(null, "select1", "select1", pubDateModeAttr);
176         handler.endElement(null, "select1", "select1");
177
178         // Publication date
179
AttributesImpl JavaDoc pubDateAttr = new AttributesImpl JavaDoc();
180         pubDateAttr
181                 .addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId() + "-publicationDate");
182         handler.startElement(null, "input", "input", pubDateAttr);
183         handler.endElement(null, "input", "input");
184
185         // Unpublication-date mode label
186
AttributesImpl JavaDoc unpubDateModeLabelAttr = new AttributesImpl JavaDoc();
187         unpubDateModeLabelAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
188                 + "-unpublicationDateMode");
189         handler.startElement(null, "label", "label", unpubDateModeLabelAttr);
190         handler.endElement(null, "label", "label");
191
192         // Unpublication date mode drop-down
193
AttributesImpl JavaDoc unpubDateModeAttr = new AttributesImpl JavaDoc();
194         unpubDateModeAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
195                 + "-unpublicationDateMode");
196         unpubDateModeAttr.addAttribute(null, "appearance", "appearance", "CDATA", "full");
197         handler.startElement(null, "select1", "select1", unpubDateModeAttr);
198         handler.endElement(null, "select1", "select1");
199
200         // Unpublication date
201
AttributesImpl JavaDoc unpubDateAttr = new AttributesImpl JavaDoc();
202         unpubDateAttr.addAttribute(null, "ref", "ref", "CDATA", siteInfo.getId()
203                 + "-unpublicationDate");
204         handler.startElement(null, "input", "input", unpubDateAttr);
205         handler.endElement(null, "input", "input");
206
207         handler.endElement(null, "group", "group");
208     }
209
210 }
211
Popular Tags