/**
  * Creates and returns a business object data attribute key from a specified business object data
  * attribute entity.
  *
  * @param businessObjectDataAttributeEntity the business object data attribute entity
  * @return the newly created business object data attribute key
  */
 private BusinessObjectDataAttributeKey getBusinessObjectDataAttributeKey(
     BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity) {
   return new BusinessObjectDataAttributeKey(
       businessObjectDataAttributeEntity
           .getBusinessObjectData()
           .getBusinessObjectFormat()
           .getBusinessObjectDefinition()
           .getNamespace()
           .getCode(),
       businessObjectDataAttributeEntity
           .getBusinessObjectData()
           .getBusinessObjectFormat()
           .getBusinessObjectDefinition()
           .getName(),
       businessObjectDataAttributeEntity
           .getBusinessObjectData()
           .getBusinessObjectFormat()
           .getUsage(),
       businessObjectDataAttributeEntity
           .getBusinessObjectData()
           .getBusinessObjectFormat()
           .getFileType()
           .getCode(),
       businessObjectDataAttributeEntity
           .getBusinessObjectData()
           .getBusinessObjectFormat()
           .getBusinessObjectFormatVersion(),
       businessObjectDataAttributeEntity.getBusinessObjectData().getPartitionValue(),
       dmHelper.getSubPartitionValues(businessObjectDataAttributeEntity.getBusinessObjectData()),
       businessObjectDataAttributeEntity.getBusinessObjectData().getVersion(),
       businessObjectDataAttributeEntity.getName());
 }
  /**
   * Creates the business object data attribute from the persisted entity.
   *
   * @param businessObjectDataAttributeEntity the business object data attribute entity
   * @return the business object data attribute
   */
  private BusinessObjectDataAttribute createBusinessObjectDataAttributeFromEntity(
      BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity) {
    // Create the business object data attribute.
    BusinessObjectDataAttribute businessObjectDataAttribute = new BusinessObjectDataAttribute();

    businessObjectDataAttribute.setId(businessObjectDataAttributeEntity.getId());
    businessObjectDataAttribute.setBusinessObjectDataAttributeKey(
        getBusinessObjectDataAttributeKey(businessObjectDataAttributeEntity));
    businessObjectDataAttribute.setBusinessObjectDataAttributeValue(
        businessObjectDataAttributeEntity.getValue());

    return businessObjectDataAttribute;
  }
  /**
   * Creates a new business object data attribute entity from the business object data entity and
   * the request information.
   *
   * @param businessObjectDataEntity the business object data entity
   * @param request the business object data attribute create request
   * @return the newly created business object data attribute entity
   */
  private BusinessObjectDataAttributeEntity createBusinessObjectDataAttributeEntity(
      BusinessObjectDataEntity businessObjectDataEntity,
      BusinessObjectDataAttributeCreateRequest request) {
    // Create a new entity.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity =
        new BusinessObjectDataAttributeEntity();

    businessObjectDataAttributeEntity.setBusinessObjectData(businessObjectDataEntity);
    businessObjectDataAttributeEntity.setName(
        request.getBusinessObjectDataAttributeKey().getBusinessObjectDataAttributeName());
    businessObjectDataAttributeEntity.setValue(request.getBusinessObjectDataAttributeValue());

    return businessObjectDataAttributeEntity;
  }
  /**
   * Updates an existing business object data attribute by key.
   *
   * @param businessObjectDataAttributeKey the business object data attribute key
   * @return the business object data attribute information
   */
  @Override
  public BusinessObjectDataAttribute updateBusinessObjectDataAttribute(
      BusinessObjectDataAttributeKey businessObjectDataAttributeKey,
      BusinessObjectDataAttributeUpdateRequest request) {
    // Validate and trim the key.
    dmHelper.validateBusinessObjectDataAttributeKey(businessObjectDataAttributeKey);

    // If namespace is not specified, get the namespace code by locating the legacy business object
    // definition.
    populateLegacyNamespace(businessObjectDataAttributeKey);

    // Get the business object format and ensure it exists.
    BusinessObjectFormatEntity businessObjectFormatEntity =
        dmDaoHelper.getBusinessObjectFormatEntity(
            new BusinessObjectFormatKey(
                businessObjectDataAttributeKey.getNamespace(),
                businessObjectDataAttributeKey.getBusinessObjectDefinitionName(),
                businessObjectDataAttributeKey.getBusinessObjectFormatUsage(),
                businessObjectDataAttributeKey.getBusinessObjectFormatFileType(),
                businessObjectDataAttributeKey.getBusinessObjectFormatVersion()));

    // Validate the attribute value.
    if (dmDaoHelper.isBusinessObjectDataAttributeRequired(
        businessObjectDataAttributeKey.getBusinessObjectDataAttributeName(),
        businessObjectFormatEntity)) {
      Assert.hasText(
          request.getBusinessObjectDataAttributeValue(),
          String.format(
              "A business object data attribute value must be specified since \"%s\" is a required attribute for business object format {%s}.",
              businessObjectDataAttributeKey.getBusinessObjectDataAttributeName(),
              dmDaoHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
    }

    // Retrieve and ensure that a business object data attribute exists with the specified key.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity =
        dmDaoHelper.getBusinessObjectDataAttributeEntity(businessObjectDataAttributeKey);

    // Update the entity with the new values.
    businessObjectDataAttributeEntity.setValue(request.getBusinessObjectDataAttributeValue());

    // Persist the entity.
    businessObjectDataAttributeEntity = dmDao.saveAndRefresh(businessObjectDataAttributeEntity);

    // Create and return the business object data attribute object from the persisted entity.
    return createBusinessObjectDataAttributeFromEntity(businessObjectDataAttributeEntity);
  }
  /**
   * Deletes an existing business object data attribute by key.
   *
   * @param businessObjectDataAttributeKey the business object data attribute key
   * @return the business object data attribute that got deleted
   */
  @Override
  public BusinessObjectDataAttribute deleteBusinessObjectDataAttribute(
      BusinessObjectDataAttributeKey businessObjectDataAttributeKey) {
    // Validate and trim the key.
    dmHelper.validateBusinessObjectDataAttributeKey(businessObjectDataAttributeKey);

    // If namespace is not specified, get the namespace code by locating the legacy business object
    // definition.
    populateLegacyNamespace(businessObjectDataAttributeKey);

    // Get the business object format and ensure it exists.
    BusinessObjectFormatEntity businessObjectFormatEntity =
        dmDaoHelper.getBusinessObjectFormatEntity(
            new BusinessObjectFormatKey(
                businessObjectDataAttributeKey.getNamespace(),
                businessObjectDataAttributeKey.getBusinessObjectDefinitionName(),
                businessObjectDataAttributeKey.getBusinessObjectFormatUsage(),
                businessObjectDataAttributeKey.getBusinessObjectFormatFileType(),
                businessObjectDataAttributeKey.getBusinessObjectFormatVersion()));

    // Make sure we are not trying to delete a required attribute.
    if (dmDaoHelper.isBusinessObjectDataAttributeRequired(
        businessObjectDataAttributeKey.getBusinessObjectDataAttributeName(),
        businessObjectFormatEntity)) {
      throw new IllegalArgumentException(
          String.format(
              "Cannot delete \"%s\" attribute since it is a required attribute for business object format {%s}.",
              businessObjectDataAttributeKey.getBusinessObjectDataAttributeName(),
              dmDaoHelper.businessObjectFormatEntityAltKeyToString(businessObjectFormatEntity)));
    }

    // Retrieve and ensure that a business object data attribute exists with the specified key.
    BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity =
        dmDaoHelper.getBusinessObjectDataAttributeEntity(businessObjectDataAttributeKey);

    // Delete the business object data attribute.
    BusinessObjectDataEntity businessObjectDataEntity =
        businessObjectDataAttributeEntity.getBusinessObjectData();
    businessObjectDataEntity.getAttributes().remove(businessObjectDataAttributeEntity);
    dmDao.saveAndRefresh(businessObjectDataEntity);

    // Create and return the business object data attribute object from the deleted entity.
    return createBusinessObjectDataAttributeFromEntity(businessObjectDataAttributeEntity);
  }