001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.common;
019:
020: import java.io.IOException;
021: import java.util.Map;
022:
023: import javax.xml.parsers.ParserConfigurationException;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.base.util.HttpClient;
027: import org.ofbiz.base.util.HttpClientException;
028: import org.ofbiz.base.util.UtilProperties;
029: import org.ofbiz.base.util.UtilXml;
030: import org.ofbiz.service.DispatchContext;
031: import org.ofbiz.service.ServiceUtil;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * OFBIZ CDyne Services: for info see http://www.cdyne.com/developers/overview.aspx
038: */
039: public class CdyneServices {
040:
041: public final static String module = CdyneServices.class.getName();
042:
043: public final static String licenseKey = UtilProperties
044: .getPropertyValue("cdyne", "LicenseKey", "0");
045:
046: /**
047: * CDyne ReturnCityState Service
048: *@param dctx The DispatchContext that this service is operating in
049: *@param context Map containing the input parameters
050: *@return Map with the result of the service, the output parameters
051: */
052: public static Map cdyneReturnCityState(DispatchContext dctx,
053: Map context) {
054: String zipcode = (String) context.get("zipcode");
055:
056: String serviceUrl = "http://ws.cdyne.com/psaddress/addresslookup.asmx/ReturnCityState?zipcode="
057: + zipcode + "&LicenseKey=" + licenseKey;
058: try {
059: String httpResponse = HttpClient.getUrlContent(serviceUrl);
060:
061: Document addressDocument = UtilXml
062: .readXmlDocument(httpResponse);
063: Element addressRootElement = addressDocument
064: .getDocumentElement();
065:
066: Map response = ServiceUtil.returnSuccess();
067: populateCdyneAddress(addressRootElement, response);
068:
069: if ("true".equals(response.get("ServiceError"))) {
070: return ServiceUtil
071: .returnError("Got ServiceError=true from CDyne ReturnCityState service; zipcode="
072: + zipcode);
073: }
074: if ("true".equals(response.get("AddressError"))) {
075: return ServiceUtil
076: .returnError("Got AddressError=true from CDyne ReturnCityState service; zipcode="
077: + zipcode);
078: }
079:
080: return response;
081: } catch (HttpClientException e) {
082: String errMsg = "Error calling CDyne service at URL ["
083: + serviceUrl + "]: " + e.toString();
084: Debug.logError(e, errMsg, module);
085: return ServiceUtil.returnError(errMsg);
086: } catch (SAXException e) {
087: String errMsg = "Error parsing XML result from CDyne service at URL ["
088: + serviceUrl + "]: " + e.toString();
089: Debug.logError(e, errMsg, module);
090: return ServiceUtil.returnError(errMsg);
091: } catch (ParserConfigurationException e) {
092: String errMsg = "Error parsing XML result from CDyne service at URL ["
093: + serviceUrl + "]: " + e.toString();
094: Debug.logError(e, errMsg, module);
095: return ServiceUtil.returnError(errMsg);
096: } catch (IOException e) {
097: String errMsg = "Error parsing XML result from CDyne service at URL ["
098: + serviceUrl + "]: " + e.toString();
099: Debug.logError(e, errMsg, module);
100: return ServiceUtil.returnError(errMsg);
101: }
102: }
103:
104: public static void populateCdyneAddress(Element addressRootElement,
105: Map targetContext) {
106: targetContext.put("ServiceError", UtilXml.childElementValue(
107: addressRootElement, "ServiceError"));
108: targetContext.put("AddressError", UtilXml.childElementValue(
109: addressRootElement, "AddressError"));
110: targetContext.put("AddressFoundBeMoreSpecific", UtilXml
111: .childElementValue(addressRootElement,
112: "AddressFoundBeMoreSpecific"));
113: targetContext.put("NeededCorrection", UtilXml
114: .childElementValue(addressRootElement,
115: "NeededCorrection"));
116:
117: targetContext.put("DeliveryAddress", UtilXml.childElementValue(
118: addressRootElement, "DeliveryAddress"));
119: targetContext.put("City", UtilXml.childElementValue(
120: addressRootElement, "City"));
121: targetContext.put("StateAbbrev", UtilXml.childElementValue(
122: addressRootElement, "StateAbbrev"));
123: targetContext.put("ZipCode", UtilXml.childElementValue(
124: addressRootElement, "ZipCode"));
125: targetContext.put("County", UtilXml.childElementValue(
126: addressRootElement, "County"));
127: targetContext.put("CountyNum", UtilXml.childElementValue(
128: addressRootElement, "CountyNum"));
129: targetContext.put("PreferredCityName", UtilXml
130: .childElementValue(addressRootElement,
131: "PreferredCityName"));
132:
133: targetContext.put("DeliveryPoint", UtilXml.childElementValue(
134: addressRootElement, "DeliveryPoint"));
135: targetContext.put("CheckDigit", UtilXml.childElementValue(
136: addressRootElement, "CheckDigit"));
137:
138: targetContext.put("CSKey", UtilXml.childElementValue(
139: addressRootElement, "CSKey"));
140: targetContext.put("FIPS", UtilXml.childElementValue(
141: addressRootElement, "FIPS"));
142:
143: targetContext.put("FromLongitude", UtilXml.childElementValue(
144: addressRootElement, "FromLongitude"));
145: targetContext.put("FromLatitude", UtilXml.childElementValue(
146: addressRootElement, "FromLatitude"));
147: targetContext.put("ToLongitude", UtilXml.childElementValue(
148: addressRootElement, "ToLongitude"));
149: targetContext.put("ToLatitude", UtilXml.childElementValue(
150: addressRootElement, "ToLatitude"));
151: targetContext.put("AvgLongitude", UtilXml.childElementValue(
152: addressRootElement, "AvgLongitude"));
153: targetContext.put("AvgLatitude", UtilXml.childElementValue(
154: addressRootElement, "AvgLatitude"));
155:
156: targetContext.put("CMSA", UtilXml.childElementValue(
157: addressRootElement, "CMSA"));
158: targetContext.put("PMSA", UtilXml.childElementValue(
159: addressRootElement, "PMSA"));
160: targetContext.put("MSA", UtilXml.childElementValue(
161: addressRootElement, "MSA"));
162: targetContext.put("MA", UtilXml.childElementValue(
163: addressRootElement, "MA"));
164:
165: targetContext.put("TimeZone", UtilXml.childElementValue(
166: addressRootElement, "TimeZone"));
167: targetContext.put("hasDaylightSavings", UtilXml
168: .childElementValue(addressRootElement,
169: "hasDaylightSavings"));
170: targetContext.put("AreaCode", UtilXml.childElementValue(
171: addressRootElement, "AreaCode"));
172: targetContext.put("LLCertainty", UtilXml.childElementValue(
173: addressRootElement, "LLCertainty"));
174:
175: targetContext.put("CensusBlockNum", UtilXml.childElementValue(
176: addressRootElement, "CensusBlockNum"));
177: targetContext.put("CensusTractNum", UtilXml.childElementValue(
178: addressRootElement, "CensusTractNum"));
179:
180: /*
181: Example URL: http://ws.cdyne.com/psaddress/addresslookup.asmx/ReturnCityState?zipcode=93940&LicenseKey=0
182: NOTE: 0 is a test LicenseKey
183:
184: Example Response:
185: <?xml version="1.0" encoding="utf-8"?>
186: <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ws.cdyne.com/">
187: <ServiceError>false</ServiceError>
188: <AddressError>false</AddressError>
189: <AddressFoundBeMoreSpecific>false</AddressFoundBeMoreSpecific>
190: <NeededCorrection>true</NeededCorrection>
191:
192: <DeliveryAddress>**UNKNOWN**</DeliveryAddress>
193: <City>DEL REY OAKS</City>
194: <StateAbbrev>CA</StateAbbrev>
195: <ZipCode>93940</ZipCode>
196: <County>MONTEREY</County>
197: <CountyNum>0</CountyNum>
198: <PreferredCityName>MONTEREY</PreferredCityName>
199:
200: <DeliveryPoint>99</DeliveryPoint>
201: <CheckDigit>0</CheckDigit>
202:
203: <CSKey>Z20854</CSKey>
204: <FIPS>06053</FIPS>
205:
206: <FromLongitude>-121.919965</FromLongitude>
207: <FromLatitude>36.362864</FromLatitude>
208: <ToLongitude>-121.647022</ToLongitude>
209: <ToLatitude>36.652645</ToLatitude>
210: <AvgLongitude>-121.7834935</AvgLongitude>
211: <AvgLatitude>36.5077545</AvgLatitude>
212:
213: <CMSA>7120</CMSA>
214: <PMSA />
215: <MSA>7120</MSA>
216: <MA>712</MA>
217:
218: <TimeZone>PST</TimeZone>
219: <hasDaylightSavings>true</hasDaylightSavings>
220: <AreaCode>831</AreaCode>
221: <LLCertainty>90</LLCertainty>
222:
223: <CensusBlockNum>9003</CensusBlockNum>
224: <CensusTractNum>0134.00</CensusTractNum>
225: </Address>
226: */
227: }
228: }
|