1
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
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 }