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: */package org.apache.geronimo.console.internaldb;
017:
018: import java.io.IOException;
019: import java.util.Collection;
020:
021: import javax.portlet.ActionRequest;
022: import javax.portlet.ActionResponse;
023: import javax.portlet.PortletConfig;
024: import javax.portlet.PortletException;
025: import javax.portlet.PortletRequestDispatcher;
026: import javax.portlet.RenderRequest;
027: import javax.portlet.RenderResponse;
028: import javax.portlet.WindowState;
029:
030: import org.apache.geronimo.console.BasePortlet;
031:
032: public class RunSQLPortlet extends BasePortlet {
033:
034: private static final String NORMALVIEW_JSP = "/WEB-INF/view/internaldb/runSQLNormal.jsp";
035:
036: private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/runSQLMaximized.jsp";
037:
038: private static final String HELPVIEW_JSP = "/WEB-INF/view/internaldb/runSQLHelp.jsp";
039:
040: private static final String CREATEDB_ACTION = "Create";
041:
042: private static final String DELETEDB_ACTION = "Delete";
043:
044: private static final String RUNSQL_ACTION = "Run SQL";
045:
046: private static final String BACKUPDB_ACTION = "Backup";
047:
048: private static final String RESTOREDB_ACTION = "Restore";
049:
050: private static RunSQLHelper sqlHelper = new RunSQLHelper();
051:
052: private static DBViewerHelper dbHelper = new DBViewerHelper();
053:
054: private PortletRequestDispatcher normalView;
055:
056: private PortletRequestDispatcher maximizedView;
057:
058: private PortletRequestDispatcher helpView;
059:
060: private Collection databases;
061:
062: private String action;
063:
064: private String createDB;
065:
066: private String deleteDB;
067:
068: private String useDB;
069:
070: private String backupDB;
071:
072: private String restoreDB;
073:
074: private String sqlStmts;
075:
076: private String actionResult;
077:
078: public void processAction(ActionRequest actionRequest,
079: ActionResponse actionResponse) throws PortletException,
080: IOException {
081: // Getting parameters here because it fails on doView()
082: action = actionRequest.getParameter("action");
083: createDB = actionRequest.getParameter("createDB");
084: deleteDB = actionRequest.getParameter("deleteDB");
085: useDB = actionRequest.getParameter("useDB");
086: backupDB = actionRequest.getParameter("backupDB");
087: restoreDB = actionRequest.getParameter("restoreDB");
088: sqlStmts = actionRequest.getParameter("sqlStmts");
089: actionResult = "";
090: if (CREATEDB_ACTION.equals(action)) {
091: actionResult = sqlHelper.createDB(createDB);
092: } else if (DELETEDB_ACTION.equals(action)) {
093: actionResult = sqlHelper.deleteDB(DerbyConnectionUtil
094: .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
099: .getDerbyHome(), backupDB);
100: } else if (RESTOREDB_ACTION.equals(action)) {
101: actionResult = sqlHelper.restoreDB(DerbyConnectionUtil
102: .getDerbyHome(), restoreDB);
103: }
104: }
105:
106: protected void doView(RenderRequest renderRequest,
107: RenderResponse renderResponse) throws IOException,
108: PortletException {
109: if (WindowState.MINIMIZED
110: .equals(renderRequest.getWindowState())) {
111: return;
112: }
113:
114: String singleSelectStmt;
115: if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
116: databases = dbHelper.getDerbyDatabases(DerbyConnectionUtil
117: .getDerbyHome());
118: renderRequest.setAttribute("databases", databases);
119: if (RUNSQL_ACTION.equals(action)) {
120: // check if it's a single Select statement
121: if ((sqlStmts != null)
122: && (sqlStmts.trim().indexOf(';') == -1)
123: && sqlStmts.trim().toUpperCase().startsWith(
124: "SELECT")
125: && RunSQLHelper.SQL_SUCCESS_MSG
126: .equals(actionResult)) {
127: singleSelectStmt = sqlStmts.trim();
128: // set action result to blank so it won't display
129: actionResult = "";
130: } else {
131: singleSelectStmt = "";
132: }
133: renderRequest.setAttribute("useDB", useDB);
134: renderRequest.setAttribute("singleSelectStmt",
135: singleSelectStmt);
136: renderRequest.setAttribute("ds", DerbyConnectionUtil
137: .getDataSource(useDB));
138: }
139: if ((action != null) && (action.trim().length() > 0)) {
140: renderRequest
141: .setAttribute("actionResult", actionResult);
142: //set action to null so that subsequent renders of portlet
143: // won't display
144: //action result if there is no action to process
145: action = null;
146: }
147: normalView.include(renderRequest, renderResponse);
148: } else {
149: maximizedView.include(renderRequest, renderResponse);
150: }
151: }
152:
153: protected void doHelp(RenderRequest renderRequest,
154: RenderResponse renderResponse) throws PortletException,
155: IOException {
156: helpView.include(renderRequest, renderResponse);
157: }
158:
159: public void init(PortletConfig portletConfig)
160: throws PortletException {
161: super .init(portletConfig);
162: normalView = portletConfig.getPortletContext()
163: .getRequestDispatcher(NORMALVIEW_JSP);
164: maximizedView = portletConfig.getPortletContext()
165: .getRequestDispatcher(MAXIMIZEDVIEW_JSP);
166: helpView = portletConfig.getPortletContext()
167: .getRequestDispatcher(HELPVIEW_JSP);
168: databases = dbHelper.getDerbyDatabases(DerbyConnectionUtil
169: .getDerbyHome());
170: }
171:
172: public void destroy() {
173: normalView = null;
174: maximizedView = null;
175: helpView = null;
176: super.destroy();
177: }
178:
179: }
|