Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

In this page you can find the example usage for com.mongodb BasicDBList get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

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);//w  w w  .ja  v  a 2 s. c  o  m
    }
    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.Report.java

License:Apache License

private BasicDBList mergeExistingScenarios(final DBCollection features, final DBObject feature,
        final String _id) {
    BasicDBList newElements = (BasicDBList) feature.get("elements");
    if (newElements == null) {
        newElements = new BasicDBList();
    }/*w w w .  ja va  2 s . c  om*/
    final List<String> newElementIds = new ArrayList<String>();

    for (int k = 0; k < newElements.size(); k++) {
        final DBObject elem = (DBObject) newElements.get(k);
        final String elem_type = (String) elem.get("type");
        if (elem_type.equalsIgnoreCase("scenario")) {
            newElementIds.add((String) elem.get("id"));
        }
    }

    final DBObject existingFeature = features.findOne(_id);
    if (existingFeature != null) {
        final BasicDBList existingElements = (BasicDBList) existingFeature.get("elements");
        if (existingElements != null) {
            for (int j = 0; j < existingElements.size(); j++) {
                final DBObject element = (DBObject) existingElements.get(j);
                final String element_type = (String) element.get("type");
                if (element_type.equalsIgnoreCase("scenario")) {
                    final String element_id = (String) element.get("id");
                    if (!newElementIds.contains(element_id)) {
                        newElements.add(element);
                    }
                }
            }
        }

    }
    return newElements;
}

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

License:Apache License

@SuppressWarnings("unchecked")
@POST//  www.j av  a  2s .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();
    }
}