use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class AbstractRecoverableService method deploy.
@Override
@Transactional
public List<DTO> deploy(IdmAttachmentDto attachment, BasePermission... permission) {
Assert.notNull(attachment, "Attachment is required.");
UUID attachmentId = attachment.getId();
Assert.notNull(attachmentId, "Persisted attachment is required.");
InputStream attachmentData = attachmentManager.getAttachmentData(attachmentId);
Assert.notNull(attachmentData, "Attachment data is required.");
//
String attachmentName = attachment.getName();
if (attachment.getMimetype().equals(MediaType.APPLICATION_XML_VALUE) || attachment.getMimetype().equals(MediaType.TEXT_XML_VALUE)) {
LOG.debug("Single resource [{}] will extracted and deployed.", attachmentName);
return Lists.newArrayList(deploy(attachmentName, attachmentData));
}
//
LOG.debug("Archive [{}] will extracted and deployed.", attachmentName);
Map<String, DTO> deploedResources = new HashMap<>();
File zipFile = attachmentManager.createTempFile();
Path zipFolder = attachmentManager.createTempDirectory(null);
try {
// copy archive
Files.copy(attachmentData, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// and extract
ZipUtils.extract(zipFile, zipFolder.toString());
//
File[] listFiles = zipFolder.toFile().listFiles();
LOG.debug("Found [{}] resources on location [{}]", listFiles == null ? 0 : listFiles.length, attachmentName);
//
if (ArrayUtils.isEmpty(listFiles)) {
return Lists.newArrayList();
}
for (File resource : listFiles) {
try {
DTO deployedResource = deploy(resource.getName(), new FileInputStream(resource), permission);
String resourceCode = deployedResource.getCode();
// log error, if resource with the same code was found twice in one resource
if (deploedResources.containsKey(resourceCode)) {
LOG.error("More templates with code [{}] found on the same location [{}].", resourceCode, attachmentName);
}
// last one wins
deploedResources.put(resourceCode, deployedResource);
} catch (IOException ex) {
LOG.error("Failed get input stream from, file name [{}].", resource.getName(), ex);
}
}
//
LOG.info("Redeployed [{}] resources from location [{}]", deploedResources.size(), attachmentName);
return Lists.newArrayList(deploedResources.values());
} catch (Exception ex) {
throw new ResultCodeException(CoreResultCode.DEPLOY_ERROR, ImmutableMap.of("path", attachmentName), ex);
} finally {
FileUtils.deleteQuietly(zipFile);
FileUtils.deleteQuietly(zipFolder.toFile());
}
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class AbstractRecoverableService method backup.
@Override
public File backup(DTO dto) {
Marshaller jaxbMarshaller = initJaxbMarshaller();
//
String directory = getBackupFolder();
File backupFolder = new File(directory);
if (!backupFolder.exists()) {
boolean success = backupFolder.mkdirs();
// if make dir after check if exist, throw error.
if (!success) {
LOG.error("Backup for resource [{}] failed, backup folder path: [{}] can't be created.", dto.getCode(), backupFolder.getAbsolutePath());
throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()));
}
}
//
T type = toType(dto);
File file = new File(getBackupFileName(directory, dto));
try {
String xsdLocation = getXsdLocation();
if (StringUtils.isNotBlank(xsdLocation)) {
jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, xsdLocation);
}
jaxbMarshaller.marshal(type, file);
//
return file;
} catch (JAXBException e) {
LOG.error("Backup for template: {} failed", dto.getCode());
throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()), e);
}
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class ParameterConverter method toBigDecimal.
/**
* Converts parameter to {@code Double} from given parameters.
*
* @param parameters
* @param parameterName
* @return
* @since 10.3.0
*/
public BigDecimal toBigDecimal(Map<String, Object> parameters, String parameterName) {
Assert.notNull(parameters, "Input parameters are required.");
Assert.notNull(parameterName, "Parameter name is required.");
//
Object value = parameters.get(parameterName);
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
//
String valueAsString = toString(value);
if (StringUtils.isNotEmpty(valueAsString)) {
try {
return new BigDecimal((String) valueAsString);
} catch (NumberFormatException ex) {
throw new ResultCodeException(CoreResultCode.BAD_VALUE, ImmutableMap.of(parameterName, valueAsString), ex);
}
}
return null;
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class ContractGuaranteeSaveProcessor method checkControlledBySlices.
/**
* Test if contract of the given contract guarantee has some slices.
*
* @param guarantee
* @return
*/
private void checkControlledBySlices(EntityEvent<IdmContractGuaranteeDto> event) {
IdmContractGuaranteeDto guarantee = event.getContent();
if (getBooleanProperty(ContractSliceManager.SKIP_CHECK_FOR_SLICES, event.getProperties())) {
return;
}
UUID contract = guarantee.getIdentityContract();
IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
sliceFilter.setParentContract(contract);
if (contract != null && sliceService.count(sliceFilter) > 0) {
throw new ResultCodeException(CoreResultCode.CONTRACT_IS_CONTROLLED_GUARANTEE_CANNOT_BE_MODIFIED, ImmutableMap.of("contractId", contract));
}
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class DelegationDefinitionSaveProcessor method process.
@Override
public EventResult<IdmDelegationDefinitionDto> process(EntityEvent<IdmDelegationDefinitionDto> event) {
IdmDelegationDefinitionDto dto = event.getContent();
if (!service.isNew(dto)) {
throw new ResultCodeException(CoreResultCode.DELEGATION_DEFINITION_CANNOT_BE_UPDATED);
}
// Validations
UUID delegateId = dto.getDelegate();
UUID delegatorId = dto.getDelegator();
Assert.notNull(delegateId, "Delegate ID cannot be null!");
Assert.notNull(delegatorId, "Delegator ID cannot be null!");
if (delegateId.equals(delegatorId)) {
throw new ResultCodeException(CoreResultCode.DELEGATION_DEFINITION_DELEGATOR_AND_DELEGATE_ARE_SAME, ImmutableMap.of("identity", delegateId));
}
dto = service.saveInternal(dto);
event.setContent(dto);
return new DefaultEventResult<>(event, this);
}
Aggregations