Skip to content

Commit

Permalink
- data request json edit (publicData)
Browse files Browse the repository at this point in the history
- Add log4
  • Loading branch information
engintruder committed Jul 9, 2015
1 parent 54f0107 commit 7e52842
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 287 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.openGDSMobileApplicationServer.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -25,214 +25,109 @@ public class PublicDataPortalServiceImp extends EgovAbstractServiceImpl implemen
@Autowired
@Qualifier("PortalDAO")
PublicData publicDataobj;

//Environment
String url="";

private String serviceName = null;
private String serviceURL = null;
private String[] resultJSONKeys = null;

Logger log = LogManager.getLogger("org.springframework");

@Override
public Object requestPublicData(JSONObject data) {
String serviceName = null;
Set<String> keys = data.keySet();
Iterator<String> it = keys.iterator();
String serviceURL, result = null;
public JSONObject requestPublicData(JSONObject data) {

Iterator<?> it = data.keySet().iterator();
String baseURL = null;
log.info(data);
while(it.hasNext()){
String tmp = it.next();
String tmp = (String) it.next();
if(tmp.equals("serviceName")){
serviceName = String.valueOf(data.get(tmp));
}
}
/**
* JSON Object
* {serviceName:"NuclearPowerPlantRealtimeLevelofRadiation", genName: ??, ServiceKey: ??}
**/
if(serviceName.equals("NuclearPowerPlantRealtimeLevelofRadiation")){
serviceURL=processNuclearPowerPlantRealtimeLevelofRadiationServiceURL(data);
baseURL = "http://www.khnp.co.kr/environ/service/realtime/radiorate?";

//Nuclear Power Plant Realtime Level of Radiation Request URL
serviceURL = processServiceURL(data, baseURL);
Document doc = publicDataobj.getXMLPublicData(serviceURL);
result = processNuclearPowerPlantRealtimeLevelofRadiationServiceData(data, doc);
return result;
}

resultJSONKeys = new String[]{"expl", "name", "time", "value"};
return processXmlbyPublicData(doc, resultJSONKeys);
}
/**
* JSON Object
* {serviceName:"GreenGasEmissionReport", startDate: ??, endDate: ??, keyValue: ??}
**/
else if(serviceName.equals("GreenGasEmissionReport")){
serviceURL=processGreenGasEmissionReportServiceURL(data);
baseURL = "http://www.kdhc.co.kr/openapi-data/service/kdhcCarbon/carbon?";
//Green Gas Emission Report Request URL
serviceURL = processServiceURL(data, baseURL);
Document doc = publicDataobj.getXMLPublicData(serviceURL);
result = processGreenGasEmissionReportServiceData(data, doc);
return result;

resultJSONKeys = new String[]{"basYm", "brNm", "tco2eqUnit", "totEmTco2eq"};
return processXmlbyPublicData(doc, resultJSONKeys);
}
/**
* JSON Object
* {serviceName:"ArpltnInforInqireSvc", sidoName: ??, numOfRows: ??, ServiceKey: ??,
* envType: ??}
**/
else if(serviceName.equals("ArpltnInforInqireSvc")){
serviceURL=processEnvironmentURL(data);
baseURL = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getCtprvnRltmMesureDnsty?";
serviceURL=processServiceURL(data, baseURL);

Document doc = publicDataobj.getXMLPublicData(serviceURL);
result = processEnvironmentData(data, doc);
return result;

resultJSONKeys = new String[]{"stationName", (String) data.get("envType") };
return processXmlbyPublicData(doc, resultJSONKeys);
}

return null;
}

//Nuclear Power Plant Realtime Level of Radiation Request URL
public String processNuclearPowerPlantRealtimeLevelofRadiationServiceURL(Map<String,Object> data){
String[] keys = {"NPP_name","keyValue"};
String[] keyValue=new String[]{"", ""};
Set<String> dataKeyNames = data.keySet();
Iterator<String> it = dataKeyNames.iterator();
while(it.hasNext()){
String tmp = it.next();
for(int i=0; i<keyValue.length; i++){
if(keys[i].equals(tmp)){
keyValue[i] = String.valueOf(data.get(tmp));
}
}
}
String portalEnvURL = "http://www.khnp.co.kr/environ/service/realtime/radiorate?genName="
+keyValue[0]+"&ServiceKey="+keyValue[1];
System.out.println(portalEnvURL);
return portalEnvURL;
}

