Search in sources :

Example 1 with JSONArray

use of org.camunda.bpm.engine.impl.util.json.JSONArray in project camunda-bpm-platform by camunda.

the class JsonUtil method jsonArrayAsList.

/**
 * Converts a {@link JSONArray} to a {@link List}. It supports nested {@link JSONObject}
 * and {@link JSONArray}.
 *
 * @param jsonArray the json array to convert
 * @return the resulting map
 */
public static List<Object> jsonArrayAsList(JSONArray jsonArray) {
    if (jsonArray == null) {
        return null;
    } else {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < jsonArray.length(); i++) {
            Object value = optJavaNull(jsonArray.get(i));
            if (JSONObject.class.isInstance(value)) {
                value = jsonObjectAsMap(JSONObject.class.cast(value));
            } else if (JSONArray.class.isInstance(value)) {
                value = jsonArrayAsList(JSONArray.class.cast(value));
            }
            list.add(value);
        }
        return list;
    }
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.camunda.bpm.engine.impl.util.json.JSONArray) JSONObject(org.camunda.bpm.engine.impl.util.json.JSONObject)

Example 2 with JSONArray

use of org.camunda.bpm.engine.impl.util.json.JSONArray in project camunda-bpm-platform by camunda.

the class JsonTaskQueryConverter method toObject.

