Skip to content
Snippets Groups Projects
Commit df3381f8 authored by Martin Lowe's avatar Martin Lowe :flag_ca: Committed by Martin Lowe
Browse files

Fixed updates for existing documents


Signed-off-by: Martin Lowe's avatarMartin Lowe <martin.lowe@eclipse-foundation.org>
parent 0b555dfb
No related branches found
No related tags found
No related merge requests found
...@@ -6,12 +6,18 @@ ...@@ -6,12 +6,18 @@
*/ */
package org.eclipsefoundation.marketplace.dao.impl; package org.eclipsefoundation.marketplace.dao.impl;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject; import javax.inject.Inject;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.conversions.Bson;
import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder; import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
...@@ -23,6 +29,7 @@ import org.eclipsefoundation.marketplace.namespace.DtoTableNames; ...@@ -23,6 +29,7 @@ import org.eclipsefoundation.marketplace.namespace.DtoTableNames;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.DeleteResult;
import io.quarkus.mongodb.ReactiveMongoClient; import io.quarkus.mongodb.ReactiveMongoClient;
...@@ -49,6 +56,9 @@ public class DefaultMongoDao implements MongoDao { ...@@ -49,6 +56,9 @@ public class DefaultMongoDao implements MongoDao {
@ConfigProperty(name = "mongodb.maintenance", defaultValue = "false") @ConfigProperty(name = "mongodb.maintenance", defaultValue = "false")
boolean maintenanceFlag; boolean maintenanceFlag;
@Inject
Instance<CodecProvider> providers;
@Inject @Inject
ReactiveMongoClient mongoClient; ReactiveMongoClient mongoClient;
...@@ -63,7 +73,8 @@ public class DefaultMongoDao implements MongoDao { ...@@ -63,7 +73,8 @@ public class DefaultMongoDao implements MongoDao {
LOGGER.debug("Getting aggregate results"); LOGGER.debug("Getting aggregate results");
// build base query // build base query
PublisherBuilder<T> builder = getCollection(q.getDocType()).aggregate(q.getPipeline(getLimit(q)), q.getDocType()); PublisherBuilder<T> builder = getCollection(q.getDocType()).aggregate(q.getPipeline(getLimit(q)),
q.getDocType());
// check if result set should be limited // check if result set should be limited
if (q.getDTOFilter().useLimit()) { if (q.getDTOFilter().useLimit()) {
builder = builder.limit(getLimit(q)); builder = builder.limit(getLimit(q));
...@@ -80,7 +91,25 @@ public class DefaultMongoDao implements MongoDao { ...@@ -80,7 +91,25 @@ public class DefaultMongoDao implements MongoDao {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Adding {} documents to MongoDB of type {}", documents.size(), q.getDocType().getSimpleName()); LOGGER.debug("Adding {} documents to MongoDB of type {}", documents.size(), q.getDocType().getSimpleName());
} }
return getCollection(q.getDocType()).insertMany(documents);
// set up upserting to not fail on updates
ReplaceOptions ro = new ReplaceOptions().upsert(true).bypassDocumentValidation(true);
// maintain a list of updates
List<CompletionStage<?>> stages = new ArrayList<>(documents.size());
Bson filter = q.getFilter();
for (T doc : documents) {
if (filter == null) {
stages.add(getCollection(q.getDocType()).insertOne(doc));
} else {
stages.add(getCollection(q.getDocType()).replaceOne(filter, doc, ro));
}
}
// convert the stages to futures, and wrap them in a completable future
List<CompletableFuture<?>> all = stages.stream().map(CompletionStage::toCompletableFuture)
.collect(Collectors.toList());
return CompletableFuture.allOf(all.toArray(new CompletableFuture[all.size()]));
} }
@Override @Override
......
...@@ -87,6 +87,9 @@ public class CatalogResource { ...@@ -87,6 +87,9 @@ public class CatalogResource {
@PUT @PUT
@RolesAllowed({ "marketplace_catalog_put", "marketplace_admin_access" }) @RolesAllowed({ "marketplace_catalog_put", "marketplace_admin_access" })
public Response putCatalog(Catalog catalog) { public Response putCatalog(Catalog catalog) {
if (catalog.getId() != null) {
params.addParam(UrlParameterNames.ID, catalog.getId());
}
MongoQuery<Catalog> q = new MongoQuery<>(params, dtoFilter); MongoQuery<Catalog> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(catalog))); StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(catalog)));
......
...@@ -87,6 +87,9 @@ public class CategoryResource { ...@@ -87,6 +87,9 @@ public class CategoryResource {
@PUT @PUT
@RolesAllowed({"marketplace_category_put", "marketplace_admin_access"}) @RolesAllowed({"marketplace_category_put", "marketplace_admin_access"})
public Response putCategory(Category category) { public Response putCategory(Category category) {
if (category.getId() != null) {
params.addParam(UrlParameterNames.ID, category.getId());
}
MongoQuery<Category> q = new MongoQuery<>(params, dtoFilter); MongoQuery<Category> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(category))); StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(category)));
......
...@@ -11,12 +11,11 @@ import java.util.List; ...@@ -11,12 +11,11 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.annotation.security.PermitAll; import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.PUT; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
...@@ -41,7 +40,7 @@ import org.slf4j.LoggerFactory; ...@@ -41,7 +40,7 @@ import org.slf4j.LoggerFactory;
* @author Martin Lowe * @author Martin Lowe
*/ */
@RequestScoped @RequestScoped
@Path("/error") @Path("/error_reports")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public class ErrorReportResource { public class ErrorReportResource {
...@@ -87,9 +86,13 @@ public class ErrorReportResource { ...@@ -87,9 +86,13 @@ public class ErrorReportResource {
* @param errorReport the ErrorReport object to insert into the database. * @param errorReport the ErrorReport object to insert into the database.
* @return response for the browser * @return response for the browser
*/ */
@PUT @POST
@RolesAllowed("error_put") @PermitAll
public Response putErrorReport(ErrorReport errorReport) { public Response putErrorReport(ErrorReport errorReport) {
// attach ID if present for update
if (errorReport.getId() != null) {
params.addParam(UrlParameterNames.ID, errorReport.getId());
}
MongoQuery<ErrorReport> q = new MongoQuery<>(params, dtoFilter); MongoQuery<ErrorReport> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
......
...@@ -165,7 +165,7 @@ public class InstallResource { ...@@ -165,7 +165,7 @@ public class InstallResource {
* @return response for the browser * @return response for the browser
*/ */
@POST @POST
@RolesAllowed({ "marketplace_install_put", "marketplace_admin_access" }) @PermitAll
@Path("/{listingId}/{version}") @Path("/{listingId}/{version}")
public Response postInstallMetrics(@PathParam("listingId") String listingId, @PathParam("version") String version, public Response postInstallMetrics(@PathParam("listingId") String listingId, @PathParam("version") String version,
Install installDetails) { Install installDetails) {
......
...@@ -98,6 +98,9 @@ public class ListingResource { ...@@ -98,6 +98,9 @@ public class ListingResource {
@PUT @PUT
@RolesAllowed({ "marketplace_listing_put", "marketplace_admin_access" }) @RolesAllowed({ "marketplace_listing_put", "marketplace_admin_access" })
public Response putListing(Listing listing) { public Response putListing(Listing listing) {
if (listing.getId() != null) {
params.addParam(UrlParameterNames.ID, listing.getId());
}
MongoQuery<Listing> q = new MongoQuery<>(params, dtoFilter); MongoQuery<Listing> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
...@@ -154,4 +157,4 @@ public class ListingResource { ...@@ -154,4 +157,4 @@ public class ListingResource {
// return the results as a response // return the results as a response
return Response.ok().build(); return Response.ok().build();
} }
} }
\ No newline at end of file
...@@ -10,6 +10,7 @@ import java.util.Arrays; ...@@ -10,6 +10,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
...@@ -85,6 +86,9 @@ public class ListingVersionResource { ...@@ -85,6 +86,9 @@ public class ListingVersionResource {
*/ */
@PUT @PUT
public Response putListingVersion(ListingVersion listingVersion) { public Response putListingVersion(ListingVersion listingVersion) {
if (listingVersion.getId() != null) {
params.addParam(UrlParameterNames.ID, listingVersion.getId());
}
MongoQuery<ListingVersion> q = new MongoQuery<>(params, dtoFilter); MongoQuery<ListingVersion> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(listingVersion))); StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(listingVersion)));
...@@ -126,6 +130,7 @@ public class ListingVersionResource { ...@@ -126,6 +130,7 @@ public class ListingVersionResource {
* @return response for the browser * @return response for the browser
*/ */
@DELETE @DELETE
@RolesAllowed({ "marketplace_version_delete", "marketplace_admin_access" })
@Path("/{listingVersionId}") @Path("/{listingVersionId}")
public Response delete(@PathParam("listingVersionId") String listingVersionId) { public Response delete(@PathParam("listingVersionId") String listingVersionId) {
params.addParam(UrlParameterNames.ID, listingVersionId); params.addParam(UrlParameterNames.ID, listingVersionId);
......
...@@ -61,7 +61,7 @@ public class MarketResource { ...@@ -61,7 +61,7 @@ public class MarketResource {
DtoFilter<Market> dtoFilter; DtoFilter<Market> dtoFilter;
@Inject @Inject
ResponseHelper responseBuider; ResponseHelper responseBuider;
@GET @GET
@PermitAll @PermitAll
public Response select() { public Response select() {
...@@ -85,8 +85,11 @@ public class MarketResource { ...@@ -85,8 +85,11 @@ public class MarketResource {
* @return response for the browser * @return response for the browser
*/ */
@PUT @PUT
@RolesAllowed("market_put") @RolesAllowed({ "marketplace_market_put", "marketplace_admin_access" })
public Response putMarket(Market market) { public Response putMarket(Market market) {
if (market.getId() != null) {
params.addParam(UrlParameterNames.ID, market.getId());
}
MongoQuery<Market> q = new MongoQuery<>(params, dtoFilter); MongoQuery<Market> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result // add the object, and await the result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment