KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > sql > struts > EditQueryAction


1 package org.jahia.clipbuilder.sql.struts;
2
3 import org.jahia.clipbuilder.sql.SessionManager;
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import org.apache.struts.action.ActionForward;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import org.apache.struts.action.ActionMapping;
9 import javax.servlet.http.*;
10 import org.apache.struts.action.ActionForm;
11 import org.jahia.clipbuilder.sql.config.DbProps;
12 import java.util.*;
13 import org.jahia.clipbuilder.sql.bean.*;
14 import org.jahia.clipbuilder.sql.util.DeployUtilities;
15 import java.io.File JavaDoc;
16 import org.jahia.clipbuilder.sql.database.hibernate.model.*;
17 import org.apache.struts.action.*;
18 import org.apache.commons.beanutils.RowSetDynaClass;
19
20 /**
21  * Description of the Class
22  *
23  *@author ktlili
24  */

25 public class EditQueryAction extends BaseAction {
26         /**
27          * Description of the Field
28          */

29         public static String JavaDoc DATABASE_MYSQL = "mysql";
30         /**
31          * Description of the Field
32          */

33         public static String JavaDoc DATABASE_ORACLE = "oracle";
34         /**
35          * Description of the Field
36          */

37         public static String JavaDoc DATABASE_POSTGRE = "postgre";
38         /**
39          * Description of the Field
40          */

41         public static String JavaDoc DATABASE_HSQL = "hsql";
42
43
44
45     /**
46      * Gets the KeyMethodMap attribute of the EditQueryAction object
47      *
48      *@return The KeyMethodMap value
49      */

50     public Map JavaDoc getKeyMethodMap() {
51         Map JavaDoc map = new HashMap JavaDoc();
52         map.put("init", "init");
53         map.put("sql.button.executeQuery", "executeQuery");
54         map.put("sql.button.executeQueryForLink", "executeQuery");
55         map.put("sql.button.deploy", "deploy");
56         map.put("sql.button.saveAsDefault", "saveOrUpdate");
57         map.put("menu.back", "backToMenuBuilder");
58         return map;
59     }
60
61
62     /**
63      * Gets the ClippersDirectoryPath attribute of the EditQueryAction object
64      *
65      *@param request Description of Parameter
66      *@return The ClippersDirectoryPath value
67      */

68     public String JavaDoc getClippersDirectoryPath(HttpServletRequest JavaDoc request) {
69         return getServlet().getServletContext().getRealPath("/") +
70                 getResources(request).getMessage("clippers.repository.path");
71     }
72
73
74     /**
75      * Gets the ClippersDirectoryPathDeploy attribute of the ManageAction object
76      *
77      *@param request Description of Parameter
78      *@return The ClippersDirectoryPathDeploy value
79      */

80     public String JavaDoc getClippersDirectoryPathDeploy(HttpServletRequest JavaDoc request) {
81         String JavaDoc path = org.jahia.clipbuilder.util.JahiaUtils.getPortletDiretcoryPath(request);
82         if (path == null) {
83             // clip builder is not part of jahia
84
path = getResources(request).getMessage("clippers.repository.deploy.path");
85         }
86         return path;
87     }
88
89
90     /**
91      * Description of the Method
92      *
93      *@param actionMapping Description of Parameter
94      *@param actionForm Description of Parameter
95      *@param request Description of Parameter
96      *@param response Description of Parameter
97      *@return Description of the Returned Value
98      */

99     public ActionForward executeQuery(ActionMapping actionMapping,
100             ActionForm actionForm,
101             HttpServletRequest JavaDoc request,
102             HttpServletResponse response) {
103         EditQueryForm form = (EditQueryForm) actionForm;
104         HttpSession session = request.getSession();
105         // get params
106
String JavaDoc title = form.getTitle();
107         String JavaDoc database = form.getDatabase();
108         String JavaDoc databaseName = form.getDatabaseName();
109         String JavaDoc databaseUrl = form.getDatabaseUrl();
110         String JavaDoc userName = form.getUserName();
111         String JavaDoc password = form.getUserPassword();
112         String JavaDoc sql = form.getSqlQuery();
113         String JavaDoc tableSize = form.getTableSize();
114
115         String JavaDoc driverAndUrl[] = buildDriverAndUrl(database, databaseName, databaseUrl);
116
117         //set properties
118
DbProps.DRIVER = driverAndUrl[0];
119         DbProps.URL = driverAndUrl[1];
120         DbProps.PASSWORD = password;
121         DbProps.USERNAME = userName;
122
123         try {
124             log.info("Executing Query method.");
125
126             if (log.isDebugEnabled()) {
127                 log.debug("Execute query method. sql : " + sql);
128             }
129
130             //If current query's results have not been cached, do so now.
131
QueryBean qb = new QueryBean();
132             qb.executeQuery(sql);
133
134             //Get column headings/query results and assign them to request attributes.
135
String JavaDoc[] queryColumns = (String JavaDoc[]) qb.getQueryColumns();
136             List queryData = (List) qb.getQueryData();
137                         RowSetDynaClass resultSet = qb.getResultSet();
138
139             session.setAttribute("title", title);
140             session.setAttribute("queryColumns", queryColumns);
141             session.setAttribute("queryData", queryData);
142             session.setAttribute("tableSize", tableSize);
143                         session.setAttribute("resultSet", resultSet);
144
145             return actionMapping.getInputForward();
146         }
147         catch (Exception JavaDoc ex) {
148             log.error("Cant execute query due to an exception",ex);
149         }
150
151         return actionMapping.getInputForward();
152     }
153
154
155     /**
156      * Description of the Method
157      *
158      *@param actionMapping Description of Parameter
159      *@param actionForm Description of Parameter
160      *@param request Description of Parameter
161      *@param response Description of Parameter
162      *@return Description of the Returned Value
163      */

164     public ActionForward deploy(ActionMapping actionMapping,
165             ActionForm actionForm,
166             HttpServletRequest JavaDoc request,
167             HttpServletResponse response) {
168         EditQueryForm form = (EditQueryForm) actionForm;
169
170         // get params
171
String JavaDoc title = form.getTitle();
172         String JavaDoc database = form.getDatabase();
173         String JavaDoc databaseName = form.getDatabaseName();
174         String JavaDoc databaseUrl = form.getDatabaseUrl();
175         String JavaDoc userName = form.getUserName();
176         String JavaDoc password = form.getUserPassword();
177         String JavaDoc sqlQuery = form.getSqlQuery();
178         String JavaDoc portletName = form.getPortletname();
179         String JavaDoc portletDescription = form.getPortletdescription();
180
181         //generate clipper.xml file
182
String JavaDoc sqlPatternDrirectoryPath = getClippersDirectoryPath(request) + File.separator + "sqlPattern" + File.separator + "sqlClipperPortletPattern.war";
183
184         //deploy
185
String JavaDoc driverAndUrl[] = buildDriverAndUrl(database, databaseName, databaseUrl);
186         String JavaDoc driver = driverAndUrl[0];
187         String JavaDoc url = driverAndUrl[1];
188         String JavaDoc patternWar = sqlPatternDrirectoryPath;
189         String JavaDoc warTarget = getClippersDirectoryPathDeploy(request) + File.separator + "sql_clip_" + title + ".war".replaceAll(" ","_");
190         try {
191             DeployUtilities.getInstance().deploy(patternWar, warTarget, portletName, portletDescription, driver, url, databaseName, userName, password, title, sqlQuery);
192         }
193         catch (Exception JavaDoc ex) {
194             ex.printStackTrace();
195             log.error("Error has occured during deuploy step: " + ex.toString());
196         }
197
198         return actionMapping.getInputForward();
199     }
200
201
202     /**
203      * Description of the Method
204      *
205      *@param actionMapping Description of Parameter
206      *@param actionForm Description of Parameter
207      *@param request Description of Parameter
208      *@param response Description of Parameter
209      *@return Description of the Returned Value
210      */

211     public ActionForward backToMenuBuilder(ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest JavaDoc request,HttpServletResponse response) {
212         return actionMapping.findForward("menuBuilder");
213     }
214
215
216
217     /**
218      * Description of the Method
219      *
220      *@param actionMapping Description of Parameter
221      *@param actionForm Description of Parameter
222      *@param request Description of Parameter
223      *@param response Description of Parameter
224      *@return Description of the Returned Value
225      */

226     public ActionForward init(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse response) {
227         EditQueryForm form = (EditQueryForm) actionForm;
228
229         //fill form from database
230
DatabaseConfiguration dConf = getDefaultConfigurationManager().
231                 getDefaultConfiguration();
232         if (dConf != null) {
233             form.setDatabase(dConf.getDatabaseType());
234             form.setDatabaseName(dConf.getDatabaseName());
235             form.setDatabaseUrl(dConf.getDatabaseUrl());
236             form.setUserName(dConf.getUserName());
237             form.setUserPassword(dConf.getUserPassword());
238         }
239         else {
240
241             //message: no default configuration
242
ActionMessages messages = new ActionMessages();
243             messages.add("msg", new ActionMessage("sql.messages.database.empty"));
244
245             saveMessages(request, messages);
246         }
247         return actionMapping.getInputForward();
248     }
249
250
251
252     /**
253      * Description of the Method
254      *
255      *@param actionMapping Description of Parameter
256      *@param actionForm Description of Parameter
257      *@param request Description of Parameter
258      *@param response Description of Parameter
259      *@return Description of the Returned Value
260      */

261     public ActionForward saveOrUpdate(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest JavaDoc request, HttpServletResponse response) {
262         EditQueryForm form = (EditQueryForm) actionForm;
263
264         //fill from form
265
DatabaseConfiguration dConf = getDefaultConfigurationManager().getDefaultConfiguration();
266         if (dConf == null) {
267             dConf = new DatabaseConfiguration();
268         }
269         dConf.setDatabaseType(form.getDatabase());
270         dConf.setDatabaseName(form.getDatabaseName());
271         dConf.setDatabaseUrl(form.getDatabaseUrl());
272         dConf.setUserName(form.getUserName());
273         dConf.setUserPassword(form.getUserPassword());
274
275         // save or update
276
getDefaultConfigurationManager().saveOrUpdateDefaultConfiguration(dConf);
277
278         return actionMapping.getInputForward();
279     }
280
281
282     /**
283      * Description of the Method
284      *
285      *@param database Description of Parameter
286      *@param databaseName Description of Parameter
287      *@param databaseUrl Description of Parameter
288      *@return Description of the Returned Value
289      */

290         private String JavaDoc[] buildDriverAndUrl(String JavaDoc database, String JavaDoc databaseName,
291                         String JavaDoc databaseUrl) {
292                 // SQL
293
String JavaDoc driver = null;
294                 String JavaDoc url = null;
295                 if (database.equalsIgnoreCase(DATABASE_MYSQL)) {
296                         driver = "com.mysql.jdbc.Driver";
297                         url = "jdbc:mysql://" + databaseUrl + "/" + databaseName;
298                 }
299                 else if (database.equalsIgnoreCase(DATABASE_ORACLE)) {
300                         driver = "oracle.jdbc.driver.OracleDriver";
301                         url = "jdbc:oracle:thin:@" + databaseUrl + ":" + databaseName;
302                 }
303                 else if (database.equalsIgnoreCase(DATABASE_POSTGRE)) {
304                         driver = "org.postgresql.Driver";
305                         url = "jdbc:postgresql://" + databaseUrl + "/" + databaseName;
306                 }
307                 else if (database.equalsIgnoreCase(DATABASE_HSQL)) {
308                         driver = "org.hsqldb.jdbcDriver";
309                         url = "jdbc:hsqldb:" + databaseUrl + "/" + databaseName;
310                 }
311
312                 String JavaDoc[] result = {driver, url};
313                 return result;
314         }
315
316
317 }
318
Popular Tags