001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/csw/manager/Manager_2_0_0.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.ogcwebservices.csw.manager; 037 038 import java.io.IOException; 039 import java.io.StringReader; 040 import java.io.StringWriter; 041 import java.util.ArrayList; 042 import java.util.List; 043 044 import javax.xml.transform.TransformerException; 045 046 import org.deegree.framework.log.ILogger; 047 import org.deegree.framework.log.LoggerFactory; 048 import org.deegree.framework.util.StringTools; 049 import org.deegree.framework.xml.NamespaceContext; 050 import org.deegree.framework.xml.XMLFragment; 051 import org.deegree.framework.xml.XMLParsingException; 052 import org.deegree.framework.xml.XMLTools; 053 import org.deegree.ogcbase.CommonNamespaces; 054 import org.deegree.ogcwebservices.EchoRequest; 055 import org.deegree.ogcwebservices.OGCWebServiceException; 056 import org.deegree.ogcwebservices.wfs.operation.transaction.TransactionResponse; 057 import org.w3c.dom.Element; 058 import org.xml.sax.SAXException; 059 060 /** 061 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a> 062 * 063 * @author last edited by: $Author: mschneider $ 064 * 065 * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 066 */ 067 068 public class Manager_2_0_0 extends AbstractManager { 069 070 private static final ILogger LOG = LoggerFactory.getLogger( Manager_2_0_0.class ); 071 072 /* 073 * (non-Javadoc) 074 * 075 * @see org.deegree.ogcwebservices.csw.manager.Manager#harvestRecords(org.deegree.ogcwebservices.csw.manager.Harvest) 076 */ 077 public EchoRequest harvestRecords( Harvest request ) 078 throws OGCWebServiceException { 079 try { 080 HarvesterFactory hf = new HarvesterFactory( harvester ); 081 AbstractHarvester h = hf.findHarvester( request ); 082 h.addRequest( request ); 083 if ( !h.isRunning() ) { 084 h.startHarvesting(); 085 } 086 if ( request.getHarvestInterval() == null ) { 087 // h.removeRequest( request ); 088 } 089 } catch ( Exception e ) { 090 LOG.logError( "could not perform harvest operation", e ); 091 throw new OGCWebServiceException( getClass().getName(), "could not perform harvest operation" 092 + e.getMessage() ); 093 } 094 095 return new EchoRequest( request.getId(), null ); 096 } 097 098 /* 099 * (non-Javadoc) 100 * 101 * @see org.deegree.ogcwebservices.csw.manager.Manager#transaction(org.deegree.ogcwebservices.csw.manager.Transaction) 102 */ 103 public TransactionResult transaction( Transaction request ) 104 throws OGCWebServiceException { 105 XMLFragment wfsTransactionDocument = null; 106 try { 107 XMLFragment transactionDocument = new XMLFragment( XMLFactory.export( request ).getRootElement() ); 108 StringWriter sww = new StringWriter( 15000 ); 109 transactionDocument.write( sww ); 110 transactionDocument.load( new StringReader( sww.getBuffer().toString() ), XMLFragment.DEFAULT_URL ); 111 wfsTransactionDocument = IN_XSL.transform( transactionDocument ); 112 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 113 LOG.logDebug( "The (first) resulting wfs:Transaction document: \n " 114 + wfsTransactionDocument.getAsPrettyString() ); 115 } 116 } catch ( SAXException saxe ) { 117 String msg = org.deegree.i18n.Messages.get( "CSW_CREATE_TRANSACTION_ERROR", saxe.getMessage() ); 118 LOG.logError( msg, saxe ); 119 throw new OGCWebServiceException( msg ); 120 } catch ( IOException ioe ) { 121 String msg = org.deegree.i18n.Messages.get( "CSW_CREATE_TRANSACTION_ERROR", ioe.getMessage() ); 122 LOG.logError( msg, ioe ); 123 throw new OGCWebServiceException( msg ); 124 } catch ( TransformerException te ) { 125 String msg = org.deegree.i18n.Messages.get( "CSW_CREATE_TRANSACTION_ERROR", te.getMessage() ); 126 LOG.logError( msg, te ); 127 throw new OGCWebServiceException( msg ); 128 } catch ( XMLParsingException xmle ) { 129 String msg = org.deegree.i18n.Messages.get( "CSW_CREATE_TRANSACTION_ERROR", xmle.getMessage() ); 130 LOG.logError( msg, xmle ); 131 throw new OGCWebServiceException( msg ); 132 } 133 134 org.deegree.ogcwebservices.wfs.operation.transaction.Transaction wfstrans = null; 135 try { 136 LOG.logDebug( "Creating a wfs transaction from the document" ); 137 wfstrans = org.deegree.ogcwebservices.wfs.operation.transaction.Transaction.create( 138 request.getId(), 139 wfsTransactionDocument.getRootElement() ); 140 } catch ( OGCWebServiceException ogcwe ) { 141 LOG.logError( ogcwe.getMessage(), ogcwe ); 142 String msg = org.deegree.i18n.Messages.get( "CSW_CREATE_TRANSACTION_ERROR2", ogcwe.getMessage() ); 143 throw new OGCWebServiceException( msg ); 144 } 145 146 Object wfsResponse = null; 147 try { 148 LOG.logDebug( "Sending the wfs transaction to the wfservice." ); 149 wfsResponse = wfsService.doService( wfstrans ); 150 } catch ( OGCWebServiceException e ) { 151 String msg = org.deegree.i18n.Messages.get( "CSW_PERFORMING_TRANSACTION_ERROR", e.getMessage() ); 152 LOG.logError( msg, e ); 153 throw new OGCWebServiceException( msg ); 154 } 155 156 if ( !( wfsResponse instanceof org.deegree.ogcwebservices.wfs.operation.transaction.TransactionResponse ) ) { 157 String msg = org.deegree.i18n.Messages.get( "CSW_WRONG_TRANSACTION_RESULTTYPE", 158 wfsResponse.getClass().getName() ); 159 LOG.logError( msg ); 160 throw new OGCWebServiceException( msg ); 161 } 162 163 TransactionResponse transResp = (TransactionResponse) wfsResponse; 164 XMLFragment wfsTransRespDoc = null; 165 try { 166 LOG.logDebug( "Parsing the wfs response." ); 167 wfsTransRespDoc = org.deegree.ogcwebservices.wfs.XMLFactory.export( transResp ); 168 } catch ( IOException e ) { 169 String msg = "export of WFS Transaction response as XML failed: " + e.getMessage(); 170 LOG.logError( msg, e ); 171 throw new OGCWebServiceException( msg ); 172 } 173 174 // -------------------------------------------------------------- 175 // the following section will replace the feature ids returned by 176 // the WFS for Insert requests by the ID of the inserted metadata sets 177 List<String> ids = new ArrayList<String>(); 178 List<Operation> ops = request.getOperations(); 179 for ( int i = 0; i < ops.size(); i++ ) { 180 if ( ops.get( i ) instanceof Insert ) { 181 try { 182 ids = extractIdentifiers( ids, (Insert) ops.get( i ) ); 183 } catch ( Exception e ) { 184 LOG.logError( e.getMessage(), e ); 185 throw new OGCWebServiceException( getClass().getName(), e.getMessage() ); 186 } 187 } 188 } 189 try { 190 if ( ids.size() > 0 ) { 191 wfsTransRespDoc = replaceIds( wfsTransRespDoc, ids ); 192 } 193 } catch ( Exception e ) { 194 LOG.logError( e.getMessage(), e ); 195 throw new OGCWebServiceException( getClass().getName(), e.getMessage() ); 196 } 197 // --------------------------------------------------------------- 198 199 TransactionResultDocument cswTransactionDocument = null; 200 try { 201 XMLFragment tmp = OUT_XSL.transform( wfsTransRespDoc ); 202 cswTransactionDocument = new TransactionResultDocument(); 203 cswTransactionDocument.setRootElement( tmp.getRootElement() ); 204 } catch ( TransformerException e ) { 205 String msg = org.deegree.i18n.Messages.get( "CSW_TRANSACTION_RESULT_TRANS_ERR", e.getMessage() ); 206 LOG.logError( msg, e ); 207 throw new OGCWebServiceException( msg ); 208 } 209 TransactionResult result = null; 210 try { 211 result = cswTransactionDocument.parseTransactionResponse( request ); 212 } catch ( XMLParsingException e ) { 213 String msg = org.deegree.i18n.Messages.get( "CSW_TRANSACTION_RESULT_PARSE_ERR" ); 214 LOG.logError( msg, e ); 215 throw new OGCWebServiceException( msg ); 216 } 217 return result; 218 } 219 220 /** 221 * replaces the id values of WFS Insert result with corresponding metadata identifieres 222 * 223 * @param wfsTransRespDoc 224 * @param ids 225 * @return an xmlFragment with the gml:Feature ids replaced with the id' s given in the list 226 * @throws XMLParsingException 227 */ 228 private XMLFragment replaceIds( XMLFragment wfsTransRespDoc, List<String> ids ) 229 throws XMLParsingException { 230 231 List<Element> nodes = XMLTools.getRequiredElements( wfsTransRespDoc.getRootElement(), 232 "./wfs:InsertResults/wfs:Feature/ogc:FeatureId", 233 CommonNamespaces.getNamespaceContext() ); 234 for ( int i = 0; i < nodes.size(); i++ ) { 235 Element elem = nodes.get( i ); 236 elem.setAttribute( "fid", ids.get( i ) ); 237 } 238 239 return wfsTransRespDoc; 240 } 241 242 /** 243 * extracts all identifiers of the records to be inserted in correct order 244 * 245 * @param ids 246 * @param insert 247 * @return a list of identifiers which should be keys to xpaths which are defined in the 248 * messages.properties 249 * @throws XMLParsingException 250 */ 251 private List<String> extractIdentifiers( List<String> ids, Insert insert ) 252 throws XMLParsingException { 253 List<Element> records = insert.getRecords(); 254 255 NamespaceContext nsc = CommonNamespaces.getNamespaceContext(); 256 257 for ( int i = 0; i < records.size(); i++ ) { 258 String xpath = getIdentifierXPath( records.get( i ) ); 259 String fileIdentifier = XMLTools.getRequiredNodeAsString( records.get( i ), xpath, nsc ); 260 ids.add( fileIdentifier ); 261 } 262 return ids; 263 } 264 265 /** 266 * returns the XPath the metadata records identifier 267 * 268 * @param metaData 269 * @return the XPath the metadata records identifier 270 */ 271 private String getIdentifierXPath( Element metaData ) { 272 273 // default is iso 19115 274 String xpath = "iso19115:fileIdentifier/smXML:CharacterString"; 275 if ( metaData != null ) { 276 String nspace = metaData.getNamespaceURI(); 277 nspace = StringTools.replace( nspace, "http://", "", true ); 278 xpath = Messages.getString( "Identifier_" + nspace ); 279 } 280 return xpath; 281 } 282 283 }