Search in sources :

Example 1 with MolgenisDataException

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

the class ExcelUtils method renameSheet.

public static void renameSheet(String newSheetname, File file, int index) {
    try (FileInputStream fis = new FileInputStream(file);
        Workbook workbook = WorkbookFactory.create(fis)) {
        workbook.setSheetName(index, newSheetname);
        workbook.write(new FileOutputStream(file));
    } catch (Exception e) {
        throw new MolgenisDataException(e);
    }
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 2 with MolgenisDataException

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

the class ImportServiceFactory method getImportService.

public ImportService getImportService(String fileName) {
    final Map<String, ImportService> importServicesMappedToExtensions = Maps.newHashMap();
    for (ImportService importService : importServices) {
        for (String extension : importService.getSupportedFileExtensions()) {
            importServicesMappedToExtensions.put(extension.toLowerCase(), importService);
        }
    }
    String extension = FileExtensionUtils.findExtensionFromPossibilities(fileName, importServicesMappedToExtensions.keySet());
    final ImportService importService = importServicesMappedToExtensions.get(extension);
    if (importService == null)
        throw new MolgenisDataException("Can not import file. No suitable importer found");
    return importService;
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 3 with MolgenisDataException

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

the class ImportServiceFactory method getImportService.

/**
 * Finds a suitable ImportService for a FileRepositoryCollection.
 * <p>
 * Import of mixed backend types in one FileRepositoryCollection isn't supported.
 *
 * @return ImportService
 * @throws MolgenisDataException if no suitable ImportService is found for the FileRepositoryCollection
 */
public ImportService getImportService(File file, RepositoryCollection source) {
    final Map<String, ImportService> importServicesMappedToExtensions = Maps.newHashMap();
    importServices.stream().filter(importService -> importService.canImport(file, source)).forEach(importService -> {
        for (String extension : importService.getSupportedFileExtensions()) {
            importServicesMappedToExtensions.put(extension.toLowerCase(), importService);
        }
    });
    String extension = FileExtensionUtils.findExtensionFromPossibilities(file.getName(), importServicesMappedToExtensions.keySet());
    final ImportService importService = importServicesMappedToExtensions.get(extension);
    if (importService == null)
        throw new MolgenisDataException("Can not import file. No suitable importer found");
    return importService;
}
Also used : Component(org.springframework.stereotype.Component) List(java.util.List) Lists(com.google.common.collect.Lists) RepositoryCollection(org.molgenis.data.RepositoryCollection) Map(java.util.Map) FileExtensionUtils(org.molgenis.data.file.util.FileExtensionUtils) Set(java.util.Set) Maps(com.google.common.collect.Maps) MolgenisDataException(org.molgenis.data.MolgenisDataException) OrderComparator(org.springframework.core.OrderComparator) File(java.io.File) Collectors.toSet(java.util.stream.Collectors.toSet) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 4 with MolgenisDataException

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

the class DynamicEntity method validateValueType.

/**
 * Validate is value is of the type defined by the attribute data type.
 *
 * @param attrName attribute name
 * @param value    value (must be of the type defined by the attribute data type.)
 */
protected void validateValueType(String attrName, Object value) {
    if (value == null) {
        return;
    }
    Attribute attr = entityType.getAttribute(attrName);
    if (attr == null) {
        throw new UnknownAttributeException(entityType, attrName);
    }
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        case BOOL:
            if (!(value instanceof Boolean)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Boolean.class.getSimpleName(), attrName));
            }
            break;
        case CATEGORICAL:
        // expected type is FileMeta. validation is not possible because molgenis-data does not depend on molgenis-file
        case FILE:
        case XREF:
            if (!(value instanceof Entity)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Entity.class.getSimpleName(), attrName));
            }
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            if (!(value instanceof Iterable)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Iterable.class.getSimpleName(), attrName));
            }
            break;
        case COMPOUND:
            throw new IllegalArgumentException(format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName));
        case DATE:
            if (!(value instanceof LocalDate)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), LocalDate.class.getSimpleName(), attrName));
            }
            break;
        case DATE_TIME:
            if (!(value instanceof Instant)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Instant.class.getSimpleName(), attrName));
            }
            break;
        case DECIMAL:
            if (!(value instanceof Double)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Double.class.getSimpleName(), attrName));
            }
            if (((Double) value).isNaN()) {
                throw new MolgenisDataException(format("Value [%s] for type [%s] is not allowed for attribute: [%s]", value.toString(), Double.class.getSimpleName(), attrName));
            }
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            if (!(value instanceof String)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), String.class.getSimpleName(), attrName));
            }
            break;
        case INT:
            if (!(value instanceof Integer)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Integer.class.getSimpleName(), attrName));
            }
            break;
        case LONG:
            if (!(value instanceof Long)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Long.class.getSimpleName(), attrName));
            }
            break;
        default:
            throw new UnexpectedEnumException(dataType);
    }
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Instant(java.time.Instant) LocalDate(java.time.LocalDate) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 5 with MolgenisDataException

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

