001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcbase/SortProperty.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     53177 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.ogcbase;
044    
045    import org.deegree.framework.util.StringTools;
046    import org.deegree.framework.xml.NamespaceContext;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.ogcwebservices.InvalidParameterValueException;
050    import org.w3c.dom.Element;
051    import org.w3c.dom.Node;
052    import org.w3c.dom.Text;
053    
054    /**
055     * Java incarnation of the <code>SortPropertyType</code> defined in <code>sort.xsd</code> from
056     * the Filter Encoding Specification.
057     * 
058     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
059     * @author last edited by: $Author: apoth $
060     * 
061     * @version $Revision: 9344 $, $Date: 2007-12-27 17:21:56 +0100 (Do, 27 Dez 2007) $
062     */
063    public class SortProperty {
064    
065        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
066    
067        // property that will act as sort criterion
068        private PropertyPath sortProperty;
069    
070        // true -> ascending, false -> descending
071        private boolean sortOrder;
072    
073        private SortProperty( PropertyPath sortProperty, boolean sortOrder ) {
074            this.sortProperty = sortProperty;
075            this.sortOrder = sortOrder;
076        }
077    
078        /**
079         * Returns the property that will act as sort criterion.
080         * 
081         * @return the property that will act as sort criterion
082         */
083        public PropertyPath getSortProperty() {
084            return this.sortProperty;
085        }
086    
087        /**
088         * Returns the sort order.
089         * 
090         * @return true, if the sort order is ascending, false if it is descending
091         */
092        public boolean getSortOrder() {
093            return this.sortOrder;
094        }
095    
096        /**
097         * Creates a new <code>SortProperty</code> instance.
098         * 
099         * @param sortProperty
100         * @param sortOrderString
101         *            must be "ASC" or "DESC"
102         * @return SortProperty instance
103         * @throws InvalidParameterValueException
104         *            if sortOrderString is not "ASC" or "DESC" 
105         */
106        public static SortProperty create( PropertyPath sortProperty, String sortOrderString )
107                                throws InvalidParameterValueException {
108            boolean sortOrder;
109            if ( "DESC".equals( sortOrderString ) ) {
110                sortOrder = false;
111            } else if ( "ASC".equals( sortOrderString ) ) {
112                sortOrder = true;
113            } else {
114                String msg = "Invalid value '" + sortOrderString
115                             + "' for 'SortOrder'. The only possible values are 'DESC' or 'ASC'";
116                throw new InvalidParameterValueException( msg );
117            }
118            return new SortProperty( sortProperty, sortOrder );
119        }
120    
121        /**
122         * Creates an array of <code>SortProperty</code> instances from the giving string encoding (as
123         * described in OGC 04-021r2, page 136).
124         * 
125         * @param sortBy
126         * @return array of <code>SortProperty</code> instances
127         * @throws InvalidParameterValueException
128         */
129        public static SortProperty[] create( String sortBy )
130                                throws InvalidParameterValueException {
131    
132            SortProperty[] sortProperties = null;
133    
134            if ( sortBy != null ) {
135                String[] parts = StringTools.toArray( sortBy, ",", false );
136                sortProperties = new SortProperty[parts.length];
137                for ( int i = 0; i < parts.length; i++ ) {
138                    boolean sortOrder = false;
139                    if ( parts[i].endsWith( ":A" ) ) {
140                        sortOrder = true;
141                    } else if ( !parts[i].endsWith( ":D" ) ) {
142                        String msg = "Invalid value '" + sortBy
143                                     + "' for parameter 'SortBy'. Format of each list "
144                                     + "item is metadata_element_name:A indicating an "
145                                     + "ascending sort or metadata_element_name:D "
146                                     + "indicating descending sort.";
147                        throw new InvalidParameterValueException( msg );
148                    }
149                    // FIXME ASAP !!!
150                    // PropertyName sortProperty = new PropertyName( parts[i].substring( 0, parts[i]
151                    // .length() - 2 ) );
152                    // sortProperties[i] = new SortProperty( sortProperty, sortOrder );
153                }
154            }
155            return sortProperties;
156        }
157    
158        /**
159         * Parses the given <code>SortProperty</code> element.
160         * 
161         * @param element
162         *            'ogc:SortProperty'-element
163         * @return corresponding <code>SortProperty</code> instance
164         * @throws XMLParsingException
165         */
166        public static SortProperty create( Element element )
167                                throws XMLParsingException {
168    
169            Node node = XMLTools.getRequiredNode( element, "ogc:PropertyName/text()", nsContext );
170            PropertyPath propertyName = OGCDocument.parsePropertyPath( (Text) node );
171            String sortOrderString = XMLTools.getNodeAsString( element, "ogc:SortOrder/text()",
172                                                               nsContext, "ASC" );
173            boolean sortOrder;
174            if ( "ASC".equals( sortOrderString ) ) {
175                sortOrder = true;
176            } else if ( "DESC".equals( sortOrderString ) ) {
177                sortOrder = false;
178            } else {
179                String msg = "Invalid value ('" + sortOrderString + "') for 'SortOrder'. The only "
180                             + "valid values are 'ASC' or 'DESC'.";
181                throw new XMLParsingException (msg);            
182            }
183    
184            SortProperty sortProperty = new SortProperty (propertyName, sortOrder);
185            return sortProperty;
186        }
187    }