KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > IssueListExport


1 package org.tigris.scarab.screens;
2
3 /* ================================================================
4  * Copyright (c) 2003 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 CollabNet <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 CollabNet.
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 COLLABNET 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 CollabNet.
47  */

48
49 import java.util.HashMap JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.Map JavaDoc;
53
54 import org.apache.turbine.RunData;
55 import org.apache.turbine.TemplateContext;
56
57 import org.tigris.scarab.attribute.DateAttribute;
58 import org.tigris.scarab.om.ScarabUser;
59 import org.tigris.scarab.om.RModuleUserAttribute;
60 import org.tigris.scarab.om.Attribute;
61 import org.tigris.scarab.om.MITList;
62 import org.tigris.scarab.util.word.QueryResult;
63 import org.tigris.scarab.tools.ScarabRequestTool;
64 import org.tigris.scarab.tools.ScarabLocalizationTool;
65
66 /**
67  * Handles export of an issue list non-web formats.
68  *
69  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
70  * @author <a HREF="mailto:stack@collab.net">St.Ack</a>
71  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
72  * @see org.tigris.scarab.screens.DataExport
73  * @since Scarab 1.0
74  */

75 public class IssueListExport extends DataExport
76 {
77     Map JavaDoc attributeTypes = new HashMap JavaDoc();
78     TemplateContext ctx = null;
79     
80     /**
81      * Writes the response.
82      */

83     public void doBuildTemplate(RunData data, TemplateContext context)
84         throws Exception JavaDoc
85     {
86         super.doBuildTemplate(data, context);
87         ctx = context;
88
89         ScarabRequestTool scarabR = getScarabRequestTool(context);
90         ScarabLocalizationTool l10n = getLocalizationTool(context);
91         ScarabUser user = (ScarabUser)data.getUser();
92         MITList mitlist = user.getCurrentMITList();
93         TSVPrinter printer = new TSVPrinter(getWriter(data));
94         List JavaDoc rmuas = scarabR.getRModuleUserAttributes();
95         writeHeading(printer, mitlist, l10n, rmuas);
96         writeRows(printer, mitlist, l10n, scarabR, rmuas);
97     }
98
99     /**
100      * Write out the tsv heading.
101      *
102      * @param printer Printer to write on.
103      * @param mitlist Result list headings.
104      * @param l10n Localization tool.
105      * @param rmuas
106      *
107      * @exception Exception
108      */

109     private void writeHeading(TSVPrinter printer, MITList mitlist,
110                               ScarabLocalizationTool l10n, List JavaDoc rmuas)
111         throws Exception JavaDoc
112     {
113         if (mitlist != null)
114         {
115             if (!mitlist.isSingleModule())
116             {
117                 printer.print(l10n.get("CapModule"));
118             }
119
120             if (!mitlist.isSingleIssueType())
121             {
122                 printer.print(l10n.get("IssueType"));
123             }
124         }
125
126         printer.print(l10n.get("IssueId"));
127
128         // ISSUE ATTRIBUTE VALUES as column headings.
129
if (containsElements(rmuas))
130         {
131             int count=0;
132             for (Iterator JavaDoc i = rmuas.iterator(); i.hasNext();)
133             {
134                 RModuleUserAttribute rmua = (RModuleUserAttribute)i.next();
135                 Attribute userAttribute = rmua.getAttribute();
136                 printer.print(userAttribute.getName());
137                 attributeTypes.put(Integer.toString(count), userAttribute.getAttributeType().getName());
138                 count++;
139             }
140         }
141     }
142
143     /**
144      * Write out tsv rows.
145      *
146      * Assumes already written out header.
147      *
148      * @param printer Printer to write on.
149      * @param mitlist Result list headings.
150      * @param l10n Localization tool.
151      * @param scarabR ScarabRequestTool to use.
152      * @param rmuas
153      *
154      * @exception Exception
155      */

156     private void writeRows(TSVPrinter printer, MITList mitlist,
157             ScarabLocalizationTool l10n, ScarabRequestTool scarabR, List JavaDoc rmuas)
158         throws Exception JavaDoc
159     {
160         for (Iterator JavaDoc i = scarabR.getCurrentSearchResults();i.hasNext();)
161         {
162             printer.println();
163             QueryResult queryResult = (QueryResult)i.next();
164             writeRow(printer, mitlist, queryResult);
165         }
166         // print a newline when we're done to complete the last line;
167
// this also ensures that the output is flushed
168
printer.println();
169     }
170
171     /**
172      * Write out a tsv row.
173      *
174      * @param printer Printer to write on.
175      * @param mitlist Result list headings.
176      * @param l10n Localization tool.
177      * @param rmuas
178      * @param issue Issue to write out in row.
179      *
180      * @exception Exception
181      */

182     private void writeRow(TSVPrinter printer, MITList mitlist, QueryResult queryResult)
183         throws Exception JavaDoc
184     {
185         if (mitlist != null)
186         {
187             if (!mitlist.isSingleModule())
188             {
189                 printer.print(queryResult.getModule().getRealName());
190             }
191
192             if (!mitlist.isSingleIssueType())
193             {
194                 printer.print(queryResult.getRModuleIssueType().getDisplayName());
195             }
196         }
197
198         printer.print(queryResult.getUniqueId());
199
200         List JavaDoc values = queryResult.getAttributeValuesAsCSV();
201         if (containsElements(values))
202         {
203             int count=0;
204             for (Iterator JavaDoc itr = values.iterator();itr.hasNext();)
205             {
206                 String JavaDoc val = (String JavaDoc)itr.next();
207                 String JavaDoc type = (String JavaDoc)attributeTypes.get(Integer.toString(count));
208                 count++;
209                 if (type.equals("date"))
210                     val = DateAttribute.dateFormat(val, getLocalizationTool(ctx).get("ShortDatePattern"));
211                 if (val.length() == 0)
212                 {
213                     printer.print(NO_CONTENT);
214                 }
215                 else
216                 {
217                     printer.print(val);
218                 }
219             }
220         }
221     }
222 }
223
Popular Tags