1 16 17 package info.jtrac.webflow; 18 19 import info.jtrac.domain.Item; 20 import info.jtrac.domain.ItemSearch; 21 import info.jtrac.domain.Space; 22 import info.jtrac.domain.User; 23 import info.jtrac.exception.SearchQueryParseException; 24 import info.jtrac.util.ValidationUtils; 25 import java.text.SimpleDateFormat ; 26 import java.util.Collections ; 27 import java.util.Date ; 28 import java.util.List ; 29 import java.util.Set ; 30 import org.acegisecurity.context.SecurityContextHolder; 31 import org.springframework.beans.propertyeditors.CustomDateEditor; 32 import org.springframework.beans.propertyeditors.CustomNumberEditor; 33 import org.springframework.beans.propertyeditors.StringTrimmerEditor; 34 import org.springframework.validation.DataBinder; 35 import org.springframework.validation.Errors; 36 import org.springframework.webflow.execution.Event; 37 import org.springframework.webflow.execution.RequestContext; 38 import org.springframework.webflow.execution.ScopeType; 39 40 43 public class ItemSearchFormAction extends AbstractFormAction { 44 45 public ItemSearchFormAction() { 46 setFormObjectClass(ItemSearch.class); 47 setFormObjectName("itemSearch"); 48 setFormObjectScope(ScopeType.FLOW); 49 } 50 51 @Override 52 protected void initBinder(RequestContext request, DataBinder binder) { 53 binder.registerCustomEditor(Integer .class, new CustomNumberEditor(Integer .class, true)); 54 binder.registerCustomEditor(Double .class, new CustomNumberEditor(Double .class, true)); 55 binder.registerCustomEditor(String .class, new StringTrimmerEditor(true)); 56 binder.registerCustomEditor(Date .class, new CustomDateEditor(new SimpleDateFormat ("yyyy-MM-dd"), true)); 57 } 58 59 @Override 60 public Object createFormObject(RequestContext context) { 61 String spaceId = ValidationUtils.getParameter(context, "spaceId"); 62 ItemSearch itemSearch = null; 63 if (spaceId == null) { 64 User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 65 List <User> users = jtrac.findUsersForUser(user); 66 context.getFlowScope().put("users", users); 67 itemSearch = new ItemSearch(user); 68 } else { 69 Space space = jtrac.loadSpace(Integer.parseInt(spaceId)); 70 itemSearch = new ItemSearch(space); 71 List <User> users = jtrac.findUsersForSpace(space.getId()); 72 context.getFlowScope().put("users", users); 73 context.getFlowScope().put("space", space); 75 } 76 return itemSearch; 77 } 78 79 85 public Event itemListViewSetup(RequestContext context) throws Exception { 86 String type = ValidationUtils.getParameter(context, "type"); 87 String status = ValidationUtils.getParameter(context, "status"); 88 ItemSearch itemSearch = (ItemSearch) getFormObject(context); 89 User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 90 Set <Long > set = Collections.singleton(user.getId()); 91 Space space = (Space) context.getFlowScope().get("space"); 92 if (type.equals("loggedBy")) { 93 itemSearch.setLoggedBySet(set); 94 } else if (type.equals("assignedTo")) { 95 itemSearch.setAssignedToSet(set); 96 } 97 if (status != null) { 98 itemSearch.setStatusSet(Collections.singleton(new Integer (status))); 99 } 100 return success(); 101 } 102 103 public Event itemSearchFormHandler(RequestContext context) throws Exception { 104 ItemSearch itemSearch = (ItemSearch) getFormObject(context); 105 List <Item> items = null; 106 try { 107 items = jtrac.findItems(itemSearch); 108 } catch (SearchQueryParseException e) { 109 Errors errors = getFormErrors(context); 110 errors.rejectValue("summary", "item_search_form.error.summary.invalid"); 111 return error(); 112 } 113 context.getRequestScope().put("items", items); 114 return success(); 115 } 116 117 public Event itemSearchPageHandler(RequestContext context) throws Exception { 118 String page = ValidationUtils.getParameter(context, "page"); 119 ItemSearch itemSearch = (ItemSearch) getFormObject(context); 120 itemSearch.setCurrentPage(Integer.parseInt(page)); 121 context.getRequestScope().put("items", jtrac.findItems(itemSearch)); 122 return success(); 123 } 124 125 public Event itemSearchBackHandler(RequestContext context) throws Exception { 126 ItemSearch itemSearch = (ItemSearch) getFormObject(context); 127 itemSearch.setCurrentPage(0); 128 return success(); 129 } 130 131 public Event itemSearchViewByIdHandler(RequestContext context) { 132 context.getRequestScope().put("calledBySearch", true); 136 return success(); 137 } 138 139 public Event itemSearchViewByRefIdHandler(RequestContext context) throws Exception { 140 String refId = ValidationUtils.getParameter(context, "refId"); 141 if (refId == null) { 142 context.getRequestScope().put("refIdError", ValidationUtils.ERROR_EMPTY_CODE); 143 return error(); 144 } 145 int pos = refId.indexOf('-'); 147 if (pos == -1) { 148 context.getRequestScope().put("refId", refId); 149 context.getRequestScope().put("refIdError", "item_search_form.error.refId.invalid"); 150 return error(); 151 } 152 long sequenceNum; 153 try { 154 sequenceNum = Long.parseLong(refId.substring(pos + 1)); 155 } catch (NumberFormatException e) { 156 context.getRequestScope().put("refId", refId); 157 context.getRequestScope().put("refIdError", "item_search_form.error.refId.invalid"); 158 return error(); 159 } 160 String prefixCode = refId.substring(0, pos).toUpperCase(); 161 logger.debug("sequenceNum = '" + sequenceNum + "', prefixCode = '" + prefixCode + "'"); 162 Item item = jtrac.loadItem(sequenceNum, prefixCode); 163 if (item == null) { 164 context.getRequestScope().put("refId", refId); 165 context.getRequestScope().put("refIdError", "item_search_form.error.refId.notFound"); 166 return error(); 167 } 168 context.getRequestScope().put("item", item); 169 return success(); 170 } 171 172 } 173 | Popular Tags |