1 16 17 package info.jtrac.webflow; 18 19 import info.jtrac.domain.Attachment; 20 import info.jtrac.domain.History; 21 import info.jtrac.domain.Item; 22 import info.jtrac.domain.ItemItem; 23 import info.jtrac.domain.ItemUser; 24 import info.jtrac.domain.Space; 25 import info.jtrac.domain.State; 26 import info.jtrac.domain.User; 27 import info.jtrac.domain.UserSpaceRole; 28 import info.jtrac.util.AttachmentUtils; 29 import info.jtrac.util.ItemUserEditor; 30 import info.jtrac.util.UserEditor; 31 import info.jtrac.util.UserUtils; 32 import info.jtrac.util.ValidationUtils; 33 import java.io.File ; 34 import java.io.Serializable ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Date ; 37 import java.util.HashSet ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Set ; 41 import org.acegisecurity.context.SecurityContextHolder; 42 import org.springframework.beans.propertyeditors.CustomDateEditor; 43 import org.springframework.beans.propertyeditors.CustomNumberEditor; 44 import org.springframework.beans.propertyeditors.StringTrimmerEditor; 45 import org.springframework.validation.DataBinder; 46 import org.springframework.validation.Errors; 47 import org.springframework.web.multipart.MultipartFile; 48 import org.springframework.web.multipart.MultipartHttpServletRequest; 49 import org.springframework.webflow.execution.Event; 50 import org.springframework.webflow.execution.RequestContext; 51 import org.springframework.webflow.execution.ScopeType; 52 import org.springframework.webflow.context.servlet.ServletExternalContext; 53 54 58 public class ItemViewFormAction extends AbstractFormAction { 59 60 public ItemViewFormAction() { 61 setFormObjectClass(ItemViewForm.class); 62 setFormObjectName("itemViewForm"); 63 setFormObjectScope(ScopeType.REQUEST); 64 } 65 66 69 public static class ItemViewForm implements Serializable { 70 71 private transient History history; 72 private int relationType; 73 private String relatedItemRefId; 74 private Set <Long > removeRelated; 75 76 public History getHistory() { 77 if (history == null) { 78 history = new History(); 79 } 80 return history; 81 } 82 83 public void setHistory(History history) { 84 this.history = history; 85 } 86 87 public int getRelationType() { 88 return relationType; 89 } 90 91 public void setRelationType(int relationType) { 92 this.relationType = relationType; 93 } 94 95 public String getRelatedItemRefId() { 96 return relatedItemRefId; 97 } 98 99 public void setRelatedItemRefId(String relatedItemRefId) { 100 this.relatedItemRefId = relatedItemRefId; 101 } 102 103 public Set <Long > getRemoveRelated() { 104 return removeRelated; 105 } 106 107 public void setRemoveRelated(Set <Long > removeRelated) { 108 this.removeRelated = removeRelated; 109 } 110 111 } 112 113 @Override 114 protected void initBinder(RequestContext request, DataBinder binder) { 115 binder.registerCustomEditor(Integer .class, new CustomNumberEditor(Integer .class, true)); 116 binder.registerCustomEditor(Double .class, new CustomNumberEditor(Double .class, true)); 117 binder.registerCustomEditor(String .class, new StringTrimmerEditor(true)); 118 binder.registerCustomEditor(Date .class, new CustomDateEditor(new SimpleDateFormat ("yyyy-MM-dd"), true)); 119 binder.registerCustomEditor(User.class, new UserEditor(jtrac)); 120 binder.registerCustomEditor(ItemUser.class, new ItemUserEditor(jtrac)); 121 } 122 123 @Override 124 public Object createFormObject(RequestContext context) { 125 Item item = null; 126 String itemId = ValidationUtils.getParameter(context, "itemId"); 127 if (itemId != null && !itemId.equals("0")) { 128 item = jtrac.loadItem(Long.parseLong(itemId)); 129 } else { 130 item = (Item) context.getRequestScope().get("item"); 131 } 132 context.getRequestScope().put("item", item); 135 ItemViewForm itemViewForm = new ItemViewForm(); 136 History history = itemViewForm.getHistory(); 137 history.setItemUsers(item.getItemUsers()); 138 return itemViewForm; 139 } 140 141 public Event itemViewSetupHandler(RequestContext context) throws Exception { 142 Item item = (Item) context.getRequestScope().get("item"); 143 User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 144 Space space = item.getSpace(); 145 List <UserSpaceRole> userSpaceRoles = jtrac.findUserRolesForSpace(space.getId()); 146 Map <Integer , String > map = item.getPermittedTransitions(user); 147 context.getRequestScope().put("transitions", map); 148 context.getRequestScope().put("transitionCount", map.size()); 149 context.getRequestScope().put("editableFields", item.getEditableFieldList(user)); 150 context.getRequestScope().put("userSpaceRoles", userSpaceRoles); 151 if (getFormErrors(context).hasErrors()) { 152 ItemViewForm itemViewForm = (ItemViewForm) getFormObject(context); 153 if (itemViewForm.getHistory().getStatus() != null) { 154 logger.debug("form being re-shown with errors, and status was not null"); 155 context.getRequestScope().put("usersAbleToTransitionFrom", 156 UserUtils.filterUsersAbleToTransitionFrom(userSpaceRoles, item.getSpace(), itemViewForm.getHistory().getStatus())); 157 } 158 } 159 return success(); 160 } 161 162 public Event itemViewHandler(RequestContext context) throws Exception { 163 ItemViewForm itemViewForm = (ItemViewForm) getFormObject(context); 164 History history = itemViewForm.getHistory(); 165 Errors errors = getFormErrors(context); 166 if (history.getStatus() != null) { 167 if (history.getStatus() != State.CLOSED && history.getAssignedTo() == null) { 168 errors.rejectValue("history.assignedTo", "item_view_form.assignedTo.error"); 169 } 170 } else { 171 if (history.getAssignedTo() != null) { 172 errors.rejectValue("history.status", "item_view_form.status.error"); 173 } 174 } 175 if (history.getComment() == null) { 176 errors.rejectValue("history.comment", ValidationUtils.ERROR_EMPTY_CODE); 177 } 178 if (errors.hasErrors()) { 179 return error(); 180 } 181 Item item = (Item) context.getRequestScope().get("item"); User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 183 history.setLoggedBy(user); 184 185 ServletExternalContext servletContext = (ServletExternalContext) context.getLastEvent().getSource(); 187 MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletContext.getRequest(); 188 MultipartFile multipartFile = request.getFile("file"); 189 Attachment attachment = null; 190 if (!multipartFile.isEmpty()) { 191 String fileName = AttachmentUtils.cleanFileName(multipartFile.getOriginalFilename()); 192 attachment = new Attachment(); 193 attachment.setFileName(fileName); 194 } 195 196 if (itemViewForm.getRelatedItemRefId() != null) { 198 String refId = itemViewForm.getRelatedItemRefId(); 199 int pos = refId.indexOf('-'); 200 long sequenceNum = Long.parseLong(refId.substring(pos + 1)); 201 String prefixCode = refId.substring(0, pos).toUpperCase(); 202 Item relatedItem = jtrac.loadItem(sequenceNum, prefixCode); 203 ItemItem itemItem = new ItemItem(item, relatedItem, itemViewForm.getRelationType()); 204 item.add(itemItem); 205 } 206 207 if (itemViewForm.removeRelated != null) { 209 Set <ItemItem> toRemove = new HashSet <ItemItem>(); 210 for (long i : itemViewForm.removeRelated) { 211 for (ItemItem ii : item.getRelatedItems()) { 212 if (ii.getId() == i) { 213 toRemove.add(ii); 214 } 215 } 216 } 217 item.getRelatedItems().removeAll(toRemove); 218 for (ItemItem ii : toRemove) { 219 jtrac.removeItemItem(ii); 220 } 221 } 222 223 jtrac.storeHistoryForItem(item, history, attachment); 224 225 if (attachment != null) { 226 File file = new File (System.getProperty("jtrac.home") + "/attachments/" + attachment.getFilePrefix() + "_" + attachment.getFileName()); 227 multipartFile.transferTo(file); 228 } 229 resetForm(context); 230 return success(); 231 } 232 233 public Event relateHandler(RequestContext context) { 234 context.getRequestScope().put("calledByRelate", true); 238 String itemId = ValidationUtils.getParameter(context, "itemId"); 239 Item item = jtrac.loadItem(Long.parseLong(itemId)); 240 context.getRequestScope().put("item", item); 241 return success(); 242 } 243 244 public Event relateSubmitHandler(RequestContext context) throws Exception { 245 ItemViewForm itemViewForm = (ItemViewForm) getFormObject(context); 246 String relatedItemRefId = ValidationUtils.getParameter(context, "relatedItemRefId"); 247 String relationType = ValidationUtils.getParameter(context, "relationType"); 248 itemViewForm.setRelatedItemRefId(relatedItemRefId); 249 itemViewForm.setRelationType(Integer.parseInt(relationType)); 250 int type = Integer.parseInt(relationType); 251 context.getRequestScope().put("relationText", ItemItem.getRelationText(type)); 252 return success(); 253 } 254 255 } 256 | Popular Tags |