001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.maps.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.webcache.WebCacheException;
026: import com.liferay.portal.kernel.webcache.WebCacheItem;
027: import com.liferay.portlet.maps.model.MapsAddress;
028: import com.liferay.util.Http;
029: import com.liferay.util.HttpUtil;
030: import com.liferay.util.Time;
031:
032: /**
033: * <a href="MapsWebCacheItem.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class MapsWebCacheItem implements WebCacheItem {
039:
040: public MapsWebCacheItem(String street, String csz) {
041: _street = street;
042: _csz = csz;
043: }
044:
045: public Object convert(String key) throws WebCacheException {
046: MapsAddress map = null;
047:
048: String street = "";
049: String city = "";
050: String state = "";
051: String zip = "";
052:
053: try {
054: street = _street;
055:
056: int pos = _csz.indexOf(StringPool.COMMA);
057:
058: if (pos != -1) {
059: city = _csz.substring(0, pos).trim();
060:
061: state = _csz.substring(pos + 1, _csz.length()).trim();
062: } else {
063: zip = _csz;
064: }
065: } catch (Exception e) {
066: return map;
067: }
068:
069: try {
070: StringMaker url = new StringMaker();
071:
072: url
073: .append("http://www.mapquest.com/maps/map.adp?country=US");
074: url.append("&address=");
075: url.append(HttpUtil.encodeURL(street));
076: url.append("&city=");
077: url.append(HttpUtil.encodeURL(city));
078: url.append("&state=");
079: url.append(HttpUtil.encodeURL(state));
080: url.append("&zipcode=");
081: url.append(HttpUtil.encodeURL(zip));
082:
083: String text = Http.URLtoString(url.toString());
084:
085: int mapDirectPos = text.indexOf("GetMapDataDirect=");
086:
087: if (mapDirectPos != -1) {
088: mapDirectPos = mapDirectPos + 17;
089: } else {
090: return map;
091: }
092:
093: String mapDirect = text.substring(mapDirectPos, text
094: .indexOf("\"", mapDirectPos));
095:
096: mapDirect = HttpUtil.decodeURL(mapDirect);
097:
098: map = new MapsAddress(street, city, state, zip, mapDirect);
099: } catch (Exception e) {
100: throw new WebCacheException(e);
101: }
102:
103: return map;
104: }
105:
106: public long getRefreshTime() {
107: return _REFRESH_TIME;
108: }
109:
110: private static final long _REFRESH_TIME = Time.DAY * 90;
111:
112: private String _street;
113: private String _csz;
114:
115: }
|