Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:com.softinstigate.restheart.db.DBDAO.java

License:Open Source License

/**
 *
 * @param dbName//from w w w.j a v a2  s  .co m
 * @param requestEtag
 * @return
 */
public static int deleteDB(String dbName, ObjectId requestEtag) {
    DB db = DBDAO.getDB(dbName);

    DBCollection coll = db.getCollection("_properties");

    BasicDBObject checkEtag = new BasicDBObject("_id", "_properties");
    checkEtag.append("_etag", requestEtag);

    DBObject exists = coll.findOne(checkEtag, fieldsToReturn);

    if (exists == null) {
        return HttpStatus.SC_PRECONDITION_FAILED;
    } else {
        db.dropDatabase();
        return HttpStatus.SC_NO_CONTENT;
    }
}

From source file:com.softinstigate.restheart.hal.Representation.java

License:Open Source License

BasicDBObject getDBObject() {
    BasicDBObject ret = new BasicDBObject(properties);

    if (!embedded.isEmpty()) {
        ret.append("_embedded", embedded);
    }//from w  w  w  . j  a  va  2 s . com

    if (!links.isEmpty()) {
        ret.append("_links", links);
    }

    return ret;
}

From source file:com.softinstigate.restheart.handlers.applicationlogic.GetRoleHandler.java

License:Open Source License

/**
 * Handles the request.//from ww  w .  j av a  2s .c om
 *
 * @param exchange
 * @param context
 * @throws Exception
 */
@Override
public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
    if (context.getMethod() == METHOD.OPTIONS) {
        exchange.getResponseHeaders().put(HttpString.tryFromString("Access-Control-Allow-Methods"), "GET");
        exchange.getResponseHeaders().put(HttpString.tryFromString("Access-Control-Allow-Headers"),
                "Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, Host, Origin, X-Requested-With, User-Agent, No-Auth-Challenge");
        exchange.setResponseCode(HttpStatus.SC_OK);
        exchange.endExchange();
    } else if (context.getMethod() == METHOD.GET) {
        String authHeader = exchange.getRequestHeaders().getFirst("Authorization");

        if (authHeader != null && authHeader.startsWith("Basic ")) {
            authHeader = authHeader.replaceAll("^Basic ", "");

            byte[] __idAndPwd;

            try {
                __idAndPwd = DatatypeConverter.parseBase64Binary(authHeader);
            } catch (IllegalArgumentException iae) {
                __idAndPwd = null;
            }

            if (__idAndPwd != null) {
                String[] idAndPwd = new String(__idAndPwd, StandardCharsets.UTF_8).split(":");

                if (idAndPwd.length == 2) {
                    Account a = idm.verify(idAndPwd[0], new PasswordCredential(idAndPwd[1].toCharArray()));

                    if (a != null) {
                        BasicDBList _roles = new BasicDBList();

                        _roles.addAll(a.getRoles());

                        BasicDBObject root = new BasicDBObject();

                        root.append("authenticated", true);
                        root.append("roles", _roles);

                        Representation rep = new Representation(url);
                        rep.addProperties(root);

                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
                                Representation.HAL_JSON_MEDIA_TYPE);
                        exchange.setResponseCode(HttpStatus.SC_OK);
                        exchange.getResponseSender().send(rep.toString());
                        exchange.endExchange();
                        return;
                    }
                }
            }
        }

        BasicDBObject root = new BasicDBObject();

        root.append("authenticated", false);
        root.append("roles", null);

        Representation rep = new Representation("/_logic/roles/mine");
        rep.addProperties(root);

        if (sendChallenge) {
            exchange.getResponseHeaders().add(WWW_AUTHENTICATE, challenge);
            exchange.setResponseCode(HttpStatus.SC_UNAUTHORIZED);
        } else {
            exchange.setResponseCode(HttpStatus.SC_OK);
        }

        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, Representation.HAL_JSON_MEDIA_TYPE);
        exchange.getResponseSender().send(rep.toString());

        exchange.endExchange();
    } else {
        exchange.setResponseCode(HttpStatus.SC_METHOD_NOT_ALLOWED);
        exchange.endExchange();
    }
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 *  Update concern details (title, category and description)
 *///from  w  ww .  ja  v  a  2s  . c o  m
