Search in sources :

Example 1 with StorageAttributeEntity

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

the class StorageServiceImpl method createStorage.

@Override
public Storage createStorage(StorageCreateRequest storageCreateRequest) {
    // Perform validation and trim.
    validateAndTrimStorageCreateRequest(storageCreateRequest);
    // Retrieve storage platform.
    StoragePlatformEntity storagePlatformEntity = storagePlatformHelper.getStoragePlatformEntity(storageCreateRequest.getStoragePlatformName());
    // See if a storage with the specified name already exists.
    StorageEntity storageEntity = storageDao.getStorageByName(storageCreateRequest.getName());
    if (storageEntity != null) {
        throw new AlreadyExistsException(String.format("Storage with name \"%s\" already exists.", storageCreateRequest.getName()));
    }
    // Create a storage entity.
    storageEntity = new StorageEntity();
    storageEntity.setName(storageCreateRequest.getName());
    storageEntity.setStoragePlatform(storagePlatformEntity);
    // Create attributes if they are specified.
    if (!CollectionUtils.isEmpty(storageCreateRequest.getAttributes())) {
        List<StorageAttributeEntity> attributeEntities = new ArrayList<>();
        storageEntity.setAttributes(attributeEntities);
        for (Attribute attribute : storageCreateRequest.getAttributes()) {
            StorageAttributeEntity attributeEntity = new StorageAttributeEntity();
            attributeEntities.add(attributeEntity);
            attributeEntity.setStorage(storageEntity);
            attributeEntity.setName(attribute.getName());
            attributeEntity.setValue(attribute.getValue());
        }
    }
    // Persist the storage entity.
    storageEntity = storageDao.saveAndRefresh(storageEntity);
    // Return the storage information.
    return createStorageFromEntity(storageEntity);
}
Also used : AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) Attribute(org.finra.herd.model.api.xml.Attribute) StoragePlatformEntity(org.finra.herd.model.jpa.StoragePlatformEntity) StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity)

Example 2 with StorageAttributeEntity

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

the class StorageServiceImpl method updateStorageAttributesHelper.

/**
 * Updates storage attributes.
 *
 * @param storageEntity the storage entity
 * @param attributes the list of attributes
 * @param storageKey the storage key
 */
private void updateStorageAttributesHelper(StorageEntity storageEntity, List<Attribute> attributes, StorageKey storageKey) {
    // Load all existing attribute entities in a map with a "lowercase" attribute name as the key for case insensitivity.
    Map<String, StorageAttributeEntity> existingAttributeEntities = new HashMap<>();
    for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) {
        String mapKey = attributeEntity.getName().toLowerCase();
        if (existingAttributeEntities.containsKey(mapKey)) {
            throw new IllegalStateException(String.format("Found duplicate attribute with name \"%s\" for \"%s\" storage.", mapKey, storageKey.getStorageName()));
        }
        existingAttributeEntities.put(mapKey, attributeEntity);
    }
    // Process the list of attributes to determine that storage attribute entities should be created, updated, or deleted.
    List<StorageAttributeEntity> createdAttributeEntities = new ArrayList<>();
    List<StorageAttributeEntity> retainedAttributeEntities = new ArrayList<>();
    if (!CollectionUtils.isEmpty(attributes)) {
        for (Attribute attribute : attributes) {
            // Use a "lowercase" attribute name for case insensitivity.
            String lowercaseAttributeName = attribute.getName().toLowerCase();
            if (existingAttributeEntities.containsKey(lowercaseAttributeName)) {
                // Check if the attribute value needs to be updated.
                StorageAttributeEntity attributeEntity = existingAttributeEntities.get(lowercaseAttributeName);
                if (!StringUtils.equals(attribute.getValue(), attributeEntity.getValue())) {
                    // Update the attribute entity.
                    attributeEntity.setValue(attribute.getValue());
                }
                // Add this entity to the list of attribute entities to be retained.
                retainedAttributeEntities.add(attributeEntity);
            } else {
                // Create a new attribute entity.
                StorageAttributeEntity attributeEntity = new StorageAttributeEntity();
                storageEntity.getAttributes().add(attributeEntity);
                attributeEntity.setStorage(storageEntity);
                attributeEntity.setName(attribute.getName());
                attributeEntity.setValue(attribute.getValue());
                // Add this entity to the list of the newly created attribute entities.
                createdAttributeEntities.add(attributeEntity);
            }
        }
    }
    // Remove any of the currently existing attribute entities that did not get onto the retained entities list.
    storageEntity.getAttributes().retainAll(retainedAttributeEntities);
    // Add all of the newly created attribute entities.
    storageEntity.getAttributes().addAll(createdAttributeEntities);
}
Also used : HashMap(java.util.HashMap) Attribute(org.finra.herd.model.api.xml.Attribute) StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity) ArrayList(java.util.ArrayList)

Example 3 with StorageAttributeEntity

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

the class StorageHelper method getStorageAttributeValueByName.

