Source Code Cross Referenced for GenericWebEvent.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » webtools » 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 » ERP CRM Financial » SourceTap CRM » org.ofbiz.webtools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: GenericWebEvent.java,v 1.3 2004/02/07 09:40:54 jonesde Exp $
003:         *
004:         *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.webtools;
025:
026:        import java.lang.reflect.Field;
027:        import java.lang.reflect.Method;
028:
029:        import javax.servlet.http.HttpServletRequest;
030:        import javax.servlet.http.HttpServletResponse;
031:
032:        import org.ofbiz.base.util.Debug;
033:        import org.ofbiz.entity.GenericDelegator;
034:        import org.ofbiz.entity.GenericEntityException;
035:        import org.ofbiz.entity.GenericValue;
036:        import org.ofbiz.entity.model.ModelEntity;
037:        import org.ofbiz.entity.model.ModelField;
038:        import org.ofbiz.entity.model.ModelFieldType;
039:        import org.ofbiz.entity.model.ModelReader;
040:        import org.ofbiz.security.Security;
041:
042:        /**
043:         * Web Event for doing updates on Generic Entities
044:         *
045:         * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
046:         * @version    $Revision: 1.3 $
047:         * @since      2.0
048:         */
049:        public class GenericWebEvent {
050:
051:            public static final String module = GenericWebEvent.class.getName();
052:
053:            /** An HTTP WebEvent handler that updates a Generic entity
054:             *
055:             * @param request The HTTP request object for the current JSP or Servlet request.
056:             * @param response The HTTP response object for the current JSP or Servlet request.
057:             * @return Returns a String specifying the outcome state of the event. This is used to decide which event to run next or which view to display. If null no event is run nor view displayed, allowing the event to call a forward on a RequestDispatcher.
058:             * @exception javax.servlet.ServletException Standard J2EE Servlet Exception
059:             * @exception java.rmi.RemoteException Standard RMI Remote Exception
060:             * @exception java.io.IOException Standard IO Exception
061:             */
062:            public static String updateGeneric(HttpServletRequest request,
063:                    HttpServletResponse response) {
064:                String errMsg = "";
065:
066:                String entityName = request.getParameter("entityName");
067:
068:                if (entityName == null || entityName.length() <= 0) {
069:                    request
070:                            .setAttribute("_ERROR_MESSAGE_",
071:                                    "The entityName was not specified, but is required.");
072:                    Debug
073:                            .logWarning(
074:                                    "[GenericWebEvent.updateGeneric] The entityName was not specified, but is required.",
075:                                    module);
076:                    return "error";
077:                }
078:
079:                Security security = (Security) request.getAttribute("security");
080:                GenericDelegator delegator = (GenericDelegator) request
081:                        .getAttribute("delegator");
082:
083:                if (security == null) {
084:                    request
085:                            .setAttribute(
086:                                    "_ERROR_MESSAGE_",
087:                                    "The security object was not found in the request, please check the control servlet init.");
088:                    Debug
089:                            .logWarning(
090:                                    "[updateGeneric] The security object was not found in the request, please check the control servlet init.",
091:                                    module);
092:                    return "error";
093:                }
094:                if (delegator == null) {
095:                    request
096:                            .setAttribute(
097:                                    "_ERROR_MESSAGE_",
098:                                    "The delegator object was not found in the request, please check the control servlet init.");
099:                    Debug
100:                            .logWarning(
101:                                    "[updateGeneric] The delegator object was not found in the request, please check the control servlet init.",
102:                                    module);
103:                    return "error";
104:                }
105:
106:                ModelReader reader = delegator.getModelReader();
107:                ModelEntity entity = null;
108:
109:                try {
110:                    entity = reader.getModelEntity(entityName);
111:                } catch (GenericEntityException e) {
112:                    Debug.logError(e, module);
113:                }
114:
115:                String updateMode = request.getParameter("UPDATE_MODE");
116:
117:                if (updateMode == null || updateMode.length() <= 0) {
118:                    request.setAttribute("_ERROR_MESSAGE_",
119:                            "Update Mode was not specified, but is required.");
120:                    Debug.logWarning(
121:                            "[updateGeneric] Update Mode was not specified, but is required; entityName: "
122:                                    + entityName, module);
123:                    return "error";
124:                }
125:
126:                // check permissions before moving on...
127:                if (!security.hasEntityPermission("ENTITY_DATA", "_"
128:                        + updateMode, request.getSession())
129:                        && !security.hasEntityPermission(entity
130:                                .getPlainTableName(), "_" + updateMode, request
131:                                .getSession())) {
132:                    request.setAttribute("_ERROR_MESSAGE_",
133:                            "You do not have sufficient permissions to "
134:                                    + updateMode + " " + entity.getEntityName()
135:                                    + " (" + entity.getPlainTableName() + "_"
136:                                    + updateMode + " or "
137:                                    + entity.getPlainTableName()
138:                                    + "_ADMIN needed).");
139:                    // not really successful, but error return through ERROR_MESSAGE, so quietly fail
140:                    return "error";
141:                }
142:
143:                GenericValue findByEntity = delegator.makeValue(entityName,
144:                        null);
145:
146:                // get the primary key parameters...
147:                for (int fnum = 0; fnum < entity.getPksSize(); fnum++) {
148:                    ModelField field = entity.getPk(fnum);
149:                    ModelFieldType type = null;
150:
151:                    try {
152:                        type = delegator.getEntityFieldType(entity, field
153:                                .getType());
154:                    } catch (GenericEntityException e) {
155:                        Debug.logWarning(e, module);
156:                        errMsg += "<li> Fatal error: field type \""
157:                                + field.getType() + "\" not found";
158:                    }
159:                    String fval = request.getParameter(field.getName());
160:
161:                    if (fval != null && fval.length() > 0) {
162:                        try {
163:                            findByEntity.setString(field.getName(), fval);
164:                        } catch (Exception e) {
165:                            errMsg = errMsg + "<li>" + field.getColName()
166:                                    + " conversion failed: \"" + fval
167:                                    + "\" is not a valid " + type.getJavaType();
168:                            Debug.logWarning("[updateGeneric] "
169:                                    + field.getColName()
170:                                    + " conversion failed: \"" + fval
171:                                    + "\" is not a valid " + type.getJavaType()
172:                                    + "; entityName: " + entityName, module);
173:                        }
174:                    }
175:                }
176:
177:                // if this is a delete, do that before getting all of the non-pk parameters and validating them
178:                if (updateMode.equals("DELETE")) {
179:                    // Remove associated/dependent entries from other tables here
180:                    // Delete actual main entity last, just in case database is set up to do a cascading delete, caches won't get cleared
181:                    try {
182:                        delegator.removeByPrimaryKey(findByEntity
183:                                .getPrimaryKey());
184:                    } catch (GenericEntityException e) {
185:                        Debug.logWarning(e, module);
186:                        request.setAttribute("_ERROR_MESSAGE_",
187:                                "Delete failed (write error)");
188:                        return "error";
189:                    }
190:
191:                    return "success";
192:                }
193:
194:                // get the non-primary key parameters
195:                for (int fnum = 0; fnum < entity.getNopksSize(); fnum++) {
196:                    ModelField field = (ModelField) entity.getNopk(fnum);
197:                    ModelFieldType type = null;
198:
199:                    try {
200:                        type = delegator.getEntityFieldType(entity, field
201:                                .getType());
202:                    } catch (GenericEntityException e) {
203:                        Debug.logWarning(e, module);
204:                        errMsg += "<li> Fatal error: field type \""
205:                                + field.getType() + "\" not found";
206:                    }
207:                    String fval = request.getParameter(field.getName());
208:
209:                    if (fval != null && fval.length() > 0) {
210:                        try {
211:                            findByEntity.setString(field.getName(), fval);
212:                        } catch (Exception e) {
213:                            errMsg = errMsg + "<li>" + field.getColName()
214:                                    + " conversion failed: \"" + fval
215:                                    + "\" is not a valid " + type.getJavaType();
216:                            Debug.logWarning("[updateGeneric] "
217:                                    + field.getColName()
218:                                    + " conversion failed: \"" + fval
219:                                    + "\" is not a valid " + type.getJavaType()
220:                                    + "; entityName: " + entityName, module);
221:                        }
222:                    }
223:                }
224:
225:                // if the updateMode is CREATE, check to see if an entity with the specified primary key already exists
226:                if (updateMode.equals("CREATE")) {
227:                    GenericValue tempEntity = null;
228:
229:                    try {
230:                        tempEntity = delegator.findByPrimaryKey(findByEntity
231:                                .getPrimaryKey());
232:                    } catch (GenericEntityException e) {
233:                        Debug.logWarning(e, module);
234:                        request
235:                                .setAttribute("_ERROR_MESSAGE_",
236:                                        "Create failed while checking if exists (read error)");
237:                        return "error";
238:                    }
239:                    if (tempEntity != null) {
240:                        errMsg = errMsg + "<li>" + entity.getEntityName()
241:                                + " already exists with primary key: "
242:                                + findByEntity.getPrimaryKey().toString()
243:                                + "; please change.";
244:                        Debug.logWarning("[updateGeneric] "
245:                                + entity.getEntityName()
246:                                + " already exists with primary key: "
247:                                + findByEntity.getPrimaryKey().toString()
248:                                + "; please change.", module);
249:                    }
250:                }
251:
252:                // Validate parameters...
253:                for (int fnum = 0; fnum < entity.getFieldsSize(); fnum++) {
254:                    ModelField field = entity.getField(fnum);
255:
256:                    for (int j = 0; j < field.getValidatorsSize(); j++) {
257:                        String curValidate = field.getValidator(j);
258:                        Class[] paramTypes = new Class[] { String.class };
259:                        Object[] params = new Object[] { findByEntity.get(
260:                                field.getName()).toString() };
261:
262:                        String className = "org.ofbiz.base.util.UtilValidate";
263:                        String methodName = curValidate;
264:
265:                        if (curValidate.indexOf('.') > 0) {
266:                            className = curValidate.substring(0, curValidate
267:                                    .lastIndexOf('.'));
268:                            methodName = curValidate.substring(curValidate
269:                                    .lastIndexOf('.') + 1);
270:                        }
271:                        Class valClass;
272:
273:                        try {
274:                            ClassLoader loader = Thread.currentThread()
275:                                    .getContextClassLoader();
276:                            valClass = loader.loadClass(className);
277:                        } catch (ClassNotFoundException cnfe) {
278:                            Debug
279:                                    .logError(
280:                                            "[updateGeneric] Could not find validation class: "
281:                                                    + className + "; ignoring.",
282:                                            module);
283:                            continue;
284:                        }
285:                        Method valMethod;
286:
287:                        try {
288:                            valMethod = valClass.getMethod(methodName,
289:                                    paramTypes);
290:                        } catch (NoSuchMethodException cnfe) {
291:                            Debug
292:                                    .logError(
293:                                            "[updateGeneric] Could not find validation method: "
294:                                                    + methodName + " of class "
295:                                                    + className + "; ignoring.",
296:                                            module);
297:                            continue;
298:                        }
299:
300:                        Boolean resultBool;
301:
302:                        try {
303:                            resultBool = (Boolean) valMethod.invoke(null,
304:                                    params);
305:                        } catch (Exception e) {
306:                            Debug.logError(
307:                                    "[updateGeneric] Could not access validation method: "
308:                                            + methodName + " of class "
309:                                            + className + "; returning true.",
310:                                    module);
311:                            resultBool = Boolean.TRUE;
312:                        }
313:
314:                        if (!resultBool.booleanValue()) {
315:                            Field msgField;
316:                            String message;
317:
318:                            try {
319:                                msgField = valClass.getField(curValidate
320:                                        + "Msg");
321:                                message = (String) msgField.get(null);
322:                            } catch (Exception e) {
323:                                Debug
324:                                        .logError(
325:                                                "[updateGeneric] Could not find validation message field: "
326:                                                        + curValidate
327:                                                        + "Msg of class "
328:                                                        + className
329:                                                        + "; returning generic validation failure message.",
330:                                                module);
331:                                message = "validation failed.";
332:                            }
333:                            errMsg = errMsg + "<li>" + field.getColName() + " "
334:                                    + curValidate + " failed: " + message;
335:                            Debug.logWarning("[updateGeneric] "
336:                                    + field.getColName() + " " + curValidate
337:                                    + " failed: " + message, module);
338:                        }
339:                    }
340:                }
341:
342:                if (errMsg.length() > 0) {
343:                    errMsg = "<br><b>The following error(s) occurred:</b><ul>"
344:                            + errMsg + "</ul>";
345:                    request.setAttribute("_ERROR_MESSAGE_", errMsg);
346:                    return "error";
347:                }
348:
349:                if (updateMode.equals("CREATE")) {
350:                    GenericValue value;
351:
352:                    try {
353:                        value = delegator.create(findByEntity.getEntityName(),
354:                                findByEntity.getAllFields());
355:                    } catch (GenericEntityException e) {
356:                        Debug.logWarning(e, module);
357:                        value = null;
358:                    }
359:                    if (value == null) {
360:                        request.setAttribute("_ERROR_MESSAGE_", "Creation of "
361:                                + entity.getEntityName()
362:                                + " failed for entity: "
363:                                + findByEntity.toString());
364:                        return "error";
365:                    }
366:                } else if (updateMode.equals("UPDATE")) {
367:                    GenericValue value = delegator.makeValue(findByEntity
368:                            .getEntityName(), findByEntity.getAllFields());
369:
370:                    try {
371:                        value.store();
372:                    } catch (GenericEntityException e) {
373:                        Debug.logWarning(e, module);
374:                        request.setAttribute("_ERROR_MESSAGE_", "Update of "
375:                                + entity.getEntityName()
376:                                + " failed for value: " + value.toString());
377:                        return "error";
378:                    }
379:                } else {
380:                    request.setAttribute("_ERROR_MESSAGE_",
381:                            "Update Mode specified (" + updateMode
382:                                    + ") was not valid.");
383:                    Debug.logWarning("updateGeneric: Update Mode specified ("
384:                            + updateMode + ") was not valid for entity: "
385:                            + findByEntity.toString(), module);
386:                    return "error";
387:                }
388:
389:                return "success";
390:            }
391:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.