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 @@
*/
package org.eclipsefoundation.marketplace.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
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.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
......@@ -23,6 +29,7 @@ import org.eclipsefoundation.marketplace.namespace.DtoTableNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.result.DeleteResult;
import io.quarkus.mongodb.ReactiveMongoClient;
......@@ -49,6 +56,9 @@ public class DefaultMongoDao implements MongoDao {
@ConfigProperty(name = "mongodb.maintenance", defaultValue = "false")
boolean maintenanceFlag;
@Inject
Instance<CodecProvider> providers;
@Inject
ReactiveMongoClient mongoClient;
......@@ -63,7 +73,8 @@ public class DefaultMongoDao implements MongoDao {
LOGGER.debug("Getting aggregate results");
// 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
if (q.getDTOFilter().useLimit()) {
builder = builder.limit(getLimit(q));
......@@ -80,7 +91,25 @@ public class DefaultMongoDao implements MongoDao {
if (LOGGER.isDebugEnabled()) {
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
......
......@@ -87,6 +87,9 @@ public class CatalogResource {
@PUT
@RolesAllowed({ "marketplace_catalog_put", "marketplace_admin_access" })
public Response putCatalog(Catalog catalog) {
if (catalog.getId() != null) {
params.addParam(UrlParameterNames.ID, catalog.getId());
}
MongoQuery<Catalog> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(catalog)));
......
......@@ -87,6 +87,9 @@ public class CategoryResource {
@PUT
@RolesAllowed({"marketplace_category_put", "marketplace_admin_access"})
public Response putCategory(Category category) {
if (category.getId() != null) {
params.addParam(UrlParameterNames.ID, category.getId());
}
MongoQuery<Category> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(category)));
......
......@@ -11,12 +11,11 @@ import java.util.List;
import java.util.Optional;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
......@@ -41,7 +40,7 @@ import org.slf4j.LoggerFactory;
* @author Martin Lowe
*/
@RequestScoped
@Path("/error")
@Path("/error_reports")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ErrorReportResource {
......@@ -87,9 +86,13 @@ public class ErrorReportResource {
* @param errorReport the ErrorReport object to insert into the database.
* @return response for the browser
*/
@PUT
@RolesAllowed("error_put")
@POST
@PermitAll
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);
// add the object, and await the result
......
......@@ -165,7 +165,7 @@ public class InstallResource {
* @return response for the browser
*/
@POST
@RolesAllowed({ "marketplace_install_put", "marketplace_admin_access" })
@PermitAll
@Path("/{listingId}/{version}")
public Response postInstallMetrics(@PathParam("listingId") String listingId, @PathParam("version") String version,
Install installDetails) {
......
......@@ -98,6 +98,9 @@ public class ListingResource {
@PUT
@RolesAllowed({ "marketplace_listing_put", "marketplace_admin_access" })
public Response putListing(Listing listing) {
if (listing.getId() != null) {
params.addParam(UrlParameterNames.ID, listing.getId());
}
MongoQuery<Listing> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result
......@@ -154,4 +157,4 @@ public class ListingResource {
// return the results as a response
return Response.ok().build();
}
}
\ No newline at end of file
}
......@@ -10,6 +10,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
......@@ -85,6 +86,9 @@ public class ListingVersionResource {
*/
@PUT
public Response putListingVersion(ListingVersion listingVersion) {
if (listingVersion.getId() != null) {
params.addParam(UrlParameterNames.ID, listingVersion.getId());
}
MongoQuery<ListingVersion> q = new MongoQuery<>(params, dtoFilter);
// add the object, and await the result
StreamHelper.awaitCompletionStage(dao.add(q, Arrays.asList(listingVersion)));
......@@ -126,6 +130,7 @@ public class ListingVersionResource {
* @return response for the browser
*/
@DELETE
@RolesAllowed({ "marketplace_version_delete", "marketplace_admin_access" })
@Path("/{listingVersionId}")
public Response delete(@PathParam("listingVersionId") String listingVersionId) {
params.addParam(UrlParameterNames.ID, listingVersionId);
......
......@@ -61,7 +61,7 @@ public class MarketResource {
DtoFilter<Market> dtoFilter;
@Inject
ResponseHelper responseBuider;
@GET
@PermitAll
public Response select() {
......@@ -85,8 +85,11 @@ public class MarketResource {
* @return response for the browser
*/
@PUT
@RolesAllowed("market_put")
@RolesAllowed({ "marketplace_market_put", "marketplace_admin_access" })
public Response putMarket(Market market) {
if (market.getId() != null) {
params.addParam(UrlParameterNames.ID, market.getId());
}
MongoQuery<Market> q = new MongoQuery<>(params, dtoFilter);
// 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