package kr.ac.kaist.resl.lilliput.util; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.restfb.types.Location; import com.restfb.types.NamedFacebookType; import com.restfb.types.Page; import com.restfb.types.User; import com.restfb.types.User.Education; public class JSONHelper { public JSONHelper() { } public JSONArray getJSONFromSTIS(Document result) { return null; } public JSONObject getJSONFromPlace(Page objectPage,String epc) { try { JSONObject jsonObject = new JSONObject(); if( objectPage.getId() != null ) jsonObject.put("fid", objectPage.getId()); if( objectPage.getCategory() != null ) jsonObject.put("category", objectPage.getCategory()); if( objectPage.getDescription() != null ) jsonObject.put("description", objectPage.getDescription()); if( objectPage.getFounded() != null ) jsonObject.put("foundedDate", objectPage.getFounded()); if( objectPage.getLink() != null ) jsonObject.put("link", objectPage.getLink()); if( objectPage.getLocation() != null ) { JSONObject jsonLoc = new JSONObject(); Location objLoc = objectPage.getLocation(); if( objLoc.getCity() != null ) jsonLoc.put("city", objLoc.getCity()); if( objLoc.getCountry() != null ) jsonLoc.put("country", objLoc.getCountry()); if( objLoc.getLatitude() != null ) jsonLoc.put("latitude", objLoc.getLatitude()); if( objLoc.getLongitude() != null ) jsonLoc.put("longitude", objLoc.getLongitude()); if( objLoc.getState() != null ) jsonLoc.put("state", objLoc.getState()); if( objLoc.getZip() != null ) jsonLoc.put("zip", objLoc.getZip()); jsonObject.put("location", objLoc); } jsonObject.put("epc", epc); if( objectPage.getPicture() != null ) jsonObject.put("picture", objectPage.getPicture()); if( objectPage.getProducts() != null ) jsonObject.put("products", objectPage.getProducts()); if( objectPage.getUsername() != null ) jsonObject.put("userName", objectPage.getUsername()); return jsonObject; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public JSONObject getJSONFromPlace(Page objectPage) { try { JSONObject jsonObject = new JSONObject(); if( objectPage.getId() != null ) jsonObject.put("fid", objectPage.getId()); if( objectPage.getCategory() != null ) jsonObject.put("category", objectPage.getCategory()); if( objectPage.getDescription() != null ) jsonObject.put("description", objectPage.getDescription()); if( objectPage.getFounded() != null ) jsonObject.put("foundedDate", objectPage.getFounded()); if( objectPage.getLink() != null ) jsonObject.put("link", objectPage.getLink()); if( objectPage.getLocation() != null ) { JSONObject jsonLoc = new JSONObject(); Location objLoc = objectPage.getLocation(); if( objLoc.getCity() != null ) jsonLoc.put("city", objLoc.getCity()); if( objLoc.getCountry() != null ) jsonLoc.put("country", objLoc.getCountry()); if( objLoc.getLatitude() != null ) jsonLoc.put("latitude", objLoc.getLatitude()); if( objLoc.getLongitude() != null ) jsonLoc.put("longitude", objLoc.getLongitude()); if( objLoc.getState() != null ) jsonLoc.put("state", objLoc.getState()); if( objLoc.getZip() != null ) jsonLoc.put("zip", objLoc.getZip()); jsonObject.put("location", objLoc); } if( objectPage.getName() != null ) jsonObject.put("epc", objectPage.getName().toLowerCase()); if( objectPage.getPicture() != null ) jsonObject.put("picture", objectPage.getPicture()); if( objectPage.getProducts() != null ) jsonObject.put("products", objectPage.getProducts()); if( objectPage.getUsername() != null ) jsonObject.put("userName", objectPage.getUsername()); return jsonObject; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public JSONObject getJSONFromObject(Page objectPage) { try { JSONObject jsonObject = new JSONObject(); if( objectPage.getId() != null ) jsonObject.put("fid", objectPage.getId()); if( objectPage.getCategory() != null ) jsonObject.put("category", objectPage.getCategory()); if( objectPage.getDescription() != null ) jsonObject.put("description", objectPage.getDescription()); if( objectPage.getFounded() != null ) jsonObject.put("foundedDate", objectPage.getFounded()); if( objectPage.getLink() != null ) jsonObject.put("link", objectPage.getLink()); if( objectPage.getLocation() != null ) { JSONObject jsonLoc = new JSONObject(); Location objLoc = objectPage.getLocation(); if( objLoc.getCity() != null ) jsonLoc.put("city", objLoc.getCity()); if( objLoc.getCountry() != null ) jsonLoc.put("country", objLoc.getCountry()); if( objLoc.getLatitude() != null ) jsonLoc.put("latitude", objLoc.getLatitude()); if( objLoc.getLongitude() != null ) jsonLoc.put("longitude", objLoc.getLongitude()); if( objLoc.getState() != null ) jsonLoc.put("state", objLoc.getState()); if( objLoc.getZip() != null ) jsonLoc.put("zip", objLoc.getZip()); jsonObject.put("location", objLoc); } if( objectPage.getName() != null ) jsonObject.put("name", objectPage.getName()); if( objectPage.getPicture() != null ) jsonObject.put("picture", objectPage.getPicture()); if( objectPage.getProducts() != null ) jsonObject.put("epc", objectPage.getProducts()); if( objectPage.getUsername() != null ) jsonObject.put("userName", objectPage.getUsername()); return jsonObject; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public JSONObject getJSONFromObject(Page objectPage, String epc) { try { JSONObject jsonObject = new JSONObject(); if( objectPage.getId() != null ) jsonObject.put("fid", objectPage.getId()); if( objectPage.getCategory() != null ) jsonObject.put("category", objectPage.getCategory()); if( objectPage.getDescription() != null ) jsonObject.put("description", objectPage.getDescription()); if( objectPage.getFounded() != null ) jsonObject.put("foundedDate", objectPage.getFounded()); if( objectPage.getLink() != null ) jsonObject.put("link", objectPage.getLink()); if( objectPage.getLocation() != null ) { JSONObject jsonLoc = new JSONObject(); Location objLoc = objectPage.getLocation(); if( objLoc.getCity() != null ) jsonLoc.put("city", objLoc.getCity()); if( objLoc.getCountry() != null ) jsonLoc.put("country", objLoc.getCountry()); if( objLoc.getLatitude() != null ) jsonLoc.put("latitude", objLoc.getLatitude()); if( objLoc.getLongitude() != null ) jsonLoc.put("longitude", objLoc.getLongitude()); if( objLoc.getState() != null ) jsonLoc.put("state", objLoc.getState()); if( objLoc.getZip() != null ) jsonLoc.put("zip", objLoc.getZip()); jsonObject.put("location", objLoc); } if( objectPage.getName() != null ) jsonObject.put("name", objectPage.getName()); if( objectPage.getPicture() != null ) jsonObject.put("picture", objectPage.getPicture()); jsonObject.put("epc", epc); if( objectPage.getUsername() != null ) jsonObject.put("userName", objectPage.getUsername()); return jsonObject; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public JSONObject getJSONFromUser(User user) { try { JSONObject jsonUser = new JSONObject(); if( user.getAbout() != null ) jsonUser.put("about", user.getAbout()); if( user.getBio() != null ) jsonUser.put("bio", user.getBio()); if( user.getAbout() != null )jsonUser.put("birthday", user.getBirthday()); if( user.getEducation() != null ) { List<Education> userEducation = user.getEducation(); JSONArray eduArray = new JSONArray(); for(int i = 0 ; i < userEducation.size() ; i ++ ) { JSONObject eduObject = new JSONObject(); Education edu = userEducation.get(i); if( edu.getYear() != null ) { NamedFacebookType eduYear = edu.getYear(); eduObject.put("year", eduYear.getName()); } if( edu.getSchool() != null ) { NamedFacebookType eduSchool = edu.getSchool(); eduObject.put("school", eduSchool.getName()); } eduObject.put("type", edu.getType()); eduArray.put(eduObject); } jsonUser.put("education", eduArray); } if( user.getEmail() != null ) jsonUser.put("email", user.getEmail()); if( user.getGender() != null ) jsonUser.put("gender", user.getGender()); if( user.getId() != null ) jsonUser.put("fid", user.getId()); if( user.getLink() != null ) jsonUser.put("link", user.getLink()); if( user.getName() != null ) jsonUser.put("name", user.getName()); if( user.getUpdatedTime() != null ) jsonUser.put("updatedTime", user.getUpdatedTime().toString()); return jsonUser; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public List<JSONObject> getJSONFromEPC(Document result) { try { List<JSONObject> returnEvent = new ArrayList<JSONObject>(); JSONObject event; //Retrieve ObjectEvents NodeList objectEvents = result.getElementsByTagName("ObjectEvent"); for(int i = 0 ; i < objectEvents.getLength() ; i ++ ) { event = new JSONObject(); //Iterating objectEvents Element objectEvent = (Element)objectEvents.item(i); //eventTime Node eventTime = objectEvent.getElementsByTagName("eventTime").item(0); String eventTimeStr = eventTime.getTextContent(); event.put("eventTime", eventTimeStr); Node epcList = objectEvent.getElementsByTagName("epcList").item(0); NodeList epcs = epcList.getChildNodes(); JSONArray epcJSONs = new JSONArray(); for(int j = 0 ; j < epcs.getLength() ; j++ ) { //EPC JSONObject epcJSON = new JSONObject(); Node epcNode = epcs.item(j); String epcString = epcNode.getTextContent(); System.out.println(epcString); epcJSON.put("epc", epcString); epcJSONs.put(epcJSON); } event.put("epcList", epcJSONs); Node bizLocation = objectEvent.getElementsByTagName("bizLocation").item(0); NodeList bizLocIDs = bizLocation.getChildNodes(); JSONArray bizLocJSONs = new JSONArray(); for(int j = 0 ; j < bizLocIDs.getLength() ; j++ ) { //Business Location JSONObject bizIDJSON = new JSONObject(); Node bizID = bizLocIDs.item(j); String bizLocationID = bizID.getTextContent(); bizIDJSON.put("id", bizLocationID); bizLocJSONs.put(bizIDJSON); } event.put("bizLocation", bizLocJSONs); returnEvent.add(event); } return returnEvent; } catch(JSONException e) { return null; } } }