Search in sources :

Example 1 with Dataset

use of org.obiba.mica.dataset.domain.Dataset in project mica2 by obiba.

the class VariableSetService method createOpalViews.

/**
 * Get a list of opal view dto for backup restore purposes
 *
 * @param variables The source variables set
 * @return a list of opal views grouped by project and entity type
 */
private List<Magma.ViewDto> createOpalViews(List<DatasetVariable> variables, MicaConfig.OpalViewsGrouping opalViewsGrouping) {
    List<Magma.ViewDto> views = new ArrayList<>();
    List<Dataset> datasets = publishedDatasetService.findByIds(variables.stream().map(DatasetVariable::getDatasetId).distinct().collect(Collectors.toList()));
    Map<String, List<String>> opalTableFullNameMap = datasets.stream().collect(Collectors.toMap(Dataset::getId, this::toOpalTableFullName));
    switch(opalViewsGrouping) {
        case PROJECT_TABLE:
            Map<String, List<DatasetVariable>> variablesGroupedByProjectTable = Maps.newHashMap();
            variables.forEach(variable -> {
                List<String> projectTables = opalTableFullNameMap.get(variable.getDatasetId());
                projectTables.forEach(projectTable -> {
                    if (!variablesGroupedByProjectTable.containsKey(projectTable))
                        variablesGroupedByProjectTable.put(projectTable, Lists.newArrayList());
                    variablesGroupedByProjectTable.get(projectTable).add(variable);
                });
            });
            for (Entry<String, List<DatasetVariable>> entry : variablesGroupedByProjectTable.entrySet()) {
                views.add(createViewDto(entry.getValue(), entry.getKey()));
            }
            break;
        case PROJECT_ENTITY_TYPE:
            Map<String, List<DatasetVariable>> variablesGroupedByProjectEntityType = Maps.newHashMap();
            variables.forEach(variable -> {
                List<String> projectTables = opalTableFullNameMap.get(variable.getDatasetId());
                projectTables.forEach(projectTable -> {
                    String projectEntityType = projectTable.split("\\.")[0] + "." + variable.getEntityType();
                    if (!variablesGroupedByProjectEntityType.containsKey(projectEntityType))
                        variablesGroupedByProjectEntityType.put(projectEntityType, Lists.newArrayList());
                    Optional<DatasetVariable> varOpt = variablesGroupedByProjectEntityType.get(projectEntityType).stream().filter(var -> var.getName().equals(variable.getName())).findFirst();
                    if (!varOpt.isPresent())
                        variablesGroupedByProjectEntityType.get(projectEntityType).add(variable);
                });
            });
            for (Entry<String, List<DatasetVariable>> entry : variablesGroupedByProjectEntityType.entrySet()) {
                views.add(createViewDto(entry.getValue(), entry.getKey(), opalTableFullNameMap));
            }
            break;
        case ENTITY_TYPE:
            Map<String, List<DatasetVariable>> variablesGroupedByEntityType = variables.stream().collect(Collectors.groupingBy(DatasetVariable::getEntityType));
            for (Entry<String, List<DatasetVariable>> entry : variablesGroupedByEntityType.entrySet()) {
                views.add(createViewDto(entry.getValue(), entry.getKey(), opalTableFullNameMap));
            }
            break;
    }
    return views;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OpalTable(org.obiba.mica.core.domain.OpalTable) java.util(java.util) LocalizedString(org.obiba.mica.core.domain.LocalizedString) MicaConfig(org.obiba.mica.micaConfig.domain.MicaConfig) Magma(org.obiba.opal.web.model.Magma) Inject(javax.inject.Inject) DocumentSet(org.obiba.mica.core.domain.DocumentSet) DocumentSetService(org.obiba.mica.core.service.DocumentSetService) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Attributes(org.obiba.mica.core.domain.Attributes) ZipEntry(java.util.zip.ZipEntry) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) OutputStream(java.io.OutputStream) PublishedDatasetVariableService(org.obiba.mica.study.service.PublishedDatasetVariableService) Validated(org.springframework.validation.annotation.Validated) DatasetCategory(org.obiba.mica.dataset.domain.DatasetCategory) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) IOException(java.io.IOException) Dataset(org.obiba.mica.dataset.domain.Dataset) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) JsonFormat(com.googlecode.protobuf.format.JsonFormat) Entry(java.util.Map.Entry) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dataset(org.obiba.mica.dataset.domain.Dataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) LocalizedString(org.obiba.mica.core.domain.LocalizedString)

Example 2 with Dataset

