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.impl.RepoServicesImpl.java

License:Apache License

@GET
@Path("/{id}/researchobjects")
@Produces(MediaType.APPLICATION_JSON)//w w  w  .ja  va 2s .com
public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(
                Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection
                .find(Filters.and(Filters.eq("Repository", id), Filters.eq("Preferences.Purpose", purpose)));
    } else if (purpose != null) {
        return Response.status(Status.BAD_REQUEST)
                .entity(new JSONObject()
                        .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString())
                .build();
    } else {
        iter = publicationsCollection.find(Filters.eq("Repository", id));
    }

    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()) {
        array.add(cursor.next());
    }
    return Response.ok(array).cacheControl(control).build();
}

From source file:org.seadpdt.impl.RepoServicesImpl.java

License:Apache License

@GET
@Path("/{id}/researchobjects/new")
@Produces(MediaType.APPLICATION_JSON)//w  w  w  . j av  a2 s  . c o m
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(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.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)/*from  w ww.j  a  va  2  s .  com*/
public Response getROsList(@QueryParam("Purpose") final String purpose) {
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(Filters.ne("Preferences.Purpose", "Testing-Only"));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection.find(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();
    }
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/new/")
@Produces(MediaType.APPLICATION_JSON)/*from  w ww.  ja  va  2 s. c om*/
public Response getNewROsList(@QueryParam("Purpose") final String purpose) {
    //Find ROs that have a status not from the services and don't include them :-)
    Document reporterRule = new Document("$ne", Constants.serviceName);
    Document reporter = new Document("reporter", reporterRule);
    Document elem = new Document("$elemMatch", reporter);
    Document not = new Document("$not", elem);
    Document match = new Document("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("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Title", 1).append("_id", 0));
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        array.put(JSON.parse(cursor.next().toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.seadpdt.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/{id}/status")
@Produces(MediaType.APPLICATION_JSON)/*from ww w . j a v  a  2s  . c om*/
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));

    if (iter == null || iter.first() == null) {
        return Response.status(ClientResponse.Status.NOT_FOUND)
                .entity(new JSONObject().put("Error", "Cannot find RO with id " + id).toString()).build();
    }

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

From source file:org.seadpdt.impl.ROServicesImpl.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;/*w w  w  .  j av a2s  . c  o 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(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.impl.ROServicesImpl.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(Response.Status.NOT_FOUND).entity("RO with ID " + id + " does not exist")
                .build();/*from  www .  ja v  a 2 s  .c o 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.impl.ROServicesImpl.java

License:Apache License

@GET
@Path("/pid/{pid}")
@Produces(MediaType.APPLICATION_JSON)/* w ww.  ja  v  a  2s  . co m*/
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)
                .entity(new JSONObject().put("Error", "Cannot find RO with PID " + pid).toString()).build();
    }
}

From source file:org.seadpdt.impl.SearchServiceImpl.java

License:Apache License

private void setROProjection(FindIterable<Document> iter) {
    iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1)
            .append("Aggregation.Creator", 1).append("Aggregation.Title", 1).append("Aggregation.Contact", 1)
            .append("Aggregation.Abstract", 1).append("Aggregation.Publishing Project Name", 1)
            .append("Aggregation.Publishing Project", 1)
            //                .append("Aggregation.Creation Date", 1)
            .append("_id", 0));
}

From source file:org.seadpdt.RepoServices.java

License:Apache License

@GET
@Path("/{id}/researchobjects")
@Produces(MediaType.APPLICATION_JSON)/*from  w  w  w  .j  av a  2s .c  o m*/
public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) {
    MongoCollection<Document> publicationsCollection = null;
    publicationsCollection = db.getCollection(MongoDB.researchObjects);
    FindIterable<Document> iter;
    if (purpose != null && purpose.equals("Production")) {
        iter = publicationsCollection.find(
                Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only")));
    } else if (purpose != null && purpose.equals("Testing-Only")) {
        iter = publicationsCollection
                .find(Filters.and(Filters.eq("Repository", id), 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(Filters.eq("Repository", id));
    }

    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()) {
        array.add(cursor.next());
    }
    return Response.ok(array).cacheControl(control).build();
}