use of eu.bcvsolutions.idm.core.api.exception.CoreException 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.exception.CoreException 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.exception.CoreException in project CzechIdMng by bcvsolutions.
the class SchedulerConfig method schedulerManager.
@DependsOn(CoreFlywayConfig.NAME)
@Bean(name = "schedulerManager")
public SchedulerManager schedulerManager(ApplicationContext context, IdmDependentTaskTriggerRepository dependentTaskTriggerRepository) {
Scheduler scheduler = schedulerFactoryBean(context).getScheduler();
SchedulerManager manager = new DefaultSchedulerManager(context, schedulerFactoryBean(context).getScheduler(), dependentTaskTriggerRepository);
// read all task - checks obsolete task types and remove them before scheduler starts automatically
try {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(DefaultSchedulerManager.DEFAULT_GROUP_NAME))) {
try {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
if (jobDetail == null) {
// job does not exists
return null;
}
// test task is still installed
jobDetail.getJobClass().getDeclaredConstructor().newInstance();
} catch (org.quartz.SchedulerException ex) {
if (ex.getCause() instanceof ClassNotFoundException) {
manager.deleteTask(jobKey.getName());
//
LOG.warn("Job [{}] inicialization failed, job class was removed, scheduled task is removed.", jobKey, ex);
} else {
throw new CoreException(ex);
}
} catch (ReflectiveOperationException | BeansException | IllegalArgumentException ex) {
manager.deleteTask(jobKey.getName());
//
LOG.warn("Job [{}] inicialization failed, scheduled task is removed", jobKey, ex);
}
}
} catch (org.quartz.SchedulerException ex) {
throw new CoreException(ex);
}
//
return manager;
}
use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.
the class SchedulerConfig method schedulerFactoryBean.
/**
* Enables scheduler injection through application
*
* @param dataSource
* @param jobFactory
* @return
* @throws IOException
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(ApplicationContext context) {
try {
Properties quartzProperties = quartzProperties();
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// update triggers in DB when config file is changed
factory.setOverwriteExistingJobs(true);
// if store is set to DB set data source, else store in RAM
Object store = quartzProperties.get("org.quartz.jobStore.class");
if (store != null && !StringUtils.equals(store.toString(), RAMJobStore.class.getCanonicalName())) {
DataSource dataSource = (DataSource) context.getBean("dataSource");
factory.setDataSource(dataSource);
}
factory.setJobFactory(jobFactory(context));
factory.setQuartzProperties(quartzProperties);
return factory;
} catch (IOException ex) {
throw new CoreException("Quartz properties initialization failed", ex);
}
}
use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.
the class ExceptionUtilsUnitTest method testLogModelLevelError.
@Test
public void testLogModelLevelError() {
ExceptionUtils.log(LOG, new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR), new CoreException("mock"));
// error is logged without model is specified
verify(LOG).error(any(String.class), any(Exception.class));
}
Aggregations