001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.console.internaldb;
019
020 import java.io.IOException;
021 import java.util.Collection;
022
023 import javax.portlet.ActionRequest;
024 import javax.portlet.ActionResponse;
025 import javax.portlet.PortletConfig;
026 import javax.portlet.PortletException;
027 import javax.portlet.PortletRequestDispatcher;
028 import javax.portlet.RenderRequest;
029 import javax.portlet.RenderResponse;
030 import javax.portlet.WindowState;
031
032 import org.apache.geronimo.console.BasePortlet;
033
034 public class RunSQLPortlet extends BasePortlet {
035
036 private static final String NORMALVIEW_JSP = "/WEB-INF/view/internaldb/runSQLNormal.jsp";
037
038 private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/runSQLMaximized.jsp";
039
040 private static final String HELPVIEW_JSP = "/WEB-INF/view/internaldb/runSQLHelp.jsp";
041
042 private static final String CREATEDB_ACTION = "Create";
043
044 private static final String DELETEDB_ACTION = "Delete";
045
046 private static final String RUNSQL_ACTION = "Run SQL";
047
048 private static final String BACKUPDB_ACTION = "Backup";
049
050 private static final String RESTOREDB_ACTION = "Restore";
051
052 private static RunSQLHelper sqlHelper = new RunSQLHelper();
053
054 private static DBViewerHelper dbHelper = new DBViewerHelper();
055
056 private PortletRequestDispatcher normalView;
057
058 private PortletRequestDispatcher maximizedView;
059
060 private PortletRequestDispatcher helpView;
061
062 private Collection databases;
063
064 private String action;
065
066 private String createDB;
067
068 private String deleteDB;
069
070 private String useDB;
071
072 private String backupDB;
073
074 private String restoreDB;
075
076 private String sqlStmts;
077
078 private String actionResult;
079
080 public void processAction(ActionRequest actionRequest,
081 ActionResponse actionResponse) throws PortletException, IOException {
082 // Getting parameters here because it fails on doView()
083 action = actionRequest.getParameter("action");
084 createDB = actionRequest.getParameter("createDB");
085 deleteDB = actionRequest.getParameter("deleteDB");
086 useDB = actionRequest.getParameter("useDB");
087 backupDB = actionRequest.getParameter("backupDB");
088 restoreDB = actionRequest.getParameter("restoreDB");
089 sqlStmts = actionRequest.getParameter("sqlStmts");
090 actionResult = "";
091 if (CREATEDB_ACTION.equals(action)) {
092 actionResult = sqlHelper.createDB(createDB);
093 } else if (DELETEDB_ACTION.equals(action)) {
094 actionResult = sqlHelper.deleteDB(DerbyConnectionUtil.getDerbyHome(), deleteDB);
095 } else if (RUNSQL_ACTION.equals(action)) {
096 actionResult = sqlHelper.runSQL(useDB, sqlStmts);
097 } else if (BACKUPDB_ACTION.equals(action)) {
098 actionResult = sqlHelper.backupDB(DerbyConnectionUtil.getDerbyHome(), backupDB);
099 } else if (RESTOREDB_ACTION.equals(action)) {
100 actionResult = sqlHelper.restoreDB(DerbyConnectionUtil.getDerbyHome(), restoreDB);
101 }
102 }
103
104 protected void doView(RenderRequest renderRequest,
105 RenderResponse renderResponse) throws IOException, PortletException {
106 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
107 return;
108 }
109
110 String singleSelectStmt;
111 if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
112 databases = dbHelper.getDerbyDatabases(DerbyConnectionUtil.getDerbyHome());
113 renderRequest.setAttribute("databases", databases);
114 if (RUNSQL_ACTION.equals(action)) {
115 // check if it's a single Select statement
116 if ((sqlStmts != null) && (sqlStmts.trim().indexOf(';') == -1)
117 && sqlStmts.trim().toUpperCase().startsWith("SELECT")
118 && RunSQLHelper.SQL_SUCCESS_MSG.equals(actionResult)) {
119 singleSelectStmt = sqlStmts.trim();
120 // set action result to blank so it won't display
121 actionResult = "";
122 } else {
123 singleSelectStmt = "";
124 }
125 renderRequest.setAttribute("useDB", useDB);
126 renderRequest
127 .setAttribute("singleSelectStmt", singleSelectStmt);
128 renderRequest.setAttribute("ds", DerbyConnectionUtil
129 .getDataSource(useDB));
130 }
131 if ((action != null) && (action.trim().length() > 0)) {
132 renderRequest.setAttribute("actionResult", actionResult);
133 //set action to null so that subsequent renders of portlet
134 // won't display
135 //action result if there is no action to process
136 action = null;
137 }
138 normalView.include(renderRequest, renderResponse);
139 } else {
140 maximizedView.include(renderRequest, renderResponse);
141 }
142 }
143
144 protected void doHelp(RenderRequest renderRequest,
145 RenderResponse renderResponse) throws PortletException, IOException {
146 helpView.include(renderRequest, renderResponse);
147 }
148
149 public void init(PortletConfig portletConfig) throws PortletException {
150 super.init(portletConfig);
151 normalView = portletConfig.getPortletContext().getRequestDispatcher(
152 NORMALVIEW_JSP);
153 maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
154 MAXIMIZEDVIEW_JSP);
155 helpView = portletConfig.getPortletContext().getRequestDispatcher(
156 HELPVIEW_JSP);
157 databases = dbHelper.getDerbyDatabases(DerbyConnectionUtil.getDerbyHome());
158 }
159
160 public void destroy() {
161 normalView = null;
162 maximizedView = null;
163 helpView = null;
164 super.destroy();
165 }
166
167 }