Search in sources :

Example 51 with MolgenisDataException

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

the class RestService method convertFile.

private FileMeta convertFile(Attribute attr, Object paramValue, Object entityId) {
    FileMeta value;
    if (paramValue != null) {
        /*
			 * If an entity is updated and no new file is passed, use the old file value
			 */
        if (!(paramValue instanceof MultipartFile)) {
            EntityType entityType = attr.getEntity();
            Attribute idAttribute = entityType.getIdAttribute();
            Object idValue = this.toEntityValue(idAttribute, entityId, null);
            Entity oldEntity = dataService.findOneById(entityType.getId(), idValue);
            if (paramValue instanceof String) {
                FileMeta entity = (FileMeta) oldEntity.getEntity(attr.getName());
                if (entity.get(FILENAME).equals(paramValue)) {
                    value = entity;
                } else {
                    throw new MolgenisDataException("Cannot update entity with file attribute without passing file," + " while changing the name of the existing file attribute");
                }
            } else {
                throw new MolgenisDataException(format("Attribute [%s] value is of type [%s] instead of [%s]", attr.getName(), paramValue.getClass().getSimpleName(), MultipartFile.class.getSimpleName()));
            }
        } else {
            MultipartFile multipartFile = (MultipartFile) paramValue;
            String id = idGenerator.generateId();
            try {
                fileStore.store(multipartFile.getInputStream(), id);
            } catch (IOException e) {
                throw new MolgenisDataException(e);
            }
            FileMeta fileEntity = fileMetaFactory.create(id);
            fileEntity.setFilename(multipartFile.getOriginalFilename());
            fileEntity.setContentType(multipartFile.getContentType());
            fileEntity.setSize(multipartFile.getSize());
            ServletUriComponentsBuilder currentRequest = servletUriComponentsBuilderFactory.fromCurrentRequest();
            UriComponents downloadUri = currentRequest.replacePath(FileDownloadController.URI + '/' + id).replaceQuery(null).build();
            fileEntity.setUrl(downloadUri.toUriString());
            dataService.add(FILE_META, fileEntity);
            value = fileEntity;
        }
    } else {
        value = null;
    }
    return value;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) MultipartFile(org.springframework.web.multipart.MultipartFile) MolgenisDataException(org.molgenis.data.MolgenisDataException) UriComponents(org.springframework.web.util.UriComponents) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Attribute(org.molgenis.data.meta.model.Attribute) IOException(java.io.IOException) FileMeta(org.molgenis.data.file.model.FileMeta)

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