use of org.obiba.mica.dataset.domain.Dataset in project mica2 by obiba.

the class DatasetDtos method fromDto.

@NotNull
public Dataset fromDto(Mica.DatasetDtoOrBuilder dto) {
    Dataset dataset = dto.hasExtension(Mica.HarmonizedDatasetDto.type) ? fromDto(dto.getExtension(Mica.HarmonizedDatasetDto.type)) : dto.hasExtension(Mica.CollectedDatasetDto.type) ? fromDto(dto.getExtension(Mica.CollectedDatasetDto.type)) : new StudyDataset();
    if (dto.hasId())
        dataset.setId(dto.getId());
    dataset.setAcronym(localizedStringDtos.fromDto(dto.getAcronymList()));
    dataset.setName(localizedStringDtos.fromDto(dto.getNameList()));
    dataset.setDescription(localizedStringDtos.fromDto(dto.getDescriptionList()));
    dataset.setEntityType(dto.getEntityType());
    dataset.setPublished(dto.getPublished());
    if (dto.getAttributesCount() > 0) {
        dto.getAttributesList().forEach(attributeDto -> dataset.addAttribute(attributeDtos.fromDto(attributeDto)));
    }
    if (dto.hasContent()) {
        dataset.setModel(JSONUtils.toMap(dto.getContent()));
    }
    return dataset;
}
Also used : StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dataset(org.obiba.mica.dataset.domain.Dataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) NotNull(javax.validation.constraints.NotNull)

Example 3 with Dataset

use of org.obiba.mica.dataset.domain.Dataset in project mica2 by obiba.

the class DatasetController method dataset.

@GetMapping("/dataset/{id:.+}")
public ModelAndView dataset(@PathVariable String id, @RequestParam(value = "draft", required = false) String shareKey) {
    Map<String, Object> params = newParameters();
    Dataset dataset = getDataset(id, shareKey);
    params.put("dataset", dataset);
    params.put("type", (dataset instanceof StudyDataset) ? "Collected" : "Harmonized");
    params.put("draft", !Strings.isNullOrEmpty(shareKey));
    if (dataset instanceof StudyDataset) {
        addStudyTableParameters(params, ((StudyDataset) dataset).getStudyTable());
    } else {
        HarmonizationDataset harmoDataset = (HarmonizationDataset) dataset;
        addHarmonizationTableParameters(params, harmoDataset.getHarmonizationTable());
        Map<String, BaseStudy> allStudies = Maps.newHashMap();
        List<BaseStudyTable> allTables = Lists.newArrayList();
        List<Map<String, Object>> studyTables = Lists.newArrayList();
        List<String> ids = Lists.newArrayList();
        for (StudyTable sTable : harmoDataset.getStudyTables()) {
            Map<String, Object> p = new HashMap<>();
            if (addStudyTableParameters(p, sTable, ids))
                allTables.add(sTable);
            if (!p.isEmpty()) {
                studyTables.add(p);
                allStudies.put(sTable.getStudyId(), (BaseStudy) p.get("study"));
            }
        }
        params.put("studyTables", studyTables);
        List<Map<String, Object>> harmonizationTables = Lists.newArrayList();
        ids.clear();
        for (HarmonizationStudyTable hTable : harmoDataset.getHarmonizationTables()) {
            Map<String, Object> p = new HashMap<>();
            if (addHarmonizationTableParameters(p, hTable, ids))
                allTables.add(hTable);
            if (!p.isEmpty()) {
                harmonizationTables.add(p);
                allStudies.put(hTable.getStudyId(), (BaseStudy) p.get("study"));
            }
        }
        params.put("harmonizationTables", harmonizationTables);
        params.put("allTables", allTables);
        params.put("allStudies", allStudies);
    }
    params.put("showDatasetContingencyLink", showDatasetContingencyLink());
    return new ModelAndView("dataset", params);
}
Also used : HashMap(java.util.HashMap) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dataset(org.obiba.mica.dataset.domain.Dataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) ModelAndView(org.springframework.web.servlet.ModelAndView) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) HarmonizationStudyTable(org.obiba.mica.core.domain.HarmonizationStudyTable) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) StudyTable(org.obiba.mica.core.domain.StudyTable) HarmonizationStudyTable(org.obiba.mica.core.domain.HarmonizationStudyTable) BaseStudyTable(org.obiba.mica.core.domain.BaseStudyTable) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with Dataset

use of org.obiba.mica.dataset.domain.Dataset in project mica2 by obiba.

the class DatasetAnalysisController method crosstab.

