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