001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/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
057     * using their identifier. The GetRecordById operation is an implementation of the Present operation
058     * from the general model. This operation presumes that a previous query has been performed in order
059     * to obtain the identifiers that may be used with this operation. For example, records returned by
060     * a GetRecords operation may contain references to other records in the catalogue that may be
061     * retrieved using the GetRecordById operation. This operation is also a subset of the GetRecords
062     * operation, and is included as a convenient short form for retrieving and linking to records in a
063     * catalogue.
064     *
065     * @version $Revision: 18195 $
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
067     * @author last edited by: $Author: mschneider $
068     *
069     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
070     *
071     * @since 2.0
072     */
073    public class GetRecordById extends AbstractCSWRequest {
074    
075        private static final long serialVersionUID = -3602776884510160189L;
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( GetRecordById.class );
078    
079        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
080    
081        private String[] ids = null;
082    
083        private String elementSetName = null;
084    
085        /**
086         * creates a <code>GetRecordById</code> request from the XML fragment passed. The passed
087         * element must be valid 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
144         * Map. Keys (parameter 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
151         *         Long.toString( 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 tmp = kvp.remove( "ID" );
158            String[] ids = StringTools.toArray( tmp, ",", true );
159    
160            return new GetRecordById( Long.toString( IDGenerator.getInstance().generateUniqueID() ), version, kvp, ids,
161                                      elementSetName );
162        }
163    
164        /**
165         * Creates a new <code>GetRecordById</code> instance from the values stored in the submitted
166         * Map. Keys (parameter names) in the Map must be uppercase.
167         *
168         * @param id
169         *            of the request, and not the requested ids.
170         *
171         * @TODO evaluate vendorSpecificParameter
172         *
173         * @param kvp
174         *            Map containing the parameters
175         * @return a GetRecordById bean representation.
176         */
177        public static GetRecordById create( String id, Map<String, String> kvp ) {
178    
179            String version = kvp.remove( "VERSION" );
180            String elementSetName = kvp.remove( "ELEMENTSETNAME" );
181            String tmp = kvp.remove( "ID" );
182            String[] ids = StringTools.toArray( tmp, ",", true );
183    
184            return new GetRecordById( id, version, kvp, ids, elementSetName );
185        }
186    
187        /**
188         *
189         * @param ids
190         *            identifiers of the requested catalogue entries
191         * @param elementSetName
192         *            requested element set (brief|summary|full). Can be <code>null</code>; will be
193         *            treaded as full.
194         */
195        GetRecordById( String id, String version, Map<String, String> vendorSpecificParameters, String[] ids,
196                       String elementSetName ) {
197            super( version, id, vendorSpecificParameters );
198            this.ids = ids;
199            this.elementSetName = elementSetName;
200        }
201    
202        /**
203         * @return the requested element set name. If the returned value equals <code>null</code> a
204         *         'summary' request shall be performed. possible values are:
205         *         <ul>
206         *         <li>brief</li>
207         *         <li>summary</li>
208         *         <li>full</li>
209         *         </ul>
210         *
211         */
212        public String getElementSetName() {
213            return elementSetName;
214        }
215    
216        /**
217         * @return the requested ids as an array of strings
218         */
219        public String[] getIds() {
220            return ids;
221        }
222    }