예제 #1
0
  private Bundle fetchCourse(Bundle bundle, String input) {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    try {
      bundle.putBoolean("valid_return", false);

      // get schedule JSONBObject
      String schedule_url = Constants.getScheduleURL(input);
      JSONObject schedulesObject = Constants.getJSONObject(schedule_url);

      // check valid data return
      if (!schedulesObject
          .getJSONObject("meta")
          .getString("message")
          .equals("Request successful")) {
        Log.d("SearchFetchTask", schedulesObject.toString());
        return bundle;
      }
      bundle.putBoolean("valid_return", true);
      JSONArray data = schedulesObject.getJSONArray("data");

      for (int i = 0; i < data.length(); i++) {
        JSONObject section = data.getJSONObject(i);
        JSONObject first_class = section.getJSONArray("classes").getJSONObject(0);
        // Log.d("SearchFetchTask",first_class.toString());
        JSONObject date = first_class.getJSONObject("date");
        JSONObject loc = first_class.getJSONObject("location");

        String section_str = section.getString("section");
        String section_type = section_str.substring(0, 3);
        String section_num = section_str.substring(section_str.length() - 3);
        if (section_num.equals("081")) bundle.putBoolean("isOnline", true);
        Log.d("SearchFetchTask", "section_type is <" + section_type + ">");
        CoursesDBHandler db = new CoursesDBHandler(mActivity);
        if (section_type.equals("LEC")) {
          String prof = first_class.getJSONArray("instructors").getString(0);

          // check if is in db
          if (db.IsInDB(section.getString("class_number"))) {
            bundle.putBoolean("course_taken", true);
            bundle.putString("course_taken_num", section.getString("class_number"));
          }
          LEC_NUM.add(section.getString("class_number"));
          LEC_SEC.add(section_str.substring(4));
          LEC_TIME.add(
              date.getString("weekdays")
                  + " "
                  + date.getString("start_time")
                  + "-"
                  + date.getString("end_time"));
          LEC_PROF.add(prof);
          LEC_LOC.add(loc.getString("building") + " " + loc.getString("room"));
          LEC_CAPACITY.add(section.getString("enrollment_capacity"));
          LEC_TOTAL.add(section.getString("enrollment_total"));
        } else if (section_type.equals("TST")) {
          bundle.putBoolean("has_tst", true);
          TST_SEC.add(section_str.substring(4));
          TST_TIME.add(
              date.getString("start_date")
                  + " "
                  + date.getString("start_time")
                  + "-"
                  + date.getString("end_time"));
          TST_CAPACITY.add(section.getString("enrollment_capacity"));
          TST_TOTAL.add(section.getString("enrollment_total"));
        } else if (section_type.equals("TUT")) {
          bundle.putBoolean("has_tut", true);
          TUT_NUM.add(section.getString("class_number"));
          TUT_SEC.add(section_str.substring(4));
          TUT_TIME.add(
              date.getString("weekdays")
                  + " "
                  + date.getString("start_time")
                  + "-"
                  + date.getString("end_time"));
          TUT_LOC.add(loc.getString("building") + " " + loc.getString("room"));
          TUT_CAPACITY.add(section.getString("enrollment_capacity"));
          TUT_TOTAL.add(section.getString("enrollment_total"));
        } else if (section_type.equals("LAB")) {
          bundle.putBoolean("has_lab", true);
        }
        // Log.d("SubjectFetchTask", "miao");
      }

      // String title = data.getString("title");
      bundle.putString("title", data.getJSONObject(0).getString("title"));
      bundle.putString(
          "courseName",
          data.getJSONObject(0).getString("subject")
              + " "
              + data.getJSONObject(0).getString("catalog_number"));
      bundle.putStringArrayList("LEC_NUM", LEC_NUM);
      bundle.putStringArrayList("LEC_SEC", LEC_SEC);
      bundle.putStringArrayList("LEC_TIME", LEC_TIME);
      bundle.putStringArrayList("LEC_PROF", LEC_PROF);
      bundle.putStringArrayList("LEC_LOC", LEC_LOC);
      bundle.putStringArrayList("LEC_CAPACITY", LEC_CAPACITY);
      bundle.putStringArrayList("LEC_TOTAL", LEC_TOTAL);

      bundle.putStringArrayList("TUT_NUM", TUT_NUM);
      bundle.putStringArrayList("TUT_SEC", TUT_SEC);
      bundle.putStringArrayList("TUT_TIME", TUT_TIME);
      bundle.putStringArrayList("TUT_LOC", TUT_LOC);
      bundle.putStringArrayList("TUT_CAPACITY", TUT_CAPACITY);
      bundle.putStringArrayList("TUT_TOTAL", TUT_TOTAL);

      bundle.putStringArrayList("TST_SEC", TST_SEC);
      bundle.putStringArrayList("TST_TIME", TST_TIME);
      bundle.putStringArrayList("TST_CAPACITY", TST_CAPACITY);
      bundle.putStringArrayList("TST_TOTAL", TST_TOTAL);

      // knowing at least a lec, can fetch term now
      String term = data.getJSONObject(0).getString("term");
      // get exam JSONBObject
      String exam_url = Constants.getExamsURL(term);
      JSONArray examData = Constants.getJSONObject(exam_url).getJSONArray("data");
      for (int i = 0; i < examData.length(); i++) {
        if (examData.getJSONObject(i).getString("course").equals(bundle.getString("courseName"))) {
          bundle.putBoolean("has_finals", true);
          JSONArray exam_sections = examData.getJSONObject(i).getJSONArray("sections");
          for (int j = 0; j < exam_sections.length(); j++) {
            FINAL_SEC.add(exam_sections.getJSONObject(j).getString("section"));
            FINAL_TIME.add(
                exam_sections.getJSONObject(j).getString("start_time")
                    + "-"
                    + exam_sections.getJSONObject(j).getString("end_time"));
            FINAL_LOC.add(exam_sections.getJSONObject(j).getString("location"));
            FINAL_DATE.add(exam_sections.getJSONObject(j).getString("date"));
          }
        }
      }
      bundle.putStringArrayList("FINAL_SEC", FINAL_SEC);
      bundle.putStringArrayList("FINAL_LOC", FINAL_LOC);
      bundle.putStringArrayList("FINAL_TIME", FINAL_TIME);
      bundle.putStringArrayList("FINAL_DATE", FINAL_DATE);

      return bundle;

    } catch (Exception e) {
      e.printStackTrace();
    }

    return bundle;
  }