1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.maps.util;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.util.WebCacheable;
28  import com.liferay.portlet.maps.model.MapsAddress;
29  import com.liferay.util.ConverterException;
30  import com.liferay.util.Http;
31  import com.liferay.util.HttpUtil;
32  import com.liferay.util.Time;
33  
34  /**
35   * <a href="MapsConverter.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class MapsConverter implements WebCacheable {
41  
42      public MapsConverter(String street, String csz) {
43          _street = street;
44          _csz = csz;
45      }
46  
47      public Object convert(String id) throws ConverterException {
48          MapsAddress map = null;
49  
50          String street = "";
51          String city = "";
52          String state = "";
53          String zip = "";
54  
55          try {
56              street = _street;
57  
58              int pos = _csz.indexOf(StringPool.COMMA);
59  
60              if (pos != -1) {
61                  city = _csz.substring(0, pos).trim();
62  
63                  state = _csz.substring(pos + 1, _csz.length()).trim();
64              }
65              else {
66                  zip = _csz;
67              }
68          }
69          catch (Exception e) {
70              return map;
71          }
72  
73          try {
74              StringMaker url = new StringMaker();
75  
76              url.append("http://www.mapquest.com/maps/map.adp?country=US");
77              url.append("&address=");
78              url.append(HttpUtil.encodeURL(street));
79              url.append("&city=");
80              url.append(HttpUtil.encodeURL(city));
81              url.append("&state=");
82              url.append(HttpUtil.encodeURL(state));
83              url.append("&zipcode=");
84              url.append(HttpUtil.encodeURL(zip));
85  
86              String text = Http.URLtoString(url.toString());
87  
88              int mapDirectPos = text.indexOf(
89                  "GetMapDataDirect=");
90  
91              if (mapDirectPos != -1) {
92                  mapDirectPos = mapDirectPos + 17;
93              }
94              else {
95                  return map;
96              }
97  
98              String mapDirect = text.substring(
99                  mapDirectPos, text.indexOf("\"", mapDirectPos));
100 
101             mapDirect = HttpUtil.decodeURL(mapDirect);
102 
103             map = new MapsAddress(street, city, state, zip, mapDirect);
104         }
105         catch (Exception e) {
106             throw new ConverterException(e);
107         }
108 
109         return map;
110     }
111 
112     public long getRefreshTime() {
113         return _REFRESH_TIME;
114     }
115 
116     private static final long _REFRESH_TIME = Time.DAY * 90;
117 
118     private String _street;
119     private String _csz;
120 
121 }