KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > internaldb > RunSQLPortlet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.internaldb;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 import javax.portlet.ActionRequest;
24 import javax.portlet.ActionResponse;
25 import javax.portlet.PortletConfig;
26 import javax.portlet.PortletException;
27 import javax.portlet.PortletRequestDispatcher;
28 import javax.portlet.RenderRequest;
29 import javax.portlet.RenderResponse;
30 import javax.portlet.WindowState;
31
32 import org.apache.geronimo.console.BasePortlet;
33
34 public class RunSQLPortlet extends BasePortlet {
35
36     private static final String JavaDoc NORMALVIEW_JSP = "/WEB-INF/view/internaldb/runSQLNormal.jsp";
37
38     private static final String JavaDoc MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/runSQLMaximized.jsp";
39
40     private static final String JavaDoc HELPVIEW_JSP = "/WEB-INF/view/internaldb/runSQLHelp.jsp";
41
42     private static final String JavaDoc CREATEDB_ACTION = "Create";
43
44     private static final String JavaDoc DELETEDB_ACTION = "Delete";
45
46     private static final String JavaDoc RUNSQL_ACTION = "Run SQL";
47
48     private static final String JavaDoc BACKUPDB_ACTION = "Backup";
49
50     private static final String JavaDoc RESTOREDB_ACTION = "Restore";
51
52     private static RunSQLHelper sqlHelper = new RunSQLHelper();
53
54     private static DBViewerHelper dbHelper = new DBViewerHelper();
55
56     private static String JavaDoc derbyHome = System.getProperty("derby.system.home");
57
58     private PortletRequestDispatcher normalView;
59
60     private PortletRequestDispatcher maximizedView;
61
62     private PortletRequestDispatcher helpView;
63
64     private Collection JavaDoc databases;
65
66     private String JavaDoc action;
67
68     private String JavaDoc createDB;
69
70     private String JavaDoc deleteDB;
71
72     private String JavaDoc useDB;
73
74     private String JavaDoc backupDB;
75
76     private String JavaDoc restoreDB;
77
78     private String JavaDoc sqlStmts;
79
80     private String JavaDoc actionResult;
81
82     public void processAction(ActionRequest actionRequest,
83             ActionResponse actionResponse) throws PortletException, IOException JavaDoc {
84         // Getting parameters here because it fails on doView()
85
action = actionRequest.getParameter("action");
86         createDB = actionRequest.getParameter("createDB");
87         deleteDB = actionRequest.getParameter("deleteDB");
88         useDB = actionRequest.getParameter("useDB");
89         backupDB = actionRequest.getParameter("backupDB");
90         restoreDB = actionRequest.getParameter("restoreDB");
91         sqlStmts = actionRequest.getParameter("sqlStmts");
92         actionResult = "";
93         if (CREATEDB_ACTION.equals(action)) {
94             actionResult = sqlHelper.createDB(createDB);
95         } else if (DELETEDB_ACTION.equals(action)) {
96             actionResult = sqlHelper.deleteDB(derbyHome, deleteDB);
97         } else if (RUNSQL_ACTION.equals(action)) {
98             actionResult = sqlHelper.runSQL(useDB, sqlStmts);
99         } else if (BACKUPDB_ACTION.equals(action)) {
100             actionResult = sqlHelper.backupDB(derbyHome, backupDB);
101         } else if (RESTOREDB_ACTION.equals(action)) {
102             actionResult = sqlHelper.restoreDB(derbyHome, restoreDB);
103         }
104     }
105
106     protected void doView(RenderRequest renderRequest,
107             RenderResponse renderResponse) throws IOException JavaDoc, PortletException {
108         if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
109             return;
110         }
111
112         String JavaDoc singleSelectStmt;
113         if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
114             databases = dbHelper.getDerbyDatabases(derbyHome);
115             renderRequest.setAttribute("databases", databases);
116             if (RUNSQL_ACTION.equals(action)) {
117                 // check if it's a single Select statement
118
if ((sqlStmts != null) && (sqlStmts.trim().indexOf(';') == -1)
119                         && sqlStmts.trim().toUpperCase().startsWith("SELECT")
120                         && RunSQLHelper.SQL_SUCCESS_MSG.equals(actionResult)) {
121                     singleSelectStmt = sqlStmts.trim();
122                     // set action result to blank so it won't display
123
actionResult = "";
124                 } else {
125                     singleSelectStmt = "";
126                 }
127                 renderRequest.setAttribute("useDB", useDB);
128                 renderRequest
129                         .setAttribute("singleSelectStmt", singleSelectStmt);
130                 renderRequest.setAttribute("ds", DerbyConnectionUtil
131                         .getDataSource(useDB));
132             }
133             if ((action != null) && (action.trim().length() > 0)) {
134                 renderRequest.setAttribute("actionResult", actionResult);
135                 //set action to null so that subsequent renders of portlet
136
// won't display
137
//action result if there is no action to process
138
action = null;
139             }
140             normalView.include(renderRequest, renderResponse);
141         } else {
142             maximizedView.include(renderRequest, renderResponse);
143         }
144     }
145
146     protected void doHelp(RenderRequest renderRequest,
147             RenderResponse renderResponse) throws PortletException, IOException JavaDoc {
148         helpView.include(renderRequest, renderResponse);
149     }
150
151     public void init(PortletConfig portletConfig) throws PortletException {
152         super.init(portletConfig);
153         normalView = portletConfig.getPortletContext().getRequestDispatcher(
154                 NORMALVIEW_JSP);
155         maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
156                 MAXIMIZEDVIEW_JSP);
157         helpView = portletConfig.getPortletContext().getRequestDispatcher(
158                 HELPVIEW_JSP);
159         databases = dbHelper.getDerbyDatabases(derbyHome);
160     }
161
162     public void destroy() {
163         normalView = null;
164         maximizedView = null;
165         helpView = null;
166         super.destroy();
167     }
168
169 }
170
Popular Tags