001    /*----------------------------------------------------------------------------
002     This file is part of deegree, http://deegree.org/
003     Copyright (C) 2001-2009 by:
004       Department of Geography, University of Bonn
005     and
006       lat/lon GmbH
007    
008     This library is free software; you can redistribute it and/or modify it under
009     the terms of the GNU Lesser General Public License as published by the Free
010     Software Foundation; either version 2.1 of the License, or (at your option)
011     any later version.
012     This library is distributed in the hope that it will be useful, but WITHOUT
013     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
015     details.
016     You should have received a copy of the GNU Lesser General Public License
017     along with this library; if not, write to the Free Software Foundation, Inc.,
018     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019    
020     Contact information:
021    
022     lat/lon GmbH
023     Aennchenstr. 19, 53177 Bonn
024     Germany
025     http://lat-lon.de/
026    
027     Department of Geography, University of Bonn
028     Prof. Dr. Klaus Greve
029     Postfach 1147, 53001 Bonn
030     Germany
031     http://www.geographie.uni-bonn.de/deegree/
032    
033     e-mail: info@deegree.org
034    ----------------------------------------------------------------------------*/
035    
036    package org.deegree.ogcwebservices.csw.discovery;
037    
038    import static org.deegree.ogcbase.CommonNamespaces.CSW202NS;
039    import static org.deegree.ogcbase.CommonNamespaces.CSWNS;
040    
041    import java.io.IOException;
042    import java.net.URL;
043    
044    import org.deegree.datatypes.QualifiedName;
045    import org.deegree.framework.util.StringTools;
046    import org.deegree.framework.xml.XMLFragment;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.ogcbase.ExceptionCode;
050    import org.deegree.ogcwebservices.InvalidParameterValueException;
051    import org.deegree.ogcwebservices.MissingParameterValueException;
052    import org.deegree.ogcwebservices.OGCWebServiceException;
053    import org.w3c.dom.Node;
054    import org.xml.sax.SAXException;
055    
056    /**
057     * Represents an XML GetRecordsResponse document of an OGC CSW 2.0 compliant service.
058     * <p>
059     * The &lt;GetRecordsResponse&gt; element is a container for the response of the GetRecords
060     * operation. Three levels of detail may be contained in the response document.
061     * <ul>
062     * <li>The &lt;RequestId&gt; element may be used to correlate the response to a GetRecords request
063     * for which a value was defined for the requestId attribute.
064     * <li>&lt;SearchStatus&gt; element must be present and indicates the status of the response. The
065     * status attribute is used to indicate the completion status of the GetRecords operation. Table 65
066     * shows the possible values for the status attribute.
067     * <li>The &lt;SearchResults&gt; element is a generic XML container for the actual response to a
068     * GetRecords request. The content of the &lt;SearchResults&gt; element is the set of records
069     * returned by the GetRecords operation. The actual records returned by the catalogue should
070     * substitute for the element &lt;csw:AbstractRecord&gt.
071     * </ul>
072     *
073     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
074     *
075     * @author last edited by: $Author: mschneider $
076     *
077     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
078     *
079     *
080     */
081    public class GetRecordsResultDocument extends XMLFragment {
082    
083        private static final long serialVersionUID = 2796229558893029054L;
084    
085        private static final String XML_TEMPLATE = "GetRecordsResponseTemplate.xml";
086    
087        /**
088         *
089         */
090        public GetRecordsResultDocument() {
091            // load later
092        }
093    
094        /**
095         * @param version
096         */
097        public GetRecordsResultDocument( String version ) {
098            super( new QualifiedName( "csw", "GetRecordsResponse", version.equals( "2.0.2" ) ? CSW202NS : CSWNS ) );
099        }
100    
101        /**
102         * Extracts a <code>GetRecordsResult</code> representation of this object.
103         *
104         * @param request
105         *
106         * @return the actual GetRecordResult as a bean.
107         * @throws MissingParameterValueException
108         * @throws InvalidParameterValueException
109         * @throws OGCWebServiceException
110         */
111        public GetRecordsResult parseGetRecordsResponse( GetRecords request )
112                                throws MissingParameterValueException, InvalidParameterValueException,
113                                OGCWebServiceException {
114            try {
115                String requestId = null;
116                SearchStatus searchStatus = null;
117                SearchResults searchResults = null;
118                // '<csw:GetRecordsResponse>'-element (required)
119                Node contextNode = XMLTools.getRequiredNode( getRootElement(), "self::csw:GetRecordsResponse", nsContext );
120    
121                // 'version'-attribute (optional)
122                String version = XMLTools.getNodeAsString( contextNode, "@version", nsContext, GetRecords.DEFAULT_VERSION );
123    
124                // '<csw:RequestId>'-element (optional)
125                requestId = XMLTools.getNodeAsString( contextNode, "csw:RequestId", nsContext, requestId );
126    
127                // '<csw:SearchStatus>'-element (required)
128                String status = XMLTools.getRequiredNodeAsString( contextNode, "csw:SearchStatus/@status", nsContext );
129                String timestamp = XMLTools.getNodeAsString( contextNode, "csw:SearchStatus/@timestamp", nsContext, null );
130                searchStatus = new SearchStatus( status, timestamp );
131    
132                // '<csw:SearchResults>'-element (required)
133                contextNode = XMLTools.getRequiredNode( contextNode, "csw:SearchResults", nsContext );
134    
135                // 'requestId'-attribute (optional)
136                requestId = XMLTools.getNodeAsString( contextNode, "@requestId", nsContext, requestId );
137    
138                // 'resultSetId'-attribute (optional)
139                String resultSetId = XMLTools.getNodeAsString( contextNode, "@resultSetId", nsContext, null );
140    
141                // 'elementSet'-attribute (optional)
142                String elementSet = XMLTools.getNodeAsString( contextNode, "@elementSet", nsContext, null );
143    
144                // 'recordSchema'-attribute (optional)
145                String recordSchema = XMLTools.getNodeAsString( contextNode, "@recordSchema", nsContext, null );
146    
147                // 'numberOfRecordsMatched'-attribute (required)
148                int numberOfRecordsMatched = XMLTools.getRequiredNodeAsInt( contextNode, "@numberOfRecordsMatched",
149                                                                            nsContext );
150    
151                // 'numberOfRecordsReturned'-attribute (required)
152                int numberOfRecordsReturned = XMLTools.getRequiredNodeAsInt( contextNode, "@numberOfRecordsReturned",
153                                                                             nsContext );
154    
155                // 'nextRecord'-attribute (required)
156                int nextRecord = XMLTools.getRequiredNodeAsInt( contextNode, "@nextRecord", nsContext );
157    
158                // 'expires'-attribute (optional)
159                String expires = XMLTools.getNodeAsString( contextNode, "@expires", nsContext, "null" );
160    
161                searchResults = new SearchResults( requestId, resultSetId, elementSet, recordSchema,
162                                                   numberOfRecordsReturned, numberOfRecordsMatched, nextRecord,
163                                                   contextNode, expires );
164                return new GetRecordsResult( request, version, searchStatus, searchResults );
165            } catch ( XMLParsingException e ) {
166                ExceptionCode code = ExceptionCode.INVALID_FORMAT;
167                throw new OGCWebServiceException( "GetRecordsResponseDocument", StringTools.stackTraceToString( e ), code );
168            }
169    
170        }
171    
172        /**
173         * creates an emtpy document as defined by the template
174         *
175         * @throws IOException
176         *             if the template could not be found
177         * @throws SAXException
178         *             if an error occurs while creating the rootnode.
179         */
180        public void createEmptyDocument()
181                                throws IOException, SAXException {
182            URL url = GetRecordsResultDocument.class.getResource( XML_TEMPLATE );
183            if ( url == null ) {
184                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
185            }
186            load( url );
187        }
188    }