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);
}
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations