/**
  * Populates a business object data attribute key with a legacy namespace if namespace if not
  * there.
  *
  * @param key the business object data attribute key
  */
 private void populateLegacyNamespace(BusinessObjectDataAttributeKey key) {
   if (StringUtils.isBlank(key.getNamespace())) {
     // Set namespace to the retrieved namespace code value for the specified legacy business
     // object definition.
     key.setNamespace(dmDaoHelper.getNamespaceCode(key.getBusinessObjectDefinitionName()));
   }
 }
  /**
   * 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);
  }