/**
   * @param request object of HttpServletRequest
   * @param eventParametersForm : eventParametersForm
   * @throws Exception : Exception
   */
  @Override
  protected void setRequestParameters(
      HttpServletRequest request, EventParametersForm eventParametersForm) throws Exception {

    // String operation = (String) request.getAttribute(Constants.OPERATION);
    String formName;

    final ReceivedEventParametersForm receivedEventParametersForm =
        (ReceivedEventParametersForm) eventParametersForm;
    if (receivedEventParametersForm.getOperation().equals(Constants.EDIT)) {
      formName = Constants.RECEIVED_EVENT_PARAMETERS_EDIT_ACTION;
    } else {
      formName = Constants.RECEIVED_EVENT_PARAMETERS_ADD_ACTION;
    }
    // set the ReceivedQuality List.
    final List qualityList =
        CDEManager.getCDEManager()
            .getPermissibleValueList(Constants.CDE_NAME_RECEIVED_QUALITY, null);
    request.setAttribute("receivedQualityList", qualityList);
    request.setAttribute("formName", formName);
  }
  /* (non-Javadoc)
   * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
   */
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    final ActionErrors errors = new ActionErrors();
    final Validator validator = new Validator();
    double aliquotQuantity = 0;
    double initialQuantity = 0;
    try {
      if (validator.isEmpty(this.className)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required", ApplicationProperties.getValue("specimen.type")));
      }
      //			if(this.labelGenType.equals("2") && validator.isEmpty(this.labelFormat))
      //			{
      //				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
      //						"collectionProtocol.labelformat", ApplicationProperties
      //						.getValue("collectionProtocol.labelformat")));
      //			}
      if (validator.isEmpty(this.type)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required", ApplicationProperties.getValue("specimen.subType")));
      }
      final List specimenClassList =
          CDEManager.getCDEManager()
              .getPermissibleValueList(Constants.CDE_NAME_SPECIMEN_CLASS, null);
      if (!Validator.isEnumeratedValue(specimenClassList, this.className)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.invalid", ApplicationProperties.getValue("specimen.type")));
      }

      if (!Validator.isEnumeratedValue(AppUtility.getSpecimenTypes(this.className), this.type)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.invalid", ApplicationProperties.getValue("specimen.subType")));
      }

      if (!validator.isEmpty(this.quantity)) {
        try {
          this.quantity = new BigDecimal(this.quantity).toPlainString();
          if (AppUtility.isQuantityDouble(this.className, this.type)) {
            if (!validator.isDouble(this.quantity, true)) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.required", ApplicationProperties.getValue("specimen.quantity")));
            }
          } else {
            if (!validator.isNumeric(this.quantity, 0)) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.format", ApplicationProperties.getValue("specimen.quantity")));
            }
          }
        } catch (final NumberFormatException exp) {
          CreateSpecimenTemplateForm.logger.info(exp.getMessage(), exp);
          exp.printStackTrace();
          errors.add(
              ActionErrors.GLOBAL_ERROR,
              new ActionError(
                  "errors.item.format", ApplicationProperties.getValue("specimen.quantity")));
        }

      } else {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.format", ApplicationProperties.getValue("specimen.quantity")));
      }

      if (!this.quantityPerAliquot.equals("")) {
        if (this.quantityPerAliquot != null && this.quantityPerAliquot.trim().length() != 0) {
          try {
            this.quantityPerAliquot = new BigDecimal(this.quantityPerAliquot).toPlainString();
            if (AppUtility.isQuantityDouble(this.className, this.type)) {
              if (!validator.isDouble(this.quantityPerAliquot.trim())) {
                errors.add(
                    ActionErrors.GLOBAL_ERROR,
                    new ActionError(
                        "errors.item.format",
                        ApplicationProperties.getValue("aliquots.qtyPerAliquot")));
              }
            } else {
              if (!validator.isPositiveNumeric(this.quantityPerAliquot.trim(), 1)) {
                errors.add(
                    ActionErrors.GLOBAL_ERROR,
                    new ActionError(
                        "errors.item.format",
                        ApplicationProperties.getValue("aliquots.qtyPerAliquot")));
              }
            }
          } catch (final NumberFormatException exp) {
            CreateSpecimenTemplateForm.logger.error(exp.getMessage(), exp);
            exp.printStackTrace();
            errors.add(
                ActionErrors.GLOBAL_ERROR,
                new ActionError(
                    "errors.item.format",
                    ApplicationProperties.getValue("aliquots.qtyPerAliquot")));
          }
        }
      }

      if (!this.noOfAliquots.equals("")) {
        this.noOfAliquots = AppUtility.isValidCount(this.noOfAliquots, errors);

        if (!validator.isNumeric(this.noOfAliquots, 1)) {
          errors.add(
              ActionErrors.GLOBAL_ERROR,
              new ActionError(
                  "errors.item.format",
                  ApplicationProperties.getValue("specimenArrayAliquots.noOfAliquots")));
        } else {
          try {
            if (this.quantityPerAliquot.equals("")) {
              if (this.quantity.equals("0") || this.quantity.equals("0")) {
                errors.add(
                    ActionErrors.GLOBAL_ERROR,
                    new ActionError(
                        "errors.invalid", ApplicationProperties.getValue("specimen.quantity")));
              } else {
                aliquotQuantity =
                    Double.parseDouble(this.quantity) / Double.parseDouble(this.noOfAliquots);
                initialQuantity =
                    Double.parseDouble(this.quantity)
                        - (aliquotQuantity * Double.parseDouble(this.noOfAliquots));
              }
            } else {
              aliquotQuantity = Double.parseDouble(this.quantityPerAliquot);
              initialQuantity = Double.parseDouble(this.quantity);
              initialQuantity =
                  initialQuantity - (aliquotQuantity * Double.parseDouble(this.noOfAliquots));
            }
            if (initialQuantity < 0) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.invalid",
                      ApplicationProperties.getValue("cpbasedentry.quantityperaliquot")));
            }
            //						if(this.labelGenTypeForAliquot.equals("2") &&
            // validator.isEmpty(this.labelFormatForAliquot))
            //						{
            //							errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
            //									"sp.req.aliquot.labelformat", ApplicationProperties
            //									.getValue("sp.req.aliquot.labelformat")));
            //						}
          } catch (final NumberFormatException exp) {
            CreateSpecimenTemplateForm.logger.error(exp.getMessage(), exp);
            errors.add(
                ActionErrors.GLOBAL_ERROR,
                new ActionError(
                    "errors.item.format", ApplicationProperties.getValue("specimen.quantity")));
          }
        }
      }

      if (this.tissueSite.equals("")) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required", ApplicationProperties.getValue("specimen.tissueSite")));
      }

      if (this.tissueSide.equals("")) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required", ApplicationProperties.getValue("specimen.tissueSide")));
      }

      if (this.noOfAliquots != null && !this.noOfAliquots.equals("")) {
        if (!this.storageLocationForAliquotSpecimen.equals("Auto")
            && !this.storageLocationForAliquotSpecimen.equals("Manual")
            && !this.storageLocationForAliquotSpecimen.equals("Virtual")) {
          errors.add(
              ActionErrors.GLOBAL_ERROR,
              new ActionError(
                  "errors.item.required",
                  ApplicationProperties.getValue("cpbasedentry.aliquotstoragelocation")));
        }
      }
      if (!this.storageLocationForSpecimen.equals("Auto")
          && !this.storageLocationForSpecimen.equals("Manual")
          && !this.storageLocationForSpecimen.equals("Virtual")) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required",
                ApplicationProperties.getValue("cpbasedentry.specimenstoragelocation")));
      }
      if (this.pathologicalStatus.equals("")) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.required",
                ApplicationProperties.getValue("specimen.pathologicalStatus")));
      }
      final List tissueSiteList =
          CDEManager.getCDEManager().getPermissibleValueList(Constants.CDE_NAME_TISSUE_SITE, null);
      if (!Validator.isEnumeratedValue(tissueSiteList, this.tissueSite)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.invalid", ApplicationProperties.getValue("specimen.tissueSite")));
      }

      final List tissueSideList =
          CDEManager.getCDEManager().getPermissibleValueList(Constants.CDE_NAME_TISSUE_SIDE, null);
      if (!Validator.isEnumeratedValue(tissueSideList, this.tissueSide)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.invalid", ApplicationProperties.getValue("specimen.tissueSide")));
      }

      final List pathologicalStatusList =
          CDEManager.getCDEManager()
              .getPermissibleValueList(Constants.CDE_NAME_PATHOLOGICAL_STATUS, null);

      if (!Validator.isEnumeratedValue(pathologicalStatusList, this.pathologicalStatus)) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError(
                "errors.item.invalid",
                ApplicationProperties.getValue("specimen.pathologicalStatus")));
      }

      if ((this.collectionEventUserId) == 0L) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError("errors.item.required", "Collection Event's user"));
      }

      // checks the collectionProcedure
      if (!validator.isValidOption(this.getCollectionEventCollectionProcedure())) {
        final String message =
            ApplicationProperties.getValue("collectioneventparameters.collectionprocedure");
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.item.required", message));
      }

      final List procedureList =
          CDEManager.getCDEManager()
              .getPermissibleValueList(Constants.CDE_NAME_COLLECTION_PROCEDURE, null);
      if (!Validator.isEnumeratedValue(
          procedureList, this.getCollectionEventCollectionProcedure())) {
        final String message = ApplicationProperties.getValue("cpbasedentry.collectionprocedure");
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.invalid", message));
      }
      // Container validation
      if (!validator.isValidOption(this.getCollectionEventContainer())) {
        final String message =
            ApplicationProperties.getValue("collectioneventparameters.container");
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.item.required", message));
      }
      final List containerList =
          CDEManager.getCDEManager().getPermissibleValueList(Constants.CDE_NAME_CONTAINER, null);
      if (!Validator.isEnumeratedValue(containerList, this.getCollectionEventContainer())) {
        final String message =
            ApplicationProperties.getValue("collectioneventparameters.container");
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.invalid", message));
      }
      if ((this.receivedEventUserId) == 0L) {
        errors.add(
            ActionErrors.GLOBAL_ERROR,
            new ActionError("errors.item.required", "Received Event's user"));
      }
      final List qualityList =
          CDEManager.getCDEManager()
              .getPermissibleValueList(Constants.CDE_NAME_RECEIVED_QUALITY, null);
      if (!Validator.isEnumeratedValue(qualityList, this.receivedEventReceivedQuality)) {
        final String message = ApplicationProperties.getValue("cpbasedentry.receivedquality");
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors.invalid", message));
      }

      if (this.className.equals(Constants.MOLECULAR)) {
        if (!validator.isDouble(this.concentration, true)) {
          errors.add(
              ActionErrors.GLOBAL_ERROR,
              new ActionError(
                  "errors.item.format", ApplicationProperties.getValue("specimen.concentration")));
        }
      }

      if (this.noOfDeriveSpecimen >= 1) {
        boolean bSpecimenClass = false;
        boolean bSpecimenType = false;
        //				boolean validateLabel = false;
        //				boolean labelGenType = false;
        //				boolean labelFormat = false;
        final Map deriveSpecimenMap = this.deriveSpecimenMap();
        final Iterator it = deriveSpecimenMap.keySet().iterator();
        while (it.hasNext()) {
          final String key = (String) it.next();
          String mapValue = (String) deriveSpecimenMap.get(key);
          if (!bSpecimenClass) {
            if (key.indexOf("specimenClass") != -1 && !validator.isValidOption(mapValue)) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.selected",
                      ApplicationProperties.getValue("collectionprotocol.specimenclass")));
              bSpecimenClass = true;
            }
          }
          //					if(!labelGenType)
          //					{
          //						if ((key.indexOf("_labelGenType")) != -1 && mapValue != null)
          //						{
          //							if (mapValue.equals("2"))
          //							{
          //								validateLabel=true;
          //								labelGenType = true;
          //							}
          //						}
          //
          //					}
          //					if(!labelFormat && labelGenType)
          //					{
          //						if(validateLabel && (key.indexOf("_labelFormat")) != -1 &&
          // validator.isEmpty(mapValue))
          //						{
          //							labelFormat = true;
          //							errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
          //									"sp.req.derivative.labelformat", ApplicationProperties
          //									.getValue("sp.req.derivative.labelformat")));
          //						}
          //					}
          if ((key.indexOf("_concentration")) != -1 && mapValue != null) {
            mapValue = new BigDecimal(mapValue).toPlainString();
            if (!validator.isDouble(mapValue, true)) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.format",
                      ApplicationProperties.getValue("specimen.concentration")));
            }
          }

          if (!bSpecimenType) {
            if (key.indexOf("specimenType") != -1 && !validator.isValidOption(mapValue)) {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.selected",
                      ApplicationProperties.getValue("collectionprotocol.specimetype")));
              bSpecimenType = true;
            }
          }

          if ((key.indexOf("_quantity")) != -1) {
            if (!validator.isEmpty(mapValue)) {
              try {
                mapValue = new BigDecimal(mapValue).toPlainString();
                if (AppUtility.isQuantityDouble(this.className, this.type)) {
                  if (!validator.isDouble(mapValue, true)) {
                    errors.add(
                        ActionErrors.GLOBAL_ERROR,
                        new ActionError(
                            "errors.item.required",
                            ApplicationProperties.getValue("specimen.quantity")));
                  }
                } else {
                  if (!validator.isNumeric(mapValue, 0)) {
                    errors.add(
                        ActionErrors.GLOBAL_ERROR,
                        new ActionError(
                            "errors.item.format",
                            ApplicationProperties.getValue(
                                "cpbasedentry.derivedspecimen.quantity")));
                  }
                }
              } catch (final NumberFormatException exp) {
                CreateSpecimenTemplateForm.logger.error(exp.getMessage(), exp);
                errors.add(
                    ActionErrors.GLOBAL_ERROR,
                    new ActionError(
                        "errors.item.format",
                        ApplicationProperties.getValue("cpbasedentry.derivedspecimen.quantity")));
              }
            } else {
              errors.add(
                  ActionErrors.GLOBAL_ERROR,
                  new ActionError(
                      "errors.item.format",
                      ApplicationProperties.getValue("cpbasedentry.derivedspecimen.quantity")));
            }
          }
        }
      }
    } catch (final Exception excp) {
      CreateSpecimenTemplateForm.logger.error(excp.getMessage(), excp);
      excp.printStackTrace();
    }
    return errors;
  }