    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/gazetteer/FindItemsCommand.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.portal.standard.gazetteer;
    037
    038    import java.util.ArrayList;
    039    import java.util.List;
    040    import java.util.Map;
    041    import java.util.UUID;
    042
    043    import org.deegree.datatypes.QualifiedName;
    044    import org.deegree.framework.util.StringTools;
    045    import org.deegree.model.filterencoding.ComplexFilter;
    046    import org.deegree.model.filterencoding.Literal;
    047    import org.deegree.model.filterencoding.LogicalOperation;
    048    import org.deegree.model.filterencoding.Operation;
    049    import org.deegree.model.filterencoding.OperationDefines;
    050    import org.deegree.model.filterencoding.PropertyIsLikeOperation;
    051    import org.deegree.model.filterencoding.PropertyName;
    052    import org.deegree.ogcbase.PropertyPath;
    053    import org.deegree.ogcbase.SortProperty;
    054    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
    055    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
    056    import org.deegree.ogcwebservices.wfs.operation.Query;
    057    import org.deegree.ogcwebservices.wfs.operation.GetFeature.RESULT_TYPE;
    058
    059    /**
    060     * TODO add class documentation here
    061     *
    062     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    063     * @author last edited by: $Author: apoth $
    064     *
    065     * @version $Revision: 23405 $, $Date: 2010-04-07 10:31:17 +0200 (Mi, 07. Apr 2010) $
    066     */
    067    public class FindItemsCommand extends AbstractGazetteerCommand {
    068
    069        private String searchString;
    070
    071        private boolean searchOnAltName;
    072
    073        @SuppressWarnings("unused")
    074        private boolean phonetic;
    075
    076        private boolean matchCase;
    077
    078        /**
    079         *
    080         * @param gazetteerAddress
    081         * @param featureType
    082         * @param properties
    083         * @param searchString
    084         * @param searchOnAltName
    085         *            if true alternativeGeographicIdentifier will be included into search
    086         * @param strict
    087         *            if false wild cards will be added to search string
    088         * @param phonetic
    089         *            if true soundex will be used for searching (gazetteer/featuetype must support this)
    090         * @param matchCase
    091         */
    092        FindItemsCommand( String gazetteerAddress, QualifiedName featureType, Map<String, String> properties,
    093                          String searchString, boolean searchOnAltName, boolean strict, boolean phonetic, boolean matchCase ) {
    094            this.gazetteerAddress = gazetteerAddress;
    095            this.featureType = featureType;
    096            this.searchString = searchString;
    097            this.searchOnAltName = searchOnAltName;
    098            this.properties = properties;
    099            this.phonetic = phonetic;
    100            this.matchCase = matchCase;
    101            if ( !strict && !searchString.trim().equalsIgnoreCase( "*" ) ) {
    102                this.searchString = StringTools.replace( searchString, "*", "/*", true );
    103                this.searchString = "*" + this.searchString + "*";
    104            }
    105        }
    106
    107        /**
    108         *
    109         * @return list of matching location instances (items)
    110         * @throws Exception
    111         */
    112        List<GazetteerItem> execute()
    113                                throws Exception {
    114            if ( !capabilitiesMap.containsKey( gazetteerAddress ) ) {
    115                loadCapabilities();
    116            }
    117            WFSCapabilities capabilities = capabilitiesMap.get( gazetteerAddress );
    118
    119            // TODO
    120            // consider soundex if defined
    121
    122            // create query filter
    123            PropertyName propertyName = new PropertyName( createPropertyPath( properties.get( "GeographicIdentifier" ) ) );
    124
    125            Literal literal = new Literal( searchString );
    126            Operation operation = new PropertyIsLikeOperation( propertyName, literal, '*', '?', '/', matchCase );
    127
    128            if ( searchOnAltName && properties.get( "AlternativeGeographicIdentifier" ) != null ) {
    129                // if search should be performed on alternativeGeographicIdentifier too
    130                // a logical OR operation must be created
    131                List<Operation> opList = new ArrayList<Operation>();
    132                opList.add( operation );
    133                propertyName = new PropertyName( createPropertyPath( properties.get( "AlternativeGeographicIdentifier" ) ) );
    134                literal = new Literal( searchString );
    135                operation = new PropertyIsLikeOperation( propertyName, literal, '*', '?', '/', matchCase );
    136                opList.add( operation );
    137                operation = new LogicalOperation( OperationDefines.OR, opList );
    138            }
    139            ComplexFilter filter = new ComplexFilter( operation );
    140
    141            // sort result by displayed property
    142            PropertyPath sortProperty = createPropertyPath( properties.get( "DisplayName" ) );
    143            SortProperty[] sp = new SortProperty[] { SortProperty.create( sortProperty, "ASC" ) };
    144
    145            // select just properties needed to fill result list
    146            PropertyPath[] propertyNames = getResultProperties( properties );
    147
    148            // create Query and GetFeature request
    149            Query query = Query.create( propertyNames, null, sp, null, null, new QualifiedName[] { featureType }, null,
    150                                        null, filter, 500, 0, RESULT_TYPE.RESULTS );
    151            GetFeature getFeature = GetFeature.create( capabilities.getVersion(), UUID.randomUUID().toString(),
    152                                                       RESULT_TYPE.RESULTS, GetFeature.FORMAT_GML3, null, 500, 0, -1, -1,
    153                                                       new Query[] { query } );
    154
    155            // perform GetFeature request and create resulting GazetteerItems list
    156            createItemsList( performGetFeature( capabilities, getFeature ) );
    157            return items;
    158        }
    159
    160    }