use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.
the class EntityToUuidConverter method convert.
@Override
public UUID convert(MappingContext<BaseEntity, UUID> context) {
if (context != null && context.getSource() != null && context.getSource().getId() instanceof UUID) {
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().newInstance();
}
dto.setTrimmed(true);
// Separate map entity to new embedded DTO
modeler.map(entity, dto);
embedded.put(field, dto);
// Add filled DTO to embedded map to parent DTO
parentDto.setEmbedded(embedded);
}
}
} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
throw new CoreException(e);
}
}
return (UUID) context.getSource().getId();
}
return null;
}
use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.
the class UuidToUuidConverter method convert.
@Override
public UUID convert(MappingContext<UUID, UUID> context) {
if (context != null && context.getSource() != null && context.getSource() instanceof UUID) {
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 = (UUID) 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 == null ? null : (UUID) context.getSource();
}
use of org.modelmapper.spi.PropertyMapping in project CzechIdMng by bcvsolutions.
the class UuidToEntityConverter method convert.
@Override
public BaseEntity convert(MappingContext<UUID, BaseEntity> context) {
if (context != null && context.getSource() != null) {
UUID sourceUUID = context.getSource();
Class<BaseEntity> entityClass = context.getDestinationType();
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