001: //$Header: $
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014:
015: This library is distributed in the hope that it will be useful,
016: but WITHOUT ANY WARRANTY; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: Lesser General Public License for more details.
019:
020: You should have received a copy of the GNU Lesser General Public
021: License along with this library; if not, write to the Free Software
022: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023:
024: Contact:
025:
026: Andreas Poth
027: lat/lon GmbH
028: Aennchenstr. 19
029: 53177 Bonn
030: Germany
031: E-Mail: poth@lat-lon.de
032:
033: Prof. Dr. Klaus Greve
034: Department of Geography
035: University of Bonn
036: Meckenheimer Allee 166
037: 53115 Bonn
038: Germany
039: E-Mail: greve@giub.uni-bonn.de
040:
041: ---------------------------------------------------------------------------*/
042:
043: package org.deegree.portal.portlet.modules.wfs.actions.portlets;
044:
045: import java.sql.Connection;
046: import java.sql.Driver;
047: import java.sql.DriverManager;
048: import java.sql.ResultSet;
049: import java.sql.SQLException;
050: import java.sql.Statement;
051: import java.util.ArrayList;
052: import java.util.List;
053:
054: import javax.servlet.ServletContext;
055: import javax.servlet.http.HttpServletRequest;
056:
057: import org.apache.jetspeed.portal.Portlet;
058: import org.apache.turbine.util.RunData;
059: import org.deegree.framework.log.ILogger;
060: import org.deegree.framework.log.LoggerFactory;
061: import org.deegree.framework.util.StringTools;
062: import org.deegree.portal.PortalException;
063: import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform;
064:
065: /**
066: * Removes a number of Features (listed by their ID).
067: *
068: * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
069: * @author last edited by: $Author:$
070: *
071: * @version $Revision:$, $Date:$
072: */
073: public class RemoveAnnotationPerform extends IGeoPortalPortletPerform {
074:
075: private static final ILogger LOG = LoggerFactory
076: .getLogger(RemoveAnnotationPerform.class);
077:
078: /**
079: *
080: * Creates an instance of a RemoveAnnotationPerform.
081: *
082: * @param request
083: * @param portlet
084: * @param servletContext
085: */
086: public RemoveAnnotationPerform(HttpServletRequest request,
087: Portlet portlet, ServletContext servletContext) {
088: super (request, portlet, servletContext);
089: }
090:
091: /**
092: * Builds up the portlet.
093: *
094: * @param data
095: * @throws PortalException
096: */
097: public void buildNormalContext(RunData data) throws PortalException {
098: super .buildNormalContext();
099:
100: if (getInitParam("driver") != null) {
101:
102: LOG
103: .logDebug("Build dataset table for annotations objects.");
104:
105: Connection con = createConnection();
106: String sql = getInitParam("SQLDisplayStatement");
107: LOG.logDebug("Clean DB Object SQL: " + sql);
108: sql = createSelectionStatement(data, sql);
109:
110: List<List<Object>> result;
111:
112: try {
113: result = createTable(con, sql);
114: LOG
115: .logDebug("Built dataset table for annotations objects: "
116: + result);
117:
118: con.close();
119: } catch (SQLException e) {
120: e.printStackTrace();
121: throw new PortalException(e.getMessage());
122: }
123: request.setAttribute("DB_OBJECTS", result);
124: }
125:
126: request.setAttribute("TITLE", getInitParam("title"));
127:
128: }
129:
130: /**
131: * Creates the SQL select statement. The sql script is taken from the initial parameter
132: * 'SQLDisplayStatement'. Username is automotically inserted, if there is a '$USERNAME'
133: * placeholder.
134: *
135: * @param data
136: * the RunData
137: * @param sql
138: * the original sql script
139: * @return the changed sql script
140: */
141: private String createSelectionStatement(RunData data, String sql) {
142: String username = data.getUser().getUserName();
143: sql = sql.replaceAll("'", "\"");
144: sql = sql.replace("$USERNAME", "'" + username + "'");
145: LOG.logDebug("Populated DB Object SQL : " + sql);
146: return sql;
147: }
148:
149: /**
150: * Creates a pseudo-table of results. The results are held in a list of lists. The first list
151: * has the strings describing the column names (or labels, if the use of 'as' - like in selece
152: * id as 'Identifier' has been made.
153: *
154: * @param con
155: * the connection
156: * @param sql
157: * the sql statement
158: * @return a List containing Lists of results
159: * @throws SQLException
160: */
161: private List<List<Object>> createTable(Connection con, String sql)
162: throws SQLException {
163:
164: Statement stm = con.createStatement();
165: stm.execute(sql);
166: ResultSet rSet = stm.getResultSet();
167:
168: List<List<Object>> rows = new ArrayList<List<Object>>();
169:
170: int colCount = rSet.getMetaData().getColumnCount();
171: LOG.logDebug("Creating dataset table for annotations objects: "
172: + colCount);
173:
174: while (rSet.next()) {
175: List<Object> cols = new ArrayList<Object>();
176:
177: for (int i = 0; i < colCount; i++) {
178: cols.add(rSet.getObject(i + 1));
179: }
180: rows.add(cols);
181: }
182:
183: if (rows.size() > 0) {
184: // if anything found, put headers in the list
185: List<Object> cols = new ArrayList<Object>(colCount);
186: for (int i = 0; i < colCount; i++) {
187: cols.add(rSet.getMetaData().getColumnLabel(i + 1));
188: }
189: rows.add(0, cols);
190: }
191: stm.close();
192: return rows;
193: }
194:
195: /**
196: * Executes a delete statement.
197: *
198: * @param data
199: * the RunData
200: * @throws PortalException
201: * if something evil happened
202: */
203: public void doDeletetransaction(RunData data)
204: throws PortalException {
205:
206: if (getInitParam("driver") != null) {
207: // let us hope and say, there's a driver
208: Connection con = createConnection();
209: try {
210: String s = parameter.get("OBJECTID");
211: String[] featureIds = StringTools
212: .toArray(s, ",;", true);
213: if (featureIds == null) {
214: LOG
215: .logDebug("No 'objectId' parameter in the request. Skipping...");
216: return;
217: }
218: for (String id : featureIds) {
219: String sql = createDeleteSQL(data, id);
220: System.out.println(sql);
221: deleteObject(con, sql);
222: }
223:
224: } catch (SQLException e) {
225: e.printStackTrace();
226: throw new PortalException(e.getMessage());
227: } finally {
228: try {
229: con.close();
230: } catch (Exception e) {
231: e.printStackTrace();
232: }
233: }
234: }
235: }
236:
237: /**
238: * Creates a delete statement based on the initial parameter 'SQLDeleteStatement' on the RunData
239: * containing the user name and on the id of the data object to be deleted.
240: *
241: * @param data
242: * the RunData object
243: * @param id
244: * the id of the dataset
245: * @return a sql with proper user name, and data set id
246: */
247: private String createDeleteSQL(RunData data, String id) {
248:
249: String sql = getInitParam("SQLDeleteStatement");
250: if (data.getUser() == null) {
251: throw new IllegalArgumentException(
252: "RunData object has null user.");
253: }
254:
255: String username = data.getUser().getUserName();
256: LOG.logDebug("Preparing deletion of object id '" + id
257: + "' for user: '" + username + "'");
258:
259: sql = sql.replace("$USERNAME", "'" + username + "'");
260: sql = sql.replace("$OBJECT_ID", "'" + id + "'");
261:
262: LOG.logDebug("Create SQL for deleting: " + sql);
263:
264: return sql;
265: }
266:
267: /**
268: *
269: * @param con
270: * @param sql
271: * @throws SQLException
272: */
273: private void deleteObject(Connection con, String sql)
274: throws SQLException {
275: Statement stmt = con.createStatement();
276: stmt.execute(sql);
277: con.commit();
278: }
279:
280: /**
281: *
282: * @return the Connection
283: * @throws PortalException
284: */
285: private Connection createConnection() throws PortalException {
286:
287: Connection con = null;
288:
289: String url = getInitParam("url");
290: String driver = getInitParam("driver");
291: String user = getInitParam("user");
292: String password = getInitParam("password");
293:
294: con = initConnection(url, driver, user, password);
295:
296: return con;
297: }
298:
299: /**
300: *
301: * @param url
302: * @param driver
303: * @param user
304: * @param password
305: * @return the Connection
306: * @throws PortalException
307: */
308: private Connection initConnection(String url, String driver,
309: String user, String password) throws PortalException {
310: LOG.logDebug("connecting database for insert ... ");
311:
312: Connection con = null;
313:
314: try {
315: Driver drv = (Driver) Class.forName(driver).newInstance();
316: DriverManager.registerDriver(drv);
317: LOG.logDebug("initializing connection with " + drv + " "
318: + url + " " + user + " " + password);
319:
320: con = DriverManager.getConnection(url, user, password);
321: } catch (SQLException e) {
322: LOG.logError("could not establish database connection: "
323: + url, e);
324: throw new PortalException(
325: "could not establish database connection: " + url,
326: e);
327: } catch (Exception e) {
328: LOG.logError(
329: "could not initialize driver class: " + driver, e);
330: throw new PortalException(
331: "could not initialize driver class: " + driver, e);
332: }
333: return con;
334: }
335: }
|