001    //$HeadURL: $
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.wcts.operation;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    import java.util.Map;
042    
043    import org.deegree.framework.util.Pair;
044    import org.deegree.i18n.Messages;
045    import org.deegree.model.crs.CRSFactory;
046    import org.deegree.model.crs.CoordinateSystem;
047    import org.deegree.model.crs.UnknownCRSException;
048    import org.deegree.ogcbase.ExceptionCode;
049    import org.deegree.ogcwebservices.OGCWebServiceException;
050    import org.deegree.ogcwebservices.wcts.WCTSExceptionCode;
051    import org.deegree.ogcwebservices.wcts.WCTService;
052    
053    /**
054     * <code>IsTransformable</code> encapsulates the bean representation of the xml-dom or kvp encoded IsTransformable
055     * request of the wcts 0.4.0
056     *
057     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
058     *
059     * @author last edited by: $Author:$
060     *
061     * @version $Revision:$, $Date:$
062     *
063     */
064    public class IsTransformable extends WCTSRequestBase {
065    
066        private static final long serialVersionUID = 5834206406155257853L;
067    
068        private final CoordinateSystem sourceCRS;
069    
070        private final CoordinateSystem targetCRS;
071    
072        private final String transformation;
073    
074        private final String method;
075    
076        private final List<Pair<String, String>> geometryTypes;
077    
078        private final List<String> simpleGeometryTypes;
079    
080        private final List<Pair<String, String>> coverageTypes;
081    
082        private final List<Pair<String, String>> interpolationTypes;
083    
084        /**
085         * @param version
086         * @param id
087         * @param sourceCRS
088         *            if <code>null</code> it is assumed that the sourceCRS is not supported.
089         * @param targetCRS
090         *            if <code>null</code> it is assumed that the targetCRS is not supported.
091         * @param transformation
092         * @param method
093         * @param geometryTypes
094         * @param coverageTypes
095         * @param interpolationTypes
096         */
097        public IsTransformable( String version, String id, CoordinateSystem sourceCRS, CoordinateSystem targetCRS,
098                                String transformation, String method, List<Pair<String, String>> geometryTypes,
099                                List<Pair<String, String>> coverageTypes, List<Pair<String, String>> interpolationTypes ) {
100    
101            super( version, id, null );
102            this.sourceCRS = sourceCRS;
103            this.targetCRS = targetCRS;
104            this.transformation = transformation;
105            this.method = method;
106            if ( coverageTypes != null ) {
107                throw new IllegalArgumentException( Messages.getMessage( "WCTS_ISTRANSFORMABLE_COVERAGE_NOT_SUPPORTED" ) );
108            }
109            this.coverageTypes = coverageTypes;
110            if ( interpolationTypes != null ) {
111                throw new IllegalArgumentException(
112                                                    Messages.getMessage( "WCTS_ISTRANSFORMABLE_INTERPOLATION_NOT_SUPPORTED" ) );
113            }
114            this.interpolationTypes = interpolationTypes;
115            this.geometryTypes = geometryTypes;
116            this.simpleGeometryTypes = null;
117        }
118    
119        /**
120         * @param version
121         * @param requestID
122         * @param sourceCRS
123         *            if <code>null</code> it is assumed that the sourceCRS is not supported.
124         * @param targetCRS
125         *            if <code>null</code> it is assumed that the targetCRS is not supported.
126         * @param geometryTypes
127         *            a list of simple geometry representations.
128         */
129        public IsTransformable( String version, String requestID, CoordinateSystem sourceCRS, CoordinateSystem targetCRS,
130                                List<String> geometryTypes ) {
131            super( version, requestID, null );
132            this.sourceCRS = sourceCRS;
133            this.targetCRS = targetCRS;
134            this.transformation = null;
135            this.method = null;
136            this.coverageTypes = null;
137            this.interpolationTypes = null;
138    
139            this.geometryTypes = null;
140            this.simpleGeometryTypes = geometryTypes;
141        }
142    
143        /**
144         * @return the sourceCRS, if <code>null</code> it can be assumed that the sourceCRS is not supported.
145         */
146        public final CoordinateSystem getSourceCRS() {
147            return sourceCRS;
148        }
149    
150        /**
151         * @return the targetCRS, if <code>null</code> it can be assumed that the targetCRS is not supported.
152         */
153        public final CoordinateSystem getTargetCRS() {
154            return targetCRS;
155        }
156    
157        /**
158         * @return the transformation currently <code>null</code>
159         */
160        public final String getTransformation() {
161            return transformation;
162        }
163    
164        /**
165         * @return the method currently <code>null</code>
166         */
167        public final String getMethod() {
168            return method;
169        }
170    
171        /**
172         * @return the geometryTypes as a list of &lt value, codeSpace &gt; pairs. May be <code>null</code>, if so, check
173         *         {@link #getSimpleGeometryTypes()} to see if the request defined geometryTypes by KVP.
174         */
175        public final List<Pair<String, String>> getGeometryTypes() {
176            return geometryTypes;
177        }
178    
179        /**
180         * @return the simpleGeometryTypes. Maybe <code>null</code>, if so check {@link #getGeometryTypes()} to see if
181         *         xml encoded geometryTypes were supplied.
182         */
183        public final List<String> getSimpleGeometryTypes() {
184            return simpleGeometryTypes;
185        }
186    
187        /**
188         * @return the coverageTypes currently <code>null</code>.
189         */
190        public final List<Pair<String, String>> getCoverageTypes() {
191            return coverageTypes;
192        }
193    
194        /**
195         * @return the interpolationTypes currently <code>null</code>.
196         */
197        public final List<Pair<String, String>> getInterpolationTypes() {
198            return interpolationTypes;
199        }
200    
201        /**
202         * Create a {@link IsTransformable}-request by extracting the values from the map, and calling the constructor with
203         * these values.
204         *
205         * @param requestID
206         *            service internal id for this request.
207         * @param map
208         *            to extract requested values from.
209         * @return the bean representation
210         * @throws OGCWebServiceException
211         *             if the map is <code>null</code> or has size==0, or the service,request parameters have none
212         *             accepted values.
213         */
214        public static IsTransformable create( String requestID, Map<String, String> map )
215                                throws OGCWebServiceException {
216            if ( map == null || map.size() == 0 ) {
217                throw new OGCWebServiceException( Messages.getMessage( "WCTS_REQUESTMAP_NULL" ),
218                                                  ExceptionCode.MISSINGPARAMETERVALUE );
219            }
220            String service = map.get( "SERVICE" );
221            if ( service == null || !"WCTS".equals( service ) ) {
222                throw new OGCWebServiceException( Messages.getMessage( "WCTS_NO_VERSION_KVP", service ),
223                                                  ExceptionCode.MISSINGPARAMETERVALUE );
224            }
225            String request = map.get( "REQUEST" );
226            if ( request == null || !"IsTransformable".equalsIgnoreCase( request ) ) {
227                throw new OGCWebServiceException( Messages.getMessage( "WCTS_NO_REQUEST_KVP", "IsTransformable" ),
228                                                  ( request == null ? ExceptionCode.MISSINGPARAMETERVALUE
229                                                                   : ExceptionCode.OPERATIONNOTSUPPORTED ) );
230            }
231            String version = map.get( "VERSION" );
232            if ( version == null || !WCTService.version.equalsIgnoreCase( version ) ) {
233                throw new OGCWebServiceException( Messages.getMessage( "WCTS_NO_VERSION_KVP", version ),
234                                                  ExceptionCode.MISSINGPARAMETERVALUE );
235            }
236    
237            String sCRS = map.get( "SOURCECRS" );
238            String tCRS = map.get( "TARGETCRS" );
239            if ( ( ( sCRS != null && !"".equals( sCRS.trim() ) ) && ( tCRS == null || "".equals( tCRS.trim() ) ) )
240                 || ( ( tCRS != null && !"".equals( tCRS.trim() ) ) && ( sCRS == null || "".equals( sCRS.trim() ) ) ) ) {
241                throw new OGCWebServiceException( Messages.getMessage( "WCTS_MISSING_MUTUAL_KEY_KVP",
242                                                                       ( ( sCRS == null ) ? "TargetCRS" : "SourceCRS" ),
243                                                                       ( ( sCRS == null ) ? "SourceCRS" : "TargetCRS" ) ),
244                                                  ExceptionCode.INVALIDPARAMETERVALUE );
245            }
246            if ( ( sCRS == null || "".equals( sCRS.trim() ) ) && ( tCRS == null || "".equals( tCRS.trim() ) ) ) {
247                String transformation = map.get( "TRANSFORMATION" );
248                if ( !( transformation == null || "".equals( transformation.trim() ) ) ) {
249                    throw new OGCWebServiceException(
250                                                      Messages.getMessage( "WCTS_OPERATION_NOT_SUPPORTED",
251                                                                           "defining of transformations (Transformation key)" ),
252                                                      ExceptionCode.OPERATIONNOTSUPPORTED );
253                }
254    
255                String method = map.get( "METHOD" );
256                if ( !( method == null || "".equals( method.trim() ) ) ) {
257                    throw new OGCWebServiceException( Messages.getMessage( "WCTS_OPERATION_NOT_SUPPORTED",
258                                                                           "transformation method (Method key)" ),
259                                                      ExceptionCode.OPERATIONNOTSUPPORTED );
260                }
261                throw new OGCWebServiceException( Messages.getMessage( "WCTS_TRANSFORMATION_NO_CRS_OR_TRANSFORM_KVP" ),
262                                                  ExceptionCode.MISSINGPARAMETERVALUE );
263    
264            }
265            CoordinateSystem sourceCRS = null;
266            CoordinateSystem targetCRS = null;
267            try {
268                sourceCRS = CRSFactory.create( WCTService.CRS_PROVIDER, sCRS );
269                targetCRS = CRSFactory.create( WCTService.CRS_PROVIDER, tCRS );
270            } catch ( UnknownCRSException e ) {
271                throw new OGCWebServiceException( e.getMessage(), WCTSExceptionCode.UNSUPPORTED_COMBINATION );
272            }
273    
274            String tmp = map.get( "GEOMETRYTYPES" );
275            List<String> geometryTypes = new ArrayList<String>( 10 );
276            if ( tmp != null && !"".equals( tmp.trim() ) ) {
277                String[] splitter = tmp.split( "," );
278                for ( String split : splitter ) {
279                    if ( split != null && !"".equals( split.trim() ) ) {
280                        geometryTypes.add( split.trim() );
281                    }
282                }
283            }
284    
285            String coverageTypes = map.get( "COVERAGETYPES" );
286            if ( !( coverageTypes == null || "".equals( coverageTypes.trim() ) ) ) {
287                throw new OGCWebServiceException( Messages.getMessage( "WCTS_OPERATION_NOT_SUPPORTED",
288                                                                       "CoverageTypes (key)" ),
289                                                  ExceptionCode.OPERATIONNOTSUPPORTED );
290            }
291    
292            String interPolationType = map.get( "INTERPOLATIONTYPES" );
293            if ( !( interPolationType == null || "".equals( interPolationType.trim() ) ) ) {
294                throw new OGCWebServiceException( Messages.getMessage( "WCTS_OPERATION_NOT_SUPPORTED",
295                                                                       "InterpolationTypes (key)" ),
296                                                  ExceptionCode.OPERATIONNOTSUPPORTED );
297            }
298    
299            return new IsTransformable( version, requestID, sourceCRS, targetCRS, geometryTypes );
300        }
301    
302    }