Example usage for com.mongodb.client FindIterable projection

List of usage examples for com.mongodb.client FindIterable projection

Introduction

In this page you can find the example usage for com.mongodb.client FindIterable projection.

Prototype

FindIterable<TResult> projection(@Nullable Bson projection);

Source Link

Document

Sets a document describing the fields to return for all matching documents.

Usage

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)/*from  w w  w.  j  av  a2  s .c om*/
public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);

    //Match ROs with no status from any repo
    Document match = new Document("Repository", id);
    Document reporter = new Document("reporter", id);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    match.put("Status", not);

    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection
                .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(ClientResponse.Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(match);
    }

    iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1)
            .append("Repository", 1).append("Status", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    Set<Document> array = new HashSet<Document>();
    while (cursor.hasNext()) {
        Document nextDoc = cursor.next();
        array.add(nextDoc);
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.ROServices.java

License:Apache License

@GET
@Path("/{id}/status")
@Produces(MediaType.APPLICATION_JSON)//from  ww  w  .  j  a  v  a 2 s. com
public Response getROStatus(@PathParam("id") String id) {
    FindIterable<Document> iter = publicationsCollection.find(new Document("Aggregation.Identifier", id));
    iter.projection(new Document("Status", 1).append("_id", 0));

    Document document = iter.first();
    document.remove("_id");
    return Response.ok(document.toJson()).cacheControl(control).build();
}

From source file:org.seadpdt.ROServices.java

License:Apache License

@DELETE
@Path("/{id}")
public Response rescindROPublicationRequest(@PathParam("id") String id) {

    // First remove map
    FindIterable<Document> iter = publicationsCollection.find(new Document("Aggregation.Identifier", id));
    Document document = iter.first();

    if (document != null) {
        boolean processing = false;
        ArrayList Statuses = (ArrayList) document.get("Status");
        for (Object status : Statuses) {
            Document statusObj = (Document) status;
            String stage = statusObj.get("stage").toString();
            if (stage.equalsIgnoreCase("Success") || stage.equalsIgnoreCase("Pending")) {
                processing = true;//from   w  w  w.  java  2s.co  m
                break;
            }
        }
        Document preferences = (Document) document.get("Preferences");
        String purpose = null;
        if (preferences != null) {
            purpose = (String) preferences.get("Purpose");
        }
        if (Constants.deleteTestRO) {
            if ((purpose == null || !purpose.equalsIgnoreCase("Testing-Only")) && processing) {
                // if server is configured to delete test ROs then still don't delete ROs that are not flagged as "Testing-Only" and in processing/deposited stage
                return Response.status(ClientResponse.Status.BAD_REQUEST).entity(
                        "Cannot revoke the request since the repository is either processing or has deposited the requested RO")
                        .build();
            }
        } else {
            if (processing) {
                // if server is configured not to delete test ROs then don't delete all ROs in processing/deposit stage
                return Response.status(ClientResponse.Status.BAD_REQUEST).entity(
                        "Cannot revoke the request since the repository is either processing or has deposited the requested RO")
                        .build();
            }
        }
    }

    iter.projection(new Document("Aggregation", 1).append("_id", 0));

    document = iter.first();
    if (document == null) {
        return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND)
                .entity("RO with ID " + id + " does not exist").build();
    }

    ObjectId mapId = new ObjectId(((Document) document.get("Aggregation")).get("authoratativeMap").toString());
    oreMapBucket.remove(mapId);
    /*DeleteResult mapDeleteResult = oreMapCollection.deleteOne(new Document(
    "describes.Identifier", id));*/
    //if (mapDeleteResult.getDeletedCount() != 1) {
    // Report error
    //System.out.println("Could not find map corresponding to " + id);
    //}

    DeleteResult dr = publicationsCollection.deleteOne(new Document("Aggregation.Identifier", id));
    if (dr.getDeletedCount() == 1) {
        return Response.status(ClientResponse.Status.OK).entity("RO Successfully Deleted").build();
    } else {
        return Response.status(ClientResponse.Status.NOT_FOUND).entity("RO with ID " + id + " does not exist")
                .build();
    }
}

From source file:org.seadpdt.ROServices.java

License:Apache License

@DELETE
@Path("/{id}/override")
public Response DeleteOverrideRO(@PathParam("id") String id) {

    // First remove map
    FindIterable<Document> iter = publicationsCollection.find(new Document("Aggregation.Identifier", id));
    iter.projection(new Document("Aggregation", 1).append("_id", 0));

    Document document = iter.first();
    if (document == null) {
        return Response.status(javax.ws.rs.core.Response.Status.NOT_FOUND)
                .entity("RO with ID " + id + " does not exist").build();
    }//from   ww w.  j  a va  2  s  . co  m

    ObjectId mapId = new ObjectId(((Document) document.get("Aggregation")).get("authoratativeMap").toString());
    oreMapBucket.remove(mapId);
    DeleteResult dr = publicationsCollection.deleteOne(new Document("Aggregation.Identifier", id));
    if (dr.getDeletedCount() == 1) {
        return Response.status(ClientResponse.Status.OK).entity("RO Successfully Deleted").build();
    } else {
        return Response.status(ClientResponse.Status.NOT_FOUND).entity("RO with ID " + id + " does not exist")
                .build();
    }
}

From source file:org.seadpdt.ROServices.java

License:Apache License

@GET
@Path("/pid/{pid}")
@Produces(MediaType.APPLICATION_JSON)/* ww  w.  j  a  v  a  2  s .  com*/
public Response getRoOfPID(@PathParam("pid") String pid) {

    BasicDBObject statusObj = new BasicDBObject();
    statusObj.put("stage", "Success");
    if (pid.contains("http://doi.org/") || pid.contains("http://dx.doi.org/") || pid.matches("^doi:.*")) {
        String pidQuery = pid.replace("http://doi.org/", "").replace("http://dx.doi.org/", "")
                .replaceAll("^doi:", "").replaceAll("^/+", "").replaceAll("/+$", "");
        Pattern pattern = Pattern.compile("(http://doi.org/|http://dx.doi.org/|doi:)" + pidQuery);
        statusObj.put("message", pattern);
    } else {
        String pidQuery = pid.replaceAll("^/+", "").replaceAll("/+$", "");
        Pattern pattern = Pattern.compile(pidQuery);
        statusObj.put("message", pattern);
    }
    BasicDBObject elemMatch = new BasicDBObject();
    elemMatch.put("$elemMatch", statusObj);
    BasicDBObject query = new BasicDBObject("Status", elemMatch);

    FindIterable<Document> iter = publicationsCollection.find(query);
    iter.projection(new Document("Aggregation.Identifier", 1).append("_id", 0));
    if (iter != null && iter.first() != null) {
        return Response.ok("{\"roId\" : \""
                + ((Document) iter.first().get("Aggregation")).get("Identifier").toString() + "\" }").build();
    } else {
        return Response.status(ClientResponse.Status.NOT_FOUND).build();
    }
}