Skip to content

Commit

Permalink
#4376 WIP. StudyTableSource interface added. New property sourceURN r…
Browse files Browse the repository at this point in the history
…eplaces opal project/table in dataset's study tables and variables. Requires ES index config update.
  • Loading branch information
ymarcon committed Jan 19, 2023
1 parent 6ea39f4 commit b4bd97d
Show file tree
Hide file tree
Showing 43 changed files with 704 additions and 714 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;

import java.io.Serializable;

public class BaseStudyTable extends OpalTable {
public class BaseStudyTable {

protected String studyId;

protected String populationId;

protected int populationWeight;

private LocalizedString name;

private LocalizedString description;

private LocalizedString additionalInformation;

private int weight;

private String sourceURN;

// legacy
private String project;

// legacy
private String table;

public String getStudyId() {
return studyId;
}
Expand Down Expand Up @@ -72,13 +86,66 @@ public void setPopulationWeight(int populationWeight) {

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("project", getProject()).add("table", getTable())
return MoreObjects.toStringHelper(this).add("source", getSourceURN())
.add("studyId", getStudyId()).add("populationId", getPopulationId())
.toString();
}

@Override
protected String getEntityId() {
return studyId;
public void setName(LocalizedString name) {
this.name = name;
}

public LocalizedString getName() {
return name;
}

public void setDescription(LocalizedString description) {
this.description = description;
}

public LocalizedString getDescription() {
return description;
}

public LocalizedString getAdditionalInformation() {
return additionalInformation;
}

public void setAdditionalInformation(LocalizedString additionalInformation) {
this.additionalInformation = additionalInformation;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

public boolean isFor(String studyId, String sourceURN) {
return this.studyId.equals(studyId) && getSourceURN().equals(sourceURN);
}

public void setSourceURN(String sourceURN) {
this.sourceURN = sourceURN;
}

public String getSourceURN() {
// legacy
if (Strings.isNullOrEmpty(sourceURN)) {
this.sourceURN = OpalTableSource.newSource(project, table).getURN();
}
return sourceURN;
}

@Deprecated
public void setProject(String project) {
this.project = project;
}

@Deprecated
public void setTable(String table) {
this.table = table;
}
}
88 changes: 0 additions & 88 deletions mica-core/src/main/java/org/obiba/mica/core/domain/OpalTable.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2022 OBiBa. All rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.obiba.mica.core.domain;

import com.google.common.base.Strings;
import org.obiba.magma.ValueTable;
import org.obiba.mica.micaConfig.service.OpalService;
import org.obiba.mica.spi.dataset.StudyTableSource;
import org.obiba.opal.rest.client.magma.RestDatasource;
import org.obiba.opal.rest.client.magma.RestValueTable;
import org.obiba.opal.web.model.Math;
import org.obiba.opal.web.model.Search;

import javax.validation.constraints.NotNull;

/**
* Connector to an Opal server, to retrieve value table and summary statistics.
*/
public class OpalTableSource implements StudyTableSource {

private OpalService opalService;

private String opalUrl;

@NotNull
private String project;

@NotNull
private String table;

public String getProject() {
return project;
}

public void setProject(String project) {
this.project = project;
}

public String getTable() {
return table;
}

public void setTable(String table) {
this.table = table;
}

@Override
public ValueTable getValueTable() {
return getDatasource().getValueTable(table);
}

@Override
public Search.QueryResultDto getFacets(Search.QueryTermsDto query) {
return getRestValueTable().getFacets(query);
}

@Override
public Math.SummaryStatisticsDto getVariableSummary(String variableName) {
return ((RestValueTable.RestVariableValueSource)getRestValueTable().getVariableValueSource(variableName)).getSummary();
}

@Override
public String getURN() {
return String.format("urn:opal:%s.%s", project, table);
}

public static boolean isFor(String sourceURN) {
return !Strings.isNullOrEmpty(sourceURN) && sourceURN.startsWith("urn:opal:");
}

public static OpalTableSource newSource(String project, String table) {
OpalTableSource source = new OpalTableSource();
source.setProject(project);
source.setTable(table);
return source;
}

public static OpalTableSource fromURN(String sourceURN) {
if (Strings.isNullOrEmpty(sourceURN) || !sourceURN.startsWith("urn:opal:"))
throw new IllegalArgumentException("Not a valid Opal table source URN: " + sourceURN);

String fullName = toTableName(sourceURN);
int sep = fullName.indexOf(".");
String project = fullName.substring(0, sep);
String table = fullName.substring(sep + 1);
return OpalTableSource.newSource(project, table);
}

public static String toTableName(String sourceURN) {
return sourceURN.replace("urn:opal:", "");
}

public void init(OpalService opalService, String opalUrl) {
this.opalService = opalService;
this.opalUrl = opalUrl;
}

private RestDatasource getDatasource() {
return opalService.getDatasource(opalUrl, project);
}

private RestValueTable getRestValueTable() {
return (RestValueTable) getDatasource().getValueTable(table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

package org.obiba.mica.core.domain;

import java.io.Serializable;

import com.google.common.base.MoreObjects;

import java.io.Serializable;

/**
* Represents a opal table that is associated to a {@link org.obiba.mica.study.domain.Study}
* {@link org.obiba.mica.study.domain.Population} {@link org.obiba.mica.study.domain.DataCollectionEvent}.
Expand Down Expand Up @@ -63,7 +63,7 @@ public boolean appliesTo(String studyId, String populationId, String dataCollect

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("project", getProject()).add("table", getTable())
return MoreObjects.toStringHelper(this).add("source", getSourceURN())
.add("dceId", getDataCollectionEventUId()).toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 OBiBa. All rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.obiba.mica.core.service;

import org.obiba.mica.core.domain.BaseStudyTable;
import org.obiba.mica.core.domain.OpalTableSource;
import org.obiba.mica.micaConfig.service.OpalService;
import org.obiba.mica.micaConfig.service.PluginsService;
import org.obiba.mica.spi.dataset.StudyTableSource;
import org.obiba.mica.spi.dataset.StudyTableSourceService;
import org.obiba.mica.study.domain.BaseStudy;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.NoSuchElementException;
import java.util.Optional;

@Service
public class StudyTableSourceServiceRegistry {

@Inject
private PluginsService pluginsService;

@Inject
private OpalService opalService;

public StudyTableSource makeSource(BaseStudy study, String sourceURN) {
if (OpalTableSource.isFor(sourceURN)) {
OpalTableSource source = OpalTableSource.fromURN(sourceURN);
source.init(opalService, study.getOpal());
return source;
}
Optional<StudyTableSourceService> serviceOptional = pluginsService.getStudyTableSourceServices().stream()
.filter(service -> service.isFor(sourceURN)).findFirst();
if (serviceOptional.isPresent()) {
// TODO add a context to the study table source
return serviceOptional.get().makeSource(sourceURN);
}
throw new NoSuchElementException("Missing study-table-source plugin to handle source: " + sourceURN);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.Map;

/**
* Proxy to Opal tables.
* Proxy to value tables.
*/
public abstract class Dataset extends AbstractModelAware implements AttributeAware, Indexable {

Expand Down
Loading

0 comments on commit b4bd97d

Please sign in to comment.