001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/csw/discovery/Query.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    
037    package org.deegree.ogcwebservices.csw.discovery;
038    
039    import java.util.ArrayList;
040    import java.util.HashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    import org.deegree.datatypes.QualifiedName;
045    import org.deegree.model.filterencoding.Filter;
046    import org.deegree.ogcbase.PropertyPath;
047    import org.deegree.ogcbase.PropertyPathFactory;
048    import org.deegree.ogcbase.SortProperty;
049    
050    /**
051     * Main component of a <code>GetRecords</code> request. A <code>GetRecords</code> request may
052     * consist of several <code>Query</code> elements.
053     *
054     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
055     *
056     * @author last edited by: $Author: mschneider $
057     *
058     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
059     */
060    
061    public class Query {
062    
063        private String elementSetName;
064    
065        private List<PropertyPath> elementNamesAsPropertyPaths;
066    
067        private Filter constraint;
068    
069        private SortProperty[] sortProperties;
070    
071        private List<QualifiedName> typeNames;
072    
073        private Map<String, QualifiedName> declaredTypeNameVariables;
074    
075        private List<QualifiedName> elementSetNameTypeNamesList;
076    
077        private Map<String, QualifiedName> elementSetNameVariables;
078    
079        /**
080         * Creates a new Query instance.
081         *
082         * @param elementSetName
083         * @param elementNames
084         *            a String array containing the requested ElementName propertypaths. If not null,
085         *            they will be converted to an ArrayList<PropertyPath>.
086         * @param sortProperties
087         * @param constraint
088         * @param typeNames
089         *            which will be transformed to a list of qualified names.
090         */
091        Query( String elementSetName, String[] elementNames, Filter constraint,
092               SortProperty[] sortProperties, String[] typeNames ) {
093            List<QualifiedName> list = new ArrayList<QualifiedName>(
094                                                                     ( ( typeNames != null ) ? typeNames.length
095                                                                                            : 0 ) );
096            if ( typeNames != null ) {
097                for ( String tName : typeNames ) {
098                    list.add( new QualifiedName( tName ) );
099                }
100            }
101            this.elementSetName = elementSetName;
102            this.elementSetNameTypeNamesList = new ArrayList<QualifiedName>();
103            elementNamesAsPropertyPaths = new ArrayList<PropertyPath>();
104            if ( elementNames != null ) {
105                for ( String en : elementNames ) {
106                    elementNamesAsPropertyPaths.add( PropertyPathFactory.createPropertyPath( new QualifiedName(
107                                                                                                                en ) ) );
108                }
109            }
110    
111            this.constraint = constraint;
112            this.sortProperties = sortProperties;
113            this.typeNames = list;
114            this.elementSetNameVariables = new HashMap<String, QualifiedName>();
115            this.declaredTypeNameVariables = new HashMap<String, QualifiedName>();
116        }
117    
118        /**
119         * @param elementSetName
120         * @param elementSetNameTypeNames
121         *            the typenames (not the variables) which should be returned inside a
122         *            GetRecordsReponse
123         * @param elementSetNameVariables
124         *            the variables (and their mapping to the TypeName) which were requested.
125         * @param elementNames
126         *            a list of propertyPath of propertys a client is interested in.
127         * @param constraint
128         * @param sortProperties
129         * @param typeNames
130         *            list of QualifiedNames which were defined in the query element.
131         * @param typeNameVariables
132         *            the variables (strings starting with an $-sign) which were declared
133         *            typeNameattribtue in the Query element.
134         */
135        public Query( String elementSetName, List<QualifiedName> elementSetNameTypeNames,
136                      Map<String, QualifiedName> elementSetNameVariables,
137                      List<PropertyPath> elementNames, Filter constraint,
138                      SortProperty[] sortProperties, List<QualifiedName> typeNames,
139                      Map<String, QualifiedName> typeNameVariables ) {
140            this.elementSetName = elementSetName;
141            this.elementSetNameTypeNamesList = elementSetNameTypeNames;
142            this.elementSetNameVariables = elementSetNameVariables;
143            this.elementNamesAsPropertyPaths = elementNames;
144            this.constraint = constraint;
145            this.sortProperties = sortProperties;
146            this.typeNames = typeNames;
147            this.declaredTypeNameVariables = typeNameVariables;
148        }
149    
150        /**
151         * Zero or one (Optional); If <tt>null</tt> then getElementNames may return a list of
152         * requested elements. If both methods returns <tt>null</tt> the default action is to present
153         * all metadata elements.
154         * <p>
155         * The ElementName parameter is used to specify one or more metadata record elements that the
156         * query should present in the response to the a GetRecords operation. Well known sets of
157         * element may be named, in which case the ElementSetName parameter may be used (e. g.brief,
158         * summary or full).
159         * <p>
160         * If neither parameter is specified, then a CSW shall present all metadata record elements
161         *
162         * @return the textual value (brief, summary, full) of the elementSetName node or null if none
163         *         was given.
164         */
165        public String getElementSetName() {
166            return elementSetName;
167        }
168    
169        /**
170         * @return an Array of element names returned by a getRecord request.
171         * @deprecated this method is replaced with the more correct
172         *             {@link #getElementNamesAsPropertyPaths()}. For now it just returns the values of
173         *             {@link PropertyPath#getAsString()} method or <code>null</code> if no
174         *             elementName were requested.
175         *
176         */
177        @Deprecated
178        public String[] getElementsNames() {
179            if ( elementNamesAsPropertyPaths == null ) {
180                return null;
181            }
182            String[] r = new String[elementNamesAsPropertyPaths.size()];
183            for ( int i = 0; i < elementNamesAsPropertyPaths.size(); ++i ) {
184                r[i] = elementNamesAsPropertyPaths.get( i ).getAsString();
185            }
186            return r;
187        }
188    
189        /**
190         * Zero or one (Optional); Default action is to execute an unconstrained query
191         *
192         * @return the Filter which was given in the query.
193         */
194        public Filter getContraint() {
195            return this.constraint;
196        }
197    
198        /**
199         * Ordered list of names of metadata elements to use for sorting the response. Format of each
200         * list item is metadata_elemen_ name:A indicating an ascending sort or metadata_ element_name:D
201         * indicating descending sort
202         * <p>
203         * The result set may be sorted by specifying one or more metadata record elements upon which to
204         * sort.
205         * <p>
206         *
207         * @todo verify return type URI[] or String
208         * @return an Array of properties for sorting the response.
209         */
210        public SortProperty[] getSortProperties() {
211            return this.sortProperties;
212        }
213    
214        /**
215         * The typeName parameter specifies the record type name that defines a set of metadata record
216         * element names which will be constrained in the predicate of the query. In addition, all or
217         * some of the these names may be specified in the query to define which metadata record
218         * elements the query should present in the response to the GetRecords operation.
219         *
220         * @return the type names of the query.
221         * @deprecated this function actually creates an Array of Strings using the values returned from
222         *             the {@link QualifiedName#getFormattedString()} method or <code>null</code> if
223         *             no typenames were requested. It is more correct to use the values of the
224         *             {@link #getTypeNamesAsList()} method
225         */
226        @Deprecated
227        public String[] getTypeNames() {
228            if ( typeNames == null ) {
229                return null;
230            }
231            String[] tNames = new String[typeNames.size()];
232            for ( int i = 0; i < typeNames.size(); ++i ) {
233                tNames[i] = typeNames.get( i ).getFormattedString();
234            }
235            return tNames;
236        }
237    
238        /**
239         * The typeName parameter specifies the record type name that defines a set of metadata record
240         * element names which will be constrained in the predicate of the query. In addition, all or
241         * some of the these names may be specified in the query to define which metadata record
242         * elements the query should present in the response to the GetRecords operation.
243         *
244         * @return the type names of the query.
245         */
246        public List<QualifiedName> getTypeNamesAsList() {
247            return typeNames;
248        }
249    
250        /**
251         * @return the variables (with a leading $ (dollar_sign) as a String), declared with the
252         *         typeNames (given as {@link QualifiedName} ).
253         */
254        public Map<String, QualifiedName> getDeclaredTypeNameVariables() {
255            return declaredTypeNameVariables;
256        }
257    
258        /**
259         * @return the requested elementNames as a list of PropertyPaths.
260         */
261        public List<PropertyPath> getElementNamesAsPropertyPaths() {
262            return elementNamesAsPropertyPaths;
263        }
264    
265        /**
266         * @return the variables which were requested in the ElementSetNames/@typeNames attribute and
267         *         the mapping to their typenames.
268         */
269        public Map<String, QualifiedName> getElementSetNameVariables() {
270            return elementSetNameVariables;
271        }
272    
273        /**
274         * @return the typenames which were requested in the ElementSetNames/@typeNames attribute.
275         */
276        public List<QualifiedName> getElementSetNameTypeNamesList() {
277            return elementSetNameTypeNamesList;
278        }
279    
280    }