package com.jiuqi.mobile.lbs.intf.pub;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import com.jqmobile.core.orm.DBColumn;
import com.jqmobile.core.orm.DBTable;
import com.jqmobile.core.utils.plain.BeanUtils;
import com.jqmobile.core.utils.plain.StringUtils;
import com.jqmobile.core.utils.xml.IXMLDocument;
import com.jqmobile.core.utils.xml.IXMLNode;
import com.jqmobile.core.utils.xml.impl.XMLFactory;
@DBTable(name = "S_LBS_GIS")
public class GisInfo {
@DBColumn(mapping=false)
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
@DBColumn(primaryId = true)
private String recid;
private String province;// ʡ��
private String city;// ����
private String code;// ����
private String country;// �ء���
private String poi;// ������־�Խ���
private String content;// �����
@DBColumn(length=1000)
private String xCross;// ����·��
@DBColumn(length=2000)
private String xmlStr;// xmlԭʼ�ı�
@DBColumn(length=1000)
private String str1;// ������ԭʼ�ı�
private double longitude;// ����
private double Latitude;// �
// private String lat;
// private String lon;
public GisInfo() {}
public GisInfo(double longitude2, double latitude2) {
this.Latitude = latitude2;
this.longitude = longitude2;
doRequest();
}
public String getRecid() {
return recid;
}
public void setRecid() {
this.recid = UUID.randomUUID().toString();
}
public void setRecid(String recid) {
this.recid = recid;
}
protected void doRequest() {
try {
long l = System.currentTimeMillis();
xmlStr = request(getUrl());
if(isEmpty(xmlStr, true)){
System.out.println(sdf.format(new Date())+":\t\t����ٶȣ��ߵ·���nullλ��");
toBaidu();
return;
}
System.out.println("���������ʱ��"+(System.currentTimeMillis()-l));
} catch (Exception e) {
// e.printStackTrace();
System.out.println(sdf.format(new Date())+":\t\t����ٶȣ��ߵ�����ʱλ��");
if(isEmpty(xmlStr, true)){
toBaidu();
return;
}
}
// try {
// xmlStr = request(getUrl());
// } catch (Exception e) {
// }
// System.out.println(xmlStr);
// gisInfo = reXML.resolveXml(lon, lat, responseMessageXml);// ����xml�ļ�
parseXml(xmlStr);
}
private void toBaidu() {
BaiduGisInfo info = new BaiduGisInfo(longitude, Latitude);
BeanUtils.getInstance().to(info, this);
}
public String getXmlStr() {
return xmlStr;
}
public void setXmlStr(String xmlStr) {
this.xmlStr = xmlStr;
}
private void parseXml(String xml) {
// if(null == xml || xml.isEmpty()){
// return;
// }
// ��������
String pro = "";// ʡ��
String cit = "";// ����
String code = "";// ����
String country = "";// ����
String poiResult = "";// ��־�Խ���
String crossResult = "";// ����·��
String content = "";
// if (responseMessageXml != null && !responseMessageXml.equals("")) {
// StringReader read = new StringReader(responseMessageXml);// ��ȡxml�ļ�
// System.out.println("��λ��ϢXML:-------------------->"
// + responseMessageXml);
// InputSource source = new InputSource(read);
// SAXBuilder sb = new SAXBuilder();
// Document doc = sb.build(source);
if(null != xml && !xml.isEmpty()){
IXMLDocument doc = XMLFactory.getByText(xml);
// ���ڵ�
if (doc != null) {
// IXMLNode root = doc.getRootElement();
IXMLNode root = doc.getRootNode();
String rootName = root.getNodeName();
if (rootName.equals("spatial_response")) {
// ȫ�ֽڵ�
IXMLNode spatialBean = root.getChilds("SpatialBean").get(0);
// ʡ��
IXMLNode province = spatialBean.getChilds("Province").get(0);
if (province.getNodeName().indexOf("Province") != -1) {
IXMLNode provinceName = province.getChilds("name").get(0);
if (provinceName.getNodeName().indexOf("name") != -1) {
pro = provinceName.getText();
}
}
// ����
IXMLNode city = spatialBean.getChilds("City").get(0);
if (city.getNodeName().indexOf("City") != -1) {
// ��������
IXMLNode cityName = city.getChilds("name").get(0);
if (cityName.getNodeName().indexOf("name") != -1) {
cit = cityName.getText();
// �������Ϊ�գ��ȼ���ʡ���磺�����С�������
if (cit.equals("") || cit == null) {
cit = pro;
}
}
// ��������
IXMLNode cityTelnum = city.getChilds("telnum").get(0);
if (cityTelnum.getNodeName().indexOf("telnum") != -1) {
code = cityTelnum.getText();
if (code.equals("") || code == null) {
code = "";
}
}
}
// ������
IXMLNode district = spatialBean.getChilds("District").get(0);
if (district.getNodeName().indexOf("District") != -1) {
// ��������
IXMLNode districtName = district.getChilds("name").get(0);
if (districtName.getNodeName().indexOf("name") != -1) {
country = districtName.getText();
// �������Ϊ��Ĭ����Ͻ��
if (country.equals("") || country == null) {
country = "��Ͻ��";
}
if (pro.equals("") || pro == null) {
country = "";
}
if (country != null && country.contains("��Դ")) {
cit = "��Դ��";
}
}
}
// ��־����
IXMLNode poiList = spatialBean.getChilds("poiList").get(0);
if (poiList.getNodeName().indexOf("poi") != -1) {
List<IXMLNode> poiLists = poiList.getChilds("poi");
for (int i = 0; i < poiLists.size(); i++) {
IXMLNode IXMLNode = (IXMLNode) poiLists.get(i);
String poiName = IXMLNode.getChilds("name").get(0).getText();
if (i == poiLists.size() - 1) {
poiResult += poiName;
} else {
poiResult += poiName + ",";
}
}
}
// ����·��
IXMLNode crossPoiList = spatialBean.getChilds("crossPoiList").get(0);
if (crossPoiList.getNodeName().indexOf("cross") != -1) {
List<IXMLNode> crossList = crossPoiList.getChilds("cross");
for (int i = 0; i < crossList.size(); i++) {
IXMLNode IXMLNode = (IXMLNode) crossList.get(0);
String crossName = IXMLNode.getChilds("name").get(0).getText();
String[] croName = crossName.split("-");
if (croName[0].equals(croName[1])) {
crossResult = croName[0];
} else {
crossResult = crossName;
}
}
}
// ����������
content = crossResult + "," + crossResult + "����";
if (crossResult == null || crossResult.equals("")) {
// ��������������
GisDaoImpl impl = new GisDaoImpl();
String simpleText = impl.getText1(longitude, Latitude);
if ("".equals(simpleText) || simpleText == null) {
content = crossResult + "������";
} else {
if (simpleText.trim().equals("����")) {
content = crossResult + simpleText;
} else {
content = simpleText;
}
}
}
// if (pro.equals("") || pro == null) {
// content = "����";
// }
} else {
System.out.println("����ߵ·���λ����Ϣ�쳣���ڵ�Ϊ--->" + rootName);
crossResult = "δ��ȡ��λ��������Ϣ��γ�ȣ�" + Latitude + "," + "���ȣ�" + longitude;
// crossResult = "���ȣ�" + lon;
content = crossResult + "����";
if (crossResult == null || crossResult.equals("")) {
// ��������������
GisDaoImpl impl = new GisDaoImpl();
String simpleText = impl.getText1(longitude, Latitude);
if ("".equals(simpleText) || simpleText == null) {
content = crossResult + "������";
} else {
if (simpleText.trim().equals("����")) {
content = crossResult + simpleText;
} else {
content = simpleText;
}
}
}
}
} else {
}
} else {
// crossResult = "γ�ȣ�" + lat;
// crossResult = "���ȣ�" + lon;
crossResult = "δ��ȡ��λ��������Ϣ��γ�ȣ�" + Latitude + "," + "���ȣ�" + longitude;
content = crossResult + "����";
if (crossResult == null || crossResult.equals("")) {
// ��������������
GisDaoImpl impl = new GisDaoImpl();
String simpleText = impl.getText1(longitude, Latitude);
if ("".equals(simpleText) || simpleText == null) {
content = crossResult + "������";
} else {
if (simpleText.trim().equals("����")) {
content = crossResult + simpleText;
} else {
content = simpleText;
}
}
}
}
// ����������
// ���浽gisInfo������
setProvince(pro);
setCity(cit);
setCode(code);
if (pro.equals("����ʡ") && cit.equals("������") && country.equals("?����")) {
setCountry("������");
} else {
setCountry(country);
}
setContent(content);
// gisInfo.setLatitude(Double.parseDouble(lat));
// gisInfo.setLongitude(Double.parseDouble(lon));
}
static String request(String strUrl) throws MalformedURLException,
IOException {
String responseMessageXml;
StringBuilder result = new StringBuilder();
URL serviceurl = new URL(strUrl);
URLConnection connection = serviceurl.openConnection();
connection.setReadTimeout(10000);
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
// result += line;
result.append(line);
result.append("\n");
}
in.close();
responseMessageXml = result.toString();
return responseMessageXml;
}
private String getUrl() {
StringBuffer xml = new StringBuffer();
xml.append("<?xml version=\"1.0\" encoding='UTF-8'?>\n");
xml.append("<spatial_request method=\"searchPoint\">\n");
xml.append("<x>" + longitude + "</x>\n");
xml.append("<y>" + Latitude + "</y>\n");
xml.append("<poiNumber>4</poiNumber>\n");
xml.append("<range>300</range>\n");
xml.append("<pattern>0</pattern>\n");
xml.append("<roadLevel>1</roadLevel>\n");
xml.append("</spatial_request>");
String xml_ = xml.toString();
try {
xml_ = URLEncoder.encode(xml.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// // GisInfo gisInfo = null;
// String responseMessageXml = null;// ���ص�xml����
String strUrl = "http://search1.mapabc.com/sisserver?&config=SPAS&enc=UTF-8&spatialXml="
+ xml_
+ "&a_k=b0a7db0b3a30f944a21c3682064dc70ef5b738b062f6479a5eca39725798b1ee300bd8d5de3a4ae3";
// String lon = String.valueOf(longitude);// ����
// String lat = String.valueOf(Latitude);// �
return strUrl;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return Latitude;
}
public void setLatitude(double latitude) {
Latitude = latitude;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getCross() {
return xCross;
}
public void setCross(String cross) {
this.xCross = cross;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPoi() {
return poi;
}
public void setPoi(String poi) {
this.poi = poi;
}
// public String getLat() {
// return lat;
// }
//
// public void setLat(String lat) {
// this.lat = lat;
// }
//
// public String getLon() {
// return lon;
// }
//
// public void setLon(String lon) {
// this.lon = lon;
// }
public static boolean isEmpty(String str, boolean trim) {
String value = str;
if (value == null) {
return true;
}
if (trim) {
value = value.trim();
}
if ("".equalsIgnoreCase(value)) {
return true;
}
int x = str.indexOf("<x>")+"<x>".length();
if (x<0) {
return true;
}
int y = str.indexOf("</x>");
if (y<0) {
return true;
}
CharSequence subSequence = str.subSequence(x, y);
if (!(subSequence.length()>0)) {
return true;
}
return false;
}
}