001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/csw/manager/CSWProfileHarvester.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.ogcwebservices.csw.manager; 045 046 import java.io.IOException; 047 import java.net.MalformedURLException; 048 import java.net.URI; 049 import java.net.URL; 050 import java.util.Date; 051 import java.util.Iterator; 052 import java.util.List; 053 054 import org.deegree.framework.log.ILogger; 055 import org.deegree.framework.log.LoggerFactory; 056 import org.deegree.framework.util.FileUtils; 057 import org.deegree.framework.util.StringTools; 058 import org.deegree.framework.xml.XMLFragment; 059 import org.deegree.framework.xml.XMLParsingException; 060 import org.deegree.framework.xml.XMLTools; 061 import org.deegree.ogcwebservices.csw.manager.HarvestRepository.ResourceType; 062 import org.xml.sax.SAXException; 063 064 /** 065 * Harverster implementation for harvesting single metadata documents. 066 * 067 * 068 * @version $Revision: 9345 $ 069 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 070 * @author last edited by: $Author: apoth $ 071 * 072 * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $ 073 * 074 * @since 2.0 075 */ 076 public class CSWProfileHarvester extends AbstractHarvester { 077 078 private static final ILogger LOG = LoggerFactory.getLogger( CSWProfileHarvester.class ); 079 080 private static CSWProfileHarvester ch = null; 081 082 /** 083 * singelton 084 * 085 * @return 086 */ 087 public static CSWProfileHarvester getInstance() { 088 if ( ch == null ) { 089 ch = new CSWProfileHarvester(); 090 } 091 return ch; 092 } 093 094 @Override 095 public void run() { 096 LOG.logDebug( "starting harvest iteration for CSWProfileHarvester." ); 097 try { 098 HarvestRepository repository = HarvestRepository.getInstance(); 099 100 List<URI> sources = repository.getSources(); 101 for ( Iterator iter = sources.iterator(); iter.hasNext(); ) { 102 URI source = (URI) iter.next(); 103 try { 104 // determine if source shall be harvested 105 if ( shallHarvest( source, ResourceType.csw_profile ) ) { 106 HarvestProcessor processor = new HarvestProcessor( this, source ); 107 processor.start(); 108 } 109 } catch ( Exception e ) { 110 LOG.logError( "Exception harvesting service: " + source, e ); 111 informResponseHandlers( source, e ); 112 } 113 } 114 } catch ( Exception e ) { 115 LOG.logError( "generell Exception harvesting services", e ); 116 } 117 118 } 119 120 /** 121 * inner class for processing asynchronous harvesting of a csw:profile metadata document 122 * 123 * @version $Revision: 9345 $ 124 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 125 * @author last edited by: $Author: apoth $ 126 * 127 * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $ 128 * 129 * @since 2.0 130 */ 131 protected class HarvestProcessor extends AbstractHarvestProcessor { 132 133 HarvestProcessor( AbstractHarvester owner, URI source ) { 134 super( owner, source ); 135 } 136 137 @Override 138 public void run() { 139 try { 140 HarvestRepository repository = HarvestRepository.getInstance(); 141 XMLFragment metaData = accessMetadata( source ); 142 Date harvestingTimestamp = repository.getNextHarvestingTimestamp( source ); 143 String trans = null; 144 // ensure that a resource just will be harvested if something has changed 145 if ( shallHarvest( source, ResourceType.csw_profile ) ) { 146 if ( repository.getLastHarvestingTimestamp( source ) == null ) { 147 trans = createInsertRequest( metaData ); 148 } else { 149 trans = createUpdateRequest( getID( metaData ), getIdentifierXPath( metaData ), metaData ); 150 } 151 performTransaction( trans ); 152 153 long ts = repository.getHarvestInterval( source ); 154 if ( ts <= 0 ) { 155 // if the harvest interval is less or equal to 0 the source 156 // shall just be harvested for one time and it will be 157 // removed from harvest cache db 158 informResponseHandlers( source ); 159 repository.dropRequest( source ); 160 } else { 161 // update timestamps just if transaction has been performed 162 // successfully 163 writeLastHarvestingTimestamp( source, harvestingTimestamp ); 164 writeNextHarvestingTimestamp( source, harvestingTimestamp ); 165 informResponseHandlers( source ); 166 } 167 } 168 169 } catch ( Exception e ) { 170 e.printStackTrace(); 171 LOG.logError( "could not perform harvest operation for source: " + source, e ); 172 try { 173 owner.informResponseHandlers( source, e ); 174 } catch ( Exception ee ) { 175 ee.printStackTrace(); 176 } 177 } 178 } 179 180 /** 181 * returns the XPath the metadata records identifier 182 * 183 * @param metaData 184 * @return the XPath the metadata records identifier 185 */ 186 private String getIdentifierXPath( XMLFragment metaData ) { 187 String xpath = "iso19115:fileIdentifier/smXML:CharacterString"; 188 if ( metaData != null ) { 189 String nspace = metaData.getRootElement().getNamespaceURI(); 190 nspace = StringTools.replace( nspace, "http://", "", true ); 191 xpath = Messages.getString( "Identifier_" + nspace ); 192 } 193 return xpath; 194 } 195 196 /** 197 * returns the identifier of a metadata record to enable its update 198 * 199 * @param metaData 200 * @return the identifier of a metadata record to enable its update 201 * @throws XMLParsingException 202 */ 203 private String getID( XMLFragment metaData ) 204 throws XMLParsingException { 205 String xpath = getIdentifierXPath( metaData ); 206 String fileIdentifier = XMLTools.getRequiredNodeAsString( metaData.getRootElement(), xpath, nsc ); 207 return fileIdentifier; 208 } 209 210 @Override 211 protected String createConstraint( String identifier, String xPath ) 212 throws IOException { 213 214 // read template from file 215 // TODO 216 // read different templates depending on metadata format 217 URL url = CatalogueHarvester.class.getResource( "iso09_constraints_template.xml" ); 218 String constraints = FileUtils.readTextFile( url ).toString(); 219 220 constraints = StringTools.replace( constraints, "$identifier$", identifier, false ); 221 222 return StringTools.replace( constraints, "$xPath$", xPath, false ); 223 224 } 225 226 /** 227 * 228 * @param source 229 * @return 230 * @throws SAXException 231 * @throws IOException 232 * @throws MalformedURLException 233 */ 234 private XMLFragment accessMetadata( URI source ) 235 throws MalformedURLException, IOException, SAXException { 236 237 XMLFragment xml = new XMLFragment(); 238 xml.load( source.toURL() ); 239 return xml; 240 } 241 242 } 243 244 }