1 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 ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 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 41 public final class ItemUtils { 42 43 public static String fixWhiteSpace(String text) { 44 if (text == null) { 45 return ""; 46 } 47 String temp = HtmlUtils.htmlEscape(text); 48 return temp.replaceAll("\n", "<br/>").replaceAll("\t", " "); 49 } 50 51 private static String fmt(String key, MessageSource messageSource, Locale locale) { 52 try { 53 return messageSource.getMessage("item_view." + key, null, locale); 54 } catch (Exception e) { 55 return "???item_view." + key + "???"; 56 } 57 } 58 59 public static String getAsHtml(Item item, MessageSource messageSource, Locale locale) { 60 return getAsHtml(item, null, null, messageSource, locale); 61 } 62 63 public static String getAsHtml(Item item, HttpServletRequest request, HttpServletResponse response) { 64 Locale locale = RequestContextUtils.getLocale(request); 65 MessageSource messageSource = RequestContextUtils.getWebApplicationContext(request); 66 return getAsHtml(item, request, response, messageSource, locale); 67 } 68 69 private static String getAsHtml(Item item, HttpServletRequest request, HttpServletResponse response, 70 MessageSource ms, Locale loc) { 71 72 boolean isWeb = request != null && response != null; 73 74 String tableStyle = " class='jtrac'"; 75 String tdStyle = ""; 76 String thStyle = ""; 77 String altStyle = " class='alt'"; 78 String labelStyle = " class='label'"; 79 80 if (!isWeb) { 81 String 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 sb = new StringBuffer (); 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 flowUrlParam = null; 99 String 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 refId = itemItem.getRelatedItem().getRefId(); 112 if (isWeb) { 113 String checked = ""; 114 Set <Long > set = itemViewForm.getRemoveRelated(); 115 if (set != null && set.contains(itemItem.getId())) { 116 checked = " checked='true'"; 117 } 118 String 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 refId = itemItem.getItem().getRefId(); 129 if (isWeb) { 130 String 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 <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 sb.append("<br/> <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 <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 href = response.encodeURL(request.getContextPath() + "/app/attachments/" + attachment.getFileName() +"?filePrefix=" + attachment.getFilePrefix()); 192 sb.append("<a target='_blank' HREF='" + href + "'>" + attachment.getFileName() + "</a> "); 193 } else { 194 sb.append("(attachment: " + attachment.getFileName() + ") "); 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 |