@Override
public TaskQuery toObject(JSONObject json) {
    TaskQueryImpl query = new TaskQueryImpl();
    if (json.has(OR_QUERIES)) {
        for (int i = 0; i < json.getJSONArray(OR_QUERIES).length(); i++) {
            query.addOrQuery((TaskQueryImpl) toObject(json.getJSONArray(OR_QUERIES).getJSONObject(i)));
        }
    }
    if (json.has(TASK_ID)) {
        query.taskId(json.getString(TASK_ID));
    }
    if (json.has(NAME)) {
        query.taskName(json.getString(NAME));
    }
    if (json.has(NAME_NOT_EQUAL)) {
        query.taskNameNotEqual(json.getString(NAME_NOT_EQUAL));
    }
    if (json.has(NAME_LIKE)) {
        query.taskNameLike(json.getString(NAME_LIKE));
    }
    if (json.has(NAME_NOT_LIKE)) {
        query.taskNameNotLike(json.getString(NAME_NOT_LIKE));
    }
    if (json.has(DESCRIPTION)) {
        query.taskDescription(json.getString(DESCRIPTION));
    }
    if (json.has(DESCRIPTION_LIKE)) {
        query.taskDescriptionLike(json.getString(DESCRIPTION_LIKE));
    }
    if (json.has(PRIORITY)) {
        query.taskPriority(json.getInt(PRIORITY));
    }
    if (json.has(MIN_PRIORITY)) {
        query.taskMinPriority(json.getInt(MIN_PRIORITY));
    }
    if (json.has(MAX_PRIORITY)) {
        query.taskMaxPriority(json.getInt(MAX_PRIORITY));
    }
    if (json.has(ASSIGNEE)) {
        query.taskAssignee(json.getString(ASSIGNEE));
    }
    if (json.has(ASSIGNEE_LIKE)) {
        query.taskAssigneeLike(json.getString(ASSIGNEE_LIKE));
    }
    if (json.has(INVOLVED_USER)) {
        query.taskInvolvedUser(json.getString(INVOLVED_USER));
    }
    if (json.has(OWNER)) {
        query.taskOwner(json.getString(OWNER));
    }
    if (json.has(ASSIGNED) && json.getBoolean(ASSIGNED)) {
        query.taskAssigned();
    }
    if (json.has(UNASSIGNED) && json.getBoolean(UNASSIGNED)) {
        query.taskUnassigned();
    }
    if (json.has(DELEGATION_STATE)) {
        query.taskDelegationState(DelegationState.valueOf(json.getString(DELEGATION_STATE)));
    }
    if (json.has(CANDIDATE_USER)) {
        query.taskCandidateUser(json.getString(CANDIDATE_USER));
    }
    if (json.has(CANDIDATE_GROUP)) {
        query.taskCandidateGroup(json.getString(CANDIDATE_GROUP));
    }
    if (json.has(CANDIDATE_GROUPS) && !json.has(CANDIDATE_USER) && !json.has(CANDIDATE_GROUP)) {
        query.taskCandidateGroupIn(getList(json.getJSONArray(CANDIDATE_GROUPS)));
    }
    if (json.has(WITH_CANDIDATE_GROUPS) && json.getBoolean(WITH_CANDIDATE_GROUPS)) {
        query.withCandidateGroups();
    }
    if (json.has(WITHOUT_CANDIDATE_GROUPS) && json.getBoolean(WITHOUT_CANDIDATE_GROUPS)) {
        query.withoutCandidateGroups();
    }
    if (json.has(WITH_CANDIDATE_USERS) && json.getBoolean(WITH_CANDIDATE_USERS)) {
        query.withCandidateUsers();
    }
    if (json.has(WITHOUT_CANDIDATE_USERS) && json.getBoolean(WITHOUT_CANDIDATE_USERS)) {
        query.withoutCandidateUsers();
    }
    if (json.has(INCLUDE_ASSIGNED_TASKS) && json.getBoolean(INCLUDE_ASSIGNED_TASKS)) {
        query.includeAssignedTasksInternal();
    }
    if (json.has(PROCESS_INSTANCE_ID)) {
        query.processInstanceId(json.getString(PROCESS_INSTANCE_ID));
    }
    if (json.has(EXECUTION_ID)) {
        query.executionId(json.getString(EXECUTION_ID));
    }
    if (json.has(ACTIVITY_INSTANCE_ID_IN)) {
        query.activityInstanceIdIn(getArray(json.getJSONArray(ACTIVITY_INSTANCE_ID_IN)));
    }
    if (json.has(CREATED)) {
        query.taskCreatedOn(new Date(json.getLong(CREATED)));
    }
    if (json.has(CREATED_BEFORE)) {
        query.taskCreatedBefore(new Date(json.getLong(CREATED_BEFORE)));
    }
    if (json.has(CREATED_AFTER)) {
        query.taskCreatedAfter(new Date(json.getLong(CREATED_AFTER)));
    }
    if (json.has(KEY)) {
        query.taskDefinitionKey(json.getString(KEY));
    }
    if (json.has(KEYS)) {
        query.taskDefinitionKeyIn(getArray(json.getJSONArray(KEYS)));
    }
    if (json.has(KEY_LIKE)) {
        query.taskDefinitionKeyLike(json.getString(KEY_LIKE));
    }
    if (json.has(PARENT_TASK_ID)) {
        query.taskParentTaskId(json.getString(PARENT_TASK_ID));
    }
    if (json.has(PROCESS_DEFINITION_KEY)) {
        query.processDefinitionKey(json.getString(PROCESS_DEFINITION_KEY));
    }
    if (json.has(PROCESS_DEFINITION_KEYS)) {
        query.processDefinitionKeyIn(getArray(json.getJSONArray(PROCESS_DEFINITION_KEYS)));
    }
    if (json.has(PROCESS_DEFINITION_ID)) {
        query.processDefinitionId(json.getString(PROCESS_DEFINITION_ID));
    }
    if (json.has(PROCESS_DEFINITION_NAME)) {
        query.processDefinitionName(json.getString(PROCESS_DEFINITION_NAME));
    }
    if (json.has(PROCESS_DEFINITION_NAME_LIKE)) {
        query.processDefinitionNameLike(json.getString(PROCESS_DEFINITION_NAME_LIKE));
    }
    if (json.has(PROCESS_INSTANCE_BUSINESS_KEY)) {
        query.processInstanceBusinessKey(json.getString(PROCESS_INSTANCE_BUSINESS_KEY));
    }
    if (json.has(PROCESS_INSTANCE_BUSINESS_KEYS)) {
        query.processInstanceBusinessKeyIn(getArray(json.getJSONArray(PROCESS_INSTANCE_BUSINESS_KEYS)));
    }
    if (json.has(PROCESS_INSTANCE_BUSINESS_KEY_LIKE)) {
        query.processInstanceBusinessKeyLike(json.getString(PROCESS_INSTANCE_BUSINESS_KEY_LIKE));
    }
    if (json.has(TASK_VARIABLES)) {
        addVariables(query, json.getJSONArray(TASK_VARIABLES), true, false);
    }
    if (json.has(PROCESS_VARIABLES)) {
        addVariables(query, json.getJSONArray(PROCESS_VARIABLES), false, true);
    }
    if (json.has(CASE_INSTANCE_VARIABLES)) {
        addVariables(query, json.getJSONArray(CASE_INSTANCE_VARIABLES), false, false);
    }
    if (json.has(DUE)) {
        query.dueDate(new Date(json.getLong(DUE)));
    }
    if (json.has(DUE_BEFORE)) {
        query.dueBefore(new Date(json.getLong(DUE_BEFORE)));
    }
    if (json.has(DUE_AFTER)) {
        query.dueAfter(new Date(json.getLong(DUE_AFTER)));
    }
    if (json.has(FOLLOW_UP)) {
        query.followUpDate(new Date(json.getLong(FOLLOW_UP)));
    }
    if (json.has(FOLLOW_UP_BEFORE)) {
        query.followUpBefore(new Date(json.getLong(FOLLOW_UP_BEFORE)));
    }
    if (json.has(FOLLOW_UP_AFTER)) {
        query.followUpAfter(new Date(json.getLong(FOLLOW_UP_AFTER)));
    }
    if (json.has(FOLLOW_UP_NULL_ACCEPTED)) {
        query.setFollowUpNullAccepted(json.getBoolean(FOLLOW_UP_NULL_ACCEPTED));
    }
    if (json.has(EXCLUDE_SUBTASKS) && json.getBoolean(EXCLUDE_SUBTASKS)) {
        query.excludeSubtasks();
    }
    if (json.has(SUSPENDED) && json.getBoolean(SUSPENDED)) {
        query.suspended();
    }
    if (json.has(ACTIVE) && json.getBoolean(ACTIVE)) {
        query.active();
    }
    if (json.has(CASE_DEFINITION_KEY)) {
        query.caseDefinitionKey(json.getString(CASE_DEFINITION_KEY));
    }
    if (json.has(CASE_DEFINITION_ID)) {
        query.caseDefinitionId(json.getString(CASE_DEFINITION_ID));
    }
    if (json.has(CASE_DEFINITION_NAME)) {
        query.caseDefinitionName(json.getString(CASE_DEFINITION_NAME));
    }
    if (json.has(CASE_DEFINITION_NAME_LIKE)) {
        query.caseDefinitionNameLike(json.getString(CASE_DEFINITION_NAME_LIKE));
    }
    if (json.has(CASE_INSTANCE_ID)) {
        query.caseInstanceId(json.getString(CASE_INSTANCE_ID));
    }
    if (json.has(CASE_INSTANCE_BUSINESS_KEY)) {
        query.caseInstanceBusinessKey(json.getString(CASE_INSTANCE_BUSINESS_KEY));
    }
    if (json.has(CASE_INSTANCE_BUSINESS_KEY_LIKE)) {
        query.caseInstanceBusinessKeyLike(json.getString(CASE_INSTANCE_BUSINESS_KEY_LIKE));
    }
    if (json.has(CASE_EXECUTION_ID)) {
        query.caseExecutionId(json.getString(CASE_EXECUTION_ID));
    }
    if (json.has(TENANT_IDS)) {
        query.tenantIdIn(getArray(json.getJSONArray(TENANT_IDS)));
    }
    if (json.has(WITHOUT_TENANT_ID)) {
        query.withoutTenantId();
    }
    if (json.has(ORDER_BY)) {
        List<QueryOrderingProperty> orderingProperties = JsonLegacyQueryOrderingPropertyConverter.INSTANCE.fromOrderByString(json.getString(ORDER_BY));
        query.setOrderingProperties(orderingProperties);
    }
    if (json.has(ORDERING_PROPERTIES)) {
        JSONArray jsonArray = json.getJSONArray(ORDERING_PROPERTIES);
        query.setOrderingProperties(JsonQueryOrderingPropertyConverter.ARRAY_CONVERTER.toObject(jsonArray));
    }
    // expressions
    Iterator jsonIterator = json.keys();
    while (jsonIterator.hasNext()) {
        String key = (String) jsonIterator.next();
        if (key.endsWith("Expression")) {
            String expression = json.getString(key);
            query.addExpression(key.substring(0, key.length() - "Expression".length()), expression);
        }
    }
    return query;
}
Also used : TaskQueryImpl(org.camunda.bpm.engine.impl.TaskQueryImpl) JSONArray(org.camunda.bpm.engine.impl.util.json.JSONArray) Iterator(java.util.Iterator) QueryOrderingProperty(org.camunda.bpm.engine.impl.QueryOrderingProperty) Date(java.util.Date)

