Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
  private Event castJSONObjToEvent(JSONObject jsonObj) {
    Event event = new Event();
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);

    try {
      String startTime = jsonObj.get("startTime").toString();
      String endTime = jsonObj.get("endTime").toString();

      event.setName(jsonObj.getString("name"));
      event.setDescription(jsonObj.getString("description"));
      event.setCategory(getCategory(jsonObj));
      event.setStatus(getStatus(jsonObj));
      event.setLocation(jsonObj.getString("location"));

      if (event.getCategory().equals(GenericEvent.Category.FLOATING)) {
        // doesn't set start time and end time
      } else {
        event.setStartTime(df.parse(startTime));
        event.setEndTime(df.parse(endTime));
      }

    } catch (JSONException e2) {
      e2.printStackTrace();
    } catch (ParseException e1) {
      e1.printStackTrace();
    }

    return event;
  }
Ejemplo n.º 3
0
  public State readStorage(String fileName) {
    String line = null;
    State state = new State();
    String jsonData = "";

    try {
      FileReader fr = new FileReader(fileName);
      BufferedReader br = new BufferedReader(fr);

      while ((line = br.readLine()) != null) {
        jsonData += line + "\n";
      }

      if (jsonData.contains("{")) {
        JSONObject object = new JSONObject(jsonData);

        JSONArray arrCompleted = object.getJSONArray("completed");
        JSONArray arrIncompleted = object.getJSONArray("incompleted");
        JSONArray arrUndetermined = object.getJSONArray("undetermined");
        JSONArray arrReserved = object.getJSONArray("reserved");

        for (int i = 0; i < arrCompleted.length(); i++) {
          Event e = castJSONObjToEvent(arrCompleted.getJSONObject(i));
          state.completedEvents.add(e);
        }
        for (int i = 0; i < arrIncompleted.length(); i++) {
          Event e = castJSONObjToEvent(arrIncompleted.getJSONObject(i));
          state.incompletedEvents.add(e);
        }
        for (int i = 0; i < arrUndetermined.length(); i++) {
          ReservedEvent e = castJSONObjToFloatingEvent(arrUndetermined.getJSONObject(i));
          state.undeterminedEvents.add(e);
        }
        for (int i = 0; i < arrReserved.length(); i++) {
          ReservedEvent e = castJSONObjToReservedEvent(arrReserved.getJSONObject(i));
          state.reservedEvents.add(e);
        }
      }
    } catch (FileNotFoundException ex) {
      System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
      ex.printStackTrace();
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    updatedDisplayedEvents(state);

    return state;
  }
Ejemplo n.º 4
0
  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();
    }
  }
Ejemplo n.º 5
0
  private ReservedEvent castJSONObjToFloatingEvent(JSONObject jsonObj) {
    ReservedEvent event = new ReservedEvent();
    try {
      event.setName(jsonObj.getString("name"));
      event.setDescription(jsonObj.getString("description"));
      event.setCategory(getCategory(jsonObj));
      event.setStatus(getStatus(jsonObj));
      event.setLocation(jsonObj.getString("location"));

    } catch (JSONException e2) {
      e2.printStackTrace();
    }

    return event;
  }
Ejemplo n.º 6
0
  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;
  }
Ejemplo n.º 7
0
  private ReservedEvent castJSONObjToReservedEvent(JSONObject jsonObj) {
    ReservedEvent event = new ReservedEvent();
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    int max = 100;
    int count = 0;

    try {
      event.setName(jsonObj.getString("name"));
      event.setDescription(jsonObj.getString("description"));
      event.setCategory(getCategory(jsonObj));
      event.setStatus(getStatus(jsonObj));
      event.setLocation(jsonObj.getString("location"));

      for (int i = 0; i < 100; i++) {
        try {
          jsonObj.getString("startTime" + i);
        } catch (JSONException e) {
          count = i;
          break;
        }
      }

      ArrayList<TimePair> reservedTimes = new ArrayList<TimePair>();
      for (int i = 0; i < count; i++) {
        TimePair t =
            new TimePair(
                df.parse(jsonObj.get("startTime" + i).toString()),
                df.parse(jsonObj.get("endTime" + i).toString()));
        reservedTimes.add(t);
      }

      event.setReservedTimes(reservedTimes);

    } catch (JSONException e2) {
      e2.printStackTrace();
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return event;
  }
Ejemplo n.º 8
0
  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;
  }