Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
14 kB
3
Indexable
Never
JSONObject jsonObject = new JSONObject();
        JSONArray changesArray = new JSONArray();
        JSONObject beforeObj = new JSONObject();
        JSONObject afterObj = new JSONObject();
        try {
            jsonObject.put("ticketSerial", oldTicket.getSerial());
            jsonObject.put("ticketId", oldTicket.getId());

            // System field
            // Subject
            String newSubject = newTicket.getSubject();
            String oldSubject = oldTicket.getSubject();
            if (!newSubject.equals(oldSubject)) {
                putChangesSystemObject(changesArray, "subject", newSubject, oldSubject);
                isChange = true;
            }
            beforeObj.put("subject", oldSubject);
            afterObj.put("subject", newSubject);

            // Assignee
            Group newGroupAssignee = newTicket.getAssigneeGroup();
            Group oldGroupAssignee = oldTicket.getAssigneeGroup();
            Agent newAgentAssignee = newTicket.getAssignee();
            Agent oldAgentAssignee = oldTicket.getAssignee();
            JSONArray newAssignee = new JSONArray();
            JSONArray oldAssignee = new JSONArray();
            if (oldGroupAssignee == null) {
                if (newGroupAssignee != null) {
                    newAssignee.put(newGroupAssignee.getId());
                    if (newAgentAssignee != null) {
                        newAssignee.put(newAgentAssignee.getUserId());
                    }
                    putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                    isChange = true;
                }
            } else {
                oldAssignee.put(oldGroupAssignee.getId());
                if (newGroupAssignee != null) {
                    newAssignee.put(newGroupAssignee.getId());
                    if (oldGroupAssignee.getId().equals(newGroupAssignee.getId())) {
                        if (oldAgentAssignee != null && newAgentAssignee != null) {
                            oldAssignee.put(oldAgentAssignee.getUserId());
                            newAssignee.put(newAgentAssignee.getUserId());
                            if (!oldAgentAssignee.getUserId().equals(newAgentAssignee.getUserId())) {
                                putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                                isChange = true;
                            }
                        } else {
                            if (oldAgentAssignee != null) {
                                oldAssignee.put(oldAgentAssignee.getUserId());
                                putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                                isChange = true;
                            }
                            if (newAgentAssignee != null) {
                                newAssignee.put(newAgentAssignee.getUserId());
                                putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                                isChange = true;
                            }
                        }
                    } else {
                        if (oldAgentAssignee != null) {
                            oldAssignee.put(oldAgentAssignee.getUserId());
                        }
                        if (newAgentAssignee != null) {
                            newAssignee.put(newAgentAssignee.getUserId());
                        }
                        putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                        isChange = true;
                    }
                } else {
                    if (oldAgentAssignee != null) {
                        oldAssignee.put(oldAgentAssignee.getUserId());
                    }
                    putChangesSystemObject(changesArray, "assignee", newAssignee, oldAssignee);
                    isChange = true;
                }
            }
            beforeObj.put("assignee", oldAssignee);
            afterObj.put("assignee", newAssignee);

            // Status
            int oldStatus = oldTicket.getStatus();
            int newStatus = newTicket.getStatus();
            if (oldStatus != newStatus) {
                putChangesSystemObject(changesArray, "status", newStatus, oldStatus);
                isChange = true;
            }
            beforeObj.put("status", oldStatus);
            afterObj.put("status", newStatus);

            // Type
            int oldType = oldTicket.getType();
            int newType = newTicket.getType();
            if (oldType != newType) {
                putChangesSystemObject(changesArray, "type", newType, oldType);
                isChange = true;
            }
            beforeObj.put("type", oldType);
            afterObj.put("type", newType);

            // Priority
            int oldPriority = oldTicket.getPriority();
            int newPriority = newTicket.getPriority();
            if (oldPriority != newPriority) {
                putChangesSystemObject(changesArray, "priority", newPriority, oldPriority);
                isChange = true;
            }
            beforeObj.put("priority", oldPriority);
            afterObj.put("priority", newPriority);

            // Ccs
            JSONArray oldCcsArray = new JSONArray();
            JSONArray newCcsArray = new JSONArray();
            List<String> oldIds = new ArrayList<>();
            if (!Utils.isListEmpty(oldTicket.getCcList())) {
                for (Agent agent : oldTicket.getCcList()) {
                    oldIds.add(agent.getUserId());
                }
                oldCcsArray = new JSONArray(oldIds);
            }
            List<String> newIds = new ArrayList<>();
            if (!Utils.isListEmpty(newTicket.getCcList())) {
                for (Agent agent : newTicket.getCcList()) {
                    newIds.add(agent.getUserId());
                }
                newCcsArray = new JSONArray(newIds);
            }
            if (!Utils.isListEmpty(oldIds) && !Utils.isListEmpty(newIds)) {
                if (oldIds.size() == newIds.size()) {
                    boolean isDifferent = false;
                    for (int i = 0; i < oldIds.size(); i++) {
                        String oldId = oldIds.get(i);
                        if (!newIds.contains(oldId)) {
                            isDifferent = true;
                            break;
                        }
                    }
                    if (isDifferent) {
                        putChangesSystemObject(changesArray, "ccs", newCcsArray, oldCcsArray);
                        isChange = true;
                    }
                } else {
                    putChangesSystemObject(changesArray, "ccs", newCcsArray, oldCcsArray);
                    isChange = true;
                }
            } else {
                if (!Utils.isListEmpty(oldIds) || !Utils.isListEmpty(newIds)) {
                    putChangesSystemObject(changesArray, "ccs", newCcsArray, oldCcsArray);
                    isChange = true;
                }
            }
            beforeObj.put("ccs", oldCcsArray);
            afterObj.put("ccs", newCcsArray);

            // CustomField
            List<Field> customFieldList = Utils.getListFromStringJSON(PrefUtils.getInstance(apiController.getContext()).getString(Constant.PREF_TICKET_FIELD, "[]"), Field.class);
            if (!Utils.isListEmpty(customFieldList)) {
                for (Field field : customFieldList) {
                    String oldValue = Utils.isStringEmpty(oldTicket.getFieldValueMap().get(field.getFieldKey())) ? null : oldTicket.getFieldValueMap().get(field.getFieldKey());
                    String newValue = Utils.isStringEmpty(newTicket.getFieldValueMap().get(field.getFieldKey())) ? null : newTicket.getFieldValueMap().get(field.getFieldKey());
                    JSONArray oldArray = new JSONArray();
                    JSONArray newArray = new JSONArray();
                    if (field.getFieldType() == Field.FieldType.MULTI_SELECT || field.getFieldType() == Field.FieldType.CASCADER) {
                        if (!Utils.isStringEmpty(oldValue)) {
                            oldArray = new JSONArray(oldValue);
                        }
                        if (!Utils.isStringEmpty(newValue)) {
                            newArray = new JSONArray(newValue);
                        }
                    }
                    boolean isDifferent = false;
                    if (!Utils.isStringEmpty(oldValue) && !Utils.isStringEmpty(newValue)) {
                        if (field.getFieldType() == Field.FieldType.MULTI_SELECT || field.getFieldType() == Field.FieldType.CASCADER) {
                            List<String> oldList = new ArrayList<>();
                            for (int i = 0; i < oldArray.length(); i++) {
                                oldList.add(oldArray.getString(i));
                            }
                            List<String> newList = new ArrayList<>();
                            for (int i = 0; i < newArray.length(); i++) {
                                newList.add(newArray.getString(i));
                            }
                            if (oldList.size() == newList.size()) {
                                for (int i = 0; i < oldArray.length(); i++) {
                                    String oldId = oldArray.getString(i);
                                    if (!newList.contains(oldId)) {
                                        isDifferent = true;
                                        break;
                                    }
                                }
                            } else {
                                isDifferent = true;
                            }
                        } else {
                            if (!oldValue.equals(newValue)) {
                                isDifferent = true;
                            }
                        }
                    } else {
                        if (field.getFieldType() == Field.FieldType.MULTI_SELECT || field.getFieldType() == Field.FieldType.CASCADER) {
                            if (!Utils.isArrayEmpty(oldArray) || !Utils.isArrayEmpty(newArray)) {
                                isDifferent = true;
                            }
                        } else {
                            if (!Utils.isStringEmpty(oldValue) || !Utils.isStringEmpty(newValue)) {
                                isDifferent = true;
                            }
                        }
                    }
                    if (isDifferent) {
                        isChange = true;
                    }

                    if (field.getFieldType() == Field.FieldType.DATE) {
                        if (isDifferent) {
                            putCustomObject(changesArray, field.getFieldName(), field.getFieldKey(), Utils.isStringEmpty(newValue) ? JSONObject.NULL : Long.parseLong(newValue));
                            putCustomObject(afterObj, field.getFieldName(), field.getFieldKey(), Utils.isStringEmpty(newValue) ? JSONObject.NULL : Long.parseLong(newValue));
                        }
                        if (!Utils.isStringEmpty(oldValue)) {
                            Object time = JSONObject.NULL;
                            try {
                                time = Utils.isStringEmpty(oldValue) ? 0 : Long.parseLong(oldValue);
                            } catch (NumberFormatException exception) {
                                exception.printStackTrace();
                            }
                            putCustomObject(beforeObj, field.getFieldName(), field.getFieldKey(), time);
                        }
                    } else if (field.getFieldType() == Field.FieldType.CHECK_BOX) {
                        if (isDifferent) {
                            putCustomObject(changesArray, field.getFieldName(), field.getFieldKey(), Utils.isStringEmpty(newValue) ? JSONObject.NULL : Boolean.parseBoolean(newValue));
                            putCustomObject(afterObj, field.getFieldName(), field.getFieldKey(), Utils.isStringEmpty(newValue) ? JSONObject.NULL : Boolean.parseBoolean(newValue));
                        }
                        if (!Utils.isStringEmpty(oldValue)) {
                            putCustomObject(beforeObj, field.getFieldName(), field.getFieldKey(), Boolean.parseBoolean(oldValue));
                        }
                    } else if (field.getFieldType() == Field.FieldType.MULTI_SELECT || field.getFieldType() == Field.FieldType.CASCADER) {
                        if (isDifferent) {
                            putCustomObject(changesArray, field.getFieldName(), field.getFieldKey(), field.getFieldType() == Field.FieldType.CASCADER ? newArray.toString() : newArray);
                            putCustomObject(afterObj, field.getFieldName(), field.getFieldKey(), field.getFieldType() == Field.FieldType.CASCADER ? newArray.toString() : newArray);
                        }
                        if (!Utils.isArrayEmpty(oldArray)) {
                            putCustomObject(beforeObj, field.getFieldName(), field.getFieldKey(), field.getFieldType() == Field.FieldType.CASCADER ? oldArray.toString() : oldArray);
                        }
                    } else {
                        if (isDifferent) {
                            putCustomObject(changesArray, field.getFieldName(), field.getFieldKey(), newValue);
                            putCustomObject(afterObj, field.getFieldName(), field.getFieldKey(), Utils.isStringEmpty(newValue) ? JSONObject.NULL : newValue);
                        }
                        if (!Utils.isStringEmpty(oldValue)) {
                            putCustomObject(beforeObj, field.getFieldName(), field.getFieldKey(), oldValue);
                        }
                    }
                }
            }
            jsonObject.put("changes", changesArray);
            jsonObject.put("before", beforeObj);
            jsonObject.put("after", afterObj);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        if (!isChange) {
            return;
        }