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