001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/csw/discovery/GetRecordById.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.ogcwebservices.csw.discovery;
044    
045    import java.util.Map;
046    
047    import org.deegree.framework.log.ILogger;
048    import org.deegree.framework.log.LoggerFactory;
049    import org.deegree.framework.util.IDGenerator;
050    import org.deegree.framework.util.StringTools;
051    import org.deegree.framework.xml.NamespaceContext;
052    import org.deegree.framework.xml.XMLParsingException;
053    import org.deegree.framework.xml.XMLTools;
054    import org.deegree.ogcbase.CommonNamespaces;
055    import org.deegree.ogcwebservices.InvalidParameterValueException;
056    import org.deegree.ogcwebservices.MissingParameterValueException;
057    import org.deegree.ogcwebservices.OGCWebServiceException;
058    import org.deegree.ogcwebservices.csw.AbstractCSWRequest;
059    import org.deegree.ogcwebservices.csw.CSWPropertiesAccess;
060    import org.deegree.ogcwebservices.csw.NonexistentCollectionException;
061    import org.deegree.ogcwebservices.csw.NonexistentTypeException;
062    import org.w3c.dom.Element;
063    
064    /**
065     * The mandatory GetRecordById request retrieves the default representation of catalogue records
066     * using their identifier. The GetRecordById operation is an implementation of the Present operation
067     * from the general model. This operation presumes that a previous query has been performed in order
068     * to obtain the identifiers that may be used with this operation. For example, records returned by
069     * a GetRecords operation may contain references to other records in the catalogue that may be
070     * retrieved using the GetRecordById operation. This operation is also a subset of the GetRecords
071     * operation, and is included as a convenient short form for retrieving and linking to records in a
072     * catalogue.
073     * 
074     * @version $Revision: 9345 $
075     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
076     * @author last edited by: $Author: apoth $
077     * 
078     * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
079     * 
080     * @since 2.0
081     */
082    public class GetRecordById extends AbstractCSWRequest {
083    
084        private static final long serialVersionUID = -3602776884510160189L;
085        
086        private static final ILogger LOG = LoggerFactory.getLogger( GetRecordById.class );
087        
088        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
089    
090        private String[] ids = null;
091    
092        private String elementSetName = null;
093    
094        /**
095         * creates a <code>GetRecordById</code> request from the XML fragment passed. The passed
096         * element must be valid against the OGC CSW 2.0 GetRecordById schema.
097         * 
098         * @param id
099         *            unique ID of the request
100         * @param root
101         *            root element of the GetRecors request
102         * @return a GetRecordById bean representation created from the xml fragment.
103         * @throws MissingParameterValueException
104         * @throws InvalidParameterValueException
105         * @throws OGCWebServiceException
106         */
107        public static GetRecordById create( String id, Element root )
108                                throws MissingParameterValueException, InvalidParameterValueException,
109                                OGCWebServiceException {
110    
111            String version = null;
112            try {
113                // first try to read verdsion attribute which is optional for CSW 2.0.0 and 2.0.1
114                version = XMLTools.getNodeAsString( root, "./@version", nsContext, null );
115            } catch ( XMLParsingException e ) {
116    
117            }
118            if ( version == null ) {
119                // if no version attribute has been set try mapping namespace URI to a version;
120                // this is not well defined for 2.0.0 and 2.0.1 which uses the same namespace.
121                // in this case 2.0.0 will be returned!
122                version = CSWPropertiesAccess.getString( root.getNamespaceURI() );
123            }
124    
125            // read class for version depenging parsing of GetRecords request from properties 
126            String className = CSWPropertiesAccess.getString( "GetRecordById" + version );
127            Class clzz = null;
128            try {
129                clzz = Class.forName( className );
130            } catch ( ClassNotFoundException e ) {
131                LOG.logError( e.getMessage(), e );
132                throw new InvalidParameterValueException( e.getMessage(), e );
133            }
134            GetRecordByIdDocument document = null;
135            try {
136                document = (GetRecordByIdDocument) clzz.newInstance();
137            } catch ( InstantiationException e ) {
138                LOG.logError( e.getMessage(), e );
139                throw new InvalidParameterValueException( e.getMessage(), e );
140            } catch ( IllegalAccessException e ) {
141                LOG.logError( e.getMessage(), e );
142                throw new InvalidParameterValueException( e.getMessage(), e );
143            }
144    
145            document.setRootElement( root );
146    
147            return document.parse( id );
148    
149        }
150    
151        /**
152         * Creates a new <code>GetRecordById</code> instance from the values stored in the submitted
153         * Map. Keys (parameter names) in the Map must be uppercase.
154         * 
155         * @TODO evaluate vendorSpecificParameter
156         * 
157         * @param kvp
158         *            Map containing the parameters
159         * @return a GetRecordById bean representation, the internal request id will be set to
160         *         Long.toString( IDGenerator.getInstance().generateUniqueID() ).
161         * @exception InvalidParameterValueException
162         * @exception MissingParameterValueException
163         * @exception NonexistentCollectionException
164         * @exception NonexistentTypeException
165         */
166        public static GetRecordById create( Map<String, String> kvp ) {
167    
168            String version = kvp.remove( "VERSION" );
169            String elementSetName = kvp.remove( "ELEMENTSETNAME" );
170            String tmp = kvp.remove( "ID" );
171            String[] ids = StringTools.toArray( tmp, ",", true );
172    
173            return new GetRecordById( Long.toString( IDGenerator.getInstance().generateUniqueID() ), 
174                                      version, kvp, ids,
175                                      elementSetName );
176        }
177    
178        /**
179         * Creates a new <code>GetRecordById</code> instance from the values stored in the submitted
180         * Map. Keys (parameter names) in the Map must be uppercase.
181         * 
182         * @param id
183         *            of the request, and not the requested ids.
184         * 
185         * @TODO evaluate vendorSpecificParameter
186         * 
187         * @param kvp
188         *            Map containing the parameters
189         * @return a GetRecordById bean representation.
190         * @exception InvalidParameterValueException
191         * @exception MissingParameterValueException
192         * @exception NonexistentCollectionException
193         * @exception NonexistentTypeException
194         */
195        public static GetRecordById create( String id, Map<String, String> kvp ) {
196    
197            String version = kvp.remove( "VERSION" );
198            String elementSetName = kvp.remove( "ELEMENTSETNAME" );
199            String tmp = kvp.remove( "ID" );
200            String[] ids = StringTools.toArray( tmp, ",", true );
201    
202            return new GetRecordById( id, version, kvp, ids, elementSetName );
203        }
204    
205        /**
206         * 
207         * @param ids
208         *            identifiers of the requested catalogue entries
209         * @param elementSetName
210         *            requested element set (brief|summary|full). Can be <code>null</code>; will be
211         *            treaded as full.
212         */
213        GetRecordById( String id, String version, Map<String, String> vendorSpecificParameters, String[] ids,
214                       String elementSetName ) {
215            super( version, id, vendorSpecificParameters );
216            this.ids = ids;
217            this.elementSetName = elementSetName;
218        }
219    
220        /**
221         * @return the requested element set name. If the returned value equals <code>null</code> a
222         *         'summary' request shall be performed. possible values are:
223         *         <ul>
224         *         <li>brief</li>
225         *         <li>summary</li>
226         *         <li>full</li>
227         *         </ul>
228         * 
229         */
230        public String getElementSetName() {
231            return elementSetName;
232        }
233    
234        /**
235         * @return the requested ids as an array of strings
236         */
237        public String[] getIds() {
238            return ids;
239        }
240    }