KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > web > tag > ItemList


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.web.tag;
18
19 import info.jtrac.domain.AbstractItem;
20 import info.jtrac.domain.Attachment;
21 import info.jtrac.domain.Field;
22 import info.jtrac.domain.History;
23 import info.jtrac.domain.ItemSearch;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.jsp.JspWriter JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
32 import org.springframework.context.MessageSource;
33 import org.springframework.web.servlet.support.RequestContextUtils;
34 import org.springframework.web.util.HtmlUtils;
35
36 public class ItemList extends SimpleTagSupport JavaDoc {
37     
38     private HttpServletRequest JavaDoc request;
39     private HttpServletResponse JavaDoc response;
40     private List JavaDoc<AbstractItem> items;
41     private ItemSearch itemSearch;
42     
43     public void setItems(List JavaDoc<AbstractItem> items) {
44         this.items = items;
45     }
46     
47     public void setItemSearch(ItemSearch itemSearch) {
48         this.itemSearch = itemSearch;
49     }
50     
51     private static String JavaDoc fmt(String JavaDoc key, MessageSource messageSource, Locale JavaDoc locale) {
52         try {
53             return messageSource.getMessage("item_list." + key, null, locale);
54         } catch (Exception JavaDoc e) {
55             return "???item_list." + key + "???";
56         }
57     }
58     
59     @Override JavaDoc
60     public void doTag() {
61         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
62         request = (HttpServletRequest JavaDoc) pageContext.getRequest();
63         response = (HttpServletResponse JavaDoc) pageContext.getResponse();
64         Locale JavaDoc loc = RequestContextUtils.getLocale(request);
65         MessageSource ms = RequestContextUtils.getWebApplicationContext(request);
66         JspWriter JavaDoc out = pageContext.getOut();
67         try {
68             
69             //=============================== PAGINATION =====================================
70
String JavaDoc flowUrlParam = "_flowExecutionKey=" + request.getAttribute("flowExecutionKey");
71             String JavaDoc flowUrl = "/flow?" + flowUrlParam;
72             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
73             long resultCount = itemSearch.getResultCount();
74             String JavaDoc recordsFound = resultCount == 1 ? "recordFound" : "recordsFound";
75             sb.append("<a HREF='" + encodeURL(flowUrl + "&_eventId=back'") + " title='" + fmt("modifySearch", ms, loc) + "'>"
76                     + resultCount + " " + fmt(recordsFound, ms, loc) + "</a>&nbsp;&nbsp;");
77             int pageSize = itemSearch.getPageSize();
78             int pageCount = 0;
79             if (pageSize != -1) {
80                 pageCount = (int) Math.ceil((double) resultCount / pageSize);
81             }
82             if (pageCount > 1) {
83                 String JavaDoc pageUrl = flowUrl + "&_eventId=page&page=";
84                 sb.append("<span class='page-links'>");
85                 int currentPage = itemSearch.getCurrentPage();
86                 if (currentPage == 0) {
87                     sb.append("&lt;&lt;&nbsp;&nbsp;");
88                 } else {
89                     sb.append("<a HREF='" + encodeURL(pageUrl + (currentPage - 1)) + "'>&lt;&lt;</a>&nbsp;&nbsp;");
90                 }
91                 for(int i = 0; i < pageCount; i++) {
92                     if (currentPage == i) {
93                         sb.append((i + 1) +"&nbsp;&nbsp;");
94                     } else {
95                         sb.append("<a HREF='" + encodeURL(pageUrl + i) + "'>" + (i + 1) +"</a>&nbsp;&nbsp;");
96                     }
97                 }
98                 if (currentPage == pageCount - 1) {
99                     sb.append("&gt;&gt;");
100                 } else {
101                     sb.append("<a HREF='" + encodeURL(pageUrl + (currentPage + 1)) + "'>&gt;&gt;</a>");
102                 }
103                 sb.append("</span>");
104             }
105             // write out record count + pagination
106
out.println("<table class='jtrac bdr-collapse' width='100%'><tr><td>" + sb + "</td>");
107             out.println("<td align='right'><a HREF='" + encodeURL("/app/item_list_excel.htm?" + flowUrlParam) + "'>("
108                     + fmt("exportToExcel", ms, loc) + ")</a></td></tr></table><p/>");
109             
110             //=============================== TABLE HEADER =====================================
111
boolean showDetail = itemSearch.isShowDetail();
112             boolean showHistory = itemSearch.isShowHistory();
113             List JavaDoc<Field> fields = itemSearch.getFields();
114             
115             out.println("<table class='jtrac'>");
116             out.println("<tr>");
117             out.println(" <th>" + fmt("id", ms, loc) + "</th>");
118             out.println(" <th>" + fmt("summary", ms, loc) + "</th>");
119             
120             if (showDetail) {
121                 out.println(" <th>" + fmt("detail", ms, loc) + "</th>");
122             }
123             
124             out.println(" <th>" + fmt("loggedBy", ms, loc) + "</th>");
125             out.println(" <th>" + fmt("status", ms, loc) + "</th>");
126             out.println(" <th>" + fmt("assignedTo", ms, loc) + "</th>");
127             for(Field field : fields) {
128                 out.println(" <th>" + field.getLabel() + "</th>");
129             }
130             out.println(" <th>" + fmt("timeStamp", ms, loc) + "</th>");
131             out.println("</tr>");
132             int count = 1;
133             String JavaDoc itemUrl = flowUrl + "&_eventId=view&itemId=";
134             String JavaDoc itemId = request.getParameter("itemId");
135             int selected = 0;
136             if (itemId != null) {
137                 selected = Integer.parseInt(itemId);
138             }
139             //=============================== ITERATE =====================================
140
for(AbstractItem item : items) {
141                 String JavaDoc rowClass = "";
142                 String JavaDoc bookmark = "";
143                 if (selected != 0 && item.getId() == selected) {
144                     rowClass = " class='selected'";
145                     bookmark = "<a name='goto'/>";
146                 } else if (count % 2 == 0) {
147                     rowClass = " class='alt'";
148                 }
149                 out.println("<tr" + rowClass + ">");
150                 String JavaDoc href = null;
151                 if (showHistory) {
152                     href = encodeURL(itemUrl + item.getParent().getId());
153                 } else {
154                     href = encodeURL(itemUrl + item.getId());
155                 }
156                 out.println(" <td>" + bookmark + "<a HREF='" + href + "'>" + item.getRefId() + "</a></td>");
157                 out.println(" <td>" + ( item.getSummary() == null ? "" : HtmlUtils.htmlEscape(item.getSummary()) ) + "</td>");
158                 
159                 if (showDetail) {
160                     if (showHistory) {
161                         History h = (History) item;
162                         out.println(" <td>");
163                         Attachment attachment = h.getAttachment();
164                         if (attachment != null) {
165                             String JavaDoc attHref = encodeURL("attachments/" + attachment.getFileName() +"?filePrefix=" + attachment.getFilePrefix());
166                             out.println("<a target='_blank' HREF='" + attHref + "'>" + attachment.getFileName() + "</a>&nbsp;");
167                         }
168                         out.println(( h.getComment() == null ? "" : HtmlUtils.htmlEscape(h.getComment()) ) + "</td>");
169                     } else {
170                         out.println(" <td>" + (item.getDetail() == null ? "" : HtmlUtils.htmlEscape(item.getDetail())) + "</td>");
171                     }
172                 }
173                 
174                 out.println(" <td>" + item.getLoggedBy().getName() + "</td>");
175                 out.println(" <td>" + item.getStatusValue() + "</td>");
176                 out.println(" <td>" + ( item.getAssignedTo() == null ? "" : item.getAssignedTo().getName() ) + "</td>");
177                 for(Field field : fields) {
178                     out.println(" <td>" + item.getCustomValue(field.getName()) + "</td>");
179                 }
180                 out.println(" <td>" + item.getTimeStamp() + "</td>");
181                 out.println("</tr>");
182                 count++;
183             }
184             out.println("</table>");
185             // again write out record count + pagination
186
out.println("<p/>" + sb);
187         } catch (IOException JavaDoc ioe) {
188             throw new RuntimeException JavaDoc(ioe);
189         }
190     }
191     
192     private String JavaDoc encodeURL(String JavaDoc url) {
193         return response.encodeURL(request.getContextPath() + url);
194     }
195     
196 }
197
Popular Tags