use of eu.bcvsolutions.idm.core.api.domain.Embedded in project CzechIdMng by bcvsolutions.
the class DefaultLookupService method lookupEmbeddedDto.
@Override
@SuppressWarnings("unchecked")
public <DTO extends BaseDto> DTO lookupEmbeddedDto(AbstractDto dto, String attributeName) {
Assert.notNull(dto, "DTO is required.");
Assert.notNull(dto.getEmbedded(), "DTO does not have embedded DTO map initialized and is required.");
Assert.hasLength(attributeName, "Singular attribute is required to get embedded DTO.");
//
// from embedded
DTO embeddedDto = DtoUtils.getEmbedded(dto, attributeName, (DTO) null);
if (embeddedDto != null) {
return embeddedDto;
}
// try to load by lookup
try {
// get target DTO type by embedded annotation - annotation is required
Field embeddableField = EntityUtils.getFirstFieldInClassHierarchy(dto.getClass(), attributeName);
if (!embeddableField.isAnnotationPresent(Embedded.class)) {
throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported. Embedded annotion is missing.", dto.getClass(), attributeName));
}
Embedded embedded = embeddableField.getAnnotation(Embedded.class);
//
// get DTO identifier ~ field value
PropertyDescriptor fieldDescriptor = EntityUtils.getFieldDescriptor(dto, attributeName);
Object fieldValue = fieldDescriptor.getReadMethod().invoke(dto);
if (!(fieldValue instanceof Serializable)) {
throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported.", dto.getClass(), attributeName));
}
//
return (DTO) lookupDto(embedded.dtoClass(), (Serializable) fieldValue);
} catch (ReflectiveOperationException | IntrospectionException ex) {
throw new IllegalArgumentException(String.format("Dto lookup for dto type [%s] attribute [%s] is not supported.", dto.getClass(), attributeName), ex);
}
}
use of eu.bcvsolutions.idm.core.api.domain.Embedded in project CzechIdMng by bcvsolutions.
the class DefaultRequestManager method dtoToMap.
private Map<String, Object> dtoToMap(AbstractDto dto) {
Map<String, Object> results = new HashMap<>();
if (dto == null) {
return results;
}
try {
List<PropertyDescriptor> descriptors = Lists.newArrayList(Introspector.getBeanInfo(dto.getClass()).getPropertyDescriptors());
List<Field> fields = //
Lists.newArrayList(dto.getClass().getDeclaredFields()).stream().filter(//
field -> !Requestable.REQUEST_ITEM_FIELD.equals(field.getName())).filter(//
field -> !Requestable.REQUEST_FIELD.equals(field.getName())).filter(//
field -> !field.isAnnotationPresent(JsonIgnore.class)).collect(//
Collectors.toList());
// Embedded objects
//
fields.stream().filter(//
field -> field.isAnnotationPresent(Embedded.class)).forEach(field -> {
results.put(field.getName(), dto.getEmbedded().get(field.getName()));
});
// Others objects
//
fields.stream().filter(//
field -> !field.isAnnotationPresent(Embedded.class)).forEach(field -> {
try {
PropertyDescriptor fieldDescriptor = //
descriptors.stream().filter(//
descriptor -> field.getName().equals(descriptor.getName())).findFirst().orElse(//
null);
if (fieldDescriptor != null) {
Object value = fieldDescriptor.getReadMethod().invoke(dto);
results.put(field.getName(), value);
}
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
throw new CoreException(e);
}
});
} catch (IntrospectionException e) {
throw new CoreException(e);
}
return results;
}
use of eu.bcvsolutions.idm.core.api.domain.Embedded 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.domain.Embedded 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.domain.Embedded 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();
}
Aggregations