public int updateExisting(int concernId, concern newConcern) {

    int result = 0;
    concernColl = repoConnection.getConcernCollection();
    checkExistingId(concernId);

    try {
        // search for the id
        BasicDBObject updateQuery = new BasicDBObject();

        BasicDBObject searchQuery = new BasicDBObject().append("concernId", concernId);
        DBObject changes = new BasicDBObject().append("title", concernTitle).append("category", concernCategory)
                .append("description", concernDescription);
        updateQuery.append("$set", changes);
        concernColl.update(searchQuery, updateQuery);
    } catch (Exception e) {
        System.out.println("Error updating concern");
    }

    return result;
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 *  Add the script (passed as scriptId) to the concern into the database
 *//*from  w  w w.  j ava2  s  .c om*/

public int addScript(int scriptId) {
    int result = 0;

    BasicDBObject scriptDetail = new BasicDBObject();
    scriptDetail.append("scriptId", scriptId);
    scriptDetail.append("scriptOrder", 99);
    BasicDBObject updateQuery = new BasicDBObject();

    DBObject listitem = new BasicDBObject("Scripts",
            new BasicDBObject("scriptId", scriptId).append("scriptOrder", 99));
    updateQuery.append("$push", listitem);
    BasicDBObject searchQuery = new BasicDBObject().append("concernId", this.concernId);
    concernColl.update(searchQuery, updateQuery);

    return result;
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 *  Add the report (passed as reportId) to the concern into the database
 *///w w  w  .j a v  a 2s .  c  o m

public int addReport(int reportId) {
    int result = 0;

    BasicDBObject reportDetail = new BasicDBObject();
    reportDetail.append("reportId", reportId);
    reportDetail.append("reportOrder", 99);
    BasicDBObject updateQuery = new BasicDBObject();

    DBObject listitem = new BasicDBObject("Reports",
            new BasicDBObject("reportId", reportId).append("reportOrder", 99));
    updateQuery.append("$push", listitem);
    BasicDBObject searchQuery = new BasicDBObject().append("concernId", this.concernId);
    concernColl.update(searchQuery, updateQuery);

    return result;
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 * remove the scriptId passed from the current concern from the database
 *///  w ww.  j  a  v a2  s . c o m
public int removeScript(int scriptId) {
    int result = 0;

    BasicDBObject scriptDetail = new BasicDBObject();
    scriptDetail.append("scriptId", scriptId);
    scriptDetail.append("scriptOrder", 99);
    BasicDBObject updateQuery = new BasicDBObject();

    DBObject listitem = new BasicDBObject("Scripts",
            new BasicDBObject("scriptId", scriptId).append("scriptOrder", 99));
    updateQuery.append("$pull", listitem);
    BasicDBObject searchQuery = new BasicDBObject().append("concernId", this.concernId);
    concernColl.update(searchQuery, updateQuery);

    return result;
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 * remove the reportId passed from the current concern from the database
 *//*from  ww w.  j  a v a 2  s.  c  o  m*/
public int removeReport(int reportId) {
    int result = 0;

    BasicDBObject reportDetail = new BasicDBObject();
    reportDetail.append("reportId", reportId);
    reportDetail.append("reportOrder", 99);
    BasicDBObject updateQuery = new BasicDBObject();

    DBObject listitem = new BasicDBObject("Reports",
            new BasicDBObject("reportId", reportId).append("reportOrder", 99));
    updateQuery.append("$pull", listitem);
    BasicDBObject searchQuery = new BasicDBObject().append("concernId", this.concernId);
    concernColl.update(searchQuery, updateQuery);

    return result;
}

From source file:com.softlyinspired.jlw.connections.dbConnection.java

License:Open Source License

public int updateExisting() {
    /* Update connection details */

    int result = 0;
    DBCollection connectionColl = repoConnection.getConnectionCollection();

    checkExisting();/*  ww w  . j  av a  2 s  .c  o  m*/

    try {
        // search for the id
        BasicDBObject updateQuery = new BasicDBObject();
        BasicDBObject searchQuery = new BasicDBObject().append("name", connectionName);
        DBObject changes = new BasicDBObject().append("driver", connectionDriver).append("url", connectionURL)
                .append("user", connectionUser).append("password", connectionPassword);
        updateQuery.append("$set", changes);
        connectionColl.update(searchQuery, updateQuery);
        JLWUtilities.scriptInfoMessage("Existing Connection Saved");
    } catch (Exception e) {
        System.out.println("Error updating connection");
    }

    return result;
}

From source file:com.softlyinspired.jlw.reports.ReportReference.java

License:Open Source License

public int updateExisting() {
    /* Update report details */

    int result = 0;
    DBCollection coll = repoConnection.getReportsCollection();
    checkExisting();/*from   w  w w  . ja va  2 s . c  o m*/

    try {
        // search for the id
        BasicDBObject updateQuery = new BasicDBObject();

        BasicDBObject searchQuery = new BasicDBObject().append("reportId", reportId);
        DBObject changes = new BasicDBObject().append("name", reportName).append("type", reportType)
                .append("description", reportDescription).append("category", reportCategory)
                .append("reportLocation", reportLocation);
        updateQuery.append("$set", changes);
        coll.update(searchQuery, updateQuery);
    } catch (Exception e) {
        System.out.println("Error updating report");
    }
    return result;
}