public void onBeforeUpdate(Record record) throws VetoException, DeadlineExceededException {
   String operationType = record.getOperationType();
   try {
     if (operationType.equals(Updates.Types.INSERT)
         || operationType.equals(Updates.Types.UPDATE)) {
       Object key = null;
       if (operationType.equals(Updates.Types.UPDATE)) {
         key = record.getKey();
       }
       if (record.has(SalaryComponentKinds.KPI_ID)) {
         Object kpiId = record.getValue(SalaryComponentKinds.KPI_ID);
         if (kpiId != null) {
           JSONArray kpiDuplicate = getSalaryComponentsKPIRecords(key, kpiId);
           if (kpiDuplicate != null && kpiDuplicate.length() > 0) {
             throw new BusinessLogicException(
                 "This Kpi Already Assigned to "
                     + kpiDuplicate.getJSONObject(0).opt(SalaryComponentKinds.NAME));
           }
         }
       }
     }
   } catch (BusinessLogicException e) {
     throw e;
   } catch (Exception e) {
     String trace =
         ExceptionUtils.getExceptionTraceMessage(SalaryComponentsBusinessLogic.class.getName(), e);
     LogUtility.writeLog("SalaryComponentsBusinessLogic >> Exception Trace >> " + trace);
     throw new BusinessLogicException("Some Unknown ErrorOccured Please Contace Admin.");
   }
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   try {
     resp.setContentType("application/vnd.ms-excel");
     String decompositionParameter = "attachment;filename=Tour_Expense_Report.xls";
     resp.setHeader("Content-disposition", decompositionParameter);
     resp.setHeader("Content-Type", "application/octet-stream");
     WritableWorkbook workbook = Workbook.createWorkbook(resp.getOutputStream());
     String key = req.getParameter("keys");
     generateEmployeePerformanceReport(workbook, key);
   } catch (Exception e) {
     String trace = ExceptionUtils.getExceptionTraceMessage(getClass().getName(), e);
     LogUtility.writeLog("TourExpenseExcelReport trace >> " + trace);
     throw new BusinessLogicException("Some Unknown Error Occured Please Contact To Admin.");
   }
 }
  public JSONArray populateEmployeeBalanceLeavesDARCL(Object employeeid, Object requestdate) {

    String applicationVersionId =
        ApiProxy.getCurrentEnvironment()
            .getVersionId()
            .substring(0, ApiProxy.getCurrentEnvironment().getVersionId().indexOf("."));
    if (applicationVersionId.equalsIgnoreCase("testviews")) {
      JSONArray balanceLeaveArray =
          com.applane.resourceoriented.hris.BalanceLeaves.employeeBalanceLeave(
              employeeid, requestdate);
      return balanceLeaveArray;
    } else {
      try {
        // GET EMPLOYEEID FROM KEY
        if ((employeeid instanceof JSONArray)) {
          employeeid = ((JSONArray) employeeid).getJSONObject(0).get(Data.Query.KEY);
        }

        if ((employeeid instanceof JSONObject)) {
          employeeid = ((JSONObject) employeeid).get(Data.Query.KEY);
        }

        JSONArray employeeLeavePolicyArray = getEmployeeLeavePolicyId(employeeid);
        if (employeeLeavePolicyArray != null && employeeLeavePolicyArray.length() > 0) {
          Object leavePolicyId = employeeLeavePolicyArray.getJSONObject(0).opt("leavepolicyid");
          Object leavePolicyFromDate =
              employeeLeavePolicyArray.getJSONObject(0).opt("leavepolicyid.fromdate");
          Object leavePolicyToDate =
              employeeLeavePolicyArray.getJSONObject(0).opt("leavepolicyid.todate");
          JSONArray employeeLeaveRuleArray = getEmployeeLeaveRule(employeeid, leavePolicyId);
          if (employeeLeaveRuleArray != null && employeeLeaveRuleArray.length() > 0) {
            JSONArray categoryTypeArray = new JSONArray();
            for (int counter = 0; counter < employeeLeaveRuleArray.length(); counter++) {
              Object leaveTypeId = employeeLeaveRuleArray.getJSONObject(counter).opt("leavetypeid");
              Object leaveTypeName =
                  employeeLeaveRuleArray.getJSONObject(counter).opt("leavetypeid.name");
              Object openingBalance =
                  employeeLeaveRuleArray.getJSONObject(counter).opt("openingbalance");
              Object assignedLeave =
                  employeeLeaveRuleArray.getJSONObject(counter).opt("assignedleaves");
              double totalMonthBetweenFromDateAneToDate =
                  getTotalMonthBetweenFromDateAneToDate(
                      Translator.dateValue(leavePolicyFromDate),
                      Translator.dateValue(leavePolicyToDate));
              double takenLeaves =
                  calculateLeaveTaken(
                      employeeid, leaveTypeId, leavePolicyFromDate, leavePolicyToDate);
              double totalAssignedLeaves =
                  Translator.doubleValue(openingBalance)
                      + (Translator.doubleValue(assignedLeave)
                          / totalMonthBetweenFromDateAneToDate);
              double balance = totalAssignedLeaves - takenLeaves;

              JSONObject leaveTypeObject = new JSONObject();
              JSONObject typeobject = new JSONObject();

              leaveTypeObject.put("__key__", leaveTypeId);
              leaveTypeObject.put("name", leaveTypeName);

              typeobject.put("leavetypeid", leaveTypeObject);
              typeobject.put("totalleaves", totalAssignedLeaves);
              typeobject.put("takenleaves", takenLeaves);
              String balanceMessage = "";
              if (balance >= 0) {
                balanceMessage = new DecimalFormat("0.#").format(balance) + " leaves pending";
              } else {
                balanceMessage =
                    new DecimalFormat("0.#").format((balance * (-1.0))) + " leaves overdue";
              }
              typeobject.put("balanceleaves", balanceMessage);
              categoryTypeArray.put(counter, typeobject);
            }
            return categoryTypeArray;
          }
        }

      } catch (Exception e) {
        LogUtility.writeLog(
            "BalanceLeaves >> populateEmployeeBalanceLeavesDARCL Exception  >> : ["
                + ExceptionUtils.getExceptionTraceMessage(getClass().getName(), e)
                + "]");
        throw new RuntimeException("Some Error Occured while Generating Leave Balance");
      }
    }
    return null;
  }
  @Override
  public void onResult(Result result) throws DeadlineExceededException {
    try {
      JSONArray records = result.getRecords();
      JSONObject parameters = result.getQuery().getParameters();

      Object monthId = parameters.opt("monthid");
      Object yearId = parameters.opt("yearid");

      LogUtility.writeLog("records >> " + records);
      Map<Integer, Double> resourceRevenewDetails = new HashMap<Integer, Double>();
      Map<Integer, String[]> resourceNameMap = new HashMap<Integer, String[]>();
      List<Integer> list = new ArrayList<Integer>();
      if (records != null) {
        for (int counter = 0; counter < records.length(); counter++) {
          JSONObject engagementDetailsObject;
          engagementDetailsObject = records.getJSONObject(counter);
          int resourceId = Translator.integerValue(engagementDetailsObject.opt("resourceid"));
          String resourceName =
              Translator.stringValue(engagementDetailsObject.opt("resourceid.name"));
          String employeeCode =
              Translator.stringValue(engagementDetailsObject.opt("resourceid.employeecode"));
          double rate = Translator.doubleValue(engagementDetailsObject.opt("deliveryid.rate"));
          double billableEfforts =
              Translator.doubleValue(engagementDetailsObject.opt("billableefforts"));
          double amount = rate * billableEfforts;
          if (!resourceNameMap.containsKey(resourceId)) {
            resourceNameMap.put(resourceId, new String[] {resourceName, employeeCode});
          }
          if (resourceRevenewDetails.containsKey(resourceId)) {
            amount += resourceRevenewDetails.get(resourceId);
          }
          resourceRevenewDetails.put(resourceId, amount);
          if (!list.contains(resourceId)) {
            list.add(resourceId);
          }
        }
        String listString = list.toString();
        if (listString.length() > 0) {
          listString = listString.substring(1, listString.length() - 1);
          HashMap<Integer, Double> employeeSalaryPaidDetsils =
              getEmployeeSalaryPaidDetsils(listString, monthId, yearId);
          JSONArray array = new JSONArray();
          for (Integer resourceId : resourceNameMap.keySet()) {
            String[] employeeDetails = resourceNameMap.get(resourceId);
            double paidSalary =
                employeeSalaryPaidDetsils.get(resourceId) == null
                    ? 0.0
                    : employeeSalaryPaidDetsils.get(resourceId);
            double revenew = resourceRevenewDetails.get(resourceId);
            JSONObject details = new JSONObject();
            details.put("resourceName", employeeDetails[0]);
            details.put("resourceCode", employeeDetails[1]);
            details.put("paidSalary", new DecimalFormat("#.##").format(paidSalary));
            details.put("revenew", new DecimalFormat("#.##").format(revenew));
            if (paidSalary < revenew) {
              details.put("profit", new DecimalFormat("#.##").format(revenew - paidSalary));
            } else {
              details.put("loss", new DecimalFormat("#.##").format(paidSalary - revenew));
            }
            array.put(details);
          }
          records.put(new JSONObject().put("revenewReport", array));
        }
      }
    } catch (JSONException e) {
      String trace = ExceptionUtils.getExceptionTraceMessage(getClass().getName(), e);
      LogUtility.writeLog("RevenewLossBusinessLogic >>  Exception >> Trace >> " + trace);
      throw new BusinessLogicException("Some Unknown Error Occured Please Contact To Admin.");
    }
  }