Search in sources :

Example 26 with MolgenisDataException

use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.

the class JobScheduler method runNow.

/**
 * Executes a {@link ScheduledJob} immediately.
 *
 * @param scheduledJobId ID of the {@link ScheduledJob} to run
 */
public synchronized void runNow(String scheduledJobId) {
    ScheduledJob scheduledJob = getJob(scheduledJobId);
    try {
        JobKey jobKey = new JobKey(scheduledJobId, SCHEDULED_JOB_GROUP);
        if (quartzScheduler.checkExists(jobKey)) {
            // Run job now
            quartzScheduler.triggerJob(jobKey);
        } else {
            // Schedule with 'now' trigger
            Trigger trigger = newTrigger().withIdentity(scheduledJobId, SCHEDULED_JOB_GROUP).startNow().build();
            schedule(scheduledJob, trigger);
        }
    } catch (SchedulerException e) {
        LOG.error("Error runNow ScheduledJob", e);
        throw new MolgenisDataException("Error job runNow", e);
    }
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) ScheduledJob(org.molgenis.jobs.model.ScheduledJob)

Example 27 with MolgenisDataException

use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.

the class PostgreSqlUtils method getPostgreSqlQueryValue.

/**
 * Returns the PostgreSQL query value for the given entity attribute. For query operators requiring a list of
 * values (e.g. IN or RANGE) this method must be called for each individual query value.
 *
 * @param queryValue value of the type that matches the attribute type
 * @param attr       attribute
 * @return PostgreSQL value
 */
static Object getPostgreSqlQueryValue(Object queryValue, Attribute attr) {
    while (true) {
        String attrName = attr.getName();
        AttributeType attrType = attr.getDataType();
        switch(attrType) {
            case BOOL:
                if (queryValue != null && !(queryValue instanceof Boolean)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Boolean.class.getSimpleName()));
                }
                return queryValue;
            case CATEGORICAL:
            // one query value
            case CATEGORICAL_MREF:
            case FILE:
            // one query value
            case MREF:
            case XREF:
            case ONE_TO_MANY:
                // queries values referencing an entity can either be the entity itself or the entity id
                if (queryValue != null) {
                    if (queryValue instanceof Entity) {
                        queryValue = ((Entity) queryValue).getIdValue();
                    }
                    attr = attr.getRefEntity().getIdAttribute();
                    continue;
                } else {
                    return null;
                }
            case DATE:
                if (queryValue != null && !(queryValue instanceof LocalDate)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), LocalDate.class.getSimpleName()));
                }
                return queryValue;
            case DATE_TIME:
                if (queryValue == null) {
                    return null;
                }
                if (!(queryValue instanceof Instant)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Instant.class.getSimpleName()));
                }
                return ((Instant) queryValue).atOffset(UTC);
            case DECIMAL:
                if (queryValue != null && !(queryValue instanceof Double)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Double.class.getSimpleName()));
                }
                return queryValue;
            case ENUM:
                // enum query values can be an enum or enum string
                if (queryValue != null) {
                    if (queryValue instanceof String) {
                        return queryValue;
                    } else if (queryValue instanceof Enum<?>) {
                        return queryValue.toString();
                    } else {
                        throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s] or [%s]", attrName, queryValue.getClass().getSimpleName(), String.class.getSimpleName(), Enum.class.getSimpleName()));
                    }
                } else {
                    return null;
                }
            case EMAIL:
            case HTML:
            case HYPERLINK:
            case SCRIPT:
            case STRING:
            case TEXT:
                if (queryValue != null && !(queryValue instanceof String)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), String.class.getSimpleName()));
                }
                return queryValue;
            case INT:
                if (queryValue != null && !(queryValue instanceof Integer)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Integer.class.getSimpleName()));
                }
                return queryValue;
            case LONG:
                if (queryValue != null && !(queryValue instanceof Long)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Long.class.getSimpleName()));
                }
                return queryValue;
            case COMPOUND:
                throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
            default:
                throw new UnexpectedEnumException(attrType);
        }
    }
}
Also used : Entity(org.molgenis.data.Entity) Instant(java.time.Instant) LocalDate(java.time.LocalDate) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 28 with MolgenisDataException

use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.

the class MappingProjectRepositoryImpl method update.

@Override
@Transactional
public void update(MappingProject mappingProject) {
    MappingProject existing = getMappingProject(mappingProject.getIdentifier());
    if (existing == null) {
        throw new MolgenisDataException("MappingProject does not exist");
    }
    Entity mappingProjectEntity = toEntity(mappingProject);
    dataService.update(MAPPING_PROJECT, mappingProjectEntity);
}
Also used : MappingProject(org.molgenis.semanticmapper.mapping.model.MappingProject) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with MolgenisDataException

use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.

the class FileRepositoryCollectionFactory method createFileRepositoryCollection.

/**
 * Factory method for creating a new FileRepositorySource
 * <p>
 * For example an excel file
 */
public FileRepositoryCollection createFileRepositoryCollection(File file) {
    Class<? extends FileRepositoryCollection> clazz;
    String extension = FileExtensionUtils.findExtensionFromPossibilities(file.getName(), fileRepositoryCollection.keySet());
    clazz = fileRepositoryCollection.get(extension);
    if (clazz == null) {
        throw new MolgenisDataException("Unknown extension for file '" + file.getName() + "'");
    }
    Constructor<? extends FileRepositoryCollection> ctor;
    try {
        ctor = clazz.getConstructor(File.class);
    } catch (Exception e) {
        throw new MolgenisDataException("Exception creating [" + clazz + "]  missing constructor FileRepositorySource(File file)");
    }
    FileRepositoryCollection fileRepositoryCollection = BeanUtils.instantiateClass(ctor, file);
    autowireCapableBeanFactory.autowireBeanProperties(fileRepositoryCollection, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    try {
        fileRepositoryCollection.init();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return fileRepositoryCollection;
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) FileRepositoryCollection(org.molgenis.data.file.support.FileRepositoryCollection) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 30 with MolgenisDataException

use of org.molgenis.data.MolgenisDataException in project molgenis by molgenis.

the class ImportRunService method createAndSendStatusMail.

private void createAndSendStatusMail(ImportRun importRun) {
    try {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(userService.getUser(importRun.getUsername()).getEmail());
        mailMessage.setSubject(createMailTitle(importRun));
        mailMessage.setText(createEnglishMailText(importRun, ZoneId.systemDefault()));
        mailSender.send(mailMessage);
    } catch (MailException mce) {
        LOG.error("Could not send import status mail", mce);
        throw new MolgenisDataException("An error occurred. Please contact the administrator.");
    }
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Aggregations

MolgenisDataException (org.molgenis.data.MolgenisDataException)51 Entity (org.molgenis.data.Entity)16 Attribute (org.molgenis.data.meta.model.Attribute)11 EntityType (org.molgenis.data.meta.model.EntityType)11 Test (org.testng.annotations.Test)7 IOException (java.io.IOException)6 List (java.util.List)6 DynamicEntity (org.molgenis.data.support.DynamicEntity)6 File (java.io.File)5 AttributeType (org.molgenis.data.meta.AttributeType)5 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)5 Instant (java.time.Instant)4 LocalDate (java.time.LocalDate)4 Collectors.toList (java.util.stream.Collectors.toList)4 DataService (org.molgenis.data.DataService)4 RepositoryCollection (org.molgenis.data.RepositoryCollection)4 String.format (java.lang.String.format)3 DateTimeParseException (java.time.format.DateTimeParseException)3 Map (java.util.Map)3 Stream (java.util.stream.Stream)3