KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.List JavaDoc;
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 /**
51  * Multiaction that backs the "Item Create / Edit" flow
52  */

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 JavaDoc
62     protected void initBinder(RequestContext request, DataBinder binder) {
63         binder.registerCustomEditor(Integer JavaDoc.class, new CustomNumberEditor(Integer JavaDoc.class, true));
64         binder.registerCustomEditor(Double JavaDoc.class, new CustomNumberEditor(Double JavaDoc.class, true));
65         binder.registerCustomEditor(String JavaDoc.class, new StringTrimmerEditor(true));
66         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(new SimpleDateFormat JavaDoc("yyyy-MM-dd"), true));
67         binder.registerCustomEditor(User.class, new UserEditor(jtrac));
68         binder.registerCustomEditor(ItemUser.class, new ItemUserEditor(jtrac));
69     }
70     
71     @Override JavaDoc
72     public Object JavaDoc createFormObject(RequestContext context) {
73         String JavaDoc 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 JavaDoc 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 JavaDoc<UserSpaceRole> userSpaceRoles = jtrac.findUserRolesForSpace(space.getId());
90         // TODO, next two can be combined, optimized
91
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 JavaDoc {
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 { // edit scenario
109
space = item.getSpace();
110             isEdit = true;
111         }
112
113         // validation
114
ValidationUtils.rejectIfEmpty(errors, "summary", "detail");
115         for (Field field : space.getMetadata().getFields().values()) {
116             Object JavaDoc o = item.getValue(field.getName());
117             if (o == null && !field.isOptional()) {
118                 errors.rejectValue(field.getName() + "", ValidationUtils.ERROR_EMPTY_CODE);
119             }
120         }
121         
122         // TODO clean this mess up with proper form backing object
123
String JavaDoc 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 JavaDoc 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 JavaDoc file = new File JavaDoc(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 JavaDoc {
165         Item item = (Item) getFormObject(context);
166         jtrac.removeItem(item);
167         return success();
168     }
169     
170 }
171
Popular Tags