KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > webflow > ItemSearchFormAction


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
41  * Multiaction that backs the "Item Search" flow
42  */

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 JavaDoc
52     protected void initBinder(RequestContext request, DataBinder binder) {
53         binder.registerCustomEditor(Integer JavaDoc.class, new CustomNumberEditor(Integer JavaDoc.class, true));
54         binder.registerCustomEditor(Double JavaDoc.class, new CustomNumberEditor(Double JavaDoc.class, true));
55         binder.registerCustomEditor(String JavaDoc.class, new StringTrimmerEditor(true));
56         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(new SimpleDateFormat JavaDoc("yyyy-MM-dd"), true));
57     }
58     
59     @Override JavaDoc
60     public Object JavaDoc createFormObject(RequestContext context) {
61         String JavaDoc spaceId = ValidationUtils.getParameter(context, "spaceId");
62         ItemSearch itemSearch = null;
63         if (spaceId == null) {
64             User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
65             List JavaDoc<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 JavaDoc<User> users = jtrac.findUsersForSpace(space.getId());
72             context.getFlowScope().put("users", users);
73             // this is just for header.jsp to show the Space navigation
74
context.getFlowScope().put("space", space);
75         }
76         return itemSearch;
77     }
78     
79     /**
80      * used only when "quick search" is invoked from the dashboard
81      * note that the itemSearch form object and the space in the context
82      * are elegantly initialized in the setupForm / createFormObject sequence
83      * refer to WEB-INF/flow/itemSearch.xml
84      */

85     public Event itemListViewSetup(RequestContext context) throws Exception JavaDoc {
86         String JavaDoc type = ValidationUtils.getParameter(context, "type");
87         String JavaDoc status = ValidationUtils.getParameter(context, "status");
88         ItemSearch itemSearch = (ItemSearch) getFormObject(context);
89         User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
90         Set JavaDoc<Long JavaDoc> 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 JavaDoc(status)));
99         }
100         return success();
101     }
102     
103     public Event itemSearchFormHandler(RequestContext context) throws Exception JavaDoc {
104         ItemSearch itemSearch = (ItemSearch) getFormObject(context);
105         List JavaDoc<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 JavaDoc {
118         String JavaDoc 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 JavaDoc {
126         ItemSearch itemSearch = (ItemSearch) getFormObject(context);
127         itemSearch.setCurrentPage(0);
128         return success();
129     }
130     
131     public Event itemSearchViewByIdHandler(RequestContext context) {
132         // there may be a better way to do this within the flow definition file
133
// but this is a "marker" for switching on the "back" hyperlink
134
// see the "input-mapper" sections in WEB-INF/flow/item_search.xml and item_view.xml
135
context.getRequestScope().put("calledBySearch", true);
136         return success();
137     }
138     
139     public Event itemSearchViewByRefIdHandler(RequestContext context) throws Exception JavaDoc {
140         String JavaDoc refId = ValidationUtils.getParameter(context, "refId");
141         if (refId == null) {
142             context.getRequestScope().put("refIdError", ValidationUtils.ERROR_EMPTY_CODE);
143             return error();
144         }
145         // TODO make this flexible, sense current space etc.
146
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 JavaDoc e) {
156             context.getRequestScope().put("refId", refId);
157             context.getRequestScope().put("refIdError", "item_search_form.error.refId.invalid");
158             return error();
159         }
160         String JavaDoc 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