001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/portlet/modules/wfs/actions/portlets/AnnotationPortletPerform.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.portal.portlet.modules.wfs.actions.portlets;
045:
046: import java.net.URLDecoder;
047: import java.sql.Connection;
048: import java.sql.Date;
049: import java.sql.Driver;
050: import java.sql.DriverManager;
051: import java.sql.PreparedStatement;
052: import java.sql.SQLException;
053:
054: import javax.servlet.ServletContext;
055: import javax.servlet.http.HttpServletRequest;
056:
057: import org.apache.jetspeed.portal.Portlet;
058: import org.deegree.datatypes.Types;
059: import org.deegree.enterprise.WebUtils;
060: import org.deegree.framework.log.ILogger;
061: import org.deegree.framework.log.LoggerFactory;
062: import org.deegree.framework.util.StringTools;
063: import org.deegree.portal.PortalException;
064:
065: /**
066: *
067: *
068: *
069: * @version $Revision: 9346 $
070: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
071: * @author last edited by: $Author: apoth $
072: *
073: * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
074: *
075: * @since 2.0
076: */
077: public class AnnotationPortletPerform extends WFSClientPortletPerform {
078:
079: private static final ILogger LOG = LoggerFactory
080: .getLogger(AnnotationPortletPerform.class);
081:
082: /**
083: *
084: * @param request
085: * @param portlet
086: * @param servletContext
087: */
088: public AnnotationPortletPerform(HttpServletRequest request,
089: Portlet portlet, ServletContext servletContext) {
090: super (request, portlet, servletContext);
091: }
092:
093: /**
094: * performs a transaction against a WFS-T or a database. The backend type to be used by a
095: * transaction depends on a portlets initParameters.
096: *
097: * @param user
098: * @throws PortalException
099: */
100: public void doTransaction(String user) throws PortalException {
101: if (getInitParam("driver") != null) {
102: doDatabaseTransaction(user);
103: } else {
104: // handle transaction against a WFS
105: super .doTransaction();
106: }
107: }
108:
109: /**
110: * performs a direct transaction into a database
111: *
112: * @param data
113: */
114: private void doDatabaseTransaction(String user)
115: throws PortalException {
116:
117: Connection con = null;
118: LOG.logDebug("connecting database for insert ... ");
119: try {
120: Driver drv = (Driver) Class.forName(getInitParam("driver"))
121: .newInstance();
122: DriverManager.registerDriver(drv);
123:
124: con = DriverManager.getConnection(getInitParam("url"),
125: getInitParam("user"), getInitParam("password"));
126: } catch (SQLException e) {
127: LOG.logError("could not establish database connection: "
128: + getInitParam("url"), e);
129: throw new PortalException(
130: "could not establish database connection: "
131: + getInitParam("url"), e);
132: } catch (Exception e) {
133: LOG.logError("could not initialize driver class: "
134: + getInitParam("driver"), e);
135: throw new PortalException(
136: "could not initialize driver class: "
137: + getInitParam("driver"), e);
138: }
139:
140: PreparedStatement stmt = null;
141: try {
142: if ("INSERT".equals(parameter.get("OPERATION"))) {
143: String sql = createGeometryFragment(getInitParam("SQLInsertTemplate"));
144: stmt = con.prepareStatement(sql);
145: LOG.logDebug("perform insert " + sql);
146: } else {
147: throw new PortalException("not a supported Operation: "
148: + parameter.get("OPERATION"));
149: }
150: stmt.setDate(1, new Date(System.currentTimeMillis()));
151:
152: String charset = WebUtils
153: .readCharsetFromContentType(request);
154:
155: String title = (String) parameter.get("TITLE");
156: if (title == null) {
157: stmt.setNull(2, Types.VARCHAR);
158: } else {
159: stmt.setString(2, URLDecoder.decode(title, charset));
160: }
161: String annotation = (String) parameter.get("ANNOTATION");
162: if (annotation == null) {
163: stmt.setNull(3, Types.VARCHAR);
164: } else {
165: stmt.setString(3, URLDecoder
166: .decode(annotation, charset));
167: }
168: String category = (String) parameter.get("CATEGORY");
169: if (category == null) {
170: stmt.setNull(4, Types.NUMERIC);
171: } else {
172: stmt.setInt(4, -1);
173: }
174:
175: stmt.setString(5, user);
176: // annotation location
177: stmt.execute();
178: } catch (SQLException e) {
179: try {
180: stmt.close();
181: } catch (Exception e1) {
182: }
183: try {
184: con.close();
185: } catch (Exception e1) {
186: }
187: LOG.logError("could not perform insert operation ", e);
188: throw new PortalException(
189: "could not perform insert operation ", e);
190: } catch (Exception e) {
191: e.printStackTrace();
192: LOG.logError("could not tranform geometry", e);
193: throw new PortalException("could not tranform geometry", e);
194: }
195:
196: }
197:
198: /**
199: *
200: * @return
201: */
202: private String createGeometryFragment(String sql) {
203: sql = StringTools.replace(sql, "$SRID", getInitParam("srid"),
204: false);
205: sql = StringTools.replace(sql, "$X", (String) parameter
206: .get("X"), false);
207: sql = StringTools.replace(sql, "$Y", (String) parameter
208: .get("Y"), false);
209:
210: return sql;
211: }
212: }
|