Search in sources :

Example 6 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class StudyPackageImportServiceImpl method importNetwork.

private void importNetwork(Network network, boolean publish, StudyPackage studyPackage) throws IOException {
    Network updated;
    try {
        Network existing = networkService.findById(network.getId());
        network.getStudyIds().stream().filter(sid -> !existing.getStudyIds().contains(sid)).forEach(sid -> existing.getStudyIds().add(sid));
        updated = existing;
    } catch (NoSuchNetworkException e) {
        updated = network;
    }
    for (Map.Entry<String, ByteSource> e : studyPackage.attachments.entrySet()) {
        Attachment attachment = network.getLogo();
        if (attachment != null && attachment.getId().equals(e.getKey())) {
            saveTempFile(attachment, e.getValue());
            updated.setLogo(attachment);
        }
    }
    networkService.save(updated);
    if (publish)
        networkService.publish(updated.getId(), true, PublishCascadingScope.ALL);
}
Also used : LocalizedString(org.obiba.mica.core.domain.LocalizedString) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) Hashing(com.google.common.hash.Hashing) AbstractProtobufProvider(org.obiba.jersey.protobuf.AbstractProtobufProvider) Inject(javax.inject.Inject) TempFile(org.obiba.mica.file.TempFile) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) PublishCascadingScope(org.obiba.mica.core.domain.PublishCascadingScope) Service(org.springframework.stereotype.Service) Map(java.util.Map) Mica(org.obiba.mica.web.model.Mica) NoSuchDatasetException(org.obiba.mica.dataset.NoSuchDatasetException) TempFileService(org.obiba.mica.file.service.TempFileService) Network(org.obiba.mica.network.domain.Network) ByteSource(com.google.common.io.ByteSource) ZipEntry(java.util.zip.ZipEntry) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dtos(org.obiba.mica.web.model.Dtos) Charsets(com.google.common.base.Charsets) Attachment(org.obiba.mica.file.Attachment) Logger(org.slf4j.Logger) Pair(org.apache.commons.math3.util.Pair) BaseStudy(org.obiba.mica.study.domain.BaseStudy) Set(java.util.Set) CollectedDatasetService(org.obiba.mica.dataset.service.CollectedDatasetService) FileSystemService(org.obiba.mica.file.service.FileSystemService) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Dataset(org.obiba.mica.dataset.domain.Dataset) NoSuchNetworkException(org.obiba.mica.network.NoSuchNetworkException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) HarmonizedDatasetService(org.obiba.mica.dataset.service.HarmonizedDatasetService) Sets(com.google.common.collect.Sets) JsonFormat(com.googlecode.protobuf.format.JsonFormat) List(java.util.List) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) NetworkService(org.obiba.mica.network.service.NetworkService) InputStream(java.io.InputStream) Network(org.obiba.mica.network.domain.Network) NoSuchNetworkException(org.obiba.mica.network.NoSuchNetworkException) ByteSource(com.google.common.io.ByteSource) Attachment(org.obiba.mica.file.Attachment) LocalizedString(org.obiba.mica.core.domain.LocalizedString) Map(java.util.Map)

Example 7 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class StudyDtosTest method createAttachment.

private Attachment createAttachment() {
    Attachment attachment = new Attachment();
    attachment.setId("123");
    attachment.setName("logo123");
    return attachment;
}
Also used : Attachment(org.obiba.mica.file.Attachment)

Example 8 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class AbstractFileSystemResource method doGetAttachment.

protected Attachment doGetAttachment(String path, @Nullable String version, @Nullable String shareKey) {
    String basePath = normalizePath(path);
    if (isPublishedFileSystem())
        subjectAclService.checkAccess("/file", basePath);
    else
        subjectAclService.checkPermission("/draft/file", "VIEW", basePath, shareKey);
    if (path.endsWith("/"))
        throw new IllegalArgumentException("Folder download is not supported");
    Pair<String, String> pathName = FileSystemService.extractPathName(basePath);
    AttachmentState state = fileSystemService.getAttachmentState(pathName.getKey(), pathName.getValue(), isPublishedFileSystem());
    if (isPublishedFileSystem())
        return state.getPublishedAttachment();
    if (Strings.isNullOrEmpty(version))
        return state.getAttachment();
    List<Attachment> attachments = fileSystemService.getAttachmentRevisions(state).stream().filter(a -> a.getId().equals(version)).collect(Collectors.toList());
    if (attachments.isEmpty())
        throw new NoSuchElementException("No file version " + version + " found at path: " + basePath);
    return attachments.get(0);
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) Attachment(org.obiba.mica.file.Attachment) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Pair(org.apache.commons.math3.util.Pair) AttachmentState(org.obiba.mica.file.AttachmentState) FileUtils(org.obiba.mica.file.FileUtils) FileSystemService(org.obiba.mica.file.service.FileSystemService) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) FileUtils.normalizePath(org.obiba.mica.file.FileUtils.normalizePath) FileUtils.isRoot(org.obiba.mica.file.FileUtils.isRoot) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) NoSuchElementException(java.util.NoSuchElementException) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AttachmentState(org.obiba.mica.file.AttachmentState) Attachment(org.obiba.mica.file.Attachment) NoSuchElementException(java.util.NoSuchElementException)