/**
 * Gets attribute value by name from the storage entity while specifying whether the attribute is required and whether the attribute value is required.
 *
 * @param attributeName the attribute name (case insensitive)
 * @param storageEntity the storage entity
 * @param attributeRequired specifies whether the attribute is mandatory (i.e. whether it has a value or not).
 * @param attributeValueRequiredIfExists specifies whether the attribute value is mandatory (i.e. the attribute must exist and its value must also contain a
 * value).
 *
 * @return the attribute value from the attribute with the attribute name.
 * @throws IllegalStateException if the attribute is mandatory and this storage contains no attribute with this attribute name or the value is blank.This
 * will produce a 500 HTTP status code error. If storage attributes are able to be updated by a REST invocation in the future, we might want to consider
 * making this a 400 instead since the user has the ability to fix the issue on their own.
 */
public String getStorageAttributeValueByName(String attributeName, StorageEntity storageEntity, boolean attributeRequired, boolean attributeValueRequiredIfExists) throws IllegalStateException {
    boolean attributeExists = false;
    String attributeValue = null;
    for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) {
        if (attributeEntity.getName().equalsIgnoreCase(attributeName)) {
            attributeExists = true;
            attributeValue = attributeEntity.getValue();
            break;
        }
    }
    // If the attribute must exist and doesn't, throw an exception.
    if (attributeRequired && !attributeExists) {
        throw new IllegalStateException(String.format("Attribute \"%s\" for \"%s\" storage must be configured.", attributeName, storageEntity.getName()));
    }
    // If the attribute is configured, but has a blank value, throw an exception.
    if (attributeExists && attributeValueRequiredIfExists && StringUtils.isBlank(attributeValue)) {
        throw new IllegalStateException(String.format("Attribute \"%s\" for \"%s\" storage must have a value that is not blank.", attributeName, storageEntity.getName()));
    }
    return attributeValue;
}
Also used : StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity)

Example 4 with StorageAttributeEntity

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

the class StorageDaoTestHelper method createStorageAttributeEntity.

/**
 * Creates and persists a new storage attribute entity.
 *
 * @param storageEntity the storage entity to add the attribute to
 * @param attributeName the attribute name
 * @param attributeValue the attribute value
 *
 * @return the newly created storage attribute entity.
 */
public StorageAttributeEntity createStorageAttributeEntity(StorageEntity storageEntity, String attributeName, String attributeValue) {
    StorageAttributeEntity storageAttributeEntity = new StorageAttributeEntity();
    storageAttributeEntity.setStorage(storageEntity);
    storageAttributeEntity.setName(attributeName);
    storageAttributeEntity.setValue(attributeValue);
    return storageDao.saveAndRefresh(storageAttributeEntity);
}
Also used : StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity)

Example 5 with StorageAttributeEntity

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

the class StorageDaoTestHelper method createStorageEntity.

/**
 * Creates and persists a new storage entity.
 *
 * @param storageName the storage name
 * @param storagePlatformEntity the storage platform entity
 *
 * @return the newly created storage entity.
 */
public StorageEntity createStorageEntity(String storageName, StoragePlatformEntity storagePlatformEntity, List<Attribute> attributes) {
    StorageEntity storageEntity = new StorageEntity();
    storageEntity.setName(storageName);
    storageEntity.setStoragePlatform(storagePlatformEntity);
    // Create the attributes if they are specified.
    if (!CollectionUtils.isEmpty(attributes)) {
        List<StorageAttributeEntity> attributeEntities = new ArrayList<>();
        storageEntity.setAttributes(attributeEntities);
        for (Attribute attribute : attributes) {
            StorageAttributeEntity attributeEntity = new StorageAttributeEntity();
            attributeEntities.add(attributeEntity);
            attributeEntity.setStorage(storageEntity);
            attributeEntity.setName(attribute.getName());
            attributeEntity.setValue(attribute.getValue());
        }
    }
    return storageDao.saveAndRefresh(storageEntity);
}
Also used : Attribute(org.finra.herd.model.api.xml.Attribute) StorageAttributeEntity(org.finra.herd.model.jpa.StorageAttributeEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity)

Aggregations

StorageAttributeEntity (org.finra.herd.model.jpa.StorageAttributeEntity)8 StorageEntity (org.finra.herd.model.jpa.StorageEntity)5 ArrayList (java.util.ArrayList)4 Attribute (org.finra.herd.model.api.xml.Attribute)4 Storage (org.finra.herd.model.api.xml.Storage)2 HashMap (java.util.HashMap)1 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)1 StorageAttributesUpdateRequest (org.finra.herd.model.api.xml.StorageAttributesUpdateRequest)1 StorageCreateRequest (org.finra.herd.model.api.xml.StorageCreateRequest)1 StorageDirectory (org.finra.herd.model.api.xml.StorageDirectory)1 StorageFile (org.finra.herd.model.api.xml.StorageFile)1 StorageKey (org.finra.herd.model.api.xml.StorageKey)1 StorageUnit (org.finra.herd.model.api.xml.StorageUnit)1 StorageUnitStatusChangeEvent (org.finra.herd.model.api.xml.StorageUnitStatusChangeEvent)1 StorageFileEntity (org.finra.herd.model.jpa.StorageFileEntity)1 StoragePlatformEntity (org.finra.herd.model.jpa.StoragePlatformEntity)1 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)1 StorageUnitStatusHistoryEntity (org.finra.herd.model.jpa.StorageUnitStatusHistoryEntity)1 Test (org.junit.Test)1