Search in sources :

Example 21 with AttributeValueListEntity

use of org.finra.herd.model.jpa.AttributeValueListEntity in project herd by FINRAOS.

the class AllowedAttributeValueServiceImpl method deleteAllowedAttributeValues.

/**
 * Deletes specified allowed attribute values from an existing attribute value list which is identified by name.
 *
 * @param request the information needed to delete the allowed attribute values
 *
 * @return the allowed attribute values that got deleted
 */
@NamespacePermission(fields = "#request.attributeValueListKey.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public AllowedAttributeValuesInformation deleteAllowedAttributeValues(AllowedAttributeValuesDeleteRequest request) {
    // Perform request validation and trim request parameters.
    validateAllowedAttributeValuesDeleteRequest(request);
    // Retrieve and ensure that a attribute value list exists with the specified name.
    AttributeValueListEntity attributeValueListEntity = attributeValueListDaoHelper.getAttributeValueListEntity(request.getAttributeValueListKey());
    // Load all existing allowed attribute value entities into a map for quick access.
    Map<String, AllowedAttributeValueEntity> allowedAttributeValueEntityMap = getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues());
    // Build a list of all allowed attribute value entities to be deleted.
    Collection<AllowedAttributeValueEntity> deletedAllowedAttributeValueEntities = new ArrayList<>();
    for (String allowedAttributeValue : request.getAllowedAttributeValues()) {
        // Find the relative allowed attribute entity.
        AllowedAttributeValueEntity allowedAttributeValueEntity = allowedAttributeValueEntityMap.get(allowedAttributeValue);
        if (allowedAttributeValueEntity != null) {
            deletedAllowedAttributeValueEntities.add(allowedAttributeValueEntity);
        } else {
            throw new ObjectNotFoundException(String.format("Allowed attribute value \"%s\" doesn't exist in \"%s\" attribute value list.", allowedAttributeValue, attributeValueListEntity.getName()));
        }
    }
    // Perform the actual deletion.
    for (AllowedAttributeValueEntity allowedAttributeValueEntity : deletedAllowedAttributeValueEntities) {
        attributeValueListEntity.getAllowedAttributeValues().remove(allowedAttributeValueEntity);
    }
    allowedAttributeValueDao.saveAndRefresh(attributeValueListEntity);
    return createAllowedAttributeValuesInformationFromEntities(attributeValueListEntity, deletedAllowedAttributeValueEntities);
}
Also used : ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) ArrayList(java.util.ArrayList) AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 22 with AttributeValueListEntity

use of org.finra.herd.model.jpa.AttributeValueListEntity in project herd by FINRAOS.

the class AllowedAttributeValueServiceImpl method createAllowedAttributeValues.

/**
 * Creates a list of allowed attribute values for an existing attribute value list key.
 *
 * @param request the information needed to create the allowed attribute values
 *
 * @return the newly created allowed attribute values
 */
@NamespacePermission(fields = "#request.attributeValueListKey.namespace", permissions = NamespacePermissionEnum.WRITE)
@Override
public AllowedAttributeValuesInformation createAllowedAttributeValues(AllowedAttributeValuesCreateRequest request) {
    // Perform request validation and trim request parameters.
    validateAllowedAttributeValuesCreateRequest(request);
    // Retrieve and ensure that a attribute value list exists with the specified name.
    AttributeValueListEntity attributeValueListEntity = attributeValueListDaoHelper.getAttributeValueListEntity(request.getAttributeValueListKey());
    // Load all existing allowed attribute value entities into a map for quick access.
    Map<String, AllowedAttributeValueEntity> allowedAttributeValueEntityMap = getAllowedAttributeValueEntityMap(attributeValueListEntity.getAllowedAttributeValues());
    // Fail if any of the allowed attribute values to be created already exist.
    for (String allowedAttributeValue : request.getAllowedAttributeValues()) {
        if (allowedAttributeValueEntityMap.containsKey(allowedAttributeValue)) {
            throw new AlreadyExistsException(String.format("Allowed attribute value \"%s\" already exists in \"%s\" attribute value list.", allowedAttributeValue, attributeValueListEntity.getName()));
        }
    }
    // Create and persist the allowed attribute value entities.
    Collection<AllowedAttributeValueEntity> createdAllowedAttributeValueEntities = new ArrayList<>();
    for (String allowedAttributeValue : request.getAllowedAttributeValues()) {
        AllowedAttributeValueEntity allowedAttributeValueEntity = new AllowedAttributeValueEntity();
        createdAllowedAttributeValueEntities.add(allowedAttributeValueEntity);
        allowedAttributeValueEntity.setAttributeValueList(attributeValueListEntity);
        allowedAttributeValueEntity.setAllowedAttributeValue(allowedAttributeValue);
        allowedAttributeValueDao.saveAndRefresh(allowedAttributeValueEntity);
    }
    allowedAttributeValueDao.saveAndRefresh(attributeValueListEntity);
    return createAllowedAttributeValuesInformationFromEntities(attributeValueListEntity, createdAllowedAttributeValueEntities);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) ArrayList(java.util.ArrayList) AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 23 with AttributeValueListEntity

