KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > admin > ViewXMLExportIssues


1 package org.tigris.scarab.screens.admin;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48  
49 // Jaav stuff
50
import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.ArrayList JavaDoc;
53
54 import org.apache.velocity.VelocityContext;
55 import org.apache.fulcrum.velocity.TurbineVelocity;
56 import org.apache.turbine.RunData;
57 import org.apache.turbine.TemplateContext;
58 import org.apache.turbine.Turbine;
59
60 import org.tigris.scarab.tools.ScarabRequestTool;
61 import org.tigris.scarab.tools.ScarabLocalizationTool;
62 import org.tigris.scarab.screens.Default;
63 import org.tigris.scarab.om.Issue;
64 import org.tigris.scarab.om.IssueManager;
65 import org.tigris.scarab.util.xmlissues.ImportIssues;
66
67 /**
68  * Sends XML Export issues contents directly to the output stream.
69  *
70  * @author <a HREF="mailto:jon@collab.net">Jon Scott Stevens</a>
71  * @version $Id: ViewXMLExportIssues.java 9412 2005-02-17 18:46:54Z jorgeuriarte $
72  */

73 public class ViewXMLExportIssues extends Default
74 {
75     /**
76      * builds up the context for display of variables on the page.
77      */

78     public void doBuildTemplate(RunData data, TemplateContext context)
79         throws Exception JavaDoc
80     {
81         super.doBuildTemplate(data, context);
82
83         // probably should use intake, but i'm being lazy for now cause
84
// this is only three form variables and not worth the trouble...
85
String JavaDoc filename = data.getParameters().getString("filename");
86         if (filename == null
87             || filename.length() == 0
88             || filename.indexOf('/') > 0
89             || filename.indexOf(':') > 0
90             || filename.indexOf(';') > 0)
91         {
92             filename = "scarab-issues-export.xml";
93         }
94
95         ScarabRequestTool scarabR = getScarabRequestTool(context);
96         org.tigris.scarab.om.Module currentModule = scarabR.getCurrentModule();
97         ScarabLocalizationTool l10n = getLocalizationTool(context);
98         String JavaDoc ids = data.getParameters().getString("exportissues");
99         context.put("exportissues", ids);
100         if (ids == null || ids.length() == 0)
101         {
102             data.setTarget("admin,XMLExportIssues.vm");
103             scarabR.setAlertMessage(l10n.get("EnterIssues"));
104             return;
105         }
106         else
107         {
108             List JavaDoc allIdList = null;
109             try
110             {
111                 // FIXME! we need this method to return valid ids, if the range
112
// is thousands of issues, we cannot post verify the issues.
113
allIdList = Issue.parseIssueList(currentModule, ids);
114             }
115             catch (Exception JavaDoc e)
116             {
117                 data.setTarget("admin,XMLExportIssues.vm");
118                 scarabR.setAlertMessage(l10n.getMessage(e));
119                 return;
120             }
121             List JavaDoc issueIdList = new ArrayList JavaDoc();
122             List JavaDoc badIdList = new ArrayList JavaDoc();
123             Integer JavaDoc currentModuleId = currentModule.getModuleId();
124             String JavaDoc defaultCode = currentModule.getCode();
125             for (Iterator JavaDoc itr = allIdList.iterator(); itr.hasNext();)
126             {
127                 String JavaDoc tmp = (String JavaDoc) itr.next();
128                 Issue issue = IssueManager.getIssueById(tmp, defaultCode);
129                 // check that the issue is in the current module, don't allow
130
// exporting of issues other than those in the current
131
// module for security reasons
132
if (issue != null && !issue.getDeleted()
133                     && issue.getModuleId().equals(currentModuleId))
134                 {
135                     issueIdList.add(tmp);
136                 }
137                 else
138                 {
139                     badIdList.add(tmp);
140                 }
141             }
142             if (issueIdList.isEmpty())
143             {
144                 data.setTarget("admin,XMLExportIssues.vm");
145                 scarabR.setAlertMessage(l10n.get("NoValidIssuesCouldBeLocated"));
146                 return;
147             }
148             else if (!badIdList.isEmpty())
149             {
150                 data.setTarget("admin,XMLExportIssues.vm");
151                 scarabR.setAlertMessage(
152                     l10n.format("FollowingIssueIdsAreInvalid",
153                     badIdList.toString()));
154                 return;
155             }
156
157             String JavaDoc contentType;
158             String JavaDoc contentDisposition;
159             if ("1".equals(data.getParameters().getString("downloadtype")))
160             {
161                 // To browser window.
162
contentType = "text/xml";
163                 contentDisposition = "inline";
164             }
165             else
166             {
167                 // Save to file. Unforunately, not all browsers are
168
// created equal, and some fail to fully heed the
169
// Content-disposition header. We hack around this by
170
// using a Content-type header which indicates binary
171
// data.
172
//contentType = "text/xml";
173
contentType = "application/octet-stream";
174                 contentDisposition = "attachment";
175             }
176             data.getResponse().setContentType(contentType);
177             data.getParameters().add("content-type", contentType);
178             contentDisposition += "; filename=" + filename;
179             data.getParameters().add("content-dispostion", contentDisposition);
180             data.getResponse().setHeader("Content-Disposition",
181                                          contentDisposition);
182     
183             context.put("issueIdList", issueIdList);
184             VelocityContext vc = new VelocityContext();
185             for (Iterator JavaDoc keys = context.keySet().iterator(); keys.hasNext(); )
186             {
187                 String JavaDoc key = (String JavaDoc) keys.next();
188                 vc.put(key, context.get(key));
189             }
190             vc.put("dtdURI", ImportIssues.SYSTEM_DTD_URI);
191
192             String JavaDoc encoding = Turbine.getConfiguration()
193                 .getString("scarab.dataexport.encoding");
194             if (encoding != null && !encoding.equals(""))
195             {
196                 TurbineVelocity.handleRequest
197                     (vc, "macros/XMLExportIssuesMacro.vm",
198                      data.getResponse().getOutputStream(), encoding, encoding);
199             }
200             else
201             {
202                 TurbineVelocity.handleRequest
203                     (vc, "macros/XMLExportIssuesMacro.vm",
204                      data.getResponse().getOutputStream());
205             }
206     
207             // we already sent the response, there is no target to render
208
data.setTarget(null);
209         }
210     }
211 }
212
Popular Tags