diff --git a/provider/storage-gc/src/main/java/org/opengroup/osdu/storage/provider/gcp/web/repository/OsmRecordsMetadataRepository.java b/provider/storage-gc/src/main/java/org/opengroup/osdu/storage/provider/gcp/web/repository/OsmRecordsMetadataRepository.java index 304758bee878fcfb8216067067d14671886a04dc..059423de5da476252d39695e41334cb2b7ee0332 100644 --- a/provider/storage-gc/src/main/java/org/opengroup/osdu/storage/provider/gcp/web/repository/OsmRecordsMetadataRepository.java +++ b/provider/storage-gc/src/main/java/org/opengroup/osdu/storage/provider/gcp/web/repository/OsmRecordsMetadataRepository.java @@ -1,6 +1,6 @@ /* - * Copyright 2020-2022 Google LLC - * Copyright 2020-2022 EPAM Systems, Inc + * Copyright 2020-2023 Google LLC + * Copyright 2020-2023 EPAM Systems, Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,17 +17,7 @@ package org.opengroup.osdu.storage.provider.gcp.web.repository; -import static org.opengroup.osdu.core.gcp.osm.model.where.condition.And.and; -import static org.opengroup.osdu.core.gcp.osm.model.where.predicate.Eq.eq; -import static org.opengroup.osdu.core.gcp.osm.model.where.predicate.In.in; -import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON; - -import java.util.AbstractMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; +import com.github.fge.jsonpatch.JsonPatch; import lombok.RequiredArgsConstructor; import lombok.extern.java.Log; import org.opengroup.osdu.core.common.model.http.CollaborationContext; @@ -43,9 +33,18 @@ import org.opengroup.osdu.core.gcp.osm.service.Transaction; import org.opengroup.osdu.core.gcp.osm.translate.Outcome; import org.opengroup.osdu.storage.provider.interfaces.IRecordsMetadataRepository; import org.opengroup.osdu.storage.provider.interfaces.ISchemaRepository; +import org.opengroup.osdu.storage.util.JsonPatchUtil; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; +import java.util.*; +import java.util.stream.Collectors; + +import static org.opengroup.osdu.core.gcp.osm.model.where.condition.And.and; +import static org.opengroup.osdu.core.gcp.osm.model.where.predicate.Eq.eq; +import static org.opengroup.osdu.core.gcp.osm.model.where.predicate.In.in; +import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_SINGLETON; + @Repository @Scope(SCOPE_SINGLETON) @Log @@ -126,6 +125,27 @@ public class OsmRecordsMetadataRepository implements IRecordsMetadataRepository< return queryByLegal(legalTagName, null, limit); } + @Override + public Map<String, String> patch( + Map<RecordMetadata, JsonPatch> jsonPatchPerRecord, + Optional<CollaborationContext> collaborationContext) { + if (Objects.nonNull(jsonPatchPerRecord)) { + Transaction txn = context.beginTransaction(getDestination()); + try { + for (RecordMetadata recordMetadata : jsonPatchPerRecord.keySet()) { + JsonPatch jsonPatch = jsonPatchPerRecord.get(recordMetadata); + RecordMetadata newRecordMetadata = + JsonPatchUtil.applyPatch(jsonPatch, RecordMetadata.class, recordMetadata); + context.upsert(newRecordMetadata, getDestination()); + } + txn.commitIfActive(); + } finally { + txn.rollbackIfActive(); + } + } + return new HashMap<>(); + } + private Destination getDestination() { return Destination.builder().partitionId(tenantInfo.getDataPartitionId()) .namespace(new Namespace(tenantInfo.getName())).kind(RECORD_KIND).build(); diff --git a/testing/storage-test-anthos/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java b/testing/storage-test-anthos/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java new file mode 100644 index 0000000000000000000000000000000000000000..ca81845fc1ca809d6643f1141c409e48b5a9d584 --- /dev/null +++ b/testing/storage-test-anthos/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2023 Google LLC + * Copyright 2020-2023 EPAM Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.opengroup.osdu.storage.records; + +import org.junit.After; +import org.junit.Before; +import org.opengroup.osdu.storage.util.AnthosTestUtils; + +public class TestPatchRecords extends PatchRecordsTest { + + @Before + @Override + public void setup() throws Exception { + this.testUtils = new AnthosTestUtils(); + super.setup(); + } + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + this.testUtils = null; + } +} diff --git a/testing/storage-test-gc/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java b/testing/storage-test-gc/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java new file mode 100644 index 0000000000000000000000000000000000000000..b5cfb2f46146fbbdbaa6a6d87c021f255b9165d8 --- /dev/null +++ b/testing/storage-test-gc/src/test/java/org/opengroup/osdu/storage/records/TestPatchRecords.java @@ -0,0 +1,39 @@ +/* + * Copyright 2020-2023 Google LLC + * Copyright 2020-2023 EPAM Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.opengroup.osdu.storage.records; + +import org.junit.After; +import org.junit.Before; +import org.opengroup.osdu.storage.util.GCPTestUtils; + +public class TestPatchRecords extends PatchRecordsTest { + + @Before + @Override + public void setup() throws Exception { + this.testUtils = new GCPTestUtils(); + super.setup(); + } + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + this.testUtils = null; + } +}