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