001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.google.utils.geocode;
016    
017    import java.util.List;
018    
019    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020    
021    import org.chenillekit.google.utils.JSONArray;
022    import org.chenillekit.google.utils.JSONException;
023    import org.chenillekit.google.utils.JSONObject;
024    
025    /**
026     * hold the result codes from gmap.
027     *
028     * @version $Id: GeoCodeResult.java 351 2008-11-25 12:40:18Z homburgs $
029     */
030    public class GeoCodeResult
031    {
032        // No errors occurred; the address was successfully parsed and its geocode has been returned. (Since 2.55)
033        public static int G_GEO_SUCCESS = 200;
034    
035        // A directions request could not be successfully parsed. (Since 2.81)
036        public static int G_GEO_BAD_REQUEST = 400;
037    
038        // A geocoding or directions request could not be successfully processed, yet the exact reason for the failure
039        // is not known. (Since 2.55)
040        public static int G_GEO_SERVER_ERROR = 500;
041    
042        // The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was
043        // specified as input. For directions requests, this means that no query was specified in the input. (Since 2.81)
044        public static int G_GEO_MISSING_QUERY = 601;
045    
046        // Synonym for G_GEO_MISSING_QUERY. (Since 2.55)
047        public static int G_GEO_MISSING_ADDRESS = 601;
048    
049        // No corresponding geographic location could be found for the specified address. This may be due to the fact that the
050        // address is relatively new, or it may be incorrect. (Since 2.55)
051        public static int G_GEO_UNKNOWN_ADDRESS = 602;
052    
053        // The geocode for the given address or the route for the given directions query cannot be returned due to
054        // legal or contractual reasons. (Since 2.55)
055        public static int G_GEO_UNAVAILABLE_ADDRESS = 603;
056    
057        // The GDirections object could not compute directions between the points mentioned in the query. This is usually because
058        // there is no route available between the two points, or because we do not have data for routing in that region. (Since 2.81)
059        public static int G_GEO_UNKNOWN_DIRECTIONS = 604;
060    
061        // The given key is either invalid or does not match the domain for which it was given. (Since 2.55)
062        public static int G_GEO_BAD_KEY = 610;
063    
064        public static int G_GEO_TOO_MANY_QUERIES = 620;
065    
066        private String name;
067        private Status status;
068        private List<Placemark> placemarks = CollectionFactory.newList();
069    
070        public GeoCodeResult(JSONObject geoCodeResult)
071        {
072            buildFromJSON(geoCodeResult);
073        }
074    
075        private void buildFromJSON(JSONObject json)
076        {
077            try
078            {
079                name = json.getString("name");
080                status = new Status(json.getJSONObject("Status"));
081    
082                if (json.has("Placemark"))
083                {
084                    JSONArray jsonArray = json.getJSONArray("Placemark");
085                    for (int i = 0; i < jsonArray.length(); i++)
086                        placemarks.add(new Placemark(jsonArray.getJSONObject(i)));
087                }
088            }
089            catch (JSONException e)
090            {
091                throw new RuntimeException(e);
092            }
093        }
094    
095        public String getName()
096        {
097            return name;
098        }
099    
100        public Status getStatus()
101        {
102            return status;
103        }
104    
105        public List<Placemark> getPlacemarks()
106        {
107            return placemarks;
108        }
109    }