KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
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 /**
55  * Multiaction that participates in the "Space Create / Edit" flow
56  * for editing individual Fields
57  */

58 public class ItemViewFormAction extends AbstractFormAction {
59     
60     public ItemViewFormAction() {
61         setFormObjectClass(ItemViewForm.class);
62         setFormObjectName("itemViewForm");
63         setFormObjectScope(ScopeType.REQUEST);
64     }
65     
66     /**
67      * form backing object
68      */

69     public static class ItemViewForm implements Serializable JavaDoc {
70         
71         private transient History history;
72         private int relationType;
73         private String JavaDoc relatedItemRefId;
74         private Set JavaDoc<Long JavaDoc> 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 JavaDoc getRelatedItemRefId() {
96             return relatedItemRefId;
97         }
98
99         public void setRelatedItemRefId(String JavaDoc relatedItemRefId) {
100             this.relatedItemRefId = relatedItemRefId;
101         }
102
103         public Set JavaDoc<Long JavaDoc> getRemoveRelated() {
104             return removeRelated;
105         }
106
107         public void setRemoveRelated(Set JavaDoc<Long JavaDoc> removeRelated) {
108             this.removeRelated = removeRelated;
109         }
110     
111     }
112     
113     @Override JavaDoc
114     protected void initBinder(RequestContext request, DataBinder binder) {
115         binder.registerCustomEditor(Integer JavaDoc.class, new CustomNumberEditor(Integer JavaDoc.class, true));
116         binder.registerCustomEditor(Double JavaDoc.class, new CustomNumberEditor(Double JavaDoc.class, true));
117         binder.registerCustomEditor(String JavaDoc.class, new StringTrimmerEditor(true));
118         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(new SimpleDateFormat JavaDoc("yyyy-MM-dd"), true));
119         binder.registerCustomEditor(User.class, new UserEditor(jtrac));
120         binder.registerCustomEditor(ItemUser.class, new ItemUserEditor(jtrac));
121     }
122     
123     @Override JavaDoc
124     public Object JavaDoc createFormObject(RequestContext context) {
125         Item item = null;
126         String JavaDoc 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         // not flow scope because of weird Hibernate Lazy loading issues
133
// hidden field "itemId" added to item_view_form.jsp
134
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 JavaDoc {
142         Item item = (Item) context.getRequestScope().get("item");
143         User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
144         Space space = item.getSpace();
145         List JavaDoc<UserSpaceRole> userSpaceRoles = jtrac.findUserRolesForSpace(space.getId());
146         Map JavaDoc<Integer JavaDoc, String JavaDoc> 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 JavaDoc {
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"); // loaded by loadFormObject() on submit
182
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
183         history.setLoggedBy(user);
184         
185         // attachment handling
186
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 JavaDoc fileName = AttachmentUtils.cleanFileName(multipartFile.getOriginalFilename());
192             attachment = new Attachment();
193             attachment.setFileName(fileName);
194         }
195         
196         // related item handling
197
if (itemViewForm.getRelatedItemRefId() != null) {
198             String JavaDoc refId = itemViewForm.getRelatedItemRefId();
199             int pos = refId.indexOf('-');
200             long sequenceNum = Long.parseLong(refId.substring(pos + 1));
201             String JavaDoc 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         // remove related items if user wanted to
208
if (itemViewForm.removeRelated != null) {
209             Set JavaDoc<ItemItem> toRemove = new HashSet JavaDoc<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 JavaDoc file = new File JavaDoc(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         // there may be a better way to do this within the flow definition file
235
// but this is a "marker" for switching on the "back" hyperlink
236
// see the "input-mapper" sections in WEB-INF/flow/item_view.xml and item_search.xml
237
context.getRequestScope().put("calledByRelate", true);
238         String JavaDoc 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 JavaDoc {
245         ItemViewForm itemViewForm = (ItemViewForm) getFormObject(context);
246         String JavaDoc relatedItemRefId = ValidationUtils.getParameter(context, "relatedItemRefId");
247         String JavaDoc 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