Search in sources :

Example 11 with CoreException

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();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 12 with CoreException

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();
}
Also used : BaseEntity(eu.bcvsolutions.idm.core.api.entity.BaseEntity) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Field(java.lang.reflect.Field) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) PropertyMapping(org.modelmapper.spi.PropertyMapping) Embedded(eu.bcvsolutions.idm.core.api.domain.Embedded) UUID(java.util.UUID)

Example 13 with CoreException

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;
}
Also used : DefaultSchedulerManager(eu.bcvsolutions.idm.core.scheduler.service.impl.DefaultSchedulerManager) Scheduler(org.quartz.Scheduler) DefaultSchedulerManager(eu.bcvsolutions.idm.core.scheduler.service.impl.DefaultSchedulerManager) SchedulerManager(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulerManager) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) BeansException(org.springframework.beans.BeansException) DependsOn(org.springframework.context.annotation.DependsOn) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 14 with CoreException

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);
    }
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) RAMJobStore(org.quartz.simpl.RAMJobStore) IOException(java.io.IOException) Properties(java.util.Properties) DataSource(javax.sql.DataSource) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 15 with CoreException

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));
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Aggregations

CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)64 UUID (java.util.UUID)16 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)15 Test (org.junit.Test)14 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)10 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)9 Field (java.lang.reflect.Field)9 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)8 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)8 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)8 List (java.util.List)8 Requestable (eu.bcvsolutions.idm.core.api.domain.Requestable)7 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)7 IdmRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRequestDto)7 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)7 RequestOperationType (eu.bcvsolutions.idm.core.api.domain.RequestOperationType)6 IntrospectionException (java.beans.IntrospectionException)6