1 /*
2 * PROJECT : DAR Runtime and Tools
3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE
5 * Version 2.1, February 1999
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package org.enableit.db.darrt.web;
22
23 import java.io.ByteArrayOutputStream;
24 import java.util.Arrays;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.log4j.Logger;
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionError;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.actions.DispatchAction;
37 import org.enableit.db.beans.Database;
38 import org.enableit.db.beans.Provider;
39 import org.enableit.db.beans.ProviderExt;
40 import org.enableit.db.beans.Schema;
41 import org.enableit.db.beans.Table;
42 import org.enableit.db.darrt.ConfigurationException;
43 import org.enableit.db.darrt.DarHandler;
44 import org.enableit.db.darrt.Globals;
45 import org.enableit.db.darrt.MetaDataFactory;
46
47
48 /***
49 * Action to handle DAR exports (and perhaps imports later).
50 *
51 * @author Tim Stephenson
52 * @version $Revision: 1.3 $ $Date: 2004/03/20 05:25:03 $
53 *
54 * @struts:action path="dar" scope="request" input="/darExport.jsp"
55 * @struts:action-forward name="schema" path="/schema.do"
56 * @struts:action-forward name="darCriteria" path="/darExport.jsp"
57 * @struts:action-forward name="darSuccess" path="/dar.jsp"
58 */
59 public class DarHandlerAction extends DispatchAction {
60 /*
61 * Properties
62 */
63
64 /***
65 * The Log4J <code>Logger</code> doing the logging.
66 */
67 private static Logger logger = Logger.getLogger(DarHandlerAction.class);
68
69 /*
70 * Methods
71 */
72
73 /***
74 * Process a request to export a DAR file from the database.
75 *
76 * @param mapping The ActionMapping used to select this instance
77 * @param form The optional ActionForm bean for this request (if any)
78 * @param request The HTTP request we are processing
79 * @param response The HTTP response we are creating
80 */
81 public ActionForward unspecified(ActionMapping mapping, ActionForm form,
82 HttpServletRequest request, HttpServletResponse response)
83 throws Exception {
84 logger.info("METHOD_ENTRY: unspecified");
85
86 // Ensure have schema definition
87 request.setAttribute(Globals.FORWARD_TO, "darCriteria");
88
89 logger.info("METHOD_EXIT: unspecified");
90
91 return mapping.findForward("schema");
92 }
93
94 /***
95 * Process a request to export a DAR file from the database.
96 *
97 * @param mapping The ActionMapping used to select this instance
98 * @param form The optional ActionForm bean for this request (if any)
99 * @param request The HTTP request we are processing
100 * @param response The HTTP response we are creating
101 */
102 public ActionForward export(ActionMapping mapping, ActionForm form,
103 HttpServletRequest request, HttpServletResponse response)
104 throws Exception {
105 logger.info("METHOD_ENTRY: export");
106
107 ActionErrors errors = (ActionErrors) request.getAttribute(Action.ERROR_KEY);
108 String forwardTo = "darSuccess";
109
110 try {
111 logger.info("tableNames: "
112 + Arrays.asList(request.getParameterValues("tableName")));
113 logger.info("dataSourceName: "
114 + request.getParameter("dataSourceName"));
115 logger.info("schemaName: " + request.getParameter("schemaName"));
116
117 // Construct new database object
118 Database database = new Database();
119 Schema schema = new Schema();
120
121 schema.setName(request.getParameter("schemaName"));
122
123 // Add table objects
124 MetaDataFactory metaData = MetaDataFactory.getInstance();
125 Provider provider = getProvider(mapping, form, request, response);
126
127 database.setProvider(provider);
128
129 for (int i = 0; i < request.getParameterValues("tableName").length;
130 i++) {
131 String tableName = request.getParameterValues("tableName")[i];
132 Table table = metaData.getMetaData(provider, tableName);
133
134 schema.addTable(table);
135 }
136
137 database.addSchema(schema);
138
139 // construct dar
140 DarHandler dh = new DarHandler();
141 ByteArrayOutputStream dar = new ByteArrayOutputStream();
142
143 dh.create(dar, database,
144 Arrays.asList(request.getParameterValues("tableName")));
145 dar.close();
146
147 // place dar into session / request
148 request.getSession().setAttribute(Globals.DAR, dar);
149 } catch (org.enableit.db.DBException e) {
150 logger.error(e.getMessage(), e);
151
152 if (errors == null) {
153 errors = new ActionErrors();
154 }
155
156 ActionError error = new ActionError("app.dbexception",
157 e.getMessage());
158
159 errors.add(ActionErrors.GLOBAL_ERROR, error);
160 saveErrors(request, errors);
161
162 return new ActionForward(mapping.getInput());
163 } catch (Exception e) {
164 logger.error(e.getMessage(), e);
165 throw new javax.servlet.ServletException(e.getMessage());
166 }
167
168 // Return the forward instruction
169 logger.info("METHOD_EXIT: export, forwarding to: " + forwardTo);
170
171 return mapping.findForward(forwardTo);
172 }
173
174 /***
175 * Setup context and return provider metadata.
176 *
177 * @param mapping The ActionMapping used to select this instance
178 * @param form The optional ActionForm bean for this request (if any)
179 * @param request The HTTP request we are processing
180 * @param response The HTTP response we are creating
181 */
182 private Provider getProvider(ActionMapping mapping, ActionForm form,
183 HttpServletRequest request, HttpServletResponse response)
184 throws ConfigurationException {
185 logger.info("METHOD_ENTRY getProvider");
186
187 // TODO: should this be app configuration or request config'n?
188 // REQUEST CONFIGURATION !
189 Provider provider = new Provider(); // (Provider)
190
191 provider = new Provider();
192 provider.setJdbc2DatasourceName(request.getParameter("dataSourceName"));
193 provider.setSchemaName(request.getParameter("schemaName"));
194
195 //getServlet().getServletContext()
196 // .setAttribute("Provider", provider) ;
197 //}
198 logger.info("METHOD_EXIT: getProvider, provider="
199 + ProviderExt.toString(provider));
200
201 return provider;
202 }
203 }
This page was automatically generated by Maven