Skip to content

Commit

Permalink
catalog: start organization implementation, #TASK-4389
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Jul 14, 2023
1 parent 0d33522 commit b76f2dc
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package org.opencb.opencga.catalog.db.api;

import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.QueryParam;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.core.models.organizations.Organization;

import java.util.HashMap;
import java.util.Map;

import static org.opencb.commons.datastore.core.QueryParam.Type.*;

public interface OrganizationDBAdaptor extends Iterable<Organization> {

enum QueryParams implements QueryParam {
UID("uid", LONG, ""),
ID("id", STRING, ""),
UUID("uuid", STRING, ""),
NAME("name", STRING, ""),
DOMAIN("domain", STRING, ""),
OWNER("owner", STRING, ""),
ADMINS("admins", TEXT_ARRAY, ""),
CREATION_DATE("creationDate", DATE, ""),
MODIFICATION_DATE("modificationDate", DATE, ""),
PROJECTS("projects", OBJECT, ""),
AUTHENTICATION_ORIGINS("authenticationOrigins", OBJECT, ""),
ATTRIBUTES("attributes", OBJECT, "");

private static Map<String, QueryParams> map = new HashMap<>();

static {
for (QueryParams params : QueryParams.values()) {
map.put(params.key(), params);
}
}

private final String key;
private Type type;
private String description;

QueryParams(String key, Type type, String description) {
this.key = key;
this.type = type;
this.description = description;
}

@Override
public String key() {
return key;
}

@Override
public Type type() {
return type;
}

@Override
public String description() {
return description;
}

public static Map<String, QueryParams> getMap() {
return map;
}

public static QueryParams getParam(String key) {
return map.get(key);
}
}

// default boolean exists(long projectId) throws CatalogDBException {
// return count(new Query(QueryParams.UID.key(), projectId)).getNumMatches() > 0;
// }
//
// default void checkId(long projectId) throws CatalogDBException {
// if (projectId < 0) {
// throw CatalogDBException.newInstance("Project id '{}' is not valid: ", projectId);
// }
//
// if (!exists(projectId)) {
// throw CatalogDBException.newInstance("Project id '{}' does not exist", projectId);
// }
// }
//
// OpenCGAResult nativeInsert(Map<String, Object> project, String userId) throws CatalogDBException;
//
// OpenCGAResult insert(Project project, String userId, QueryOptions options)
// throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult<Project> get(String userId, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult<Project> get(long project, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult incrementCurrentRelease(long projectId) throws CatalogDBException;
//
// long getId(String userId, String projectAlias) throws CatalogDBException;
//
// String getOwnerId(long projectId) throws CatalogDBException;
//
//
// default OpenCGAResult<Long> count() throws CatalogDBException {
// return count(new Query());
// }
//
// OpenCGAResult<Long> count(Query query) throws CatalogDBException;
//
// OpenCGAResult<Long> count(Query query, String user, StudyPermissions.Permissions studyPermission)
// throws CatalogDBException, CatalogAuthorizationException;
//
// default OpenCGAResult distinct(String field) throws CatalogDBException {
// return distinct(new Query(), field);
// }
//
// OpenCGAResult distinct(Query query, String field) throws CatalogDBException;
//
//
// default OpenCGAResult stats() {
// return stats(new Query());
// }
//
// OpenCGAResult stats(Query query);
//
//
// OpenCGAResult<Project> get(Query query, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult<Project> get(Query query, QueryOptions options, String user) throws CatalogDBException, CatalogParameterException;
//
// default List<OpenCGAResult<Project>> get(List<Query> queries, QueryOptions options) throws CatalogDBException {
// Objects.requireNonNull(queries);
// List<OpenCGAResult<Project>> queryResults = new ArrayList<>(queries.size());
// for (Query query : queries) {
// queryResults.add(get(query, options));
// }
// return queryResults;
// }
//
// OpenCGAResult nativeGet(Query query, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult nativeGet(Query query, QueryOptions options, String user)
// throws CatalogDBException, CatalogAuthorizationException;
//
// default List<OpenCGAResult> nativeGet(List<Query> queries, QueryOptions options) throws CatalogDBException {
// Objects.requireNonNull(queries);
// List<OpenCGAResult> queryResults = new ArrayList<>(queries.size());
// for (Query query : queries) {
// queryResults.add(nativeGet(query, options));
// }
// return queryResults;
// }
//
// OpenCGAResult<Project> update(long id, ObjectMap parameters, QueryOptions queryOptions)
// throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult<Long> update(Query query, ObjectMap parameters, QueryOptions queryOptions) throws CatalogDBException;
//
// OpenCGAResult delete(Project project) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult delete(Query query) throws CatalogDBException;
//
// default OpenCGAResult<Project> delete(long id, QueryOptions queryOptions) throws CatalogDBException {
// throw new NotImplementedException("");
// }
//
// @Deprecated
// default OpenCGAResult<Long> delete(Query query, QueryOptions queryOptions) throws CatalogDBException {
// throw new NotImplementedException("");
// }
//
// @Deprecated
// default OpenCGAResult<Project> remove(long id, QueryOptions queryOptions) throws CatalogDBException {
// throw new NotImplementedException("");
// }
//
// @Deprecated
// default OpenCGAResult<Long> remove(Query query, QueryOptions queryOptions) throws CatalogDBException {
// throw new NotImplementedException("");
// }
//
// OpenCGAResult<Project> restore(long id, QueryOptions queryOptions)
// throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException;
//
// OpenCGAResult<Long> restore(Query query, QueryOptions queryOptions) throws CatalogDBException;
//
//
//// OpenCGAResult<Long> updateStatus(Query query, Status status) throws CatalogDBException;
//
//
@Override
default DBIterator<Organization> iterator() {
try {
return iterator(new Query(), new QueryOptions());
} catch (CatalogDBException e) {
throw new RuntimeException(e);
}
}

DBIterator<Organization> iterator(Query query, QueryOptions options) throws CatalogDBException;

// default DBIterator nativeIterator() throws CatalogDBException {
// return nativeIterator(new Query(), new QueryOptions());
// }
//
// DBIterator nativeIterator(Query query, QueryOptions options) throws CatalogDBException;
//
// DBIterator<Project> iterator(Query query, QueryOptions options, String user)
// throws CatalogDBException, CatalogAuthorizationException;
//
// DBIterator nativeIterator(Query query, QueryOptions options, String user)
// throws CatalogDBException, CatalogAuthorizationException;
//
// OpenCGAResult rank(Query query, String field, int numResults, boolean asc) throws CatalogDBException;
//
// OpenCGAResult groupBy(Query query, String field, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult groupBy(Query query, List<String> fields, QueryOptions options) throws CatalogDBException;
//
// OpenCGAResult groupBy(Query query, String field, QueryOptions options, String user)
// throws CatalogDBException, CatalogAuthorizationException;
//
// OpenCGAResult groupBy(Query query, List<String> fields, QueryOptions options, String user)
// throws CatalogDBException, CatalogAuthorizationException;
//
// @Override
// default void forEach(Consumer action) {
// try {
// forEach(new Query(), action, new QueryOptions());
// } catch (CatalogDBException e) {
// throw new RuntimeException(e);
// }
// }
//
// void forEach(Query query, Consumer<? super Object> action, QueryOptions options) throws CatalogDBException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
public class MongoDBAdaptorFactory implements DBAdaptorFactory {

public static final String ORGANIZATION_COLLECTION = "organization";
public static final String USER_COLLECTION = "user";
public static final String STUDY_COLLECTION = "study";
public static final String FILE_COLLECTION = "file";
Expand Down Expand Up @@ -87,6 +88,7 @@ public class MongoDBAdaptorFactory implements DBAdaptorFactory {
@Deprecated
public static final String OLD_DELETED_INTERPRETATION_COLLECTION = "deleted_interpretation";

public static final String DELETED_ORGANIZATION_COLLECTION = "organization_deleted";
public static final String DELETED_USER_COLLECTION = "user_deleted";
public static final String DELETED_STUDY_COLLECTION = "study_deleted";
public static final String DELETED_FILE_COLLECTION = "file_deleted";
Expand All @@ -104,6 +106,7 @@ public class MongoDBAdaptorFactory implements DBAdaptorFactory {
public static final String AUDIT_COLLECTION = "audit";

public static final List<String> COLLECTIONS_LIST = Arrays.asList(
ORGANIZATION_COLLECTION,
USER_COLLECTION,
STUDY_COLLECTION,
FILE_COLLECTION,
Expand All @@ -122,6 +125,7 @@ public class MongoDBAdaptorFactory implements DBAdaptorFactory {
PANEL_ARCHIVE_COLLECTION,
INTERPRETATION_ARCHIVE_COLLECTION,

DELETED_ORGANIZATION_COLLECTION,
DELETED_USER_COLLECTION,
DELETED_STUDY_COLLECTION,
DELETED_FILE_COLLECTION,
Expand All @@ -147,6 +151,7 @@ public class MongoDBAdaptorFactory implements DBAdaptorFactory {

private MongoDBCollection metaCollection;
private Map<String, MongoDBCollection> collections;
private OrganizationMongoDBAdaptor organizationDBAdaptor;
private UserMongoDBAdaptor userDBAdaptor;
private StudyMongoDBAdaptor studyDBAdaptor;
private IndividualMongoDBAdaptor individualDBAdaptor;
Expand Down Expand Up @@ -266,6 +271,10 @@ public MetaMongoDBAdaptor getCatalogMetaDBAdaptor() {
return metaDBAdaptor;
}

public OrganizationMongoDBAdaptor getOrganizationDBAdaptor() {
return organizationDBAdaptor;
}

@Override
public UserMongoDBAdaptor getCatalogUserDBAdaptor() {
return userDBAdaptor;
Expand Down Expand Up @@ -354,6 +363,7 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
metaCollection = mongoDataStore.getCollection(METADATA_COLLECTION);
MongoDBCollection migrationCollection = mongoDataStore.getCollection(MIGRATION_COLLECTION);

MongoDBCollection organizationCollection = mongoDataStore.getCollection(ORGANIZATION_COLLECTION);
MongoDBCollection userCollection = mongoDataStore.getCollection(USER_COLLECTION);
MongoDBCollection studyCollection = mongoDataStore.getCollection(STUDY_COLLECTION);
MongoDBCollection fileCollection = mongoDataStore.getCollection(FILE_COLLECTION);
Expand All @@ -372,6 +382,7 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
MongoDBCollection panelArchivedCollection = mongoDataStore.getCollection(PANEL_ARCHIVE_COLLECTION);
MongoDBCollection interpretationArchivedCollection = mongoDataStore.getCollection(INTERPRETATION_ARCHIVE_COLLECTION);

MongoDBCollection deletedOrganizationCollection = mongoDataStore.getCollection(DELETED_ORGANIZATION_COLLECTION);
MongoDBCollection deletedUserCollection = mongoDataStore.getCollection(DELETED_USER_COLLECTION);
MongoDBCollection deletedStudyCollection = mongoDataStore.getCollection(DELETED_STUDY_COLLECTION);
MongoDBCollection deletedFileCollection = mongoDataStore.getCollection(DELETED_FILE_COLLECTION);
Expand All @@ -390,6 +401,7 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
collections.put(METADATA_COLLECTION, metaCollection);
collections.put(MIGRATION_COLLECTION, migrationCollection);

collections.put(ORGANIZATION_COLLECTION, organizationCollection);
collections.put(USER_COLLECTION, userCollection);
collections.put(STUDY_COLLECTION, studyCollection);
collections.put(FILE_COLLECTION, fileCollection);
Expand All @@ -408,6 +420,7 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
collections.put(PANEL_ARCHIVE_COLLECTION, panelArchivedCollection);
collections.put(INTERPRETATION_ARCHIVE_COLLECTION, interpretationArchivedCollection);

collections.put(DELETED_ORGANIZATION_COLLECTION, deletedOrganizationCollection);
collections.put(DELETED_USER_COLLECTION, deletedUserCollection);
collections.put(DELETED_STUDY_COLLECTION, deletedStudyCollection);
collections.put(DELETED_FILE_COLLECTION, deletedFileCollection);
Expand All @@ -429,6 +442,8 @@ private void connect(Configuration catalogConfiguration) throws CatalogDBExcepti
catalogConfiguration, this);
jobDBAdaptor = new JobMongoDBAdaptor(jobCollection, deletedJobCollection, catalogConfiguration, this);
projectDBAdaptor = new ProjectMongoDBAdaptor(userCollection, deletedUserCollection, catalogConfiguration, this);
organizationDBAdaptor = new OrganizationMongoDBAdaptor(organizationCollection, deletedOrganizationCollection, catalogConfiguration,
this);
sampleDBAdaptor = new SampleMongoDBAdaptor(sampleCollection, sampleArchivedCollection, deletedSampleCollection,
catalogConfiguration, this);
studyDBAdaptor = new StudyMongoDBAdaptor(studyCollection, deletedStudyCollection, catalogConfiguration, this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.opencb.opencga.catalog.db.mongodb;

import com.mongodb.client.ClientSession;
import org.bson.Document;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.mongodb.MongoDBCollection;
import org.opencb.commons.datastore.mongodb.MongoDBIterator;
import org.opencb.opencga.catalog.db.api.DBIterator;
import org.opencb.opencga.catalog.db.api.OrganizationDBAdaptor;
import org.opencb.opencga.catalog.db.mongodb.converters.OrganizationConverter;
import org.opencb.opencga.catalog.db.mongodb.iterators.ProjectCatalogMongoDBIterator;
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.core.config.Configuration;
import org.opencb.opencga.core.models.organizations.Organization;
import org.slf4j.LoggerFactory;

public class OrganizationMongoDBAdaptor extends MongoDBAdaptor implements OrganizationDBAdaptor {

private final MongoDBCollection organizationCollection;
private final MongoDBCollection deletedOrganizationCollection;
private OrganizationConverter organizationConverter;

public OrganizationMongoDBAdaptor(MongoDBCollection organizationCollection, MongoDBCollection deletedOrganizationCollection,
Configuration configuration, MongoDBAdaptorFactory dbAdaptorFactory) {
super(configuration, LoggerFactory.getLogger(ProjectMongoDBAdaptor.class));
this.dbAdaptorFactory = dbAdaptorFactory;
this.organizationCollection = organizationCollection;
this.deletedOrganizationCollection = deletedOrganizationCollection;
this.organizationConverter = new OrganizationConverter();
}

@Override
public DBIterator<Organization> iterator(Query query, QueryOptions options) throws CatalogDBException {
return iterator(null, query, options);
}

public DBIterator<Organization> iterator(ClientSession clientSession, Query query, QueryOptions options) throws CatalogDBException {
MongoDBIterator<Document> mongoCursor = getMongoCursor(clientSession, query, options);
return new ProjectCatalogMongoDBIterator<>(mongoCursor, clientSession, organizationConverter, dbAdaptorFactory, options, null);
}

private MongoDBIterator<Document> getMongoCursor(ClientSession clientSession, Query query, QueryOptions options) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.opencb.opencga.catalog.db.mongodb.converters;

import org.apache.avro.generic.GenericRecord;
import org.bson.Document;
import org.opencb.opencga.core.models.common.mixins.GenericRecordAvroJsonMixin;
import org.opencb.opencga.core.models.organizations.Organization;

public class OrganizationConverter extends OpenCgaMongoConverter<Organization> {

public OrganizationConverter() {
super(Organization.class);
getObjectMapper().addMixIn(GenericRecord.class, GenericRecordAvroJsonMixin.class);
}

@Override
public Document convertToStorageType(Organization object) {
Document document = super.convertToStorageType(object);
document.put("uid", document.getInteger("uid").longValue());
return document;
}
}
Loading

0 comments on commit b76f2dc

Please sign in to comment.