use of eu.bcvsolutions.idm.core.api.domain.Embedded in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method addEmbedded.
/**
* Loads and adds DTOs by embedded annotation
*
* @param dto
* @param requestId
*
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws IntrospectionException
* @throws InstantiationException
*/
private void addEmbedded(AbstractDto dto, UUID requestId) throws ReflectiveOperationException, IllegalArgumentException, IntrospectionException {
Assert.notNull(dto, "DTO is required!");
Field[] fields = dto.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = field.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
// If DTO class is abstract then continue
if (Modifier.isAbstract(embeddedAnnotation.dtoClass().getModifiers())) {
continue;
}
Object value = EntityUtils.getEntityValue(dto, field.getName());
if (value instanceof UUID) {
// Create mock instance of embedded DTO only with ID
UUID id = (UUID) value;
AbstractDto embeddedDto = null;
if (Requestable.class.isAssignableFrom(embeddedAnnotation.dtoClass())) {
embeddedDto = embeddedAnnotation.dtoClass().getDeclaredConstructor().newInstance();
embeddedDto.setId(id);
Requestable originalEmbeddedDto = this.getDtoService((Requestable) embeddedDto).get(embeddedDto.getId());
if (originalEmbeddedDto != null) {
// Call standard method for load request's DTO with original DTO
embeddedDto = (AbstractDto) this.get(requestId, originalEmbeddedDto);
} else {
// Call standard method for load request's DTO with mock DTO (only with ID)
embeddedDto = (AbstractDto) this.get(requestId, (Requestable) embeddedDto);
}
} else {
// If embedded DTO is not Requestable, then standard service is using
embeddedDto = (AbstractDto) lookupService.getDtoService(embeddedAnnotation.dtoClass()).get(id);
}
if (embeddedDto == null) {
continue;
}
embeddedDto.setTrimmed(true);
dto.getEmbedded().put(field.getName(), embeddedDto);
}
}
}
}
}
use of eu.bcvsolutions.idm.core.api.domain.Embedded in project CzechIdMng by bcvsolutions.
the class UuidToEntityConverter method convert.
@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
Class<BaseEntity> entityClass = context.getDestinationType();
UUID sourceUuid = context.getSource();
//
if (sourceUuid != null) {
MappingContext<?, ?> parentContext = context.getParent();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
try {
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getSourceType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
EntityLookup<?> lookup = getLookupService().getEntityLookup(embeddedAnnotation.dtoClass());
if (lookup != null) {
return lookup.lookup(sourceUuid);
}
}
}
} catch (NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
// We do not have lookup by embedded annotation. We try load service for entity
EntityLookup<?> lookup = getLookupService().getEntityLookup(entityClass);
if (lookup != null) {
return lookup.lookup(sourceUuid);
}
}
return null;
}
use of eu.bcvsolutions.idm.core.api.domain.Embedded in project CzechIdMng by bcvsolutions.
the class UuidToEntityConditionalConverter method convert.
@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
Class<BaseEntity> entityClass = context.getDestinationType();
UUID sourceUuid = context.getSource();
//
if (sourceUuid == null) {
return null;
}
MappingContext<?, ?> parentContext = context.getParent();
PropertyMapping propertyMapping = (PropertyMapping) context.getMapping();
// Find name of field by property mapping
String field = propertyMapping.getLastDestinationProperty().getName();
try {
// Find field in DTO class
Field fieldTyp = getFirstFieldInClassHierarchy(parentContext.getSourceType(), field);
if (fieldTyp.isAnnotationPresent(Embedded.class)) {
Embedded embeddedAnnotation = fieldTyp.getAnnotation(Embedded.class);
if (embeddedAnnotation.enabled()) {
EntityLookup<?> lookup = getLookupService().getEntityLookup(embeddedAnnotation.dtoClass());
if (lookup != null) {
return lookup.lookup(sourceUuid);
}
}
}
} catch (NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
// We do not have lookup by embedded annotation. We try load service for entity
EntityLookup<?> lookup = getLookupService().getEntityLookup(entityClass);
if (lookup != null) {
return lookup.lookup(sourceUuid);
}
//
return null;
}
Aggregations