public void populateFromJsObject(JSONObject cateObj) throws JSONException, STDException {
    if (cateObj == null) {
      Logger.w(TAG, "Input result obj should NOT be NULL");
      return;
    }

    _id = cateObj.getInt(TAGNAME_ACCESS_CATE_ID);
    _name = cateObj.getString(TAGNAME_ACCESS_CATE_NAME);
    if (cateObj.has(TAGNAME_APPLICATION_TYPES)) {
      // TODO: populate app types and ids
      // managedAppTypes = cateObj.getString(TAGNAME_APPLICATION_TYPES);
    }

    if (cateObj.has(TAGNAME_ACCESS_RULES) == true) {
      JSONArray rulesAry = cateObj.getJSONArray(TAGNAME_ACCESS_RULES);
      for (int m = 0; m < rulesAry.length(); m++) {
        JSONObject ruleObj = rulesAry.getJSONObject(m);

        AccessRule aRule = new AccessRule();
        aRule.setAccessType(ruleObj.getInt(TAGNAME_RULE_AUTH_TYPE));
        Recurrence recur = Recurrence.getInstance(ruleObj.getInt(TAGNAME_RULE_REPEAT_TYPE));
        if (recur.getRecurType() != Recurrence.DAILY) {
          recur.setRecurValue(ruleObj.getInt(TAGNAME_RULE_REPEAT_VALUE));
        }
        aRule.setRecurrence(recur);

        JSONArray timerangeAry = ruleObj.getJSONArray(TAGNAME_ACCESS_TIMERANGES);
        for (int k = 0; k < timerangeAry.length(); k++) {
          JSONObject trObj = timerangeAry.getJSONObject(k);

          TimeRange tr = new TimeRange();
          String time = trObj.getString(TAGNAME_RULE_REPEAT_STARTTIME);
          int idx = time.indexOf(':');
          int hour = Integer.parseInt(time.substring(0, idx));
          int min = Integer.parseInt(time.substring(idx + 1));
          tr.setTime(TimeRange.TIME_TYPE_START, hour, min);

          time = trObj.getString(TAGNAME_RULE_REPEAT_ENDTIME);
          idx = time.indexOf(':');
          hour = Integer.parseInt(time.substring(0, idx));
          min = Integer.parseInt(time.substring(idx + 1));
          tr.setTime(TimeRange.TIME_TYPE_END, hour, min);

          aRule.addTimeRange(tr);
        } // for time_ranges

        addAccessRule(aRule);
      } // for rules
    } // if
  } // populate
  public JSONObject toJsonObject() throws JSONException {
    JSONArray jsonRulesAry = new JSONArray();
    if (_rulesList != null) {
      for (int k = 0; k < _rulesList.size(); k++) {
        AccessRule aRule = _rulesList.get(k);

        JSONObject jsonRuleObj = new JSONObject();
        jsonRuleObj.put(TAGNAME_RULE_AUTH_TYPE, aRule.getAccessType());
        jsonRuleObj.put(TAGNAME_RULE_REPEAT_TYPE, aRule.getRecurType());
        jsonRuleObj.put(TAGNAME_RULE_REPEAT_VALUE, aRule.getRecurrence().getRecurValue());

        JSONArray jsonTrsAry = new JSONArray();
        List<TimeRange> trsList = aRule.getTimeRangeList();
        for (TimeRange tr : trsList) {
          JSONObject jsonTrObj = new JSONObject();
          jsonTrObj.put(TAGNAME_RULE_REPEAT_STARTTIME, tr.getStartTime().toString());
          jsonTrObj.put(TAGNAME_RULE_REPEAT_ENDTIME, tr.getEndTime().toString());

          jsonTrsAry.put(jsonTrObj);
        }

        jsonRuleObj.put(TAGNAME_ACCESS_TIMERANGES, jsonTrsAry);
        jsonRulesAry.put(jsonRuleObj);
      }
    }

    JSONObject result = new JSONObject();

    result.put(TAGNAME_ACCESS_CATE_ID, _id);
    result.put(TAGNAME_ACCESS_CATE_NAME, _name);

    String managedAppTypesStr = "";
    if (managedAppTypesIdSet != null && managedAppTypesIdSet.size() > 0) {
      for (Integer appTypeId : managedAppTypesIdSet) {
        managedAppTypesStr += DataManager.getInstance().getAppTypeName(appTypeId);
      }
    }
    result.put(TAGNAME_APPLICATION_TYPES, managedAppTypesStr);

    result.put(TAGNAME_ACCESS_RULES, jsonRulesAry);

    return result;
  }
  @Override
  public String toString() {
    StringBuffer buff = new StringBuffer();
    buff.append("\nCate ID: " + _id).append("\tCate Name: " + _name);
    for (AccessRule rule : _rulesList) {
      buff.append("\nRule auth type: " + rule.getAccessType())
          .append("\nRule recur type: " + rule.getRecurrence().getName())
          .append("\tRecur value: " + rule.getRecurrence().getRecurValue());

      for (TimeRange tr : rule.getTimeRangeList()) {
        buff.append("\nStart Time: " + tr.getStartTime().toString())
            .append("\tEnd Time: " + tr.getEndTime().toString());
      }
    }

    for (ClientAppInfo appInfo : _managedAppsMap.keySet()) {
      buff.append("\nManaged App: " + appInfo.getAppName())
          .append(", " + appInfo.getAppPkgname())
          .append(", " + appInfo.getAppClassname());
    }

    return buff.toString();
  }
 public void addAccessRule(AccessRule rule) {
   if (rule != null) {
     rule.setAdhereCategory(this);
     _rulesList.add(rule);
   }
 }