001    //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $
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.portal.cataloguemanager.control;
037    
038    import java.util.ArrayList;
039    import java.util.Enumeration;
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.concurrent.Callable;
044    
045    import javax.servlet.http.HttpServletRequest;
046    
047    import org.apache.commons.httpclient.HttpMethod;
048    import org.deegree.framework.concurrent.ExecutionFinishedEvent;
049    import org.deegree.framework.concurrent.Executor;
050    import org.deegree.framework.log.ILogger;
051    import org.deegree.framework.log.LoggerFactory;
052    import org.deegree.framework.util.HttpUtils;
053    import org.deegree.framework.util.Pair;
054    import org.deegree.framework.xml.NamespaceContext;
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.framework.xml.XMLTools;
057    import org.deegree.ogcbase.CommonNamespaces;
058    import org.deegree.ogcwebservices.csw.discovery.GetRecords;
059    import org.deegree.ogcwebservices.csw.discovery.XMLFactory;
060    import org.w3c.dom.Element;
061    import org.w3c.dom.Node;
062    
063    /**
064     * TODO add class documentation here
065     * 
066     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
067     * @author last edited by: $Author: admin $
068     * 
069     * @version $Revision: $, $Date: $
070     */
071    abstract class AbstractSearchListener extends AbstractMetadataListener {
072    
073        private static final ILogger LOG = LoggerFactory.getLogger( AbstractSearchListener.class );
074    
075        protected CatalogueManagerConfiguration config;
076    
077        protected List<SearchResultInfo> searchResultInfos = new ArrayList<SearchResultInfo>();
078    
079        protected static NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
080    
081        /**
082         * 
083         * @param getRecords
084         * @return
085         * @throws Throwable
086         */
087        protected List<Pair<String, XMLFragment>> performQuery( GetRecords getRecords )
088                                throws Throwable {
089            XMLFragment gr = XMLFactory.exportWithVersion( getRecords );
090            LOG.logDebug( "GetRecords request: ", gr.getAsPrettyString() );
091            List<String> csw = config.getSearchableCSW();
092            List<Callable<XMLFragment>> callables = new ArrayList<Callable<XMLFragment>>();
093            callables.add( new GetRecordsCallable( config.getCatalogueURL(), gr ) );
094            for ( String address : csw ) {
095                callables.add( new GetRecordsCallable( address, gr ) );
096            }
097    
098            Executor exe = Executor.getInstance();
099            List<ExecutionFinishedEvent<XMLFragment>> events = exe.performSynchronously( callables );
100    
101            List<Pair<String, XMLFragment>> result = new ArrayList<Pair<String, XMLFragment>>();
102    
103            String xpath1 = "csw202:SearchResults/@numberOfRecordsReturned";
104            String xpath2 = "csw202:SearchResults/@numberOfRecordsMatched";
105            for ( ExecutionFinishedEvent<XMLFragment> executionFinishedEvent : events ) {
106                SearchResultInfo sri = new SearchResultInfo();
107                XMLFragment xml = executionFinishedEvent.getResult();
108                String cswAddress = ( (GetRecordsCallable) executionFinishedEvent.getTask() ).getCsw();
109                // store meta information for search result
110                sri.setNumberOfRecordsReturned( XMLTools.getNodeAsInt( xml.getRootElement(), xpath1, nsc, 0 ) );
111                sri.setNumberOfRecordsMatched( XMLTools.getNodeAsInt( xml.getRootElement(), xpath2, nsc, 0 ) );
112                sri.setCswURL( cswAddress );
113                // TODO
114                // set catalogue name to search result info
115                searchResultInfos.add( sri );
116                result.add( new Pair<String, XMLFragment>( cswAddress, xml ) );
117                if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
118                    LOG.logDebug( "queried catalogue: ", cswAddress );
119                    if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
120                        LOG.logDebug( xml.getAsPrettyString() );
121                    }
122                }
123            }
124            return result;
125        }
126    
127        /**
128         * @param result
129         *            first parameter of pair contains CSW URL second one contains GetRecords result
130         * @return first string of pair contains CSW URL second one contains formatted GetRecords result
131         * @throws Exception
132         */
133        protected List<SearchResultBean> formatResult( List<Pair<String, XMLFragment>> result )
134                                throws Exception {
135            List<SearchResultBean> formattedResult = new ArrayList<SearchResultBean>();
136    
137            for ( Pair<String, XMLFragment> pair : result ) {
138                List<Node> nodes = XMLTools.getNodes( pair.second.getRootElement(), "./csw202:SearchResults/*", nsc );
139                for ( Node node : nodes ) {
140                    Element root = (Element) node;
141                    try {
142                        SearchResultBean srb = new SearchResultBean();
143                        srb.setCsw( pair.first );
144                        String s = XMLTools.getNodeAsString( root, config.getXPath( "abstract_" ), nsc, "-" );
145                        srb.setAbstr( s );
146    
147                        s = XMLTools.getNodeAsString( root, config.getXPath( "identifier" ), nsc, "" );
148                        if ( s == null ) {
149                            s = XMLTools.getNodeAsString( root, config.getXPath( "resourceIdentifier1" ), nsc, null );
150                        }
151                        if ( s == null ) {
152                            s = XMLTools.getNodeAsString( root, config.getXPath( "resourceIdentifier2" ), nsc, null );
153                        }
154                        if ( s == null ) {
155                            s = XMLTools.getNodeAsString( root, config.getXPath( "srvResourceIdentifier1" ), nsc, null );
156                        }
157                        if ( s == null ) {
158                            s = XMLTools.getNodeAsString( root, config.getXPath( "srvResourceIdentifier2" ), nsc, null );
159                        }
160                        
161                        srb.setId( s );
162                        s = XMLTools.getNodeAsString( root, config.getXPath( "serviceTitle" ), nsc, null );
163                        if ( s == null ) {
164                            s = XMLTools.getRequiredNodeAsString( root, config.getXPath( "datasetTitle" ), nsc );
165                        }
166                        srb.setTitle( s );
167                        s = XMLTools.getNodeAsString( root, config.getXPath( "modified" ), nsc, "-" );
168                        srb.setModified( s );
169                        s = XMLTools.getRequiredNodeAsString( root, config.getXPath( "hlevel" ), nsc );
170                        srb.setHierarchyLevel( s );
171                        s = XMLTools.getNodeAsString( root, config.getXPath( "overview" ), nsc, null );
172                        srb.setOverview( s );
173                        String west = XMLTools.getNodeAsString( root, config.getXPath( "srvWest" ), nsc, null );
174                        if ( west == null ) {
175                            west = XMLTools.getRequiredNodeAsString( root, config.getXPath( "west" ), nsc );
176                        }
177                        String east = XMLTools.getNodeAsString( root, config.getXPath( "srvEast" ), nsc, null );
178                        if ( east == null ) {
179                            east = XMLTools.getRequiredNodeAsString( root, config.getXPath( "east" ), nsc );
180                        }
181                        String south = XMLTools.getNodeAsString( root, config.getXPath( "srvSouth" ), nsc, null );
182                        if ( south == null ) {
183                            south = XMLTools.getRequiredNodeAsString( root, config.getXPath( "south" ), nsc );
184                        }
185                        String north = XMLTools.getNodeAsString( root, config.getXPath( "srvNorth" ), nsc, null );
186                        if ( north == null ) {
187                            north = XMLTools.getRequiredNodeAsString( root, config.getXPath( "north" ), nsc );
188                        }
189                        srb.setBbox( west + ' ' + south + ' ' + east + ' ' + north );
190                        formattedResult.add( srb );
191                    } catch ( Exception e ) {
192                        LOG.logError( e );                    
193                    }
194                }
195            }
196            return formattedResult;
197        }
198    
199        // ////////////////////////////////////////////////////////////////////////////////////////////////////
200        // inner classes
201        // ////////////////////////////////////////////////////////////////////////////////////////////////////
202    
203        private class GetRecordsCallable implements Callable<XMLFragment> {
204    
205            private String csw;
206    
207            private XMLFragment getRecords;
208    
209            /**
210             * 
211             * @param csw
212             * @param getRecords
213             */
214            GetRecordsCallable( String csw, XMLFragment getRecords ) {
215                this.csw = csw;
216                this.getRecords = getRecords;
217            }
218    
219            /**
220             * @return the csw
221             */
222            public String getCsw() {
223                return csw;
224            }
225    
226            /*
227             * (non-Javadoc)
228             * 
229             * @see java.util.concurrent.Callable#call()
230             */
231            @SuppressWarnings("unchecked")
232            public XMLFragment call()
233                                    throws Exception {
234                LOG.logDebug( "connect to: ", csw );
235                Enumeration<String>  en =  ((HttpServletRequest)getRequest()).getHeaderNames();
236                Map<String, String> map = new HashMap<String, String>();
237                while ( en.hasMoreElements() ) {
238                    String name = (String) en.nextElement();
239                    map.put( name, ((HttpServletRequest)getRequest()).getHeader( name ) );
240                }
241                HttpMethod method = HttpUtils.performHttpPost( csw, getRecords, 60000, null, null, map );
242                XMLFragment xml = new XMLFragment();
243                xml.load( method.getResponseBodyAsStream(), csw );
244                return xml;
245            }
246    
247        }
248    
249    }