V1_0.java [src/java/m/wqm/rfactor] Revision: 91f7ddb2d664f3272f689562ba3e1ec48a9b23f5 Date: Fri Aug 07 02:44:02 MDT 2015
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package m.wqm.rfactor;
import csip.ModelDataService;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import javax.ws.rs.Path;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author Sandeep
*/
@Name("WQM-12")
@Description("R Factor for an Area of Analysis")
@Path("m/wqm/rfactor/1.0")
public class V1_0 extends ModelDataService{
private final String USER = "postgres";
private final String PASS = "admin";
private final String HOST = "csip.engr.colostate.edu";
private final String PORT = "5435";
private final String DBNAME = "r2gis";
private final String SERVER = "localhost:5432/postgres";
private final String JDBC_TYPE = "jdbc:postgresql://";
private final String CLASS_NAME = "org.postgresql.Driver";
private final String CONNECTION = JDBC_TYPE + HOST + ":" + PORT + "/" + DBNAME;
private String aoaId;
private JSONArray polygon,coordinates;
private String rFactor;
String points,finalpoints="";
JSONArray aoaArr;
ArrayList<AoA> list;
ArrayList<String> rfactorList;
ArrayList<Double> temp;
@Override
public void preProcess() throws ServiceException, JSONException
{
list=new ArrayList<AoA>();
rfactorList= new ArrayList<String>();
aoaArr = getJSONArrayParam("aoas");
for(int i = 0; i < aoaArr.length(); i++) {
//Map individual JSONObject & extract values
Map<String, JSONObject> thisAoA = JSONUtils.preprocess(aoaArr.getJSONArray(i));
aoaId=JSONUtils.getStringParam(thisAoA,"aoa_id","unknown");
polygon=JSONUtils.getJSONArrayParam(thisAoA,"aoa_geometry");
coordinates=polygon.getJSONArray(0);
points=coordinates.toString();
points=points.replace("[","");
points=points.replace("]","");
points=points.replace(","," ");
String temp[] = points.split(" ");
finalpoints="";
finalpoints+=temp[0];
for(int j=1;j<temp.length;j++)
{
if(j%2==0)
finalpoints+=","+temp[j];
else
finalpoints+=" "+temp[j];
}
list.add(new AoA(aoaId,finalpoints));
}
}
@Override
public String process()
{
Connection tempCon=null;
Statement stmt=null;
try {
Class.forName(CLASS_NAME);
tempCon=DriverManager.getConnection(CONNECTION,"postgres","admin");
tempCon.setAutoCommit(false);
stmt=tempCon.createStatement();
// geometryPoint=geometryPoint.replace(",", " ");
for(int i=0;i<list.size();i++)
{
String points= list.get(i).getCoordinates();
String gisQuery="SELECT m.co_fips, r2_path, r2_name, m.ei_rang, st_area(st_intersection(ST_PolygonFromText('POLYGON(("+points+"))'), geometry)) / st_area(ST_PolygonFromText('POLYGON(("+points+"))'))*100 as percentAoi,st_area(st_intersection(ST_PolygonFromText('POLYGON(("+points+"))'), geometry))/43560 as sizeAoi FROM cli_geom AS g, map_climates AS m WHERE ST_Intersects(ST_PolygonFromText('POLYGON(("+points+"))'), g.geometry) AND g.co_fips = m.co_fips and st_isvalid(geometry)='t' and st_isvalid(ST_PolygonFromText('POLYGON(("+points+"))'))='t';";
//stmt.execute(gisQuery);
//statement.execute(query1);
// statement.execute("create table temp;");
ResultSet results = stmt.executeQuery(gisQuery);
String urlName=null;
while(results.next())
{
//System.out.println(results.getString("co_fips"));
String path=results.getString("r2_path");
String county=results.getString("r2_name");
urlName="http://csip.engr.colostate.edu/r2/"+path.replace(" ","%20")+"/"+county.replace(" ","%20")+".xml";
//System.out.println(results.getString("r2_path"));
//System.out.println(results.getString("r2_name"));
urlName=urlName.replace("\\","/");
// System.out.println(urlName);
rFactor=getAndProcessXml(urlName);
rfactorList.add(rFactor);
}
list.get(i).setRFactor(rfactorList);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return EXEC_OK;
}
@Override
public void postProcess() throws JSONException
{
JSONArray finalArr= new JSONArray();
for(int i=0;i<list.size();i++){
JSONArray resultArr = new JSONArray();
temp=new ArrayList<Double>();
for(int j=0;j<list.get(i).getRFactor().size();j++)
temp.add(Double.parseDouble(list.get(i).getRFactor().get(j)));
Collections.sort(temp);
resultArr.put(JSONUtils.dataDesc("AoAId",list.get(i).getAoAId(),"Area of Analysis Identifier"));
resultArr.put(JSONUtils.dataDesc("RFactor",temp.get(temp.size()-1),"R Factor"));
finalArr.put(resultArr);
}
putResult("",finalArr);
}
public String getAndProcessXml(String url) throws ParserConfigurationException, MalformedURLException, SAXException, IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
URL Url = new URL(url);
URLConnection conn = Url.openConnection();
Document document = db.parse(conn.getInputStream());
document.getDocumentElement().normalize();
Element root=document.getDocumentElement();
NodeList nList= document.getElementsByTagName("Flt");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(temp);
// System.out.println(""); //Just a separator
if (node.getNodeType() == Node.ELEMENT_NODE)
{
//Print each employee's detail
Element eElement = (Element) node;
String Name=eElement.getElementsByTagName("Name").item(0).getTextContent();
if(Name.equals("R_FACTOR"))
{
return eElement.getElementsByTagName("Data").item(0).getTextContent();
}
}
}
return null;
}
}