KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > util > ItemUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
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 package info.jtrac.util;
18
19 import info.jtrac.domain.Attachment;
20 import info.jtrac.domain.Field;
21 import info.jtrac.domain.History;
22 import info.jtrac.domain.Item;
23 import info.jtrac.domain.ItemItem;
24 import info.jtrac.webflow.ItemViewFormAction.ItemViewForm;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import org.springframework.context.MessageSource;
32 import org.springframework.util.StringUtils;
33 import org.springframework.web.servlet.support.RequestContextUtils;
34 import org.springframework.web.util.HtmlUtils;
35
36 /**
37  * Utilities to convert an Item into HTML etc.
38  * The getAsHtml() routine is used to diplay an item - within a tag lib for JSP
39  * And we are able to re-use this to send HTML e-mail etc.
40  */

41 public final class ItemUtils {
42     
43     public static String JavaDoc fixWhiteSpace(String JavaDoc text) {
44         if (text == null) {
45             return "";
46         }
47         String JavaDoc temp = HtmlUtils.htmlEscape(text);
48         return temp.replaceAll("\n", "<br/>").replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
49     }
50     
51     private static String JavaDoc fmt(String JavaDoc key, MessageSource messageSource, Locale JavaDoc locale) {
52         try {
53             return messageSource.getMessage("item_view." + key, null, locale);
54         } catch (Exception JavaDoc e) {
55             return "???item_view." + key + "???";
56         }
57     }
58     
59     public static String JavaDoc getAsHtml(Item item, MessageSource messageSource, Locale JavaDoc locale) {
60         return getAsHtml(item, null, null, messageSource, locale);
61     }
62     
63     public static String JavaDoc getAsHtml(Item item, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
64         Locale JavaDoc locale = RequestContextUtils.getLocale(request);
65         MessageSource messageSource = RequestContextUtils.getWebApplicationContext(request);
66         return getAsHtml(item, request, response, messageSource, locale);
67     }
68     
69     private static String JavaDoc getAsHtml(Item item, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
70             MessageSource ms, Locale JavaDoc loc) {
71         
72         boolean isWeb = request != null && response != null;
73         
74         String JavaDoc tableStyle = " class='jtrac'";
75         String JavaDoc tdStyle = "";
76         String JavaDoc thStyle = "";
77         String JavaDoc altStyle = " class='alt'";
78         String JavaDoc labelStyle = " class='label'";
79         
80         if (!isWeb) {
81             // inline CSS so that HTML mail works across most mail-reader clients
82
String JavaDoc tdCommonStyle = "border: 1px solid black";
83             tableStyle = " class='jtrac' style='border-collapse: collapse; font-family: Arial; font-size: 80%'";
84             tdStyle = " style='" + tdCommonStyle + "'";
85             thStyle = " style='" + tdCommonStyle + "; background: #CCCCCC'";
86             altStyle = " style='background: #DEDEFF'";
87             labelStyle = " style='" + tdCommonStyle + "; background: #CCCCCC; font-weight: bold; text-align: right'";
88         }
89
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91         sb.append("<table width='100%'" + tableStyle + ">");
92         sb.append("<tr" + altStyle + ">");
93         sb.append(" <td" + labelStyle + ">" + fmt("id", ms, loc) + "</td>");
94         sb.append(" <td" + tdStyle + ">" + item.getRefId() + "</td>");
95         sb.append(" <td" + labelStyle + ">" + fmt("relatedItems", ms, loc) + "</td>");
96         sb.append(" <td colspan='3'" + tdStyle + ">");
97         if (item.getRelatedItems() != null || item.getRelatingItems() != null) {
98             String JavaDoc flowUrlParam = null;
99             String JavaDoc flowUrl = null;
100             if (isWeb) {
101                 flowUrlParam = "_flowExecutionKey=" + request.getAttribute("flowExecutionKey");
102                 flowUrl = "/flow?" + flowUrlParam;
103             }
104             if (item.getRelatedItems() != null) {
105                 ItemViewForm itemViewForm = null;
106                 if (isWeb) {
107                     itemViewForm = (ItemViewForm) request.getAttribute("itemViewForm");
108                     sb.append("<input type='hidden' name='_removeRelated'/>");
109                 }
110                 for(ItemItem itemItem : item.getRelatedItems()) {
111                     String JavaDoc refId = itemItem.getRelatedItem().getRefId();
112                     if (isWeb) {
113                         String JavaDoc checked = "";
114                         Set JavaDoc<Long JavaDoc> set = itemViewForm.getRemoveRelated();
115                         if (set != null && set.contains(itemItem.getId())) {
116                             checked = " checked='true'";
117                         }
118                         String JavaDoc url = flowUrl + "&_eventId=viewRelated&itemId=" + itemItem.getRelatedItem().getId();
119                         refId = "<a HREF='" + response.encodeURL(request.getContextPath() + url) + "'>" + refId + "</a>"
120                                 + "<input type='checkbox' name='removeRelated' value='"
121                                 + itemItem.getId() + "' title='" + fmt("remove", ms, loc) + "'" + checked + "/>";
122                     }
123                     sb.append(fmt(itemItem.getRelationText(), ms, loc) + " " + refId + " ");
124                 }
125             }
126             if (item.getRelatingItems() != null) {
127                 for(ItemItem itemItem : item.getRelatingItems()) {
128                     String JavaDoc refId = itemItem.getItem().getRefId();
129                     if (isWeb) {
130                         String JavaDoc url = flowUrl + "&_eventId=viewRelated&itemId=" + itemItem.getItem().getId();
131                         refId = "<a HREF='" + response.encodeURL(request.getContextPath() + url) + "'>" + refId + "</a>";
132                     }
133                     sb.append(refId + " " + fmt(itemItem.getRelationText() + "This", ms, loc) + ". ");
134                 }
135             }
136         }
137         sb.append(" </td>");
138         sb.append("</tr>");
139         sb.append("<tr>");
140         sb.append(" <td width='20%'" + labelStyle + ">" + fmt("status", ms, loc) + "</td>");
141         sb.append(" <td" + tdStyle + ">" + item.getStatusValue() + "</td>");
142         sb.append(" <td" + labelStyle + ">" + fmt("loggedBy", ms, loc) + "</td>");
143         sb.append(" <td" + tdStyle + ">" + item.getLoggedBy().getName() + "</td>");
144         sb.append(" <td" + labelStyle + ">" + fmt("assignedTo", ms, loc) + "</td>");
145         sb.append(" <td width='15%'" + tdStyle + ">" + ( item.getAssignedTo() == null ? "" : item.getAssignedTo().getName() ) + "</td>");
146         sb.append("</tr>");
147         sb.append("<tr" + altStyle + ">");
148         sb.append(" <td" + labelStyle + ">" + fmt("summary", ms, loc) + "</td>");
149         sb.append(" <td colspan='5'" + tdStyle + ">" + HtmlUtils.htmlEscape(item.getSummary()) + "</td>");
150         sb.append("</tr>");
151         sb.append("<tr>");
152         sb.append(" <td valign='top'" + labelStyle + ">" + fmt("detail", ms, loc) + "</td>");
153         sb.append(" <td colspan='5'" + tdStyle + ">" + fixWhiteSpace(item.getDetail()) + "</td>");
154         sb.append("</tr>");
155         
156         int row = 0;
157         Map JavaDoc<Field.Name, Field> fields = item.getSpace().getMetadata().getFields();
158         for(Field.Name fieldName : item.getSpace().getMetadata().getFieldOrder()) {
159             Field field = fields.get(fieldName);
160             sb.append("<tr" + ( row % 2 == 0 ? altStyle : "" ) + ">");
161             sb.append(" <td" + labelStyle + ">" + field.getLabel() + "</td>");
162             sb.append(" <td colspan='5'" + tdStyle + ">" + item.getCustomValue(fieldName) + "</td>");
163             sb.append("</tr>");
164             row ++;
165         }
166         sb.append("</table>");
167         
168         //=========================== HISTORY ==================================
169
sb.append("<br/>&nbsp;<b" + tableStyle + ">" + fmt("history", ms, loc) + "</b>");
170         sb.append("<table width='100%'" + tableStyle + ">");
171         sb.append("<tr>");
172         sb.append(" <th" + thStyle + ">" + fmt("loggedBy", ms, loc) + "</th><th" + thStyle + ">" + fmt("status", ms, loc) + "</th>"
173                 + "<th" + thStyle + ">" + fmt("assignedTo", ms, loc) + "</th><th" + thStyle + ">" + fmt("comment", ms, loc) + "</th><th" + thStyle + ">" + fmt("timeStamp", ms, loc) + "</th>");
174         List JavaDoc<Field> editable = item.getSpace().getMetadata().getEditableFields();
175         for(Field field : editable) {
176             sb.append("<th" + thStyle + ">" + field.getLabel() + "</th>");
177         }
178         sb.append("</tr>");
179         
180         if (item.getHistory() != null) {
181             row = 1;
182             for(History history : item.getHistory()) {
183                 sb.append("<tr valign='top'" + ( row % 2 == 0 ? altStyle : "" ) + ">");
184                 sb.append(" <td" + tdStyle + ">" + history.getLoggedBy().getName() + "</td>");
185                 sb.append(" <td" + tdStyle + ">" + history.getStatusValue() +"</td>");
186                 sb.append(" <td" + tdStyle + ">" + ( history.getAssignedTo() == null ? "" : history.getAssignedTo().getName() ) + "</td>");
187                 sb.append(" <td" + tdStyle + ">");
188                 Attachment attachment = history.getAttachment();
189                 if (attachment != null) {
190                     if (request != null && response != null) {
191                         String JavaDoc href = response.encodeURL(request.getContextPath() + "/app/attachments/" + attachment.getFileName() +"?filePrefix=" + attachment.getFilePrefix());
192                         sb.append("<a target='_blank' HREF='" + href + "'>" + attachment.getFileName() + "</a>&nbsp;");
193                     } else {
194                         sb.append("(attachment:&nbsp;" + attachment.getFileName() + ")&nbsp;");
195                     }
196                 }
197                 sb.append(fixWhiteSpace(history.getComment()));
198                 sb.append(" </td>");
199                 sb.append(" <td" + tdStyle + ">" + history.getTimeStamp() + "</td>");
200                 for(Field field : editable) {
201                     sb.append("<td" + tdStyle + ">" + history.getCustomValue(field.getName()) + "</td>");
202                 }
203                 sb.append("</tr>");
204                 row++;
205             }
206         }
207         sb.append("</table>");
208         return sb.toString();
209     }
210     
211 }
212
Popular Tags