Source Code Cross Referenced for ItemController.java in  » J2EE » JOnAS-4.8.6 » olstore » controller » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » olstore.controller 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2006 Red Hat, Inc. All rights reserved.
003:         *
004:         * This library is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2.1 of the License, or any later version.
008:         *
009:         * This library is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:         * Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public
015:         * License along with this library; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
017:         * USA
018:         *
019:         * Component of: Red Hat Application Server
020:         * 
021:         * Initial Developers: Greg Lapouchnian
022:         *                     Patrick Smith
023:         *
024:         */package olstore.controller;
025:
026:        import java.math.BigDecimal;
027:        import java.util.ArrayList;
028:        import java.util.Iterator;
029:        import java.util.Map;
030:        import java.util.Vector;
031:
032:        import javax.servlet.http.HttpServletRequest;
033:        import javax.servlet.http.HttpServletResponse;
034:
035:        import olstore.domain.logic.OlstoreFacade;
036:        import olstore.dto.ItemValue;
037:        import olstore.dto.PictureValue;
038:        import olstore.dto.PropertyValue;
039:
040:        import org.springframework.validation.BindException;
041:        import org.springframework.validation.ValidationUtils;
042:        import org.springframework.web.servlet.ModelAndView;
043:        import org.springframework.web.servlet.mvc.SimpleFormController;
044:
045:        /**
046:         *  Controls the creating of a new item.
047:         */
048:        public class ItemController extends SimpleFormController {
049:
050:            // This form's olstore instance injected by spring.
051:            private OlstoreFacade olstore;
052:
053:            /**
054:             * Creates a new item controller with values set for the form's view as well as
055:             * the command class to use as the form's backing object.
056:             *
057:             */
058:            public ItemController() {
059:                setSessionForm(true);
060:                setValidateOnBinding(false);
061:                setCommandClass(ItemValue.class);
062:                setFormView("createItem");
063:                setSuccessView("createItem");
064:            }
065:
066:            /**
067:             * Spring injection method to set this form's olstore instance.
068:             * @param olstore this form's olstore instance object.
069:             */
070:            public void setOlstore(OlstoreFacade olstore) {
071:                this .olstore = olstore;
072:            }
073:
074:            /**
075:             * Returns this form's backing object.
076:             * 
077:             * @param request the HttpServletRequest
078:             * @return this form's backing object.
079:             */
080:            protected Object formBackingObject(HttpServletRequest request)
081:                    throws Exception {
082:                String itemId = request.getParameter("itemId");
083:                if (itemId != null) {
084:                    return olstore.getItemById(itemId);
085:                } else {
086:                    ItemValue value = new ItemValue();
087:                    value.setTypeId(olstore.getDefaultType());
088:                    value.setProperties(olstore.getPropertiesForType(olstore
089:                            .getDefaultType()));
090:                    value.setPrice(new BigDecimal(0));
091:                    return value;
092:                }
093:            }
094:
095:            /**
096:             * The validation method used to validate the fields of the command object.
097:             * 
098:             * @param request the HttpServletRequest 
099:             * @param command this form's backing object.
100:             * @param errors the errors that occurred while validating this form.
101:             */
102:            protected void onBindAndValidate(HttpServletRequest request,
103:                    Object command, BindException errors) throws Exception {
104:
105:                if (!isFormChangeRequest(request)) {
106:                    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
107:                            "NAME_REQUIRED", "Item name is required.");
108:                    ValidationUtils.rejectIfEmptyOrWhitespace(errors,
109:                            "shortDesc", "SHRT_REQUIRED",
110:                            "Short description is required.");
111:                    ValidationUtils.rejectIfEmptyOrWhitespace(errors,
112:                            "longDesc", "LONG_REQUIRED",
113:                            "Long description is required.");
114:                    ItemValue item = (ItemValue) command;
115:                    Iterator i = item.getProperties().iterator();
116:                    while (i.hasNext()) {
117:                        PropertyValue prop = (PropertyValue) i.next();
118:                        if (prop.getName() == null
119:                                || prop.getName().length() == 0) {
120:                            errors.rejectValue("properties", "PROP_REQUIRED",
121:                                    "Property name is required.");
122:                        }
123:                    }
124:                }
125:            }
126:
127:            /**
128:             * Returns a basic map to use in this form's view to store basic information.
129:             * @param request the HttpServletRequest.
130:             * @return the model for this form's view.
131:             */
132:            protected Map referenceData(HttpServletRequest request)
133:                    throws Exception {
134:                Map model = ControllerHelper.initModel(request, olstore);
135:                model.put("page_title", "Create/Edit an Item - Olstore");
136:                model.put("types", this .olstore.getTypes());
137:                return model;
138:            }
139:
140:            /**
141:             * Handles when this form is submitted and returns a new model and view
142:             * to represent the form.
143:             * 
144:             * @param request the HttpServletRequest.
145:             * @param response the HttpServletResponse.
146:             * @param errors the errors that occured during validation.
147:             * @return a new model and view to represent this form.
148:             */
149:            protected ModelAndView onSubmit(HttpServletRequest request,
150:                    HttpServletResponse response, Object command,
151:                    BindException errors) throws Exception {
152:
153:                ItemValue item = (ItemValue) command;
154:
155:                // Checks to see if there are any items or images already in this item
156:                // if there aren't any, set an empty one to prevent problems on the view.
157:                try {
158:                    if (item.getProperties() == null) {
159:                        item.setProperties(new ArrayList());
160:                    }
161:                    if (item.getPictures() == null) {
162:                        item.setPictures(new ArrayList());
163:                    }
164:
165:                    this .olstore.saveItem(item);
166:                } catch (Exception ex) {
167:                    return showForm(request, response, errors);
168:                }
169:
170:                // supply the errors to the view inside the model map
171:                Map model = errors.getModel();
172:                // add the reference data once again so it will continue to be displayed
173:                model.putAll(referenceData(request));
174:                return new ModelAndView("createItem", model);
175:            }
176:
177:            /**
178:             * If the incoming request is to change the form, this method is invoked.
179:             * 
180:             * @param request the HttpServletRequest.
181:             * @param response the HttpServletResponse.
182:             * @param command this form's backing object.
183:             */
184:            protected void onFormChange(HttpServletRequest request,
185:                    HttpServletResponse response, Object command)
186:                    throws Exception {
187:                ItemValue item = (ItemValue) command;
188:                if (request.getParameter("add-picture") != null) {
189:                    try {
190:                        item.getPictures().add(new PictureValue());
191:                    } catch (NullPointerException e) {
192:                        Vector v = new Vector();
193:                        v.add(new PictureValue());
194:                        item.setPictures(v);
195:                    }
196:                } else {
197:                    item.setProperties(olstore.getPropertiesForType(item
198:                            .getTypeId()));
199:                }
200:            }
201:
202:            /**
203:             * Checks to see if the request is to change the form or a regular submit request.
204:             * 
205:             * @param request the HttpServletRequest.
206:             * @return true if the request is to change the form, false otherwise.
207:             */
208:            protected boolean isFormChangeRequest(HttpServletRequest request) {
209:                return request.getParameter("save-item") == null;
210:            }
211:
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.