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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations