001    //$HeadURL: http://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/ogcwebservices/csw/discovery/GetRecordById.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.discovery;
037    
038    import java.util.Map;
039    
040    import org.deegree.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.framework.util.IDGenerator;
043    import org.deegree.framework.util.StringTools;
044    import org.deegree.framework.xml.NamespaceContext;
045    import org.deegree.framework.xml.XMLParsingException;
046    import org.deegree.framework.xml.XMLTools;
047    import org.deegree.ogcbase.CommonNamespaces;
048    import org.deegree.ogcwebservices.InvalidParameterValueException;
049    import org.deegree.ogcwebservices.MissingParameterValueException;
050    import org.deegree.ogcwebservices.OGCWebServiceException;
051    import org.deegree.ogcwebservices.csw.AbstractCSWRequest;
052    import org.deegree.ogcwebservices.csw.CSWPropertiesAccess;
053    import org.w3c.dom.Element;
054    
055    /**
056     * The mandatory GetRecordById request retrieves the default representation of catalogue records using their identifier.
057     * The GetRecordById operation is an implementation of the Present operation from the general model. This operation
058     * presumes that a previous query has been performed in order to obtain the identifiers that may be used with this
059     * operation. For example, records returned by a GetRecords operation may contain references to other records in the
060     * catalogue that may be retrieved using the GetRecordById operation. This operation is also a subset of the GetRecords
061     * operation, and is included as a convenient short form for retrieving and linking to records in a catalogue.
062     * 
063     * @version $Revision: 31089 $
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author last edited by: $Author: lbuesching $
066     * 
067     * @version 1.0. $Revision: 31089 $, $Date: 2011-06-22 10:30:37 +0200 (Mi, 22 Jun 2011) $
068     * 
069     * @since 2.0
070     */
071    public class GetRecordById extends AbstractCSWRequest {
072    
073        private static final long serialVersionUID = -3602776884510160189L;
074    
075        private static final ILogger LOG = LoggerFactory.getLogger( GetRecordById.class );
076    
077        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
078    
079        private String[] ids = null;
080    
081        private String elementSetName = null;
082    
083        private String outputSchema = null;
084    
085        /**
086         * creates a <code>GetRecordById</code> request from the XML fragment passed. The passed element must be valid
087         * against the OGC CSW 2.0 GetRecordById schema.
088         * 
089         * @param id
090         *            unique ID of the request
091         * @param root
092         *            root element of the GetRecors request
093         * @return a GetRecordById bean representation created from the xml fragment.
094         * @throws MissingParameterValueException
095         * @throws InvalidParameterValueException
096         * @throws OGCWebServiceException
097         */
098        public static GetRecordById create( String id, Element root )
099                                throws MissingParameterValueException, InvalidParameterValueException,
100                                OGCWebServiceException {
101    
102            String version = null;
103            try {
104                // first try to read verdsion attribute which is optional for CSW 2.0.0 and 2.0.1
105                version = XMLTools.getNodeAsString( root, "./@version", nsContext, null );
106            } catch ( XMLParsingException e ) {
107                // default version?
108            }
109            if ( version == null ) {
110                // if no version attribute has been set try mapping namespace URI to a version;
111                // this is not well defined for 2.0.0 and 2.0.1 which uses the same namespace.
112                // in this case 2.0.0 will be returned!
113                version = CSWPropertiesAccess.getString( root.getNamespaceURI() );
114            }
115    
116            // read class for version depenging parsing of GetRecords request from properties
117            String className = CSWPropertiesAccess.getString( "GetRecordById" + version );
118            Class<?> clzz = null;
119            try {
120                clzz = Class.forName( className );
121            } catch ( ClassNotFoundException e ) {
122                LOG.logError( e.getMessage(), e );
123                throw new InvalidParameterValueException( e.getMessage(), e );
124            }
125            GetRecordByIdDocument document = null;
126            try {
127                document = (GetRecordByIdDocument) clzz.newInstance();
128            } catch ( InstantiationException e ) {
129                LOG.logError( e.getMessage(), e );
130                throw new InvalidParameterValueException( e.getMessage(), e );
131            } catch ( IllegalAccessException e ) {
132                LOG.logError( e.getMessage(), e );
133                throw new InvalidParameterValueException( e.getMessage(), e );
134            }
135    
136            document.setRootElement( root );
137    
138            return document.parse( id );
139    
140        }
141    
142        /**
143         * Creates a new <code>GetRecordById</code> instance from the values stored in the submitted Map. Keys (parameter
144         * names) in the Map must be uppercase.
145         * 
146         * @TODO evaluate vendorSpecificParameter
147         * 
148         * @param kvp
149         *            Map containing the parameters
150         * @return a GetRecordById bean representation, the internal request id will be set to Long.toString(
151         *         IDGenerator.getInstance().generateUniqueID() ).
152         */
153        public static GetRecordById create( Map<String, String> kvp ) {
154    
155            String version = kvp.remove( "VERSION" );
156            String elementSetName = kvp.remove( "ELEMENTSETNAME" );
157            String outputSchema = null;
158            if ( "2.0.2".equals( version ) ) {
159                outputSchema = kvp.remove( "OUTPUTSCHEMA" );
160            }
161            String tmp = kvp.remove( "ID" );
162            String[] ids = StringTools.toArray( tmp, ",", true );
163    
164            return new GetRecordById( Long.toString( IDGenerator.getInstance().generateUniqueID() ), version, kvp, ids,
165                                      elementSetName, outputSchema );
166        }
167    
168        /**
169         * Creates a new <code>GetRecordById</code> instance from the values stored in the submitted Map. Keys (parameter
170         * names) in the Map must be uppercase.
171         * 
172         * @param id
173         *            of the request, and not the requested ids.
174         * 
175         * @TODO evaluate vendorSpecificParameter
176         * 
177         * @param kvp
178         *            Map containing the parameters
179         * @return a GetRecordById bean representation.
180         */
181        public static GetRecordById create( String id, Map<String, String> kvp ) {
182    
183            String version = kvp.remove( "VERSION" );
184            String elementSetName = kvp.remove( "ELEMENTSETNAME" );
185            String outputSchema = null;
186            if ( "2.0.2".equals( version ) ) {
187                outputSchema = kvp.remove( "OUTPUTSCHEMA" );
188            }
189            String tmp = kvp.remove( "ID" );
190            String[] ids = StringTools.toArray( tmp, ",", true );
191    
192            return new GetRecordById( id, version, kvp, ids, elementSetName, outputSchema );
193        }
194    
195        /**
196         * 
197         * @param ids
198         *            identifiers of the requested catalogue entries
199         * @param elementSetName
200         *            requested element set (brief|summary|full). Can be <code>null</code>; will be treaded as full.
201         */
202        GetRecordById( String id, String version, Map<String, String> vendorSpecificParameters, String[] ids,
203                       String elementSetName ) {
204            super( version, id, vendorSpecificParameters );
205            this.ids = ids;
206            this.elementSetName = elementSetName;
207        }
208    
209        /**
210         * 
211         * @param ids
212         *            identifiers of the requested catalogue entries
213         * @param elementSetName
214         *            requested element set (brief|summary|full). Can be <code>null</code>; will be treaded as full.
215         */
216        GetRecordById( String id, String version, Map<String, String> vendorSpecificParameters, String[] ids,
217                       String elementSetName, String outputSchema ) {
218            super( version, id, vendorSpecificParameters );
219            this.ids = ids;
220            this.elementSetName = elementSetName;
221            this.outputSchema = outputSchema;
222        }
223    
224        /**
225         * @return the requested element set name. If the returned value equals <code>null</code> a 'summary' request shall
226         *         be performed. possible values are:
227         *         <ul>
228         *         <li>brief</li>
229         *         <li>summary</li>
230         *         <li>full</li>
231         *         </ul>
232         * 
233         */
234        public String getElementSetName() {
235            return elementSetName;
236        }
237    
238        /**
239         * @return the requested outputSchema. If the returned value equals <code>null</code> dublin core
240         *         (http://www.opengis.net/cat/csw/2.0.2) should be returned by the the CSW 2.02.
241         */
242        public String getOutputSchema() {
243            return outputSchema;
244        }
245    
246        /**
247         * @return the requested ids as an array of strings
248         */
249        public String[] getIds() {
250            return ids;
251        }
252    }