1 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 ; 25 import java.util.List ; 26 import java.util.Locale ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.jsp.JspWriter ; 30 import javax.servlet.jsp.PageContext ; 31 import javax.servlet.jsp.tagext.SimpleTagSupport ; 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 { 37 38 private HttpServletRequest request; 39 private HttpServletResponse response; 40 private List <AbstractItem> items; 41 private ItemSearch itemSearch; 42 43 public void setItems(List <AbstractItem> items) { 44 this.items = items; 45 } 46 47 public void setItemSearch(ItemSearch itemSearch) { 48 this.itemSearch = itemSearch; 49 } 50 51 private static String fmt(String key, MessageSource messageSource, Locale locale) { 52 try { 53 return messageSource.getMessage("item_list." + key, null, locale); 54 } catch (Exception e) { 55 return "???item_list." + key + "???"; 56 } 57 } 58 59 @Override 60 public void doTag() { 61 PageContext pageContext = (PageContext ) getJspContext(); 62 request = (HttpServletRequest ) pageContext.getRequest(); 63 response = (HttpServletResponse ) pageContext.getResponse(); 64 Locale loc = RequestContextUtils.getLocale(request); 65 MessageSource ms = RequestContextUtils.getWebApplicationContext(request); 66 JspWriter out = pageContext.getOut(); 67 try { 68 69 String flowUrlParam = "_flowExecutionKey=" + request.getAttribute("flowExecutionKey"); 71 String flowUrl = "/flow?" + flowUrlParam; 72 StringBuffer sb = new StringBuffer (); 73 long resultCount = itemSearch.getResultCount(); 74 String 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> "); 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 pageUrl = flowUrl + "&_eventId=page&page="; 84 sb.append("<span class='page-links'>"); 85 int currentPage = itemSearch.getCurrentPage(); 86 if (currentPage == 0) { 87 sb.append("<< "); 88 } else { 89 sb.append("<a HREF='" + encodeURL(pageUrl + (currentPage - 1)) + "'><<</a> "); 90 } 91 for(int i = 0; i < pageCount; i++) { 92 if (currentPage == i) { 93 sb.append((i + 1) +" "); 94 } else { 95 sb.append("<a HREF='" + encodeURL(pageUrl + i) + "'>" + (i + 1) +"</a> "); 96 } 97 } 98 if (currentPage == pageCount - 1) { 99 sb.append(">>"); 100 } else { 101 sb.append("<a HREF='" + encodeURL(pageUrl + (currentPage + 1)) + "'>>></a>"); 102 } 103 sb.append("</span>"); 104 } 105 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 boolean showDetail = itemSearch.isShowDetail(); 112 boolean showHistory = itemSearch.isShowHistory(); 113 List <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 itemUrl = flowUrl + "&_eventId=view&itemId="; 134 String itemId = request.getParameter("itemId"); 135 int selected = 0; 136 if (itemId != null) { 137 selected = Integer.parseInt(itemId); 138 } 139 for(AbstractItem item : items) { 141 String rowClass = ""; 142 String 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 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 attHref = encodeURL("attachments/" + attachment.getFileName() +"?filePrefix=" + attachment.getFilePrefix()); 166 out.println("<a target='_blank' HREF='" + attHref + "'>" + attachment.getFileName() + "</a> "); 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 out.println("<p/>" + sb); 187 } catch (IOException ioe) { 188 throw new RuntimeException (ioe); 189 } 190 } 191 192 private String encodeURL(String url) { 193 return response.encodeURL(request.getContextPath() + url); 194 } 195 196 } 197 | Popular Tags |