use of org.finra.herd.model.jpa.BusinessObjectDefinitionAttributeEntity in project herd by FINRAOS.
the class BusinessObjectDefinitionDaoHelper method createBusinessObjectDefinitionEntity.
/**
* Create Business Object Definition Entity
* @param request business object definition create request
* @return Business Object Definition Entity
*/
public BusinessObjectDefinitionEntity createBusinessObjectDefinitionEntity(BusinessObjectDefinitionCreateRequest request) {
// Perform the validation.
validateBusinessObjectDefinitionCreateRequest(request);
// Get the namespace and ensure it exists.
NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(request.getNamespace());
// Get the data provider and ensure it exists.
DataProviderEntity dataProviderEntity = dataProviderDaoHelper.getDataProviderEntity(request.getDataProviderName());
// Get business object definition key.
BusinessObjectDefinitionKey businessObjectDefinitionKey = new BusinessObjectDefinitionKey(request.getNamespace(), request.getBusinessObjectDefinitionName());
// Ensure a business object definition with the specified key doesn't already exist.
BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(businessObjectDefinitionKey);
if (businessObjectDefinitionEntity != null) {
throw new AlreadyExistsException(String.format("Unable to create business object definition with name \"%s\" because it already exists for namespace \"%s\".", businessObjectDefinitionKey.getBusinessObjectDefinitionName(), businessObjectDefinitionKey.getNamespace()));
}
// Create a new entity.
businessObjectDefinitionEntity = new BusinessObjectDefinitionEntity();
businessObjectDefinitionEntity.setNamespace(namespaceEntity);
businessObjectDefinitionEntity.setName(request.getBusinessObjectDefinitionName());
businessObjectDefinitionEntity.setDescription(request.getDescription());
businessObjectDefinitionEntity.setDataProvider(dataProviderEntity);
businessObjectDefinitionEntity.setDisplayName(request.getDisplayName());
// Create the attributes if they are specified.
if (!CollectionUtils.isEmpty(request.getAttributes())) {
List<BusinessObjectDefinitionAttributeEntity> attributeEntities = new ArrayList<>();
businessObjectDefinitionEntity.setAttributes(attributeEntities);
for (Attribute attribute : request.getAttributes()) {
BusinessObjectDefinitionAttributeEntity attributeEntity = new BusinessObjectDefinitionAttributeEntity();
attributeEntities.add(attributeEntity);
attributeEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
}
}
// Persist the change event entity
saveBusinessObjectDefinitionChangeEvents(businessObjectDefinitionEntity);
// Persist and return the new entity.
return businessObjectDefinitionDao.saveAndRefresh(businessObjectDefinitionEntity);
}
use of org.finra.herd.model.jpa.BusinessObjectDefinitionAttributeEntity in project herd by FINRAOS.
the class BusinessObjectDefinitionDaoTestHelper method createBusinessObjectDefinitionEntity.
/**
* Creates and persists a new business object definition entity.
*
* @return the newly created business object definition entity
*/
public BusinessObjectDefinitionEntity createBusinessObjectDefinitionEntity(NamespaceEntity namespaceEntity, String businessObjectDefinitionName, DataProviderEntity dataProviderEntity, String businessObjectDefinitionDescription, String displayName, List<Attribute> attributes, List<SampleDataFile> sampleDataFiles) {
BusinessObjectDefinitionEntity businessObjectDefinitionEntity = new BusinessObjectDefinitionEntity();
businessObjectDefinitionEntity.setNamespace(namespaceEntity);
businessObjectDefinitionEntity.setDataProvider(dataProviderEntity);
businessObjectDefinitionEntity.setName(businessObjectDefinitionName);
businessObjectDefinitionEntity.setDescription(businessObjectDefinitionDescription);
businessObjectDefinitionEntity.setDisplayName(displayName);
// Create business object definition attribute entities if they are specified.
if (!CollectionUtils.isEmpty(attributes)) {
List<BusinessObjectDefinitionAttributeEntity> attributeEntities = new ArrayList<>();
businessObjectDefinitionEntity.setAttributes(attributeEntities);
for (Attribute attribute : attributes) {
BusinessObjectDefinitionAttributeEntity attributeEntity = new BusinessObjectDefinitionAttributeEntity();
attributeEntities.add(attributeEntity);
attributeEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
}
}
// Create business object definition sample data file entities if they are specified.
if (!CollectionUtils.isEmpty(sampleDataFiles)) {
// Create a storage entity if needed.
StorageEntity storageEntity = storageDao.getStorageByName(AbstractDaoTest.STORAGE_NAME);
if (storageEntity == null) {
storageEntity = storageDaoTestHelper.createStorageEntity(AbstractDaoTest.STORAGE_NAME);
}
// Create sample data file entities.
List<BusinessObjectDefinitionSampleDataFileEntity> sampleDataFileEntities = new ArrayList<>();
businessObjectDefinitionEntity.setSampleDataFiles(sampleDataFileEntities);
for (SampleDataFile sampleDataFile : sampleDataFiles) {
BusinessObjectDefinitionSampleDataFileEntity sampleDataFileEntity = new BusinessObjectDefinitionSampleDataFileEntity();
sampleDataFileEntities.add(sampleDataFileEntity);
sampleDataFileEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
sampleDataFileEntity.setDirectoryPath(sampleDataFile.getDirectoryPath());
sampleDataFileEntity.setFileName(sampleDataFile.getFileName());
sampleDataFileEntity.setFileSizeBytes(AbstractDaoTest.FILE_SIZE_1_KB);
sampleDataFileEntity.setStorage(storageEntity);
}
}
return businessObjectDefinitionDao.saveAndRefresh(businessObjectDefinitionEntity);
}
use of org.finra.herd.model.jpa.BusinessObjectDefinitionAttributeEntity in project herd by FINRAOS.
the class BusinessObjectDefinitionServiceImpl method updateBusinessObjectDefinitionEntity.
/**
* Update and persist the business object definition per specified update request.
*
* @param businessObjectDefinitionEntity the business object definition entity
* @param request the business object definition update request
*/
private void updateBusinessObjectDefinitionEntity(BusinessObjectDefinitionEntity businessObjectDefinitionEntity, BusinessObjectDefinitionUpdateRequest request) {
// Update the entity with the new description value.
businessObjectDefinitionEntity.setDescription(request.getDescription());
businessObjectDefinitionEntity.setDisplayName(request.getDisplayName());
// Update the attributes.
// Load all existing attribute entities in a map with a "lowercase" attribute name as the key for case insensitivity.
Map<String, BusinessObjectDefinitionAttributeEntity> existingAttributeEntities = new HashMap<>();
for (BusinessObjectDefinitionAttributeEntity attributeEntity : businessObjectDefinitionEntity.getAttributes()) {
String mapKey = attributeEntity.getName().toLowerCase();
if (existingAttributeEntities.containsKey(mapKey)) {
throw new IllegalStateException(String.format("Found duplicate attribute with name \"%s\" for business object definition {namespace: \"%s\", businessObjectDefinitionName: \"%s\"}.", mapKey, businessObjectDefinitionEntity.getNamespace().getCode(), businessObjectDefinitionEntity.getName()));
}
existingAttributeEntities.put(mapKey, attributeEntity);
}
// Process the list of attributes to determine that business object definition attribute entities should be created, updated, or deleted.
List<BusinessObjectDefinitionAttributeEntity> createdAttributeEntities = new ArrayList<>();
List<BusinessObjectDefinitionAttributeEntity> retainedAttributeEntities = new ArrayList<>();
if (!CollectionUtils.isEmpty(request.getAttributes())) {
for (Attribute attribute : request.getAttributes()) {
// 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.
BusinessObjectDefinitionAttributeEntity attributeEntity = existingAttributeEntities.get(lowercaseAttributeName);
if (!StringUtils.equals(attribute.getValue(), attributeEntity.getValue())) {
// Update the business object attribute entity.
attributeEntity.setValue(attribute.getValue());
}
// Add this entity to the list of business object definition attribute entities to be retained.
retainedAttributeEntities.add(attributeEntity);
} else {
// Create a new business object attribute entity.
BusinessObjectDefinitionAttributeEntity attributeEntity = new BusinessObjectDefinitionAttributeEntity();
businessObjectDefinitionEntity.getAttributes().add(attributeEntity);
attributeEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
attributeEntity.setName(attribute.getName());
attributeEntity.setValue(attribute.getValue());
// Add this entity to the list of the newly created business object definition attribute entities.
retainedAttributeEntities.add(attributeEntity);
}
}
}
// Remove any of the currently existing attribute entities that did not get onto the retained entities list.
businessObjectDefinitionEntity.getAttributes().retainAll(retainedAttributeEntities);
// Add all of the newly created business object definition attribute entities.
businessObjectDefinitionEntity.getAttributes().addAll(createdAttributeEntities);
// Persist the change event entity
saveBusinessObjectDefinitionChangeEvents(businessObjectDefinitionEntity);
// Persist the entity.
businessObjectDefinitionDao.saveAndRefresh(businessObjectDefinitionEntity);
}
use of org.finra.herd.model.jpa.BusinessObjectDefinitionAttributeEntity in project herd by FINRAOS.
the class BusinessObjectDefinitionServiceImpl method createBusinessObjectDefinitionFromEntity.
/**
* Creates a business object definition from the persisted entity.
*
* @param businessObjectDefinitionEntity the business object definition entity
*
* @return the business object definition
*/
private BusinessObjectDefinition createBusinessObjectDefinitionFromEntity(BusinessObjectDefinitionEntity businessObjectDefinitionEntity, Boolean includeBusinessObjectDefinitionUpdateHistory) {
// Create a business object definition.
BusinessObjectDefinition businessObjectDefinition = new BusinessObjectDefinition();
businessObjectDefinition.setId(businessObjectDefinitionEntity.getId());
businessObjectDefinition.setNamespace(businessObjectDefinitionEntity.getNamespace().getCode());
businessObjectDefinition.setBusinessObjectDefinitionName(businessObjectDefinitionEntity.getName());
businessObjectDefinition.setDescription(businessObjectDefinitionEntity.getDescription());
businessObjectDefinition.setDataProviderName(businessObjectDefinitionEntity.getDataProvider().getName());
businessObjectDefinition.setDisplayName(businessObjectDefinitionEntity.getDisplayName());
// Add attributes.
List<Attribute> attributes = new ArrayList<>();
businessObjectDefinition.setAttributes(attributes);
for (BusinessObjectDefinitionAttributeEntity attributeEntity : businessObjectDefinitionEntity.getAttributes()) {
attributes.add(new Attribute(attributeEntity.getName(), attributeEntity.getValue()));
}
if (businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat() != null) {
BusinessObjectFormatEntity descriptiveFormatEntity = businessObjectDefinitionEntity.getDescriptiveBusinessObjectFormat();
DescriptiveBusinessObjectFormat descriptiveBusinessObjectFormat = new DescriptiveBusinessObjectFormat();
businessObjectDefinition.setDescriptiveBusinessObjectFormat(descriptiveBusinessObjectFormat);
descriptiveBusinessObjectFormat.setBusinessObjectFormatUsage(descriptiveFormatEntity.getUsage());
descriptiveBusinessObjectFormat.setBusinessObjectFormatFileType(descriptiveFormatEntity.getFileType().getCode());
descriptiveBusinessObjectFormat.setBusinessObjectFormatVersion(descriptiveFormatEntity.getBusinessObjectFormatVersion());
}
// Add sample data files.
List<SampleDataFile> sampleDataFiles = new ArrayList<>();
businessObjectDefinition.setSampleDataFiles(sampleDataFiles);
for (BusinessObjectDefinitionSampleDataFileEntity sampleDataFileEntity : businessObjectDefinitionEntity.getSampleDataFiles()) {
sampleDataFiles.add(new SampleDataFile(sampleDataFileEntity.getDirectoryPath(), sampleDataFileEntity.getFileName()));
}
// Add auditable fields.
businessObjectDefinition.setCreatedByUserId(businessObjectDefinitionEntity.getCreatedBy());
businessObjectDefinition.setLastUpdatedByUserId(businessObjectDefinitionEntity.getUpdatedBy());
businessObjectDefinition.setLastUpdatedOn(HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDefinitionEntity.getUpdatedOn()));
// Add change events.
final List<BusinessObjectDefinitionChangeEvent> businessObjectDefinitionChangeEvents = new ArrayList<>();
if (BooleanUtils.isTrue(includeBusinessObjectDefinitionUpdateHistory)) {
businessObjectDefinitionEntity.getChangeEvents().forEach(businessObjectDefinitionChangeEventEntity -> {
DescriptiveBusinessObjectFormatUpdateRequest descriptiveBusinessObjectFormatUpdateRequest = null;
if (businessObjectDefinitionChangeEventEntity.getFileType() != null) {
descriptiveBusinessObjectFormatUpdateRequest = new DescriptiveBusinessObjectFormatUpdateRequest(businessObjectDefinitionChangeEventEntity.getUsage(), businessObjectDefinitionChangeEventEntity.getFileType());
}
businessObjectDefinitionChangeEvents.add(new BusinessObjectDefinitionChangeEvent(businessObjectDefinitionChangeEventEntity.getDisplayName(), businessObjectDefinitionChangeEventEntity.getDescription(), descriptiveBusinessObjectFormatUpdateRequest, HerdDateUtils.getXMLGregorianCalendarValue(businessObjectDefinitionChangeEventEntity.getCreatedOn()), businessObjectDefinitionChangeEventEntity.getCreatedBy()));
});
}
businessObjectDefinition.setBusinessObjectDefinitionChangeEvents(businessObjectDefinitionChangeEvents);
return businessObjectDefinition;
}
Aggregations