Example 9 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class DraftFileSystemResource method doReinstate.

private void doReinstate(String path, String versionId) {
    Attachment attachment = doGetAttachment(path, versionId);
    fileSystemService.reinstate(attachment);
}
Also used : Attachment(org.obiba.mica.file.Attachment)

Example 10 with Attachment

use of org.obiba.mica.file.Attachment in project mica2 by obiba.

the class DraftHarmonizationStudyResource method file.

@Path("/file/{fileId}")
public FileResource file(@PathParam("fileId") String fileId, @QueryParam("key") String key) {
    checkPermission("/draft/harmonization-study", "VIEW", key);
    FileResource fileResource = applicationContext.getBean(FileResource.class);
    HarmonizationStudy study = studyService.findDraft(id);
    if (study.hasLogo() && study.getLogo().getId().equals(fileId)) {
        fileResource.setAttachment(study.getLogo());
    } else {
        List<Attachment> attachments = fileSystemService.findAttachments(String.format("^/harmonization-study/%s", study.getId()), false).stream().filter(a -> a.getId().equals(fileId)).collect(Collectors.toList());
        if (attachments.isEmpty())
            throw NoSuchEntityException.withId(Attachment.class, fileId);
        fileResource.setAttachment(attachments.get(0));
    }
    return fileResource;
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ConstraintException(org.obiba.mica.study.ConstraintException) Path(javax.ws.rs.Path) HashMap(java.util.HashMap) Scope(org.springframework.context.annotation.Scope) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) AbstractGitPersistableResource(org.obiba.mica.AbstractGitPersistableResource) QueryParam(javax.ws.rs.QueryParam) Lists(com.google.common.collect.Lists) PublishCascadingScope(org.obiba.mica.core.domain.PublishCascadingScope) SubjectAclResource(org.obiba.mica.security.rest.SubjectAclResource) Map(java.util.Map) Mica(org.obiba.mica.web.model.Mica) DefaultValue(javax.ws.rs.DefaultValue) HarmonizationStudyState(org.obiba.mica.study.domain.HarmonizationStudyState) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) DELETE(javax.ws.rs.DELETE) FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) HarmonizationStudy(org.obiba.mica.study.domain.HarmonizationStudy) HarmonizationStudyService(org.obiba.mica.study.service.HarmonizationStudyService) FileSystemService(org.obiba.mica.file.service.FileSystemService) IOException(java.io.IOException) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) Maps(com.google.common.collect.Maps) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) HarmonizedDatasetService(org.obiba.mica.dataset.service.HarmonizedDatasetService) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Component(org.springframework.stereotype.Component) Response(javax.ws.rs.core.Response) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) PUT(javax.ws.rs.PUT) AbstractGitPersistableService(org.obiba.mica.core.service.AbstractGitPersistableService) HarmonizationStudy(org.obiba.mica.study.domain.HarmonizationStudy) FileResource(org.obiba.mica.file.rest.FileResource) Attachment(org.obiba.mica.file.Attachment) Path(javax.ws.rs.Path)

Aggregations

Attachment (org.obiba.mica.file.Attachment)16 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 Dtos (org.obiba.mica.web.model.Dtos)6 IOException (java.io.IOException)5 Map (java.util.Map)5 NotNull (javax.validation.constraints.NotNull)5 NoSuchEntityException (org.obiba.mica.NoSuchEntityException)5 FileSystemService (org.obiba.mica.file.service.FileSystemService)5 Mica (org.obiba.mica.web.model.Mica)5 Strings (com.google.common.base.Strings)4 Maps (com.google.common.collect.Maps)4 Nullable (javax.annotation.Nullable)4 Timed (com.codahale.metrics.annotation.Timed)3 Lists (com.google.common.collect.Lists)3 ObjectId (org.bson.types.ObjectId)3 AttachmentState (org.obiba.mica.file.AttachmentState)3 Throwables (com.google.common.base.Throwables)2 Sets (com.google.common.collect.Sets)2