private JSONObject castEventToJSONObj(Event event) { JSONObject jsonObj = new JSONObject(); DateFormat dfDeadline = new SimpleDateFormat("yyyy-M-dd"); Date deadLine = null; try { deadLine = dfDeadline.parse("1970-01-01"); } catch (ParseException e) { e.printStackTrace(); } try { jsonObj.put("name", event.getName()); jsonObj.put("description", event.getDescription()); jsonObj.put("category", event.getCategory()); jsonObj.put("status", event.getStatus()); jsonObj.put("location", event.getLocation()); if (event.getCategory().equals(GenericEvent.Category.DEADLINE)) { jsonObj.put("startTime", deadLine); jsonObj.put("endTime", event.getEndTime()); } else { jsonObj.put("startTime", event.getStartTime()); jsonObj.put("endTime", event.getEndTime()); } } catch (JSONException e) { e.printStackTrace(); } return jsonObj; }
public void stateToStorage(State completeState, String fileName) { clearFile(fileName); JSONArray arrCompleted = new JSONArray(); JSONArray arrIncompleted = new JSONArray(); JSONArray arrUndetermined = new JSONArray(); JSONArray arrReserved = new JSONArray(); for (Event e : completeState.completedEvents) { JSONObject object = new JSONObject(); object = castEventToJSONObj(e); arrCompleted.put(object); } for (Event e : completeState.incompletedEvents) { JSONObject object = new JSONObject(); object = castEventToJSONObj(e); arrIncompleted.put(object); } for (ReservedEvent e : completeState.undeterminedEvents) { JSONObject object = new JSONObject(); object = castFloatingEventToJSONObj(e); arrUndetermined.put(object); } for (ReservedEvent e : completeState.reservedEvents) { JSONObject object = new JSONObject(); object = castReservedEventToJSONObj(e); arrReserved.put(object); } JSONObject o = new JSONObject(); try { o.put("completed", arrCompleted); o.put("incompleted", arrIncompleted); o.put("undetermined", arrUndetermined); o.put("reserved", arrReserved); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { FileWriter fw = new FileWriter(fileName, true); PrintWriter pw = new PrintWriter(fw); pw.println(o.toString()); pw.close(); } catch (IOException ex) { ex.printStackTrace(); } }
private JSONObject castFloatingEventToJSONObj(ReservedEvent event) { JSONObject jsonObj = new JSONObject(); try { jsonObj.put("name", event.getName()); jsonObj.put("description", event.getDescription()); jsonObj.put("category", event.getCategory()); jsonObj.put("status", event.getStatus()); jsonObj.put("location", event.getLocation()); jsonObj.put("startTime", ""); jsonObj.put("endTime", ""); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj; }
private JSONObject castReservedEventToJSONObj(ReservedEvent event) { JSONObject jsonObj = new JSONObject(); try { jsonObj.put("name", event.getName()); jsonObj.put("description", event.getDescription()); jsonObj.put("category", event.getCategory()); jsonObj.put("status", event.getStatus()); jsonObj.put("location", event.getLocation()); int count = event.getReservedTimes().size(); for (int i = 0; i < count; i++) { jsonObj.put("startTime" + i, event.getReservedTimes().get(i).getStartTime()); jsonObj.put("endTime" + i, event.getReservedTimes().get(i).getEndTime()); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj; }