Ejemplo n.º 1
0
  public static String timeSheetChecker(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) session.getAttribute("delegator");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    List<Map<String, Object>> noTimeEntryList = new LinkedList<Map<String, Object>>();
    String partyId = userLogin.getString("partyId");
    Timestamp now = UtilDateTime.nowTimestamp();
    Timestamp weekStart = UtilDateTime.getWeekStart(now);

    if (UtilValidate.isEmpty(delegator)) {
      delegator = (Delegator) request.getAttribute("delegator");
    }

    try {
      // should be scrum team or scrum master.
      EntityConditionList<EntityExpr> exprOrs =
          EntityCondition.makeCondition(
              UtilMisc.toList(
                  EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, "SCRUM_TEAM"),
                  EntityCondition.makeCondition(
                      "roleTypeId", EntityOperator.EQUALS, "SCRUM_MASTER")),
              EntityOperator.OR);
      EntityConditionList<EntityCondition> exprAnds =
          EntityCondition.makeCondition(
              UtilMisc.toList(
                  exprOrs,
                  EntityCondition.makeCondition("partyId", EntityOperator.EQUALS, partyId)),
              EntityOperator.AND);
      List<GenericValue> partyRoleList =
          EntityQuery.use(delegator).from("PartyRole").where(exprAnds).queryList();
      if (UtilValidate.isNotEmpty(partyRoleList)) {
        List<GenericValue> timesheetList =
            EntityQuery.use(delegator)
                .from("Timesheet")
                .where("partyId", partyId, "statusId", "TIMESHEET_IN_PROCESS")
                .cache(true)
                .queryList();
        if (UtilValidate.isNotEmpty(timesheetList)) {
          for (GenericValue timesheetMap : timesheetList) {
            String timesheetId = timesheetMap.getString("timesheetId");
            Timestamp timesheetDate = timesheetMap.getTimestamp("fromDate");
            // check monday - friday
            for (int i = 0; i < 5; i++) {
              Timestamp realTimeDate = UtilDateTime.addDaysToTimestamp(timesheetDate, i);
              Timestamp nowStartDate = UtilDateTime.getDayStart(now);
              // compare week and compare date
              if ((timesheetDate.compareTo(weekStart) <= 0)
                  && (realTimeDate.compareTo(nowStartDate) < 0)) {
                // check time entry
                List<GenericValue> timeEntryList =
                    timesheetMap.getRelated(
                        "TimeEntry",
                        UtilMisc.toMap(
                            "partyId",
                            partyId,
                            "timesheetId",
                            timesheetId,
                            "fromDate",
                            realTimeDate),
                        null,
                        false);
                // check EmplLeave
                List<GenericValue> emplLeaveList =
                    EntityQuery.use(delegator)
                        .from("EmplLeave")
                        .where("partyId", partyId, "fromDate", realTimeDate)
                        .cache(true)
                        .queryList();
                if (UtilValidate.isEmpty(timeEntryList) && UtilValidate.isEmpty(emplLeaveList)) {
                  Map<String, Object> noEntryMap = new HashMap<String, Object>();
                  noEntryMap.put("timesheetId", timesheetId);
                  noTimeEntryList.add(noEntryMap);
                  break;
                }
              }
            }
          }
        }
      }
    } catch (GenericEntityException EntEx) {
      EntEx.printStackTrace();
      Debug.logError(EntEx.getMessage(), module);
    }
    if (UtilValidate.isNotEmpty(noTimeEntryList)) {
      StringBuilder warningDataBuffer = new StringBuilder();
      int size = noTimeEntryList.size();
      for (Map<String, Object> dataMap : noTimeEntryList) {
        if (--size == 0) {
          warningDataBuffer.append(dataMap.get("timesheetId"));
        } else {
          warningDataBuffer.append(dataMap.get("timesheetId")).append(", ");
        }
        warningDataBuffer.append(dataMap.get("timesheetId"));
      }
      String warningData = warningDataBuffer.toString();
      Debug.logInfo("The following time sheet no time entry: [" + warningData + "]", module);
      request.setAttribute(
          "_ERROR_MESSAGE_",
          UtilProperties.getMessage(
              "scrumUiLabels",
              "ScrumTimesheetWarningMessage",
              UtilMisc.toMap("warningMessage", warningData),
              UtilHttp.getLocale(request)));
    }
    return "success";
  }