Commit 7eb24bb9 authored by Nikita Ruzhevsky's avatar Nikita Ruzhevsky
Browse files

added list schema infos

parent 949174ef
Pipeline #59669 passed with stages
in 4 minutes and 14 seconds
package org.opengroup.osdu.core.mongodb.converter;
import org.opengroup.osdu.core.mongodb.dto.SchemaIdentityDto;
import org.opengroup.osdu.core.mongodb.dto.SchemaInfoDto;
import org.opengroup.osdu.core.mongodb.entity.EntityId;
import org.opengroup.osdu.core.mongodb.entity.SchemaIdentityDoc;
import org.opengroup.osdu.core.mongodb.entity.SchemaInfoDoc;
import org.springframework.stereotype.Component;
@SuppressWarnings("DuplicatedCode")
@Component
public class SchemaInfoConverter {
public SchemaInfoDto toDto(SchemaInfoDoc doc) {
return null;
SchemaInfoDto dto = new SchemaInfoDto();
dto.setSchemaIdentity(idToDto(doc.getSchemaIdentity()));
dto.setCreatedBy(doc.getCreatedBy());
dto.setDateCreated(doc.getDateCreated());
dto.setScope(doc.getScope());
dto.setStatus(doc.getStatus());
dto.setSupersededBy(idToDto(doc.getSupersededBy()));
return dto;
}
public SchemaInfoDoc toDoc(SchemaInfoDto dto, String tenant) {
return null;
SchemaInfoDoc doc = new SchemaInfoDoc();
doc.setId(new EntityId(dto.getSchemaIdentity().getId(), tenant));
doc.setTenant(tenant);
doc.setSchemaIdentity(idToDoc(dto.getSchemaIdentity()));
doc.setCreatedBy(dto.getCreatedBy());
doc.setDateCreated(dto.getDateCreated());
doc.setScope(dto.getScope());
doc.setStatus(dto.getStatus());
doc.setSupersededBy(idToDoc(dto.getSupersededBy()));
return doc;
}
private SchemaIdentityDto idToDto(SchemaIdentityDoc doc) {
SchemaIdentityDto dto = new SchemaIdentityDto();
dto.setId(doc.getId());
dto.setAuthority(doc.getAuthority());
dto.setSource(doc.getSource());
dto.setEntityType(doc.getEntityType());
dto.setSchemaVersionMajor(doc.getSchemaVersionMajor());
dto.setSchemaVersionMinor(doc.getSchemaVersionMinor());
dto.setSchemaVersionPatch(doc.getSchemaVersionPatch());
return dto;
}
private SchemaIdentityDoc idToDoc(SchemaIdentityDto dto) {
SchemaIdentityDoc doc = new SchemaIdentityDoc();
doc.setId(dto.getId());
doc.setAuthority(dto.getAuthority());
doc.setSource(dto.getSource());
doc.setEntityType(dto.getEntityType());
doc.setSchemaVersionMajor(dto.getSchemaVersionMajor());
doc.setSchemaVersionMinor(dto.getSchemaVersionMinor());
doc.setSchemaVersionPatch(dto.getSchemaVersionPatch());
return doc;
}
}
package org.opengroup.osdu.core.mongodb.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class QueryParamsDto {
private String authority;
private String source;
private String entityType;
private Long schemaVersionMajor;
private Long schemaVersionMinor;
private Long schemaVersionPatch;
private int limit;
private int offset;
private String status;
private String scope;
private Boolean latestVersion;
}
......@@ -10,9 +10,12 @@ public class SchemaInfoDoc {
@Id
private EntityId id;
// this is used in getSchemaInfoList method
private String tenant;
private SchemaIdentityDoc schemaIdentity;
private String createdBy;
// todo discuss about saving time zones
private Date dateCreated;
private SchemaStatus status;
private SchemaScope scope;
......
package org.opengroup.osdu.core.mongodb.helper;
import org.opengroup.osdu.core.mongodb.converter.SchemaInfoConverter;
import org.opengroup.osdu.core.mongodb.dto.QueryParamsDto;
import org.opengroup.osdu.core.mongodb.dto.SchemaInfoDto;
import org.opengroup.osdu.core.mongodb.entity.EntityId;
import org.opengroup.osdu.core.mongodb.entity.SchemaInfoDoc;
import org.opengroup.osdu.core.mongodb.util.ExceptionGenerator;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@Component
public class SchemaInfoStoreHelper {
private static final Class<SchemaInfoDoc> CLASS = SchemaInfoDoc.class;
private static final String ID = "_id";
private static final String TENANT = "tenant";
private final BasicMongoDBHelper mongoDBHelper;
private final SchemaInfoConverter schemaInfoConverter;
......@@ -24,7 +32,7 @@ public class SchemaInfoStoreHelper {
public SchemaInfoDto getSchemaInfo(String schemaId, String tenant) {
EntityId id = new EntityId(schemaId, tenant);
SchemaInfoDoc doc = mongoDBHelper.get("_id", id, CLASS);
SchemaInfoDoc doc = mongoDBHelper.get(ID, id, CLASS);
if (doc == null) {
throw ExceptionGenerator.notFound(id.toString());
}
......@@ -37,7 +45,7 @@ public class SchemaInfoStoreHelper {
public SchemaInfoDto createSchemaInfo(SchemaInfoDto dto, String tenant) {
SchemaInfoDoc doc = schemaInfoConverter.toDoc(dto, tenant);
EntityId id = doc.getId();
if (mongoDBHelper.existsByField("_id", id, CLASS)) {
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
throw ExceptionGenerator.alreadyExists(id.toString());
}
mongoDBHelper.save(doc);
......@@ -47,23 +55,35 @@ public class SchemaInfoStoreHelper {
public SchemaInfoDto updateSchemaInfo(SchemaInfoDto dto, String tenant) {
SchemaInfoDoc doc = schemaInfoConverter.toDoc(dto, tenant);
EntityId id = doc.getId();
if (!mongoDBHelper.existsByField("_id", id, CLASS)) {
if (!mongoDBHelper.existsByField(ID, id, CLASS)) {
throw ExceptionGenerator.notFound(id.toString());
}
mongoDBHelper.save(doc);
return schemaInfoConverter.toDto(doc);
}
/**
* Do a search through all the entities in DB
*/
public List<SchemaInfoDto> getSchemaInfoList(QueryParamsDto queryParams, String tenant) {
Query query = transformQueryParamsToQuery(queryParams, tenant);
List<SchemaInfoDoc> docs = mongoDBHelper.find(query, CLASS);
return docs.stream()
.map(schemaInfoConverter::toDto)
.collect(Collectors.toList());
}
/**
* By default, mongo-core module doesn't know about global system configs, so you have to get tenant list prior to
* this method
*
* @param tenants tenant list to find the non-unique entry
* @return true if any of the combination tenant-id exists in DB
*/
public boolean isUnique(String schemaId, List<String> tenants) {
for (String tenant : tenants) {
EntityId id = new EntityId(schemaId, tenant);
if (mongoDBHelper.existsByField("_id", id, CLASS)) {
if (mongoDBHelper.existsByField(ID, id, CLASS)) {
return false;
}
}
......@@ -72,16 +92,41 @@ public class SchemaInfoStoreHelper {
public boolean cleanSchema(String schemaId, String tenant) {
EntityId id = new EntityId(schemaId, tenant);
return mongoDBHelper.delete("_id", id, CLASS);
return mongoDBHelper.delete(ID, id, CLASS);
}
private Query transformQueryParamsToQuery(QueryParamsDto qp, String tenant) {
AtomicReference<Criteria> criteria = new AtomicReference<>(Criteria.where(TENANT).is(tenant));
BiConsumer<String, Object> paramAdder = (parameterName, value) -> {
if (value != null) {
criteria.set(criteria.get().and(parameterName).is(value));
}
};
paramAdder.accept("schemaIdentity.authority", qp.getAuthority());
paramAdder.accept("schemaIdentity.source", qp.getSource());
paramAdder.accept("schemaIdentity.entityType", qp.getEntityType());
paramAdder.accept("schemaIdentity.schemaVersionMajor", qp.getSchemaVersionMajor());
paramAdder.accept("schemaIdentity.schemaVersionMinor", qp.getSchemaVersionMinor());
paramAdder.accept("schemaIdentity.schemaVersionPatch", qp.getSchemaVersionPatch());
paramAdder.accept("status", qp.getStatus());
paramAdder.accept("scope", qp.getScope());
Query query = Query.query(criteria.get());
if (qp.getLimit() > 0) {
query = query.limit(qp.getLimit());
}
query = query.skip(qp.getOffset());
return query;
}
/*
TODO figure out how this method should work
String getLatestMinorVerSchema(SchemaInfo schemaInfo) throws ApplicationException;
List<SchemaInfo> getSchemaInfoList(QueryParams queryParams, String tenantId) throws ApplicationException;
*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment