Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.core;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Set;
- import org.jleaf.util.GsonUtil;
- import org.json.simple.JSONArray;
- /**
- * This class represent internal system exception that occurs in Core
- * Application Framework. This exception is independent to business rules.
- *
- * @author Ali Irawan
- * @version 1.0
- * @see org.jleaf.core.CoreExceptionConstants
- */
- public class CoreException extends RuntimeException {
- private static final long serialVersionUID = -7142974010179649962L;
- private String errorKey;
- private Object[] params;
- private Dto errorParamDto;
- private static final String PREFIX_ERROR_PARAM_DTO_LIST_KEY = "_";
- /**
- * Construct core exception
- */
- public CoreException(String errorKey) {
- this(errorKey, null, (Object[]) null);
- }
- /**
- * Constructor for creating exception object
- *
- * @param errorKey
- * one of enumeration value of ErrorConstants
- * @param params
- * additional parameters
- */
- public CoreException(String errorKey, Object... params) {
- this(errorKey, null, params);
- }
- public CoreException(String errorKey, Dto errorParamDto, Object... params) {
- this.errorKey = errorKey;
- this.params = params;
- checkErrorParamDto(errorParamDto);
- this.errorParamDto = errorParamDto;
- }
- /**
- * Get system error key
- *
- * @return a unique key to identify error
- */
- public String getErrorKey() {
- return this.errorKey;
- }
- /**
- * Get list of additional parameters value
- *
- * @return array of string contains additional parameters information
- */
- public Object[] getParamValues() {
- return this.params;
- }
- /**
- * Get dto of error list
- *
- * @return errorParamDto contains detail list for error
- */
- public Dto getErrorParamDto() {
- return this.errorParamDto;
- }
- /**
- * Convert to string
- */
- @Override
- public String toString() {
- if (params == null) {
- if (errorParamDto == null) {
- return "Error key : " + errorKey + " with params null and errorParamDto null";
- } else {
- return "Error key : " + errorKey + " with params null and errorParamDto:" + errorParamDto.toString();
- }
- } else {
- if (errorParamDto == null) {
- return "Error key : " + errorKey + " with params " + Arrays.asList(params);
- } else {
- return "Error key : " + errorKey + " with params " + Arrays.asList(params) + " and errorParamDto:"
- + errorParamDto.toString();
- }
- }
- }
- /**
- * Convert to JSON string
- */
- public String toJsonString() {
- return GsonUtil.toJsonString(this);
- }
- /**
- * checking for errorParamDto.
- * standard format for errorParamDto
- * {
- * "xx":[
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"}
- * ],
- * "_xx":[
- * {"key":"header1"},
- * {"key":"header2"}
- * ],
- * "yy":[
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"},
- * {"header1":"value1", "header2":"value2"}
- * ],
- * "_yy":[
- * {"key":"header1"},
- * {"key":"header2"}
- * ]
- * }
- *
- * @param errorParamDto
- */
- @SuppressWarnings("unchecked")
- private void checkErrorParamDto(Dto errorParamDto) {
- if (errorParamDto != null) {
- JSONArray keyColumnNotFound = new JSONArray();
- JSONArray keyNotList = new JSONArray();
- Set<String> keys = errorParamDto.keys();
- for (String key : keys) {
- if (!key.startsWith(PREFIX_ERROR_PARAM_DTO_LIST_KEY)) {
- if (!errorParamDto.containsKey(PREFIX_ERROR_PARAM_DTO_LIST_KEY + key)) {
- keyColumnNotFound.add(key);
- } else {
- if (!(errorParamDto.get(key) instanceof List)) {
- keyNotList.add(key);
- }
- }
- }
- }
- if (keyColumnNotFound.size() != 0 || keyNotList.size() != 0) {
- throw new InfrastructureException(InfrastructureExceptionConstants.INVALID_ERROR_PARAM_DTO, keyColumnNotFound,
- keyNotList);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement