Search in sources :

Example 31 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class RestServiceTest method toEntityFileValueWithoutFileInRequest.

@Test
public void toEntityFileValueWithoutFileInRequest() throws ParseException {
    int entityId = 12345;
    String fileName = "File name";
    String fileAttrName = "fileAttr";
    String oldEntityTypeId = "oldEntityTypeId";
    EntityType oldEntityType = mock(EntityType.class);
    Entity oldEntity = mock(Entity.class);
    FileMeta storedFileMeta = mock(FileMeta.class);
    Attribute fileAttr = mock(Attribute.class);
    Attribute idAttr = mock(Attribute.class);
    when(fileAttr.getName()).thenReturn(fileAttrName);
    when(fileAttr.getEntity()).thenReturn(oldEntityType);
    when(fileAttr.getDataType()).thenReturn(FILE);
    when(oldEntityType.getId()).thenReturn(oldEntityTypeId);
    when(oldEntityType.getIdAttribute()).thenReturn(idAttr);
    when(idAttr.getDataType()).thenReturn(INT);
    when(oldEntity.getEntity(fileAttrName)).thenReturn(storedFileMeta);
    when(storedFileMeta.get(FileMetaMetaData.FILENAME)).thenReturn(fileName);
    when(dataService.findOneById(fileAttr.getEntity().getId(), entityId)).thenReturn(oldEntity);
    Object result = restService.toEntityValue(fileAttr, fileName, entityId);
    assertEquals(result, storedFileMeta);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Example 32 with FileMeta

use of org.molgenis.data.file.model.FileMeta 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

FileMeta (org.molgenis.data.file.model.FileMeta)32 Test (org.testng.annotations.Test)14 File (java.io.File)12 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 Attribute (org.molgenis.data.meta.model.Attribute)5 Entity (org.molgenis.data.Entity)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 InputStream (java.io.InputStream)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 EntityType (org.molgenis.data.meta.model.EntityType)3 IOException (java.io.IOException)2 Stream (java.util.stream.Stream)2 ZipFile (net.lingala.zip4j.core.ZipFile)2 ZipException (net.lingala.zip4j.exception.ZipException)2 Repository (org.molgenis.data.Repository)2 EntityImportReport (org.molgenis.data.importer.EntityImportReport)2 ImportService (org.molgenis.data.importer.ImportService)2 FileIngestJobExecution (org.molgenis.file.ingest.meta.FileIngestJobExecution)2 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponents (org.springframework.web.util.UriComponents)2