Example 3 with JSONArray

use of org.camunda.bpm.engine.impl.util.json.JSONArray in project camunda-bpm-platform by camunda.

the class JsonQueryOrderingPropertyConverter method toJsonObject.

public JSONObject toJsonObject(QueryOrderingProperty property) {
    JSONObject jsonObject = new JSONObject();
    JsonUtil.addField(jsonObject, RELATION, property.getRelation());
    QueryProperty queryProperty = property.getQueryProperty();
    if (queryProperty != null) {
        JsonUtil.addField(jsonObject, QUERY_PROPERTY, queryProperty.getName());
        JsonUtil.addField(jsonObject, QUERY_PROPERTY_FUNCTION, queryProperty.getFunction());
    }
    Direction direction = property.getDirection();
    if (direction != null) {
        JsonUtil.addField(jsonObject, DIRECTION, direction.getName());
    }
    if (property.hasRelationConditions()) {
        JSONArray relationConditionsJson = JsonQueryFilteringPropertyConverter.ARRAY_CONVERTER.toJsonArray(property.getRelationConditions());
        JsonUtil.addField(jsonObject, RELATION_CONDITIONS, relationConditionsJson);
    }
    return jsonObject;
}
Also used : JSONObject(org.camunda.bpm.engine.impl.util.json.JSONObject) VariableInstanceQueryProperty(org.camunda.bpm.engine.impl.VariableInstanceQueryProperty) QueryProperty(org.camunda.bpm.engine.query.QueryProperty) JSONArray(org.camunda.bpm.engine.impl.util.json.JSONArray) Direction(org.camunda.bpm.engine.impl.Direction)

