/**
   * Validates that the existance of the building room number is consistent with the asset type
   * requirements.
   *
   * @param roomNumber
   * @param detail
   * @prarm asset
   * @return boolean
   * @deprecated this method is replaced by
   *     validateBuildingCodeAndRoomNumber(BarcodeInventoryErrorDetail, Asset)
   */
  @Deprecated
  protected boolean validateBuildingRoomNumber(
      String roomNumber, BarcodeInventoryErrorDetail detail, Asset asset) {
    boolean result = true;
    String label =
        SpringContext.getBean(DataDictionaryService.class)
            .getDataDictionary()
            .getBusinessObjectEntry(BarcodeInventoryErrorDetail.class.getName())
            .getAttributeDefinition(CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER)
            .getLabel();
    // String description = asset.getCapitalAssetType().getCapitalAssetTypeDescription();
    String description = asset.getCapitalAssetTypeCode();

    // if the asset has empty building room number, then the BCIE should too
    if (StringUtils.isBlank(asset.getBuildingRoomNumber())) {
      if (StringUtils.isNotBlank(roomNumber)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_NOT_ALLOWED_FIELD,
                label,
                description);
        result &= false;
      }
    }
    // otherwise the BCIE should have a non-empty and existing active building room number
    else {
      HashMap<String, Object> fields = new HashMap<String, Object>();
      fields.put(KFSPropertyConstants.CAMPUS_CODE, detail.getCampusCode());
      fields.put(KFSPropertyConstants.BUILDING_CODE, detail.getBuildingCode());
      fields.put(KFSPropertyConstants.BUILDING_ROOM_NUMBER, detail.getBuildingRoomNumber());
      Room room = getBusinessObjectService().findByPrimaryKey(Room.class, fields);

      if (StringUtils.isBlank(roomNumber)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_REQUIRED_FIELD,
                label,
                description);
        result &= false;
      } else if (ObjectUtils.isNull(room)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
                label);
        result = false;
      } else if (!room.isActive()) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
                label);
        result &= false;
      }
    }

    return result;
  }
 /**
  * Determines the document had all its records corrected
  *
  * @return boolean
  */
 public boolean isDocumentCorrected() {
   for (BarcodeInventoryErrorDetail detail : this.getBarcodeInventoryErrorDetail()) {
     if (detail
         .getErrorCorrectionStatusCode()
         .equals(CamsConstants.BarCodeInventoryError.STATUS_CODE_ERROR)) return false;
   }
   return true;
 }
  /**
   * Validates the condition code exists in the condition table
   *
   * @param conditionCode
   * @param detail
   * @return boolean
   */
  protected boolean validateConditionCode(
      String conditionCode, BarcodeInventoryErrorDetail detail) {
    boolean result = true;
    String label =
        SpringContext.getBean(DataDictionaryService.class)
            .getDataDictionary()
            .getBusinessObjectEntry(BarcodeInventoryErrorDetail.class.getName())
            .getAttributeDefinition(CamsPropertyConstants.BarcodeInventory.ASSET_CONDITION_CODE)
            .getLabel();

    AssetCondition condition;
    HashMap<String, Object> fields = new HashMap<String, Object>();
    fields.put(
        CamsPropertyConstants.BarcodeInventory.ASSET_CONDITION_CODE,
        detail.getAssetConditionCode());
    condition = getBusinessObjectService().findByPrimaryKey(AssetCondition.class, fields);

    if (ObjectUtils.isNull(condition)) {
      GlobalVariables.getMessageMap()
          .putError(
              CamsPropertyConstants.BarcodeInventory.ASSET_CONDITION_CODE,
              CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
              label);
      result &= false;
    } else if (!condition.isActive()) {
      GlobalVariables.getMessageMap()
          .putError(
              CamsPropertyConstants.BarcodeInventory.ASSET_CONDITION_CODE,
              CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
              label);
      result &= false;
    }

    return result;
  }
  /**
   * Validates the campus code exists in the campus code table
   *
   * @param campusCode
   * @param detail
   * @return boolean
   */
  protected boolean validateCampusCode(String campusCode, BarcodeInventoryErrorDetail detail) {
    boolean result = true;
    String label =
        SpringContext.getBean(DataDictionaryService.class)
            .getDataDictionary()
            .getBusinessObjectEntry(BarcodeInventoryErrorDetail.class.getName())
            .getAttributeDefinition(CamsPropertyConstants.BarcodeInventory.CAMPUS_CODE)
            .getLabel();

    Campus campus;
    HashMap<String, Object> fields = new HashMap<String, Object>();
    fields.put(KFSPropertyConstants.CAMPUS_CODE, detail.getCampusCode());
    campus =
        SpringContext.getBean(CampusService.class)
            .getCampus(campusCode /*RICE_20_REFACTORME  fields */);

    if (ObjectUtils.isNull(campus)) {
      GlobalVariables.getMessageMap()
          .putError(
              CamsPropertyConstants.BarcodeInventory.CAMPUS_CODE,
              CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
              label);
      result = false;
    } else if (!campus.isActive()) {
      GlobalVariables.getMessageMap()
          .putError(
              CamsPropertyConstants.BarcodeInventory.CAMPUS_CODE,
              CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
              label);
      result &= false;
    }
    return result;
  }
  /**
   * Invokes several methods that validates each barcode error record
   *
   * @param barcodeInventoryErrorDetails
   * @return boolean
   */
  public boolean validateBarcodeInventoryErrorDetail(
      BarcodeInventoryErrorDocument document, boolean updateStatus) {
    String errorPath = "";
    boolean valid = true;
    List<BarcodeInventoryErrorDetail> barcodeInventoryErrorDetails =
        document.getBarcodeInventoryErrorDetail();
    List<BarcodeInventoryErrorDetail> inventory = new ArrayList<BarcodeInventoryErrorDetail>();

    Long lineNumber = new Long(0);
    for (BarcodeInventoryErrorDetail barcodeInventoryErrorDetail : barcodeInventoryErrorDetails) {
      barcodeInventoryErrorDetail.setErrorDescription("");
      if (barcodeInventoryErrorDetail
          .getErrorCorrectionStatusCode()
          .equals(CamsConstants.BarCodeInventoryError.STATUS_CODE_ERROR)) {
        valid = true;
        errorPath =
            CamsConstants.DOCUMENT_PATH
                + "."
                + CamsPropertyConstants.BarcodeInventory.BARCODE_INVENTORY_DETAIL
                + "["
                + (lineNumber.intValue())
                + "]";
        GlobalVariables.getMessageMap().addToErrorPath(errorPath);

        Asset asset =
            validateTagNumberAndRetrieveActiveAsset(
                barcodeInventoryErrorDetail.getAssetTagNumber());
        valid &= ObjectUtils.isNotNull(asset);
        valid &=
            this.validateCampusCode(
                barcodeInventoryErrorDetail.getCampusCode(), barcodeInventoryErrorDetail);
        if (ObjectUtils.isNotNull(asset)) {
          valid &= this.validateBuildingCodeAndRoomNumber(barcodeInventoryErrorDetail, asset);
        }
        // valid &= this.validateBuildingCode(barcodeInventoryErrorDetail.getBuildingCode(),
        // barcodeInventoryErrorDetail, asset);
        // valid &=
        // this.validateBuildingRoomNumber(barcodeInventoryErrorDetail.getBuildingRoomNumber(),
        // barcodeInventoryErrorDetail, asset);
        valid &=
            this.validateConditionCode(
                barcodeInventoryErrorDetail.getAssetConditionCode(), barcodeInventoryErrorDetail);
        valid &= this.validateInventoryDate(barcodeInventoryErrorDetail.getUploadScanTimestamp());
        valid &=
            this.validateTaggingLock(
                barcodeInventoryErrorDetail.getAssetTagNumber(), document.getDocumentNumber());

        if (!valid) {
          barcodeInventoryErrorDetail.setErrorCorrectionStatusCode(
              CamsConstants.BarCodeInventoryError.STATUS_CODE_ERROR);

          // Getting the errors from GlobalVariables.
          barcodeInventoryErrorDetail.setErrorDescription(getErrorMessages(errorPath));
        } else {
          if (updateStatus) {
            barcodeInventoryErrorDetail.setErrorCorrectionStatusCode(
                CamsConstants.BarCodeInventoryError.STATUS_CODE_CORRECTED);
          }

          barcodeInventoryErrorDetail.setErrorDescription("NONE");
        }
        GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
      }
      lineNumber++;
    }

    /*
     * Since this document displays the asset lock error messages on the error description field, we don't want to display the
     * same message at the top of the document. Therefore, deleteLockErrorMessages method deletes such errors from the
     * GlobalVariables object.
     */
    deleteLockErrorMessages();

    return true;
  }
  /**
   * Validates that the existance of the building code and room number is consistent with the asset
   * type requirements.
   *
   * @param detail
   * @prarm asset
   * @return boolean
   */
  protected boolean validateBuildingCodeAndRoomNumber(
      BarcodeInventoryErrorDetail detail, Asset asset) {
    boolean result = true;

    String campusCode = detail.getCampusCode();
    String buildingCode = detail.getBuildingCode();
    String roomNumber = detail.getBuildingRoomNumber();
    String labelBuilding =
        SpringContext.getBean(DataDictionaryService.class)
            .getDataDictionary()
            .getBusinessObjectEntry(BarcodeInventoryErrorDetail.class.getName())
            .getAttributeDefinition(CamsPropertyConstants.BarcodeInventory.BUILDING_CODE)
            .getLabel();
    String labelRoom =
        SpringContext.getBean(DataDictionaryService.class)
            .getDataDictionary()
            .getBusinessObjectEntry(BarcodeInventoryErrorDetail.class.getName())
            .getAttributeDefinition(CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER)
            .getLabel();

    String assetTypeCode = asset.getCapitalAssetTypeCode();
    AssetType assetType = asset.getCapitalAssetType();

    // retrieve building
    HashMap<String, Object> fields = new HashMap<String, Object>();
    fields.put(KFSPropertyConstants.CAMPUS_CODE, campusCode);
    fields.put(KFSPropertyConstants.BUILDING_CODE, buildingCode);
    Building building = getBusinessObjectService().findByPrimaryKey(Building.class, fields);

    // retrieve room
    fields.put(KFSPropertyConstants.BUILDING_ROOM_NUMBER, roomNumber);
    Room room = getBusinessObjectService().findByPrimaryKey(Room.class, fields);

    // if movingIndicator is true and requiredBuildingIndicator is false, then both building and
    // room are required
    if (assetType.isMovingIndicator() && !assetType.isRequiredBuildingIndicator()) {
      // check building
      if (StringUtils.isBlank(buildingCode)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_REQUIRED_FIELD,
                labelBuilding,
                assetTypeCode);
        result &= false;
      } else if (ObjectUtils.isNull(building)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
                labelBuilding);
        result &= false;
      } else if (!building.isActive()) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
                labelBuilding);
        result &= false;
      }

      // check room
      if (StringUtils.isBlank(roomNumber)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_REQUIRED_FIELD,
                labelRoom,
                assetTypeCode);
        result &= false;
      } else if (ObjectUtils.isNull(room)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
                labelRoom);
        result = false;
      } else if (!room.isActive()) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
                labelRoom);
        result &= false;
      }
    }

    // if movingIndicator is false and requiredBuildingIndicator is true, then building is required
    // while room is not allowed
    else if (!assetType.isMovingIndicator() && assetType.isRequiredBuildingIndicator()) {
      // check building
      if (StringUtils.isBlank(buildingCode)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_REQUIRED_FIELD,
                labelBuilding,
                assetTypeCode);
        result &= false;
      } else if (ObjectUtils.isNull(building)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_INVALID_FIELD,
                labelBuilding);
        result &= false;
      } else if (!building.isActive()) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_INACTIVE_FIELD,
                labelBuilding);
        result &= false;
      }

      // check room
      if (StringUtils.isNotBlank(roomNumber)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_NOT_ALLOWED_FIELD,
                labelRoom,
                assetTypeCode);
        result &= false;
      }
    }

    // if both movingIndicator and requiredBuildingIndicator are false, then neither building nor
    // room is allowed
    else if (!assetType.isMovingIndicator() && !assetType.isRequiredBuildingIndicator()) {
      // check building
      if (StringUtils.isNotBlank(buildingCode)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_CODE,
                CamsKeyConstants.BarcodeInventory.ERROR_NOT_ALLOWED_FIELD,
                labelBuilding,
                assetTypeCode);
        result &= false;
      }

      // check room
      if (StringUtils.isNotBlank(roomNumber)) {
        GlobalVariables.getMessageMap()
            .putError(
                CamsPropertyConstants.BarcodeInventory.BUILDING_ROOM_NUMBER,
                CamsKeyConstants.BarcodeInventory.ERROR_NOT_ALLOWED_FIELD,
                labelRoom,
                assetTypeCode);
        result &= false;
      }
    }

    return result;
  }