Exemplo n.º 1
0
  public ActionForward execute(
      ActionMapping actionMapping,
      ActionForm actionForm,
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse) {
    LazyValidatorForm frm = (LazyValidatorForm) actionForm;

    if (formCanceled(frm)) {
      return actionMapping.findForward(SUCCESS);
    }

    resetFocusControl(frm, CANCEL);
    ActionMessages msgs = new ActionMessages();
    if (!formSaved(frm)) {
      setFormMode(frm, ((String) httpServletRequest.getParameter(BROWSE_ACTION)));
      setFormId(frm, (String) httpServletRequest.getParameter(BROWSE_ID));
      try {
        httpServletRequest
            .getSession()
            .setAttribute("hozehKindList", Misc.listHardCode(Constants.TableId.HOZEH_KIND));

        LoginInfo loginInfo = getLoginInfo(httpServletRequest);
        if (!loginInfo.userHasAccess(getFormId(frm), getFormMode(frm))) {
          if (isFormInModifyMode(frm)
              && loginInfo.userHasAccess(getFormId(frm), Constants.ActionType.ENQUERY)) {
            setFormMode(frm, Constants.ActionType.ENQUERY);
          } else {
            throw new Exception(INVALID_ACCESS);
          }
        }
        if (!isFormInAddMode(frm)) {
          String formKey = (String) httpServletRequest.getParameter(BROWSE_KEY);
          frm.set("formKey", formKey);
          String key[] = formKey.split(Constants.DATA_SEPARATOR_SPLIT);
          short officeCode = Short.parseShort(key[0]);
          short hozehCode = Short.parseShort(key[1]);
          String hozehKind = key[2];

          HozehInfo inf = Place.getHozehDetails(officeCode, hozehCode, hozehKind);
          frm.set(OFFICE_CODE, Short.toString(inf.getOfficeCode()));
          frm.set(HOZEH_CODE, Short.toString(inf.getHozehCode()));
          frm.set(HOZEH_KIND, inf.getHozehKind());
          frm.set(HOZEH_DESC, inf.getHozehDesc());
          if (inf.isInactive()) {
            frm.set(CODE_ACTIVE_FLAG, "on");
          }
        }
        String officeCode = (String) frm.get(OFFICE_CODE);
        if (Utils.isEmpty(officeCode)) {
          officeCode = "0";
          frm.set(OFFICE_CODE, officeCode);
        }
        frm.set(OFFICE_NAME, Place.getOfficeName(Short.parseShort(officeCode)));

        if (isFormInAddMode(frm)) {
          resetFocusControl(frm, HOZEH_CODE);
        } else if (isFormInModifyMode(frm)) {
          resetFocusControl(frm, HOZEH_DESC);
        }
      } catch (Exception ex) {
        addError(msgs, ex.getMessage());
        saveErrors(httpServletRequest, msgs);
      }
      return actionMapping.findForward(EDIT);
    } else {
      HozehInfo inf = new HozehInfo();
      resetFocusControl(frm, "");

      if (isFormInAddMode(frm)) {

        String officeCode = ((String) frm.get(OFFICE_CODE)).trim();
        resetFocusControl(frm, "");
        if (Utils.isEmpty(officeCode)) {
          addError(msgs, FIELD_CAN_NOT_BE_EMPTY, "اداره");
          setFocusControl(frm, OFFICE_CODE);
        } else if (!Utils.isValidNotZeroNumber(officeCode, 3)) {
          addError(msgs, FIELD_INVALID, "اداره");
          setFocusControl(frm, OFFICE_CODE);
        } else {
          inf.setOfficeCode(Short.parseShort(officeCode));
        }

        String hozehCode = ((String) frm.get(HOZEH_CODE)).trim();
        if (Utils.isEmpty(hozehCode)) {
          addError(msgs, FIELD_CAN_NOT_BE_EMPTY, "كد");
          setFocusControl(frm, HOZEH_CODE);
        } else if (!Utils.isValidNotZeroNumber(hozehCode, 3)) {
          addError(msgs, FIELD_INVALID, "كد");
          setFocusControl(frm, HOZEH_CODE);
        } else {
          inf.setHozehCode(Short.parseShort(hozehCode));
        }

        String hozehKind = ((String) frm.get(HOZEH_KIND)).trim();
        if (Utils.isEmpty(hozehKind)) {
          addError(msgs, FIELD_CAN_NOT_BE_EMPTY, "نوع");
          setFocusControl(frm, HOZEH_KIND);
        } else if (hozehKind.length() > 1) {
          addError(msgs, FIELD_INVALID, "نوع");
          setFocusControl(frm, HOZEH_KIND);
        } else {
          inf.setHozehKind(hozehKind);
        }

      } else {
        String formKey = (String) frm.get("formKey");
        String key[] = formKey.split(Constants.DATA_SEPARATOR_SPLIT);

        inf.setOfficeCode(Short.parseShort(key[0]));
        inf.setHozehCode(Short.parseShort(key[1]));
        inf.setHozehKind(key[2]);
      }

      if (!isFormInDeleteMode(frm)) {
        String hozehDesc = Utils.charVal((String) frm.get(HOZEH_DESC));
        frm.set(HOZEH_DESC, hozehDesc);
        if (Utils.isEmpty(hozehDesc)) {
          addError(msgs, FIELD_CAN_NOT_BE_EMPTY, "شرح");
          setFocusControl(frm, HOZEH_DESC);
        } else if (hozehDesc.length() > 50) {
          addError(msgs, FIELD_INVALID, "شرح");
          setFocusControl(frm, HOZEH_DESC);
        } else {
          inf.setHozehDesc(hozehDesc);
        }

        if (frm.get(CODE_ACTIVE_FLAG) == null) {
          inf.setCodeActiveFlag(Constants.CodeActiveFlag.ACTIVE);
        } else {
          inf.setCodeActiveFlag(Constants.CodeActiveFlag.INACTIVE);
        }
      }

      if (!msgs.isEmpty()) {
        saveErrors(httpServletRequest, msgs);
        return actionMapping.findForward(EDIT);
      }

      resetFocusControl(frm, CANCEL);
      try {
        Place.saveHozeh(getFormMode(frm), inf);
        httpServletRequest.setAttribute(BROWSE_KEY, inf.getKey());
        return actionMapping.findForward(SUCCESS);
      } catch (Exception ex) {
        resetFocusControl(frm, CANCEL);
        addError(msgs, ex.getMessage());
        saveErrors(httpServletRequest, msgs);
        return actionMapping.findForward(EDIT);
      }
    }
  }