//Nuclear Power Plant Realtime Level of Radiation Data(XML) Parsing :)
public String processNuclearPowerPlantRealtimeLevelofRadiationServiceData(Map<String,Object> data, Document doc){
Element root = doc.getRootElement();
String result = "{\"row\":[";

List<Element> bodyNodes = root.getChildren("body");
for(Element bodyNode : bodyNodes){
List<Element> itemsNodes = bodyNode.getChildren("items");
for(Element itemNode : itemsNodes){
List<Element> itemValues = itemNode.getChildren("item");
for(Element itemValue : itemValues){
List<Element> nodes = itemValue.getChildren();
for(Element node : nodes){
if(node.getName().equals("expl"))
result +="{\"expl\":\"" + node.getValue() + "\",";
if(node.getName().equals("name"))
result +="\"name\":\"" + node.getValue() + "\",";
if(node.getName().equals("time"))
result +="\"time\":\"" + node.getValue() + "\",";
if(node.getName().equals("value"))
result +="\"value\":\"" + node.getValue() + "\"},";
}
}
public String processServiceURL(JSONObject data,String baseURL){
String url = baseURL;
Iterator<?> it = data.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
if (key.equals("serviceName")){
continue;
}
url += key + "=" + data.get(key) + "&";
}

result = result.substring(0,result.length()-1);
result +="]}";
System.out.println(result);
return result;
log.info(url);
return url;
}

//Green Gas Emission Report Request URL
public String processGreenGasEmissionReportServiceURL(Map<String,Object> data){
String[] keys = {"startDate","endDate","keyValue"};
String[] keyValue=new String[]{"","",""};
Set<String> dataKeyNames = data.keySet();
Iterator<String> it = dataKeyNames.iterator();
while(it.hasNext()){
String tmp = it.next();
for(int i=0; i<keyValue.length; i++){
if(keys[i].equals(tmp)){
keyValue[i] = String.valueOf(data.get(tmp));
}
}
}

//http://www.kdhc.co.kr/openapi-data/service/kdhcCarbon/carbon?startDate=201007&endDate=201009&pageNo=1&rowOfNums=10&serviceKey=인증키
String portalEnvURL = "http://www.kdhc.co.kr/openapi-data/service/kdhcCarbon/carbon?startDate="
+keyValue[0]+"&endDate="+keyValue[1]+"&ServiceKey="+keyValue[2]+"&numOfRows=100";
System.out.println(portalEnvURL);
return portalEnvURL;
}

//Green Gas Emission Report Data(XML) Parsing :)
public String processGreenGasEmissionReportServiceData(Map<String,Object> data, Document doc){
Element root = doc.getRootElement();
String result = "{\"row\":[";
@SuppressWarnings("unchecked")
public JSONObject processXmlbyPublicData(Document src, String[] keys){
Element root = src.getRootElement();
JSONObject result = new JSONObject();
JSONArray list = new JSONArray();

List<Element> bodyNodes = root.getChildren("body");
for(Element bodyNode : bodyNodes){
List<Element> itemsNodes = bodyNode.getChildren("items");
for(Element itemNode : itemsNodes){
List<Element> itemValues = itemNode.getChildren("item");
for(Element itemValue : itemValues){
List<Element> nodes = itemValue.getChildren();
for(Element node : nodes){
if(node.getName().equals("basYm"))
result +="{\"basYm\":\"" + node.getValue() + "\",";
if(node.getName().equals("brNm"))
result +="\"brNm\":\"" + node.getValue() + "\",";
if(node.getName().equals("tco2eqUnit"))
result +="\"tco2eqUnit\":\"" + node.getValue() + "\",";
if(node.getName().equals("totEmTco2eq"))
result +="\"totEmTco2eq\":\"" + node.getValue() + "\"},";
List<Element> contents = itemNode.getChildren("item");
for (int i=0; i<contents.size(); i++){
JSONObject jsonObj = new JSONObject();
Element content = contents.get(i);
for (int j=0; j<keys.length; j++){
String contentKey = keys[j];
String contentValue = content.getChildText(keys[j]);
jsonObj.put(contentKey, contentValue);
}
list.add(jsonObj);
}
}
}

result = result.substring(0,result.length()-1);
result +="]}";
System.out.println(result);
result.put("row", list);
log.info(result);
return result;
}





public String processEnvironmentURL(Map<String,Object> data){
String[] keys = {"serviceName","areaType","keyValue","envType"};
String[] keyValue=new String[]{"","","",""};
Set<String> dataKeyNames = data.keySet();
Iterator<String> it = dataKeyNames.iterator();
while(it.hasNext()){
String tmp = it.next();
for(int i=0; i<keyValue.length; i++){
if(keys[i].equals(tmp)){
keyValue[i] = String.valueOf(data.get(tmp));
}
}
}
String portalEnvURL = "http://openapi.airkorea.or.kr/openapi/services/rest/"+
keyValue[0]+"/getCtprvnRltmMesureDnsty?sidoName="+keyValue[1]+
"&numOfRows=100&ServiceKey="+keyValue[2];
System.out.println(portalEnvURL);
return portalEnvURL;
}




public String processEnvironmentData(Map<String,Object> data, Document doc){
String[] keys = {"serviceName","areaType","keyValue","envType"};
String[] keyValue=new String[]{"","","",""};
Set<String> dataKeyNames = data.keySet();
Iterator<String> it = dataKeyNames.iterator();
while(it.hasNext()){
String tmp = it.next();
for(int i=0; i<keyValue.length; i++){
if(keys[i].equals(tmp)){
keyValue[i] = String.valueOf(data.get(tmp));
}
}
}
Element root = doc.getRootElement();
String result = "{\"row\":[";

List<Element> bodyNodes = root.getChildren("body");
for(Element bodyNode : bodyNodes){
List<Element> itemsNodes = bodyNode.getChildren("items");
for(Element itemNode : itemsNodes){
List<Element> itemValues = itemNode.getChildren("item");
for(Element itemValue : itemValues){
List<Element> nodes = itemValue.getChildren();
for(Element node : nodes){
if(node.getName().equals("stationName")){
result +="{\"stationName\":\""+node.getValue()+"\",";
}
if(node.getName().equals(keyValue[3])){
result +="\""+keyValue[3]+"\":\""+node.getValue().trim()+"\"},";

}
}
}
}
}
result = result.substring(0,result.length()-1);
result +="]}";
return result;
}

}
Loading

0 comments on commit 7e52842

Please sign in to comment.