KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > admin > ApplicationFormController


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.web.controller.admin;
21
22 import org.apache.log4j.Logger;
23 import org.openi.application.Application;
24 import org.openi.xml.BeanStorage;
25 import org.springframework.validation.BindException;
26 import org.springframework.web.servlet.ModelAndView;
27 import org.springframework.web.servlet.mvc.SimpleFormController;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31
32 /**
33  * @author Uddhab Pant <br>
34  * @version $Revision: 1.8 $ $Date: 2006/04/12 00:39:12 $ <br>
35  *
36  * Controller for handling <strong>edit application config</strong> request.
37  * This class is responsible:
38  * <ul>
39  * <li> to display edit application form
40  * <li> form validation
41  * <li> form submit
42  * </ul>
43  *
44  */

45 public class ApplicationFormController extends SimpleFormController {
46     private static Logger logger = Logger.getLogger(ApplicationFormController.class);
47
48     /**
49      * Retrieve a backing object for the current form from the given request
50      * @param request HttpServletRequest
51      * @return Object as application config object
52      * @throws Exception
53      */

54     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request)
55         throws Exception JavaDoc {
56         try {
57             new BeanStorage().restoreBeanFromFile(getApplicationConfigPath(),
58                 Application.getInstance());
59
60             return Application.getInstance();
61         } catch (Exception JavaDoc e) {
62             logger.error("Exception:", e);
63             throw e;
64         }
65     }
66
67     /**
68      * Callback for custom post-processing in terms of binding and validation.
69      * Called on each submit, after standard binding and validation, but before error evaluation.
70      * @param request HttpServletRequest
71      * @param command Object
72      * @param errors BindException
73      * @throws Exception
74      */

75     protected void onBindAndValidate(HttpServletRequest JavaDoc request,
76         Object JavaDoc command, BindException errors) throws Exception JavaDoc {
77         if (request.getParameter("basicAuthentication") == null) {
78             Application.getInstance().setBasicAuthentication(false);
79         } else {
80             Application.getInstance().setBasicAuthentication(true);
81         }
82         
83         if(request.getParameter("sql2005Compatiblilty") == null ) {
84             Application.getInstance().setSql2005Compatiblilty(false);
85         } else {
86             Application.getInstance().setSql2005Compatiblilty(true);
87         }
88     }
89
90     /**
91      * Submit callback with all parameters. Called in case of submit without
92      * errors reported by the registered validator, or on every submit if no validator.
93      * @param request HttpServletRequest
94      * @param response HttpServletResponse
95      * @param command Object
96      * @param errors BindException
97      * @return ModelAndView
98      * @throws Exception
99      */

100     protected ModelAndView onSubmit(HttpServletRequest JavaDoc request,
101         HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
102         throws Exception JavaDoc {
103         try {
104             if (request.getParameter("save") != null) {
105                 new BeanStorage().saveBeanToFile(getApplicationConfigPath(),
106                     command);
107                 
108                 if(!Application.getInstance().isSql2005Compatiblilty() ) {
109                     System.setProperty("javax.xml.soap.SOAPConnectionFactory","com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnectionFactory");
110                 } else {
111                     System.setProperty("javax.xml.soap.SOAPConnectionFactory","org.openi.soap.client.HttpSOAPConnectionFactory");
112                 }
113                 
114                 // update application object in servlet context.
115
this.getServletContext()
116                     .setAttribute("application", (Application) command);
117             }
118
119             return super.onSubmit(request, response, command, errors);
120         } catch (Exception JavaDoc e) {
121             logger.error("Exception:", e);
122             throw e;
123         }
124     }
125
126     /**
127      * Returns application config file name including path.
128      * @return String
129      */

130     private String JavaDoc getApplicationConfigPath() {
131         //get real path of application which is something like '/apache/Tomcat 5.5/webapps/openi/
132
String JavaDoc appPath = this.getServletContext().getRealPath("/");
133
134         //removing last character if it is '/'
135
if ((appPath.charAt(appPath.length() - 1) == '\\')
136                 || (appPath.charAt(appPath.length() - 1) == '/')) {
137             appPath = appPath.substring(0, appPath.length() - 1);
138         }
139
140         String JavaDoc appConfigPath = appPath + "-projects/WEB-INF/application.xml";
141
142         logger.info("app config path:" + appConfigPath);
143
144         return appConfigPath;
145     }
146 }
147
Popular Tags