the class NegotiatorController method validateNegotiatorExport.

@PostMapping("/validate")
@ResponseBody
public ExportValidationResponse validateNegotiatorExport(@RequestBody NegotiatorRequest request) {
    boolean isValidRequest = true;
    String message = "";
    List<String> enabledCollectionsLabels;
    List<String> disabledCollectionLabels;
    NegotiatorEntityConfig entityConfig = getNegotiatorEntityConfig(request.getEntityId());
    if (null != entityConfig) {
        LOG.info("Validating negotiator request\n\n{}", request);
        List<Entity> collectionEntities = getCollectionEntities(request);
        List<Entity> disabledCollections = getDisabledCollections(collectionEntities, entityConfig);
        Function<Entity, String> getLabel = entity -> entity.getLabelValue().toString();
        disabledCollectionLabels = disabledCollections.stream().map(getLabel).collect(toList());
        enabledCollectionsLabels = collectionEntities.stream().filter(e -> !disabledCollections.contains(e)).map(getLabel).collect(toList());
        if (!disabledCollections.isEmpty()) {
            message = messageSource.getMessage("dataexplorer_directory_disabled", new Object[] { disabledCollections.size(), collectionEntities.size() }, getLocale());
        }
        if (collectionEntities.isEmpty() || (collectionEntities.size() == disabledCollections.size())) {
            isValidRequest = false;
            message = messageSource.getMessage("dataexplorer_directory_no_rows", new Object[] {}, getLocale());
        }
    } else {
        throw new MolgenisDataException(messageSource.getMessage("dataexplorer_directory_no_config", new Object[] {}, getLocale()));
    }
    return ExportValidationResponse.create(isValidRequest, message, enabledCollectionsLabels, disabledCollectionLabels);
}
Also used : PluginController(org.molgenis.web.PluginController) NegotiatorConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorConfig) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse) LoggerFactory(org.slf4j.LoggerFactory) QueryImpl(org.molgenis.data.support.QueryImpl) Controller(org.springframework.stereotype.Controller) Attribute(org.molgenis.data.meta.model.Attribute) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Function(java.util.function.Function) PluginPermission(org.molgenis.data.plugin.model.PluginPermission) Objects.requireNonNull(java.util.Objects.requireNonNull) PluginIdentity(org.molgenis.data.plugin.model.PluginIdentity) JsMagmaScriptEvaluator(org.molgenis.js.magma.JsMagmaScriptEvaluator) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) RestTemplate(org.springframework.web.client.RestTemplate) MessageSource(org.springframework.context.MessageSource) RestClientException(org.springframework.web.client.RestClientException) QueryRsqlConverter(org.molgenis.data.rest.convert.QueryRsqlConverter) Logger(org.slf4j.Logger) URI(org.molgenis.dataexplorer.negotiator.NegotiatorController.URI) LocaleContextHolder.getLocale(org.springframework.context.i18n.LocaleContextHolder.getLocale) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) NegotiatorEntityConfigMeta(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfigMeta) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) HttpEntity(org.springframework.http.HttpEntity) Base64(java.util.Base64) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ENABLED_EXPRESSION(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfigMeta.ENABLED_EXPRESSION) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) NegotiatorEntityConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfig) MolgenisDataException(org.molgenis.data.MolgenisDataException) Entity(org.molgenis.data.Entity) NegotiatorEntityConfig(org.molgenis.dataexplorer.negotiator.config.NegotiatorEntityConfig) HttpEntity(org.springframework.http.HttpEntity) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException)

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