Example usage for com.mongodb.gridfs GridFS GridFS

List of usage examples for com.mongodb.gridfs GridFS GridFS

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS GridFS.

Prototype

public GridFS(final DB db) 

Source Link

Document

Creates a GridFS instance for the default bucket "fs" in the given database.

Usage

From source file:xbdd.webapp.resource.feature.Report.java

License:Apache License

@PUT
@Path("/{product}/{major}.{minor}.{servicePack}/{build}")
public DBObject putReport(@BeanParam final Coordinates coordinates, final DBObject root) throws IOException {
    final BasicDBList doc = (BasicDBList) root;
    final DB grid = this.client.getDB("grid");
    final GridFS gridFS = new GridFS(grid);
    final DB bdd = this.client.getDB("bdd");
    final DBCollection features = bdd.getCollection("features");
    updateSummaryDocument(bdd, coordinates);

    for (int i = 0; i < doc.size(); i++) {
        // take each feature and give it a unique id.
        final DBObject feature = (DBObject) doc.get(i);
        final String _id = coordinates.getFeature_Id((String) feature.get("id"));
        feature.put("_id", _id);
        embedSteps(feature, gridFS, coordinates); // extract embedded content and hyperlink to it.
        packBackgroundsInToScenarios(feature); // nest background elements within their scenarios
        final BasicDBObject featureCo = coordinates.getReportCoordinates();
        feature.put("coordinates", featureCo);

        final BasicDBList newElements = mergeExistingScenarios(features, feature, _id);
        feature.put("elements", newElements);

        final String originalStatus = StatusHelper.getFeatureStatus(feature);
        feature.put("calculatedStatus", originalStatus);
        feature.put("originalAutomatedStatus", originalStatus);
        this.log.info("Saving: " + feature.get("name") + " - " + feature.get("calculatedStatus"));
        this.log.trace("Adding feature:" + JSON.serialize(feature));
        features.save(feature);//from w w  w  .j av a2s. c  om
    }
    final DBCursor cursor = features.find(coordinates.getReportCoordinatesQueryObject()); // get new co-ordinates to exclude the "version"
    // field
    final List<DBObject> returns = new ArrayList<DBObject>();
    try {
        while (cursor.hasNext()) {
            returns.add(cursor.next());
        }
    } finally {
        cursor.close();
    }
    final BasicDBList list = new BasicDBList();
    list.addAll(returns);
    updateStatsDocument(bdd, coordinates, list);
    return list;
}

From source file:xbdd.webapp.resource.feature.UploadAttachment.java

License:Apache License

@SuppressWarnings("unchecked")
@POST// w w w  .  j  a v a2  s.co  m
@Path("/{elementId}/{report}/{version}/{build}/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String setAttachment(@PathParam("report") final String report,
        @PathParam("version") final String version, @PathParam("build") final String build,
        @PathParam("id") final String id, @PathParam("elementId") final String elementId,
        @FormDataParam("attachmentfile") final File file,
        @FormDataParam("attachmentfile") final FormDataBodyPart body, @Context final HttpServletRequest req)
        throws IOException {
    try (final InputStream is = new FileInputStream(file.getAbsolutePath())) {
        final String elementIdMod = elementId.replace("&2F", "/");
        final DB gridDB = this.client.getDB("grid");
        final GridFS gridFS = new GridFS(gridDB);
        final GridFSInputFile gridFile = gridFS.createFile(is);
        gridFile.setFilename(
                body.getMediaType().toString().split("/")[0] + ".x1.mu." + UUID.randomUUID().toString());
        gridFile.setContentType(body.getMediaType().toString());
        gridFile.save();
        final DB bddDB = this.client.getDB("bdd");
        final DBCollection collection = bddDB.getCollection("features");
        // // get object
        final String featureId = report + "/" + version + "/" + build + "/" + id;
        final DBObject feature = collection.findOne(new BasicDBObject("_id", featureId));
        final BasicDBList elements = (BasicDBList) feature.get("elements");
        String scenarioName = "";
        if (elements != null) {
            for (int i = 0; i < elements.size(); i++) {
                final DBObject scenario = (DBObject) elements.get(i);
                if (scenario.get("id").equals(id + ";" + elementIdMod)) {
                    scenarioName = (String) scenario.get("name");
                    // get steps
                    final BasicDBList steps = (BasicDBList) scenario.get("steps");
                    final DBObject laststep = (DBObject) steps.get(steps.size() - 1);
                    List<String> embeddings = new ArrayList<String>();
                    if (laststep.containsField("embeddings")) {
                        embeddings = (ArrayList<String>) laststep.get("embeddings");
                    }
                    embeddings.add(gridFile.getFilename()); // get existng then add to them
                    laststep.put("embeddings", embeddings);
                    steps.set(steps.size() - 1, laststep);
                    scenario.put("steps", steps);
                    elements.set(i, scenario);
                }
            }
        }
        feature.put("elements", elements);
        feature.put("statusLastEditedBy", req.getRemoteUser());
        feature.put("lastEditOn", new Date());

        // add edit update
        BasicDBList edits = (BasicDBList) feature.get("edits");
        if (edits == null) {
            edits = new BasicDBList();
        }
        final BasicDBList temp = new BasicDBList();
        temp.add(new BasicDBObject().append("id", "embeddings").append("added", gridFile.getFilename())
                .append("removed", null));

        final BasicDBList masks = new BasicDBList();
        masks.add(new BasicDBObject().append("scenario", scenarioName).append("changes", temp));

        final BasicDBObject edit = new BasicDBObject().append("name", feature.get("statusLastEditedBy"))
                .append("date", feature.get("lastEditOn")).append("prev", feature.get("calculatedStatus"))
                .append("curr", feature.get("calculatedStatus")).append("stepChanges", masks);

        final BasicDBList newEdits = new BasicDBList();
        newEdits.add(edit);
        newEdits.addAll(edits);
        feature.put("edits", newEdits);

        collection.save(feature);
        return gridFile.getFilename();
    }
}