Exemple #1
0
 /**
  * Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
  *
  * @param names A JSONArray containing a list of key strings. These will be paired with the
  *     values.
  * @return A JSONObject, or null if there are no names or if this JSONArray has no values.
  * @throws JSONException If any of the names are null.
  */
 public JSONObject toJSONObject(JSONArray names) throws JSONException {
   if (names == null || names.length() == 0 || length() == 0) {
     return null;
   }
   JSONObject jo = new JSONObject();
   for (int i = 0; i < names.length(); i += 1) {
     jo.put(names.getString(i), this.opt(i));
   }
   return jo;
 }
Exemple #2
0
  /**
   * Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is
   * added.
   *
   * <p>Warning: This method assumes that the signalData structure is acyclical.
   *
   * @return The writer.
   * @throws JSONException
   */
  public Writer write(Writer writer) throws JSONException {
    try {
      boolean b = false;
      int len = length();

      writer.write('[');

      for (int i = 0; i < len; i += 1) {
        if (b) {
          writer.write(',');
        }
        Object v = this.myArrayList.get(i);
        if (v instanceof JSONObject) {
          ((JSONObject) v).write(writer);
        } else if (v instanceof JSONArray) {
          ((JSONArray) v).write(writer);
        } else {
          writer.write(JSONObject.valueToString(v));
        }
        b = true;
      }
      writer.write(']');
      return writer;
    } catch (IOException e) {
      throw new JSONException(e);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see activitipoc.IFormHandler#parseResult(java.lang.String)
   */
  public FormResult parseResult(String data) {
    final Map<String, Object> parameters = new LinkedHashMap<String, Object>();
    final Map<String, Collection<String>> routes = new LinkedHashMap<String, Collection<String>>();

    JSONObject jObject = new JSONObject(data);
    Iterator<?> keys = jObject.keys();

    while (keys.hasNext()) {

      // TODO dangerous cast
      String key = (String) keys.next();

      // separate properties from roles
      if (key.startsWith(SINGLE_USER_KEY_PREFIX)) {
        routes.put(
            key.replaceFirst(SINGLE_USER_KEY_PREFIX, ""), Arrays.asList(jObject.getString(key)));
      } else if (key.startsWith(GROUP_USER_KEY_PREFIX)) {
        Set<String> users = new HashSet<String>();
        JSONArray usersJ = jObject.getJSONArray(key);
        for (int i = 0; i < usersJ.length(); i++) {
          users.add(usersJ.getString(i));
        }
        routes.put(key.replaceFirst(GROUP_USER_KEY_PREFIX, ""), users);
      } else {
        Object value = jObject.get(key);
        parameters.put(key, value);
      }
    }

    return new FormResult() {

      public Map<String, Collection<String>> getRolesToUsersMapping() {
        return routes;
      }

      public Map<String, Object> getProperties() {
        return parameters;
      }
    };
  }
  /**
   * Add users and roles to the string representation of a JSON msg.
   *
   * @param base
   * @param singleRoles
   * @param groupRoles
   * @param users
   * @return the base String with the necessary fields to add users and roles
   */
  private String concatUsersAndRoles(
      String base,
      Collection<String> singleRoles,
      Collection<String> groupRoles,
      Collection<String> users) {

    // insert roles to users mapping
    JSONObject res = new JSONObject(base);

    JSONArray userArray = new JSONArray(users);

    for (String role : singleRoles) {
      JSONObject o = new JSONObject();
      o.put("title", "Role: " + role);
      o.put("type", "string");
      o.put("enum", userArray);
      o.put("required", true);
      res.getJSONObject("schema").put(SINGLE_USER_KEY_PREFIX + role, o);
    }

    for (String role : groupRoles) {
      JSONObject items = new JSONObject();
      items.put("type", "string");
      items.put("enum", userArray);

      JSONObject o = new JSONObject();
      o.put("title", "Group Role: " + role);
      o.put("type", "array");
      o.put("items", items);
      o.put("minItems", 1);
      o.put("required", true);
      res.getJSONObject("schema").put(GROUP_USER_KEY_PREFIX + role, o);
    }

    JSONObject rolesForm = new JSONObject();
    rolesForm.put("type", "fieldset");
    rolesForm.put("title", "Roles Assignment");

    JSONArray items = new JSONArray();
    for (String role : singleRoles) {
      JSONObject o = new JSONObject();
      o.put("key", SINGLE_USER_KEY_PREFIX + role);
      items.put(o);
    }
    for (String role : groupRoles) {
      JSONObject o = new JSONObject();
      o.put("key", GROUP_USER_KEY_PREFIX + role);
      o.put("type", "checkboxes");
      items.put(o);
    }

    rolesForm.put("items", items);

    JSONArray form = res.getJSONArray("form");
    // remove this object and insert it back at the end
    Object submitButton = form.remove(form.length() - 1);
    form.put(rolesForm);
    form.put(submitButton);

    return res.toString();
  }