KickJava   Java API By Example, From Geeks To Geeks.

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


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.project.DirectoryLister;
24 import org.openi.project.ProjectContext;
25 import org.openi.security.*;
26 import org.openi.web.UploadFile;
27 import org.springframework.validation.BindException;
28 import org.springframework.web.servlet.ModelAndView;
29 import org.springframework.web.servlet.mvc.SimpleFormController;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import java.io.File JavaDoc;
37
38
39 /**
40  * @author Uddhab Pant <br>
41  *
42  * This controller returns directory list as form backing object. It receives
43  * file upload request and delegate upload task to UploadFile class.
44  *
45  *
46  */

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

58     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request) throws
59             Exception JavaDoc {
60         Map JavaDoc model;
61         List JavaDoc dirList;
62
63         try {
64             model = new HashMap JavaDoc();
65
66             HttpSession JavaDoc session = request.getSession();
67
68             projectContext = (ProjectContext) session
69                              .getAttribute("projectContext");
70
71             // get directory list
72
List JavaDoc modules = projectContext.getProjectModules(projectContext
73                     .hasPermission(Permission.SAVE_PRIVATE),
74                     projectContext.hasPermission(Permission.SAVE_PUBLIC));
75
76             dirList = DirectoryLister.findProjectDirectories(projectContext
77                     .getProjectDirectory(), modules);
78
79             model.put("dirList", dirList);
80
81             return model;
82         } catch (Exception JavaDoc e) {
83             logger.error("Exception:", e);
84             throw e;
85         }
86     }
87
88     /**
89      * Submit callback with all parameters. Called in case of submit without
90      * errors reported by the registered validator, or on every submit if no validator.
91      * @param request HttpServletRequest
92      * @param response HttpServletResponse
93      * @param command Object
94      * @param errors BindException
95      * @return ModelAndView
96      * @throws Exception
97      */

98     protected ModelAndView onSubmit(HttpServletRequest JavaDoc request,
99                                     HttpServletResponse JavaDoc response,
100                                     Object JavaDoc command,
101                                     BindException errors) throws Exception JavaDoc {
102         Map JavaDoc model = (Map JavaDoc) command;
103         logger.debug("action=" + request.getParameter("action"));
104         if ("Save".equals(request.getParameter("action"))) {
105             String JavaDoc datasource = request.getParameter("jdbcDatasource");
106             String JavaDoc file = request.getParameter("selectedTemplate");
107             if( projectContext.getProject().getJdbcMap()==null)
108                  projectContext.getProject().setJdbcMap(new HashMap JavaDoc());
109             projectContext.getProject().getJdbcMap().put(file, datasource);
110             projectContext.saveProject();
111             model.put("message", "dataSourceSetted.message");
112             model.put("messageClass", "infoMessage");
113         } else {
114             try {
115                 //delegate file upload task to UploadFile.
116
String JavaDoc filename = new UploadFile().uploadFile(request);
117
118                 if (filename != null && filename.endsWith(".jrxml")) {
119                     filename = new File(filename).getCanonicalPath();
120                     String JavaDoc projectDir = new File(projectContext.
121                                                  getProjectDirectory()).
122                                         getCanonicalPath();
123                     if (!filename.startsWith(projectDir)) {
124                         model.put("message", "datasourceSet.error");
125                         model.put("messageClass", "errorMessage");
126                         return new ModelAndView(getSuccessView(), "model",
127                                                 model);
128                     } else {
129
130                         filename = filename.substring(projectDir.length() + 1).replace('\\','/');
131                         Map JavaDoc map = projectContext.getJdbcDatasourceMap();
132
133                         if (map.size() == 0) {
134                             model.put("message",
135                                       "noJdbcDatasourceToMap.message");
136                             model.put("messageClass", "infoMessage");
137                             return new ModelAndView(getSuccessView(), "model",
138                                     model);
139                         }
140                         model.put("message", "setJdbcDatasource.message");
141                         model.put("messageClass", "infoMessage");
142                         model.put("datasource",
143                                   projectContext.getJdbcDatasourceMap());
144                         model.put("selectedTemplate",
145                                   projectContext.
146                                   extractModulePath(filename));
147                         return new ModelAndView("datasourceSetterView", "model",
148                                                 model);
149                     }
150
151                 }
152                 model.put("message", "upload.message");
153                 model.put("messageClass", "infoMessage");
154
155             } catch (Exception JavaDoc e) {
156                 logger.error("Exception:", e);
157                 model.put("message", "upload.error");
158                 model.put("messageClass", "errorMessage");
159             }
160         }
161         return new ModelAndView(getSuccessView(), "model", model);
162     }
163 }
164
Popular Tags