1 17 18 package org.apache.geronimo.console.internaldb; 19 20 import java.io.IOException ; 21 import java.util.Collection ; 22 23 import javax.portlet.ActionRequest; 24 import javax.portlet.ActionResponse; 25 import javax.portlet.PortletConfig; 26 import javax.portlet.PortletException; 27 import javax.portlet.PortletRequestDispatcher; 28 import javax.portlet.RenderRequest; 29 import javax.portlet.RenderResponse; 30 import javax.portlet.WindowState; 31 32 import org.apache.geronimo.console.BasePortlet; 33 34 public class RunSQLPortlet extends BasePortlet { 35 36 private static final String NORMALVIEW_JSP = "/WEB-INF/view/internaldb/runSQLNormal.jsp"; 37 38 private static final String MAXIMIZEDVIEW_JSP = "/WEB-INF/view/internaldb/runSQLMaximized.jsp"; 39 40 private static final String HELPVIEW_JSP = "/WEB-INF/view/internaldb/runSQLHelp.jsp"; 41 42 private static final String CREATEDB_ACTION = "Create"; 43 44 private static final String DELETEDB_ACTION = "Delete"; 45 46 private static final String RUNSQL_ACTION = "Run SQL"; 47 48 private static final String BACKUPDB_ACTION = "Backup"; 49 50 private static final String RESTOREDB_ACTION = "Restore"; 51 52 private static RunSQLHelper sqlHelper = new RunSQLHelper(); 53 54 private static DBViewerHelper dbHelper = new DBViewerHelper(); 55 56 private static String derbyHome = System.getProperty("derby.system.home"); 57 58 private PortletRequestDispatcher normalView; 59 60 private PortletRequestDispatcher maximizedView; 61 62 private PortletRequestDispatcher helpView; 63 64 private Collection databases; 65 66 private String action; 67 68 private String createDB; 69 70 private String deleteDB; 71 72 private String useDB; 73 74 private String backupDB; 75 76 private String restoreDB; 77 78 private String sqlStmts; 79 80 private String actionResult; 81 82 public void processAction(ActionRequest actionRequest, 83 ActionResponse actionResponse) throws PortletException, IOException { 84 action = actionRequest.getParameter("action"); 86 createDB = actionRequest.getParameter("createDB"); 87 deleteDB = actionRequest.getParameter("deleteDB"); 88 useDB = actionRequest.getParameter("useDB"); 89 backupDB = actionRequest.getParameter("backupDB"); 90 restoreDB = actionRequest.getParameter("restoreDB"); 91 sqlStmts = actionRequest.getParameter("sqlStmts"); 92 actionResult = ""; 93 if (CREATEDB_ACTION.equals(action)) { 94 actionResult = sqlHelper.createDB(createDB); 95 } else if (DELETEDB_ACTION.equals(action)) { 96 actionResult = sqlHelper.deleteDB(derbyHome, deleteDB); 97 } else if (RUNSQL_ACTION.equals(action)) { 98 actionResult = sqlHelper.runSQL(useDB, sqlStmts); 99 } else if (BACKUPDB_ACTION.equals(action)) { 100 actionResult = sqlHelper.backupDB(derbyHome, backupDB); 101 } else if (RESTOREDB_ACTION.equals(action)) { 102 actionResult = sqlHelper.restoreDB(derbyHome, restoreDB); 103 } 104 } 105 106 protected void doView(RenderRequest renderRequest, 107 RenderResponse renderResponse) throws IOException , PortletException { 108 if (WindowState.MINIMIZED.equals(renderRequest.getWindowState())) { 109 return; 110 } 111 112 String singleSelectStmt; 113 if (WindowState.NORMAL.equals(renderRequest.getWindowState())) { 114 databases = dbHelper.getDerbyDatabases(derbyHome); 115 renderRequest.setAttribute("databases", databases); 116 if (RUNSQL_ACTION.equals(action)) { 117 if ((sqlStmts != null) && (sqlStmts.trim().indexOf(';') == -1) 119 && sqlStmts.trim().toUpperCase().startsWith("SELECT") 120 && RunSQLHelper.SQL_SUCCESS_MSG.equals(actionResult)) { 121 singleSelectStmt = sqlStmts.trim(); 122 actionResult = ""; 124 } else { 125 singleSelectStmt = ""; 126 } 127 renderRequest.setAttribute("useDB", useDB); 128 renderRequest 129 .setAttribute("singleSelectStmt", singleSelectStmt); 130 renderRequest.setAttribute("ds", DerbyConnectionUtil 131 .getDataSource(useDB)); 132 } 133 if ((action != null) && (action.trim().length() > 0)) { 134 renderRequest.setAttribute("actionResult", actionResult); 135 action = null; 139 } 140 normalView.include(renderRequest, renderResponse); 141 } else { 142 maximizedView.include(renderRequest, renderResponse); 143 } 144 } 145 146 protected void doHelp(RenderRequest renderRequest, 147 RenderResponse renderResponse) throws PortletException, IOException { 148 helpView.include(renderRequest, renderResponse); 149 } 150 151 public void init(PortletConfig portletConfig) throws PortletException { 152 super.init(portletConfig); 153 normalView = portletConfig.getPortletContext().getRequestDispatcher( 154 NORMALVIEW_JSP); 155 maximizedView = portletConfig.getPortletContext().getRequestDispatcher( 156 MAXIMIZEDVIEW_JSP); 157 helpView = portletConfig.getPortletContext().getRequestDispatcher( 158 HELPVIEW_JSP); 159 databases = dbHelper.getDerbyDatabases(derbyHome); 160 } 161 162 public void destroy() { 163 normalView = null; 164 maximizedView = null; 165 helpView = null; 166 super.destroy(); 167 } 168 169 } 170 | Popular Tags |