Advertisement
aadddrr

CoreException

Oct 9th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. package org.jleaf.core;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Set;
  6.  
  7. import org.jleaf.util.GsonUtil;
  8. import org.json.simple.JSONArray;
  9.  
  10. /**
  11.  * This class represent internal system exception that occurs in Core
  12.  * Application Framework. This exception is independent to business rules.
  13.  *
  14.  * @author Ali Irawan
  15.  * @version 1.0
  16.  * @see org.jleaf.core.CoreExceptionConstants
  17.  */
  18. public class CoreException extends RuntimeException {
  19.  
  20.     private static final long serialVersionUID = -7142974010179649962L;
  21.     private String errorKey;
  22.     private Object[] params;
  23.     private Dto errorParamDto;
  24.  
  25.     private static final String PREFIX_ERROR_PARAM_DTO_LIST_KEY = "_";
  26.  
  27.     /**
  28.      * Construct core exception
  29.      */
  30.     public CoreException(String errorKey) {
  31.         this(errorKey, null, (Object[]) null);
  32.     }
  33.  
  34.     /**
  35.      * Constructor for creating exception object
  36.      *
  37.      * @param errorKey
  38.      *            one of enumeration value of ErrorConstants
  39.      * @param params
  40.      *            additional parameters
  41.      */
  42.     public CoreException(String errorKey, Object... params) {
  43.         this(errorKey, null, params);
  44.     }
  45.  
  46.     public CoreException(String errorKey, Dto errorParamDto, Object... params) {
  47.         this.errorKey = errorKey;
  48.         this.params = params;
  49.  
  50.         checkErrorParamDto(errorParamDto);
  51.         this.errorParamDto = errorParamDto;
  52.     }
  53.  
  54.     /**
  55.      * Get system error key
  56.      *
  57.      * @return a unique key to identify error
  58.      */
  59.     public String getErrorKey() {
  60.         return this.errorKey;
  61.     }
  62.  
  63.     /**
  64.      * Get list of additional parameters value
  65.      *
  66.      * @return array of string contains additional parameters information
  67.      */
  68.     public Object[] getParamValues() {
  69.         return this.params;
  70.     }
  71.  
  72.     /**
  73.      * Get dto of error list
  74.      *
  75.      * @return errorParamDto contains detail list for error
  76.      */
  77.     public Dto getErrorParamDto() {
  78.         return this.errorParamDto;
  79.     }
  80.  
  81.     /**
  82.      * Convert to string
  83.      */
  84.     @Override
  85.     public String toString() {
  86.         if (params == null) {
  87.             if (errorParamDto == null) {
  88.                 return "Error key : " + errorKey + " with params null and errorParamDto null";
  89.             } else {
  90.                 return "Error key : " + errorKey + " with params null and errorParamDto:" + errorParamDto.toString();
  91.             }
  92.         } else {
  93.             if (errorParamDto == null) {
  94.                 return "Error key : " + errorKey + " with params " + Arrays.asList(params);
  95.             } else {
  96.                 return "Error key : " + errorKey + " with params " + Arrays.asList(params) + " and errorParamDto:"
  97.                         + errorParamDto.toString();
  98.  
  99.             }
  100.         }
  101.     }
  102.  
  103.     /**
  104.      * Convert to JSON string
  105.      */
  106.     public String toJsonString() {
  107.         return GsonUtil.toJsonString(this);
  108.     }
  109.  
  110.     /**
  111.      * checking for errorParamDto.
  112.      * standard format for errorParamDto
  113.      *  {
  114.      *      "xx":[
  115.      *          {"header1":"value1", "header2":"value2"},
  116.      *          {"header1":"value1", "header2":"value2"},
  117.      *          {"header1":"value1", "header2":"value2"},
  118.      *          {"header1":"value1", "header2":"value2"}
  119.      *      ],
  120.      *      "_xx":[
  121.      *          {"key":"header1"},
  122.      *          {"key":"header2"}
  123.      *      ],
  124.      *      "yy":[
  125.      *          {"header1":"value1", "header2":"value2"},
  126.      *          {"header1":"value1", "header2":"value2"},
  127.      *          {"header1":"value1", "header2":"value2"},
  128.      *          {"header1":"value1", "header2":"value2"}
  129.      *      ],
  130.      *      "_yy":[
  131.      *          {"key":"header1"},
  132.      *          {"key":"header2"}
  133.      *      ]
  134.      *  }
  135.      *
  136.      * @param errorParamDto
  137.      */
  138.     @SuppressWarnings("unchecked")
  139.     private void checkErrorParamDto(Dto errorParamDto) {
  140.         if (errorParamDto != null) {
  141.             JSONArray keyColumnNotFound = new JSONArray();
  142.             JSONArray keyNotList = new JSONArray();
  143.  
  144.             Set<String> keys = errorParamDto.keys();
  145.             for (String key : keys) {
  146.                 if (!key.startsWith(PREFIX_ERROR_PARAM_DTO_LIST_KEY)) {
  147.                     if (!errorParamDto.containsKey(PREFIX_ERROR_PARAM_DTO_LIST_KEY + key)) {
  148.                         keyColumnNotFound.add(key);
  149.                     } else {
  150.                         if (!(errorParamDto.get(key) instanceof List)) {
  151.                             keyNotList.add(key);
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.  
  157.             if (keyColumnNotFound.size() != 0 || keyNotList.size() != 0) {
  158.                 throw new InfrastructureException(InfrastructureExceptionConstants.INVALID_ERROR_PARAM_DTO, keyColumnNotFound,
  159.                         keyNotList);
  160.             }
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement