use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class RequestManagerTest method testDeleteRequestIntegrity.
@Test
public void testDeleteRequestIntegrity() {
// Log as admin, but not user 'admin' (we don't want to skip WF)
IdmIdentityDto adminUser = getHelper().createIdentity();
loginAsAdmin(adminUser.getUsername());
// Create role
IdmRoleDto role = getHelper().createRole();
// Create request
IdmRequestDto request = requestManager.createRequest(role);
Assert.assertNotNull(request);
// Create guarantee
IdmIdentityDto guarantee = getHelper().createIdentity();
IdmRoleGuaranteeDto roleGuarantee = new IdmRoleGuaranteeDto();
roleGuarantee.setRole(role.getId());
roleGuarantee.setGuarantee(guarantee.getId());
Requestable requestablePost = requestManager.post(request.getId(), roleGuarantee);
IdmRequestItemDto changeRequestItem = DtoUtils.getEmbedded((AbstractDto) requestablePost, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class);
Assert.assertEquals(RequestOperationType.ADD, changeRequestItem.getOperation());
Assert.assertTrue(requestablePost instanceof IdmRoleGuaranteeDto);
// Change role (without save)
role.setDescription(getHelper().createName());
role.setPriority(1000);
// Create request item
Requestable requestable = requestManager.post(request.getId(), role);
Assert.assertNotNull(requestable);
Assert.assertNotNull(requestable.getRequestItem());
changeRequestItem = DtoUtils.getEmbedded((AbstractDto) requestable, Requestable.REQUEST_ITEM_FIELD, IdmRequestItemDto.class);
Assert.assertEquals(RequestOperationType.UPDATE, changeRequestItem.getOperation());
Assert.assertTrue(requestable instanceof IdmRoleDto);
// Request should be in concept state
request = requestService.get(request.getId());
Assert.assertEquals(RequestState.CONCEPT, request.getState());
// Two items should be created
Assert.assertEquals(2, requestManager.findRequestItems(request.getId(), null).size());
// Delete the request
requestService.delete(request);
IdmRequestDto requestDeleted = requestService.get(request.getId());
Assert.assertNull(requestDeleted);
// All items should be deleted
Assert.assertEquals(0, requestManager.findRequestItems(request.getId(), null).size());
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class UuidToUuidConverter method convert.
@Override
public UUID convert(MappingContext<UUID, UUID> context) {
if (context.getSource() == null) {
return null;
}
MappingContext<?, ?> parentContext = context.getParent();
if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
try {
AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
UUID entityId = context.getSource();
Map<String, BaseDto> embedded = parentDto.getEmbedded();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
// Load DTO service by dtoClass and get DTO by UUID
ReadDtoService<?, ?> lookup = getLookupService().getDtoService(embeddedAnnotation.dtoClass());
if (lookup != null) {
AbstractDto dto = (AbstractDto) lookup.get(entityId);
dto.setTrimmed(true);
embedded.put(field, dto);
// Add filled DTO to embedded map to parent DTO
parentDto.setEmbedded(embedded);
}
}
}
} catch (NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
}
return context.getSource();
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class EntityToUuidConditionalConverter method convert.
@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
if (context.getSource() == null || !(context.getSource().getId() instanceof UUID)) {
return null;
}
//
MappingContext<?, ?> parentContext = context.getParent();
if (parentContext == null || parentContext.getDestination() == null || !AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) || parentContext.getSource() == null || !BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
return (UUID) context.getSource().getId();
}
//
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
// dto type
Class<?> destinationType = parentContext.getDestinationType();
try {
AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
BaseEntity entity = (BaseEntity) context.getSource();
Map<String, BaseDto> embedded = parentDto.getEmbedded();
// Find field in DTO class
// FIXME: Embedded will not be set, if field name is different with getter and setter (fieldType == null when setter has different name).
Field fieldType = getFirstFieldInClassHierarchy(destinationType, field);
if (fieldType.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldType.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
// If has field Embedded (enabled) annotation, then
// we will create new
// instance of DTO
//
AbstractDto dto = null;
// If dto class is abstract get dto from lookup
if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
dto = (AbstractDto) getLookupService().lookupDto(entity.getClass(), entity.getId());
} else {
dto = embeddedAnnotation.dtoClass().getDeclaredConstructor().newInstance();
}
dto.setTrimmed(true);
// Separate map entity to new embedded DTO
try {
dto = getLookupService().toDto(entity, dto, null);
} catch (Exception ex) {
LOG.warn("DTO [{}] cannot be converted into embedded by underlying service, try to convert by default converter.", dto.getClass().getCanonicalName(), ex);
modeler.map(entity, dto);
}
embedded.put(field, dto);
// Add filled DTO to embedded map to parent DTO
parentDto.setEmbedded(embedded);
}
}
} catch (NoSuchFieldException ex) {
// FIXME: Embedded will not be set, if field name is different with getter and setter (fieldType == null when setter has different name).
LOG.debug("Field [{}] in dto class [{}] not found, embedded dto cannot be filled. Different dto field name vs. getter and setter name is not supported.", field, destinationType);
} catch (ReflectiveOperationException ex) {
throw new CoreException(ex);
}
//
return (UUID) context.getSource().getId();
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class EntityToUuidConverter method convert.
@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
if (context.getSource() == null || !(context.getSource().getId() instanceof UUID)) {
return null;
}
MappingContext<?, ?> parentContext = context.getParent();
if (parentContext != null && parentContext.getDestination() != null && AbstractDto.class.isAssignableFrom(parentContext.getDestinationType()) && parentContext.getSource() != null && BaseEntity.class.isAssignableFrom(parentContext.getSourceType())) {
try {
AbstractDto parentDto = (AbstractDto) parentContext.getDestination();
BaseEntity entity = (BaseEntity) context.getSource();
Map<String, BaseDto> embedded = parentDto.getEmbedded();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getDestinationType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
// If has field Embedded (enabled) annotation, then
// we will create new
// instance of DTO.
//
AbstractDto dto = null;
// If dto class is abstract get dto from lookup.
if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
dto = (AbstractDto) getLookupService().lookupDto(entity.getClass(), entity.getId());
} else {
dto = embeddedAnnotation.dtoClass().getDeclaredConstructor().newInstance();
}
dto.setTrimmed(true);
// Separate map entity to new embedded DTO.
try {
dto = getLookupService().toDto(entity, dto, null);
} catch (Exception ex) {
LOG.warn("DTO [{}] cannot be converted into embedded by underlying service, try to convert by default converter.", dto.getClass().getCanonicalName(), ex);
modeler.map(entity, dto);
}
embedded.put(field, dto);
// Add filled DTO to embedded map to parent DTO.
parentDto.setEmbedded(embedded);
}
}
} catch (ReflectiveOperationException ex) {
throw new CoreException(ex);
}
}
return (UUID) context.getSource().getId();
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class IdmTokenController method toFilter.
@Override
protected IdmTokenFilter toFilter(MultiValueMap<String, Object> parameters) {
IdmTokenFilter filter = new IdmTokenFilter(parameters, getParameterConverter());
// owner decorator
String ownerId = getParameterConverter().toString(parameters, IdmEntityEventFilter.PARAMETER_OWNER_ID);
UUID ownerUuid = null;
String ownerType = filter.getOwnerType();
if (StringUtils.isNotEmpty(ownerType) && StringUtils.isNotEmpty(ownerId)) {
// try to find entity owner by Codeable identifier
AbstractDto owner = manager.findOwner(ownerType, ownerId);
if (owner != null) {
ownerUuid = owner.getId();
} else {
LOG.debug("Entity type [{}] with identifier [{}] does not found, raw ownerId will be used as uuid.", ownerType, ownerId);
// Better exception for FE.
try {
DtoUtils.toUuid(ownerId);
} catch (ClassCastException ex) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", ownerId), ex);
}
}
}
if (ownerUuid == null) {
ownerUuid = getParameterConverter().toUuid(parameters, IdmEntityEventFilter.PARAMETER_OWNER_ID);
}
filter.setOwnerId(ownerUuid);
//
return filter;
}
Aggregations