use of org.finra.herd.model.jpa.AttributeValueListEntity in project herd by FINRAOS.

the class AllowedAttributeValueServiceImpl method getAllowedAttributeValues.

/**
 * Retrieves existing allowed attribute values based on the specified key
 *
 * @param attributeValueListKey the attribute value list key
 *
 * @return the allowed attribute values information
 */
@NamespacePermission(fields = "#attributeValueListKey.namespace", permissions = NamespacePermissionEnum.READ)
@Override
public AllowedAttributeValuesInformation getAllowedAttributeValues(AttributeValueListKey attributeValueListKey) {
    // Perform validation and trim of the input parameters.
    attributeValueListHelper.validateAttributeValueListKey(attributeValueListKey);
    // Retrieve and ensure that a attribute value list exists with the specified name.
    AttributeValueListEntity attributeValueListEntity = attributeValueListDaoHelper.getAttributeValueListEntity(attributeValueListKey);
    // Retrieve a list of allowed attribute values.
    List<AllowedAttributeValueEntity> allowedAttributeValueEntities = allowedAttributeValueDao.getAllowedAttributeValuesByAttributeValueListKey(attributeValueListKey);
    return createAllowedAttributeValuesInformationFromEntities(attributeValueListEntity, allowedAttributeValueEntities);
}
Also used : AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 24 with AttributeValueListEntity

use of org.finra.herd.model.jpa.AttributeValueListEntity in project herd by FINRAOS.

the class AttributeValueListServiceImpl method createAttributeValueListEntity.

/**
 * Creates and persists a new attribute value list entity from the request information.
 *
 * @param request the request
 * @param namespaceEntity the namespace
 *
 * @return the newly created attribute value list entity
 */
private AttributeValueListEntity createAttributeValueListEntity(AttributeValueListCreateRequest request, NamespaceEntity namespaceEntity) {
    // Create a new entity.
    AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity();
    attributeValueListEntity.setNamespace(namespaceEntity);
    attributeValueListEntity.setName(request.getAttributeValueListKey().getAttributeValueListName());
    // Persist and return the newly created entity.
    return attributeValueListDao.saveAndRefresh(attributeValueListEntity);
}
Also used : AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity)

Example 25 with AttributeValueListEntity

use of org.finra.herd.model.jpa.AttributeValueListEntity in project herd by FINRAOS.

the class AttributeValueListDaoHelperTest method testGetAttributeValueListEntity.

@Test
public void testGetAttributeValueListEntity() {
    // Create an attribute value list key.
    AttributeValueListKey attributeValueListKey = new AttributeValueListKey(ATTRIBUTE_VALUE_LIST_NAMESPACE, ATTRIBUTE_VALUE_LIST_NAME);
    // Create an attribute value list entity.
    AttributeValueListEntity attributeValueListEntity = new AttributeValueListEntity();
    // Mock calls to external methods.
    when(attributeValueListDao.getAttributeValueListByKey(attributeValueListKey)).thenReturn(attributeValueListEntity);
    // Call the method under test.
    AttributeValueListEntity result = attributeValueListDaoHelper.getAttributeValueListEntity(attributeValueListKey);
    // Verify the external calls.
    verify(attributeValueListDao).getAttributeValueListByKey(attributeValueListKey);
    verifyNoMoreInteractions(attributeValueListDao);
    // Validate the result.
    assertEquals(attributeValueListEntity, result);
}
Also used : AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AttributeValueListKey(org.finra.herd.model.api.xml.AttributeValueListKey) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Aggregations

AttributeValueListEntity (org.finra.herd.model.jpa.AttributeValueListEntity)27 AttributeValueListKey (org.finra.herd.model.api.xml.AttributeValueListKey)14 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)14 Test (org.junit.Test)12 AllowedAttributeValueEntity (org.finra.herd.model.jpa.AllowedAttributeValueEntity)11 ArrayList (java.util.ArrayList)9 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)6 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 Predicate (javax.persistence.criteria.Predicate)3 AllowedAttributeValuesInformation (org.finra.herd.model.api.xml.AllowedAttributeValuesInformation)3 AttributeValueList (org.finra.herd.model.api.xml.AttributeValueList)3 GlobalAttributeDefinitionEntity (org.finra.herd.model.jpa.GlobalAttributeDefinitionEntity)3 Order (javax.persistence.criteria.Order)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 AllowedAttributeValuesCreateRequest (org.finra.herd.model.api.xml.AllowedAttributeValuesCreateRequest)2 AllowedAttributeValuesDeleteRequest (org.finra.herd.model.api.xml.AllowedAttributeValuesDeleteRequest)2 AttributeValueListCreateRequest (org.finra.herd.model.api.xml.AttributeValueListCreateRequest)2 GlobalAttributeDefinition (org.finra.herd.model.api.xml.GlobalAttributeDefinition)2 GlobalAttributeDefinitionKey (org.finra.herd.model.api.xml.GlobalAttributeDefinitionKey)2