1 16 17 package info.jtrac.webflow; 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.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.text.SimpleDateFormat ; 35 import java.util.Date ; 36 import java.util.List ; 37 import org.acegisecurity.context.SecurityContextHolder; 38 import org.springframework.beans.propertyeditors.CustomDateEditor; 39 import org.springframework.beans.propertyeditors.CustomNumberEditor; 40 import org.springframework.beans.propertyeditors.StringTrimmerEditor; 41 import org.springframework.validation.DataBinder; 42 import org.springframework.validation.Errors; 43 import org.springframework.web.multipart.MultipartFile; 44 import org.springframework.web.multipart.MultipartHttpServletRequest; 45 import org.springframework.webflow.execution.Event; 46 import org.springframework.webflow.execution.RequestContext; 47 import org.springframework.webflow.execution.ScopeType; 48 import org.springframework.webflow.context.servlet.ServletExternalContext; 49 50 53 public class ItemFormAction extends AbstractFormAction { 54 55 public ItemFormAction() { 56 setFormObjectClass(Item.class); 57 setFormObjectName("item"); 58 setFormObjectScope(ScopeType.REQUEST); 59 } 60 61 @Override 62 protected void initBinder(RequestContext request, DataBinder binder) { 63 binder.registerCustomEditor(Integer .class, new CustomNumberEditor(Integer .class, true)); 64 binder.registerCustomEditor(Double .class, new CustomNumberEditor(Double .class, true)); 65 binder.registerCustomEditor(String .class, new StringTrimmerEditor(true)); 66 binder.registerCustomEditor(Date .class, new CustomDateEditor(new SimpleDateFormat ("yyyy-MM-dd"), true)); 67 binder.registerCustomEditor(User.class, new UserEditor(jtrac)); 68 binder.registerCustomEditor(ItemUser.class, new ItemUserEditor(jtrac)); 69 } 70 71 @Override 72 public Object createFormObject(RequestContext context) { 73 String itemId = ValidationUtils.getParameter(context, "itemId"); 74 Item item = null; 75 Space space = null; 76 if (itemId != null && !itemId.equals("0")) { 77 item = jtrac.loadItem(Long.parseLong(itemId)); 78 space = item.getSpace(); 79 } else { 80 item = new Item(); 81 String spaceId = ValidationUtils.getParameter(context, "spaceId"); 82 if (spaceId == null) { 83 space = (Space) context.getFlowScope().get("space"); 84 } else { 85 space = jtrac.loadSpace(Integer.parseInt(spaceId)); 86 } 87 } 88 context.getFlowScope().put("space", space); 89 List <UserSpaceRole> userSpaceRoles = jtrac.findUserRolesForSpace(space.getId()); 90 context.getFlowScope().put("userSpaceRoles", userSpaceRoles); 92 context.getFlowScope().put("usersAbleToTransitionFrom", UserUtils.filterUsersAbleToTransitionFrom(userSpaceRoles, space, State.OPEN)); 93 return item; 94 } 95 96 public Event itemFormHandler(RequestContext context) throws Exception { 97 Item item = (Item) getFormObject(context); 98 Errors errors = getFormErrors(context); 99 Space space = null; 100 boolean isEdit = false; 101 if (item.getId() == 0) { 102 item.setStatus(State.OPEN); 103 User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 104 item.setLoggedBy(user); 105 space = (Space) context.getFlowScope().get("space"); 106 item.setSpace(space); 107 ValidationUtils.rejectIfEmpty(errors, "assignedTo"); 108 } else { space = item.getSpace(); 110 isEdit = true; 111 } 112 113 ValidationUtils.rejectIfEmpty(errors, "summary", "detail"); 115 for (Field field : space.getMetadata().getFields().values()) { 116 Object o = item.getValue(field.getName()); 117 if (o == null && !field.isOptional()) { 118 errors.rejectValue(field.getName() + "", ValidationUtils.ERROR_EMPTY_CODE); 119 } 120 } 121 122 String comment = ValidationUtils.getParameter(context, "comment"); 124 if (errors.hasErrors() || (isEdit && comment == null)) { 125 context.getRequestScope().put("comment", comment); 126 if ((isEdit && comment == null)) { 127 context.getRequestScope().put("commentError", ValidationUtils.ERROR_EMPTY_CODE); 128 } 129 return error(); 130 } 131 132 ServletExternalContext servletContext = (ServletExternalContext) context.getLastEvent().getSource(); 133 MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletContext.getRequest(); 134 MultipartFile multipartFile = request.getFile("file"); 135 Attachment attachment = null; 136 if (!isEdit && !multipartFile.isEmpty()) { 137 String fileName = AttachmentUtils.cleanFileName(multipartFile.getOriginalFilename()); 138 attachment = new Attachment(); 139 attachment.setFileName(fileName); 140 } 141 142 if (isEdit) { 143 History history = new History(item); 144 history.setAssignedTo(null); 145 history.setStatus(null); 146 history.setComment(comment); 147 jtrac.storeHistoryForItem(item, history, attachment); 148 } else { 149 jtrac.storeItem(item, attachment); 150 } 151 152 if (attachment != null) { 153 File file = new File (System.getProperty("jtrac.home") + "/attachments/" + attachment.getFilePrefix() + "_" + attachment.getFileName()); 154 multipartFile.transferTo(file); 155 } 156 157 if (isEdit) { 158 return new Event(this, "edit"); 159 } 160 161 return success(); 162 } 163 164 public Event itemDeleteHandler(RequestContext context) throws Exception { 165 Item item = (Item) getFormObject(context); 166 jtrac.removeItem(item); 167 return success(); 168 } 169 170 } 171 | Popular Tags |