@GetMapping("/dataset-crosstab/{id:.+}")
public ModelAndView crosstab(HttpServletRequest request, @PathVariable String id, @RequestParam(value = "var1", required = false) String var1, @RequestParam(value = "var2", required = false) String var2) {
    // check if service is available
    if (!micaConfigService.getConfig().isContingencyEnabled()) {
        return new ModelAndView("redirect:/dataset/" + id);
    }
    Subject subject = SecurityUtils.getSubject();
    String contextPath = micaConfigService.getContextPath();
    if (!subject.isAuthenticated() && (micaConfigService.getConfig().isVariableSummaryRequiresAuthentication() || subjectAclService.hasDatasetContingencyPermissions())) {
        String query = "";
        if (!Strings.isNullOrEmpty(var1))
            query = "var1=" + var1;
        if (!Strings.isNullOrEmpty(var2))
            query = (Strings.isNullOrEmpty(query) ? "" : query + "&") + "var2=" + var2;
        String redirect = encode(contextPath + "/dataset-crosstab/" + id + (Strings.isNullOrEmpty(query) ? "" : "?") + query);
        return new ModelAndView("redirect:/signin?redirect=" + redirect);
    }
    // check if user is permitted
    if (!subjectAclService.isDatasetContingencyPermitted()) {
        return new ModelAndView("redirect:/dataset/" + id);
    }
    ModelAndView mv = new ModelAndView("dataset-crosstab");
    Dataset dataset = getDataset(id);
    mv.getModelMap().addAttribute("dataset", dataset);
    String type = (dataset instanceof StudyDataset) ? "Collected" : "Harmonized";
    String variableType = (dataset instanceof StudyDataset) ? "Collected" : "Dataschema";
    mv.getModelMap().addAttribute("type", type);
    if (!Strings.isNullOrEmpty(var1)) {
        mv.getModelMap().addAttribute("var1", var1);
        DatasetVariable variable1 = getDatasetVariable(id + ":" + var1 + ":" + variableType, var1);
        mv.getModelMap().addAttribute("variable1", getDatasetVariableJSON(variable1));
    }
    if (!Strings.isNullOrEmpty(var2)) {
        mv.getModelMap().addAttribute("var2", var2);
        DatasetVariable variable2 = getDatasetVariable(id + ":" + var2 + ":" + variableType, var2);
        mv.getModelMap().addAttribute("variable2", getDatasetVariableJSON(variable2));
    }
    return mv;
}
Also used : DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Dataset(org.obiba.mica.dataset.domain.Dataset) ModelAndView(org.springframework.web.servlet.ModelAndView) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Subject(org.apache.shiro.subject.Subject) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with Dataset

use of org.obiba.mica.dataset.domain.Dataset in project mica2 by obiba.

the class DraftHarmonizedDatasetResource method update.

@PUT
@Timed
public Response update(Mica.DatasetDto datasetDto, @Context UriInfo uriInfo, @Nullable @QueryParam("comment") String comment) {
    checkPermission("/draft/harmonized-dataset", "EDIT");
    if (!datasetDto.hasId() || !datasetDto.getId().equals(id))
        throw new IllegalArgumentException("Not the expected dataset id");
    Dataset dataset = dtos.fromDto(datasetDto);
    if (!(dataset instanceof HarmonizationDataset))
        throw new IllegalArgumentException("An harmonization dataset is expected");
    datasetService.save((HarmonizationDataset) dataset, comment);
    return Response.noContent().build();
}
Also used : Dataset(org.obiba.mica.dataset.domain.Dataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Dataset (org.obiba.mica.dataset.domain.Dataset)18 StudyDataset (org.obiba.mica.dataset.domain.StudyDataset)13 HarmonizationDataset (org.obiba.mica.dataset.domain.HarmonizationDataset)8 Lists (com.google.common.collect.Lists)7 Inject (javax.inject.Inject)7 Component (org.springframework.stereotype.Component)6 Timed (com.codahale.metrics.annotation.Timed)5 DatasetVariable (org.obiba.mica.dataset.domain.DatasetVariable)5 Collectors (java.util.stream.Collectors)4 Collectors.toList (java.util.stream.Collectors.toList)4 Stream (java.util.stream.Stream)4 NotNull (javax.validation.constraints.NotNull)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 Async (org.springframework.scheduling.annotation.Async)4 Strings (com.google.common.base.Strings)3 java.util (java.util)3 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 NoSuchDatasetException (org.obiba.mica.dataset.NoSuchDatasetException)3