Example 4 with JSONArray

use of org.camunda.bpm.engine.impl.util.json.JSONArray in project camunda-bpm-platform by camunda.

the class JsonUtilTest method testJsonArrayToList.

@Test
@SuppressWarnings("unchecked")
public void testJsonArrayToList() {
    assertNull(jsonArrayAsList(null));
    JSONArray jsonArray = new JSONArray();
    List<Object> list = jsonArrayAsList(jsonArray);
    assertTrue(list.isEmpty());
    jsonArray.put(true);
    jsonArray.put(12);
    jsonArray.put(11.1);
    jsonArray.put(13l);
    jsonArray.put("test");
    jsonArray.put(Collections.singletonList("test"));
    jsonArray.put(Collections.singletonMap("test", "test"));
    jsonArray.put(new Date(0));
    jsonArray.put(JSONObject.NULL);
    list = jsonArrayAsList(jsonArray);
    assertEquals(9, list.size());
    assertEquals(true, list.get(0));
    assertEquals(12, list.get(1));
    assertEquals(11.1, list.get(2));
    assertEquals(13l, list.get(3));
    assertEquals("test", list.get(4));
    List<Object> embeddedList = (List<Object>) list.get(5);
    assertEquals(1, embeddedList.size());
    assertEquals("test", embeddedList.get(0));
    Map<String, Object> embeddedMap = (Map<String, Object>) list.get(6);
    assertEquals(1, embeddedMap.size());
    assertTrue(embeddedMap.containsKey("test"));
    assertEquals("test", embeddedMap.get("test"));
    Date embeddedDate = (Date) list.get(7);
    assertEquals(new Date(0), embeddedDate);
    Object null_ = list.get(8);
    assertNull(null_);
}
Also used : JSONArray(org.camunda.bpm.engine.impl.util.json.JSONArray) JSONObject(org.camunda.bpm.engine.impl.util.json.JSONObject) List(java.util.List) JsonUtil.jsonArrayAsList(org.camunda.bpm.engine.impl.util.JsonUtil.jsonArrayAsList) JsonUtil.jsonObjectAsMap(org.camunda.bpm.engine.impl.util.JsonUtil.jsonObjectAsMap) Map(java.util.Map) Date(java.util.Date) Test(org.junit.Test)

Example 5 with JSONArray

use of org.camunda.bpm.engine.impl.util.json.JSONArray in project camunda-bpm-platform by camunda.

the class JsonUtilTest method testJsonArrayNullRetained.

@Test
public void testJsonArrayNullRetained() {
    String json = "[null]";
    JSONArray array = new JSONArray(json);
    assertEquals(1, array.length());
    List<Object> list = jsonArrayAsList(array);
    assertEquals(1, list.size());
    assertNull(list.get(0));
}
Also used : JSONArray(org.camunda.bpm.engine.impl.util.json.JSONArray) JSONObject(org.camunda.bpm.engine.impl.util.json.JSONObject) Test(org.junit.Test)

Aggregations

JSONArray (org.camunda.bpm.engine.impl.util.json.JSONArray)9 JSONObject (org.camunda.bpm.engine.impl.util.json.JSONObject)7 Date (java.util.Date)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 TaskQueryImpl (org.camunda.bpm.engine.impl.TaskQueryImpl)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Direction (org.camunda.bpm.engine.impl.Direction)1 QueryOrderingProperty (org.camunda.bpm.engine.impl.QueryOrderingProperty)1 VariableInstanceQueryProperty (org.camunda.bpm.engine.impl.VariableInstanceQueryProperty)1 JsonUtil.jsonArrayAsList (org.camunda.bpm.engine.impl.util.JsonUtil.jsonArrayAsList)1 JsonUtil.jsonObjectAsMap (org.camunda.bpm.engine.impl.util.JsonUtil.jsonObjectAsMap)1 QueryProperty (org.camunda.bpm.engine.query.QueryProperty)1