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