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:
020: import javax.portlet.ActionRequest;
021: import javax.portlet.ActionResponse;
022: import javax.portlet.PortletConfig;
023: import javax.portlet.PortletException;
024: import javax.portlet.PortletRequestDispatcher;
025: import javax.portlet.RenderRequest;
026: import javax.portlet.RenderResponse;
027: import javax.portlet.WindowState;
028:
029: import org.apache.geronimo.console.BasePortlet;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: public class DBViewerPortlet extends BasePortlet {
034:
035: private final static Log log = LogFactory
036: .getLog(DBViewerPortlet.class);
037:
038: private static final int RDBMS_DERBY = 1;
039:
040: private static final int RDBMS_MSSQL = 2;
041:
042: private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/dbViewerMaximized.jsp";
043:
044: private static final String HELPVIEW_JSP = "/WEB-INF/view/internaldb/dbViewerHelp.jsp";
045:
046: private static final String LISTDATABASES_JSP = "/WEB-INF/view/internaldb/listDatabases.jsp";
047:
048: private static final String LISTTABLES_JSP = "/WEB-INF/view/internaldb/listTables.jsp";
049:
050: private static final String VIEWTABLECONTENTS_JSP = "/WEB-INF/view/internaldb/viewTableContents.jsp";
051:
052: private static final String LISTDB_ACTION = "listDatabases";
053:
054: private static final String LISTTBLS_ACTION = "listTables";
055:
056: private static final String VIEWTBLCONTENTS_ACTION = "viewTableContents";
057:
058: private static DBViewerHelper helper = new DBViewerHelper();
059:
060: private PortletRequestDispatcher maximizedView;
061:
062: private PortletRequestDispatcher helpView;
063:
064: private PortletRequestDispatcher listDatabasesView;
065:
066: private PortletRequestDispatcher listTablesView;
067:
068: private PortletRequestDispatcher viewTableContentsView;
069:
070: public void processAction(ActionRequest actionRequest,
071: ActionResponse actionResponse) throws PortletException,
072: IOException {
073: // getting parameters here because it fails on doView()
074: String action = actionRequest.getParameter("action");
075: String db = actionRequest.getParameter("db");
076: String tbl = actionRequest.getParameter("tbl");
077: String viewTables = actionRequest.getParameter("viewTables");
078: String rdbms = actionRequest.getParameter("rdbms");
079: // pass them to the render request
080: if (action != null) {
081: actionResponse.setRenderParameter("action", action);
082: }
083: if (db != null) {
084: actionResponse.setRenderParameter("db", db);
085: }
086: if (tbl != null) {
087: actionResponse.setRenderParameter("tbl", tbl);
088: }
089: if (viewTables != null) {
090: actionResponse.setRenderParameter("viewTables", viewTables);
091: }
092: if (rdbms != null) {
093: actionResponse.setRenderParameter("rdbms", rdbms);
094: }
095: }
096:
097: protected void doView(RenderRequest renderRequest,
098: RenderResponse renderResponse) throws IOException,
099: PortletException {
100: if (WindowState.MINIMIZED
101: .equals(renderRequest.getWindowState())) {
102: return;
103: }
104: String action = renderRequest.getParameter("action");
105: String db = renderRequest.getParameter("db");
106: String tbl = renderRequest.getParameter("tbl");
107: String viewTables = renderRequest.getParameter("viewTables");
108: String rdbms = renderRequest.getParameter("rdbms");
109: int rdbmsParam = (rdbms == null ? RDBMS_DERBY : Integer
110: .parseInt(rdbms));
111:
112: if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
113: if (rdbmsParam == RDBMS_DERBY) {
114: // Check is database & table is valid
115: if (LISTTBLS_ACTION.equals(action)
116: || VIEWTBLCONTENTS_ACTION.equals(action)) {
117: if (!helper.isDBValid(DerbyConnectionUtil
118: .getDerbyHome(), db)) {
119: // DB not valid
120: log.error("Database is not valid: " + db);
121: action = "";
122: }
123: }
124: if (VIEWTBLCONTENTS_ACTION.equals(action)) {
125: if (!helper.isTblValid(db, tbl)) {
126: // Table not valid
127: log.error("Table is not valid: " + tbl);
128: action = "";
129: }
130: }
131: }
132:
133: renderRequest.setAttribute("rdbms", rdbms);
134: if (LISTTBLS_ACTION.equals(action)) {
135: renderRequest.setAttribute("db", db);
136: renderRequest.setAttribute("viewTables", viewTables);
137: renderRequest.setAttribute("ds", DerbyConnectionUtil
138: .getDataSource(db));
139: listTablesView.include(renderRequest, renderResponse);
140: } else if (VIEWTBLCONTENTS_ACTION.equals(action)) {
141: renderRequest.setAttribute("db", db);
142: renderRequest.setAttribute("tbl", tbl);
143: renderRequest.setAttribute("viewTables", viewTables);
144: renderRequest.setAttribute("ds", DerbyConnectionUtil
145: .getDataSource(db));
146: viewTableContentsView.include(renderRequest,
147: renderResponse);
148: } else {
149: renderRequest.setAttribute("databases", helper
150: .getDerbyDatabases(DerbyConnectionUtil
151: .getDerbyHome()));
152: listDatabasesView
153: .include(renderRequest, renderResponse);
154: }
155: } else {
156: maximizedView.include(renderRequest, renderResponse);
157: }
158: }
159:
160: protected void doHelp(RenderRequest renderRequest,
161: RenderResponse renderResponse) throws PortletException,
162: IOException {
163: helpView.include(renderRequest, renderResponse);
164: }
165:
166: public void init(PortletConfig portletConfig)
167: throws PortletException {
168: super .init(portletConfig);
169: maximizedView = portletConfig.getPortletContext()
170: .getRequestDispatcher(MAXIMIZEDVIEW_JSP);
171: helpView = portletConfig.getPortletContext()
172: .getRequestDispatcher(HELPVIEW_JSP);
173: listDatabasesView = portletConfig.getPortletContext()
174: .getRequestDispatcher(LISTDATABASES_JSP);
175: listTablesView = portletConfig.getPortletContext()
176: .getRequestDispatcher(LISTTABLES_JSP);
177: viewTableContentsView = portletConfig.getPortletContext()
178: .getRequestDispatcher(VIEWTABLECONTENTS_JSP);
179: }
180:
181: public void destroy() {
182: maximizedView = null;
183: helpView = null;
184: listDatabasesView = null;
185: listTablesView = null;
186: viewTableContentsView = null;
187: super.destroy();
188: }
189:
190: }
|