KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import javax.portlet.ActionRequest;
23 import javax.portlet.ActionResponse;
24 import javax.portlet.PortletConfig;
25 import javax.portlet.PortletException;
26 import javax.portlet.PortletRequestDispatcher;
27 import javax.portlet.RenderRequest;
28 import javax.portlet.RenderResponse;
29 import javax.portlet.WindowState;
30
31 import org.apache.geronimo.console.BasePortlet;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 public class DBViewerPortlet extends BasePortlet {
36
37     private final static Log log = LogFactory.getLog(DBViewerPortlet.class);
38
39     private static final int RDBMS_DERBY = 1;
40
41     private static final int RDBMS_MSSQL = 2;
42
43     private static final String JavaDoc MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/dbViewerMaximized.jsp";
44
45     private static final String JavaDoc HELPVIEW_JSP = "/WEB-INF/view/internaldb/dbViewerHelp.jsp";
46
47     private static final String JavaDoc LISTDATABASES_JSP = "/WEB-INF/view/internaldb/listDatabases.jsp";
48
49     private static final String JavaDoc LISTTABLES_JSP = "/WEB-INF/view/internaldb/listTables.jsp";
50
51     private static final String JavaDoc VIEWTABLECONTENTS_JSP = "/WEB-INF/view/internaldb/viewTableContents.jsp";
52
53     private static final String JavaDoc LISTDB_ACTION = "listDatabases";
54
55     private static final String JavaDoc LISTTBLS_ACTION = "listTables";
56
57     private static final String JavaDoc VIEWTBLCONTENTS_ACTION = "viewTableContents";
58
59     private static final String JavaDoc DERBY_HOME = System.getProperty("derby.system.home");
60
61     private static DBViewerHelper helper = new DBViewerHelper();
62
63     private PortletRequestDispatcher maximizedView;
64
65     private PortletRequestDispatcher helpView;
66
67     private PortletRequestDispatcher listDatabasesView;
68
69     private PortletRequestDispatcher listTablesView;
70
71     private PortletRequestDispatcher viewTableContentsView;
72
73     public void processAction(ActionRequest actionRequest,
74                               ActionResponse actionResponse) throws PortletException, IOException JavaDoc {
75         // getting parameters here because it fails on doView()
76
String JavaDoc action = actionRequest.getParameter("action");
77         String JavaDoc db = actionRequest.getParameter("db");
78         String JavaDoc tbl = actionRequest.getParameter("tbl");
79         String JavaDoc viewTables = actionRequest.getParameter("viewTables");
80         String JavaDoc rdbms = actionRequest.getParameter("rdbms");
81         // pass them to the render request
82
if (action != null) {
83             actionResponse.setRenderParameter("action", action);
84         }
85         if (db != null) {
86             actionResponse.setRenderParameter("db", db);
87         }
88         if (tbl != null) {
89             actionResponse.setRenderParameter("tbl", tbl);
90         }
91         if (viewTables != null) {
92             actionResponse.setRenderParameter("viewTables", viewTables);
93         }
94         if (rdbms != null) {
95             actionResponse.setRenderParameter("rdbms", rdbms);
96         }
97     }
98
99     protected void doView(RenderRequest renderRequest,
100                           RenderResponse renderResponse) throws IOException JavaDoc, PortletException {
101         if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) {
102             return;
103         }
104         String JavaDoc action = renderRequest.getParameter("action");
105         String JavaDoc db = renderRequest.getParameter("db");
106         String JavaDoc tbl = renderRequest.getParameter("tbl");
107         String JavaDoc viewTables = renderRequest.getParameter("viewTables");
108         String JavaDoc rdbms = renderRequest.getParameter("rdbms");
109         int rdbmsParam = (rdbms == null ? RDBMS_DERBY : Integer.parseInt(rdbms));
110
111         if (WindowState.NORMAL.equals(renderRequest.getWindowState())) {
112             if (rdbmsParam == RDBMS_DERBY) {
113                 // Check is database & table is valid
114
if (LISTTBLS_ACTION.equals(action)
115                         || VIEWTBLCONTENTS_ACTION.equals(action)) {
116                     if (!helper.isDBValid(DERBY_HOME, db)) {
117                         // DB not valid
118
log.error("Database is not valid: " + db);
119                         action = "";
120                     }
121                 }
122                 if (VIEWTBLCONTENTS_ACTION.equals(action)) {
123                     if (!helper.isTblValid(db, tbl)) {
124                         // Table not valid
125
log.error("Table is not valid: " + tbl);
126                         action = "";
127                     }
128                 }
129             }
130
131             renderRequest.setAttribute("rdbms", rdbms);
132             if (LISTTBLS_ACTION.equals(action)) {
133                 renderRequest.setAttribute("db", db);
134                 renderRequest.setAttribute("viewTables", viewTables);
135                 renderRequest.setAttribute("ds", DerbyConnectionUtil
136                         .getDataSource(db));
137                 listTablesView.include(renderRequest, renderResponse);
138             } else if (VIEWTBLCONTENTS_ACTION.equals(action)) {
139                 renderRequest.setAttribute("db", db);
140                 renderRequest.setAttribute("tbl", tbl);
141                 renderRequest.setAttribute("viewTables", viewTables);
142                 renderRequest.setAttribute("ds", DerbyConnectionUtil
143                         .getDataSource(db));
144                 viewTableContentsView.include(renderRequest, renderResponse);
145             } else {
146                 renderRequest.setAttribute("databases", helper
147                         .getDerbyDatabases(DERBY_HOME));
148                 listDatabasesView.include(renderRequest, renderResponse);
149             }
150         } else {
151             maximizedView.include(renderRequest, renderResponse);
152         }
153     }
154
155     protected void doHelp(RenderRequest renderRequest,
156                           RenderResponse renderResponse) throws PortletException, IOException JavaDoc {
157         helpView.include(renderRequest, renderResponse);
158     }
159
160     public void init(PortletConfig portletConfig) throws PortletException {
161         super.init(portletConfig);
162         maximizedView = portletConfig.getPortletContext().getRequestDispatcher(
163                 MAXIMIZEDVIEW_JSP);
164         helpView = portletConfig.getPortletContext().getRequestDispatcher(
165                 HELPVIEW_JSP);
166         listDatabasesView = portletConfig.getPortletContext()
167                 .getRequestDispatcher(LISTDATABASES_JSP);
168         listTablesView = portletConfig.getPortletContext()
169                 .getRequestDispatcher(LISTTABLES_JSP);
170         viewTableContentsView = portletConfig.getPortletContext()
171                 .getRequestDispatcher(VIEWTABLECONTENTS_JSP);
172     }
173
174     public void destroy() {
175         maximizedView = null;
176         helpView = null;
177         listDatabasesView = null;
178         listTablesView = null;
179         viewTableContentsView = null;
180         super.destroy();
181     }
182
183 }
Popular Tags