Search in sources :

Example 6 with AllowedAttributeValueEntity

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

the class AllowedAttributeValueDaoTestHelper method createAllowedAttributeValueEntities.

/**
 * Returns a list of test allowed attribute value entities.
 *
 * @return the list of allowed attribute value entities
 */
public List<AllowedAttributeValueEntity> createAllowedAttributeValueEntities(AttributeValueListKey attributeValueListKey, List<String> allowedAttributeValues) {
    AttributeValueListEntity attributeValueListEntity = attributeValueListDao.getAttributeValueListByKey(attributeValueListKey);
    if (attributeValueListEntity == null) {
        attributeValueListEntity = attributeValueListDaoTestHelper.createAttributeValueListEntity(attributeValueListKey.getNamespace(), attributeValueListKey.getAttributeValueListName());
    }
    List<AllowedAttributeValueEntity> allowedAttributeValueEntities = new ArrayList<>();
    for (String allowedAttributeValue : allowedAttributeValues) {
        AllowedAttributeValueEntity allowedAttributeValueEntity = new AllowedAttributeValueEntity();
        allowedAttributeValueEntity.setAttributeValueList(attributeValueListEntity);
        allowedAttributeValueEntity.setAllowedAttributeValue(allowedAttributeValue);
        allowedAttributeValueEntities.add(allowedAttributeValueDao.saveAndRefresh(allowedAttributeValueEntity));
    }
    return allowedAttributeValueEntities;
}
Also used : ArrayList(java.util.ArrayList) AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity)

Example 7 with AllowedAttributeValueEntity

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

the class AllowedAttributeValueDaoTest method testGetAllowedAttributeValues.

@Test
public void testGetAllowedAttributeValues() {
    // Create attribute value list key.
    AttributeValueListKey attributeValueListKey = new AttributeValueListKey(NAMESPACE_CODE, ATTRIBUTE_VALUE_LIST_NAME);
    List<String> allowedAttributeValueList = allowedAttributeValueDaoTestHelper.getTestUnsortedAllowedAttributeValues();
    // Create and persist a attribute value list key entity.
    attributeValueListDaoTestHelper.createAttributeValueListEntity(NAMESPACE_CODE, ATTRIBUTE_VALUE_LIST_NAME);
    // Create and persist a list of allowed attribute values.
    allowedAttributeValueDaoTestHelper.createAllowedAttributeValueEntities(attributeValueListKey, allowedAttributeValueList);
    // Get the allowed attribute values for the specified key.
    List<AllowedAttributeValueEntity> responseEntities = allowedAttributeValueDao.getAllowedAttributeValuesByAttributeValueListKey(attributeValueListKey);
    // Create a list of allowed attribute values.
    List<String> allowedAttributesResponse = new ArrayList<>();
    responseEntities.forEach((responseEntity) -> {
        allowedAttributesResponse.add(responseEntity.getAllowedAttributeValue());
    });
    // Validate the response is sorted by allowed attribute values.
    Collections.sort(allowedAttributeValueList);
    assertEquals(allowedAttributeValueList, allowedAttributesResponse);
}
Also used : ArrayList(java.util.ArrayList) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) AttributeValueListKey(org.finra.herd.model.api.xml.AttributeValueListKey) Test(org.junit.Test)

Example 8 with AllowedAttributeValueEntity

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

the class AllowedAttributeValueDaoImpl method getAllowedAttributeValuesByAttributeValueListKey.

@Override
public List<AllowedAttributeValueEntity> getAllowedAttributeValuesByAttributeValueListKey(AttributeValueListKey attributeValueListKey) {
    // Create the criteria builder and the criteria.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<AllowedAttributeValueEntity> criteria = builder.createQuery(AllowedAttributeValueEntity.class);
    // The criteria root is the allowed attribute value.
    Root<AllowedAttributeValueEntity> allowedAttributeValueEntityRoot = criteria.from(AllowedAttributeValueEntity.class);
    // Join to the other tables we can filter on.
    Join<AllowedAttributeValueEntity, AttributeValueListEntity> attributeValueListEntityJoin = allowedAttributeValueEntityRoot.join(AllowedAttributeValueEntity_.attributeValueList);
    Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityJoin.join(AttributeValueListEntity_.namespace);
    // Create the standard restrictions (i.e. the standard where clauses).
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(builder.upper(attributeValueListEntityJoin.get(AttributeValueListEntity_.name)), attributeValueListKey.getAttributeValueListName().toUpperCase()));
    predicates.add(builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)), attributeValueListKey.getNamespace().toUpperCase()));
    // Order the results by allowed attribute value.
    Order orderByAllowedAttributeValue = builder.asc(allowedAttributeValueEntityRoot.get(AllowedAttributeValueEntity_.allowedAttributeValue));
    // Add the clauses for the query.
    criteria.select(allowedAttributeValueEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderByAllowedAttributeValue);
    // Execute the query and return the results.
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Order(javax.persistence.criteria.Order) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) ArrayList(java.util.ArrayList) AllowedAttributeValueEntity(org.finra.herd.model.jpa.AllowedAttributeValueEntity) AttributeValueListEntity(org.finra.herd.model.jpa.AttributeValueListEntity) Predicate(javax.persistence.criteria.Predicate)

Example 9 with AllowedAttributeValueEntity

use of org.finra.herd.model.jpa.AllowedAttributeValueEntity 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 10 with AllowedAttributeValueEntity

use of org.finra.herd.model.jpa.AllowedAttributeValueEntity 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)

Aggregations

AllowedAttributeValueEntity (org.finra.herd.model.jpa.AllowedAttributeValueEntity)13 AttributeValueListEntity (org.finra.herd.model.jpa.AttributeValueListEntity)11 ArrayList (java.util.ArrayList)8 AttributeValueListKey (org.finra.herd.model.api.xml.AttributeValueListKey)7 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)6 Test (org.junit.Test)6 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)3 AllowedAttributeValuesInformation (org.finra.herd.model.api.xml.AllowedAttributeValuesInformation)3 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)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 GlobalAttributeDefinitionEntity (org.finra.herd.model.jpa.GlobalAttributeDefinitionEntity)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Order (javax.persistence.criteria.Order)1 Predicate (javax.persistence.criteria.Predicate)1