Example usage for com.mongodb BasicDBObject containsField

List of usage examples for com.mongodb BasicDBObject containsField

Introduction

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

Prototype

public boolean containsField(final String field) 

Source Link

Document

Checks if this object contains a given field

Usage

From source file:uk.ac.ncl.aries.entanglement.player.spi.CreateEdgePlayer.java

License:Apache License

@Override
public void playItem(NodeDAO nodeDao, EdgeDAO edgeDao, RevisionItem item) throws LogPlayerException {
    try {//from   w w w  .j ava 2  s . c om
        CreateEdge ce = (CreateEdge) item.getOp();

        /*
         * Edges MUST have a UID, a type, a 'from node uid', and a 'to node uid', as
         * well as a 'from node type' and a 'to node type'.
         * Edges may optionally have from/to node names, if those nodes have well
         * known names. 
         * 
         * However, the DBObject that we receive in the <code>RevisionItem<code>
         * here may not have one or more of these required items set. For example,
         * if the process that created the RevisionItem knows only the node name(s),
         * and not their IDs, then it won't have specified the IDs. 
         * Another example is the edge unique ID - the caller potentially doesn't 
         * care about what UID is assigned to the edge, and therefore for efficiency
         * reasons, won't specify one.
         * 
         * Here, we need to set any required values that don't currently have values
         * specified for them by the incoming RevisionItem.
         */

        BasicDBObject serializedEdge = ce.getEdge();

        //Check that both node's type names are set
        if (!serializedEdge.containsField(EdgeDAO.FIELD_FROM_NODE_TYPE)
                || !serializedEdge.containsField(EdgeDAO.FIELD_TO_NODE_TYPE)) {
            throw new LogPlayerException("Can't play operation: " + item.getOp() + ". Either "
                    + EdgeDAO.FIELD_FROM_NODE_TYPE + " or " + EdgeDAO.FIELD_TO_NODE_TYPE + " were not set.");
        }

        // Generate a unique ID for this edge, if one doesn't already exist
        if (!serializedEdge.containsField(EdgeDAO.FIELD_UID)) {
            serializedEdge.put(EdgeDAO.FIELD_UID, UidGenerator.generateUid());
        }

        //If no 'from' node UID is specified, then locate it from the name field
        if (!serializedEdge.containsField(EdgeDAO.FIELD_FROM_NODE_UID)) {
            if (!serializedEdge.containsField(EdgeDAO.FIELD_FROM_NODE_NAME)) {
                throw new LogPlayerException("Can't play operation: " + item.getOp()
                        + ". You must set at least at least one of these: " + EdgeDAO.FIELD_FROM_NODE_UID + ", "
                        + EdgeDAO.FIELD_FROM_NODE_NAME);
            }
            String nodeType = serializedEdge.getString(EdgeDAO.FIELD_FROM_NODE_TYPE);
            String nodeName = serializedEdge.getString(EdgeDAO.FIELD_FROM_NODE_NAME);
            String nodeUid = nodeDao.lookupUniqueIdForName(nodeType, nodeName);
            serializedEdge.put(EdgeDAO.FIELD_FROM_NODE_UID, nodeUid);
        }

        //If no 'to' node UID is specified, then locate it from the name field
        if (!serializedEdge.containsField(EdgeDAO.FIELD_TO_NODE_UID)) {
            if (!serializedEdge.containsField(EdgeDAO.FIELD_TO_NODE_NAME)) {
                throw new LogPlayerException("Can't play operation: " + item.getOp()
                        + ". You must set at least at least one of these: " + EdgeDAO.FIELD_TO_NODE_UID + ", "
                        + EdgeDAO.FIELD_TO_NODE_NAME);
            }
            String nodeType = serializedEdge.getString(EdgeDAO.FIELD_TO_NODE_TYPE);
            String nodeName = serializedEdge.getString(EdgeDAO.FIELD_TO_NODE_NAME);
            String nodeUid = nodeDao.lookupUniqueIdForName(nodeType, nodeName);
            serializedEdge.put(EdgeDAO.FIELD_TO_NODE_UID, nodeUid);
        }

        edgeDao.store(serializedEdge);
    } catch (Exception e) {
        throw new LogPlayerException("Failed to play command", e);
    }
}

From source file:uk.ac.ncl.aries.entanglement.player.spi.CreateNodeIfNotExistsPlayer.java

License:Apache License

@Override
public void playItem(NodeDAO nodeDao, EdgeDAO edgeDao, RevisionItem item) throws LogPlayerException {
    try {/* w  w  w  .  j av a  2s  .c om*/
        /******************
         * TODO: instead of just returning if the node already exists, we might 
         *       want to update its contents.
         */

        /*
         * Nodes MUST have a UID, and a type. A 'well known name' may also optionally
         * be set. 
         * 
         * However, the DBObject that we receive in the <code>RevisionItem<code>
         * here may not have one or more of these required items set. For example,
         * if the process that created the RevisionItem only cares about the 
         * 'well known name', then it may not have specified a UID. In this case,
         * we need to generate a UID here.
         * 
         * Here, we need to set any required values that don't currently have values
         * specified for them by the incoming RevisionItem and check that others
         * exist.
         */
        CreateNodeIfNotExists cn = (CreateNodeIfNotExists) item.getOp();

        BasicDBObject serializedNode = cn.getNode();

        // Find out whether this node already exists by UID
        String nodeUid = null;
        boolean exists;
        if (serializedNode.containsField(NodeDAO.FIELD_UID)) {
            nodeUid = serializedNode.getString(NodeDAO.FIELD_UID);
            exists = nodeDao.existsByUid(nodeUid);
            if (exists) {
                return; // For now, do nothing if a node with the same UID already exists
            }
        }

        // Node type is a required property
        if (!serializedNode.containsField(NodeDAO.FIELD_TYPE)) {
            throw new LogPlayerException("Can't play operation: " + item.getOp() + ". Property "
                    + NodeDAO.FIELD_TYPE + " was not set.");
        }

        // Find out whether this node already exists by type|name
        String nodeType = serializedNode.getString(NodeDAO.FIELD_TYPE);
        String nodeName = serializedNode.getString(NodeDAO.FIELD_NAME);
        if (serializedNode.containsField(NodeDAO.FIELD_NAME)) {
            exists = nodeDao.existsByName(nodeType, nodeName);
            if (exists) {
                return; // For now, do nothing if a node with the same type and name combo already exists
            }
        }

        /*
         * If we get here, then the node does not currently exist.
         */

        // Generate a UID for this node, if one does not already exist
        if (!serializedNode.containsField(NodeDAO.FIELD_UID)) {
            serializedNode.put(NodeDAO.FIELD_UID, UidGenerator.generateUid());
        }

        nodeDao.store(serializedNode);
    } catch (Exception e) {
        throw new LogPlayerException("Failed to play command", e);
    }
}

From source file:uk.ac.ncl.aries.entanglement.player.spi.CreateNodePlayer.java

License:Apache License

@Override
public void playItem(NodeDAO nodeDao, EdgeDAO edgeDao, RevisionItem item) throws LogPlayerException {
    try {//w  ww  .j a v  a2 s.  c om
        CreateNode cn = (CreateNode) item.getOp();

        BasicDBObject serializedNode = cn.getNode();

        // Node type is a required property
        if (!serializedNode.containsField(NodeDAO.FIELD_TYPE)) {
            throw new LogPlayerException("Can't play operation: " + item.getOp() + ". Property "
                    + NodeDAO.FIELD_TYPE + " was not set.");
        }

        /*
         * If we get here, then the node does not currently exist.
         */

        // Generate a UID for this node, if one does not already exist
        if (!serializedNode.containsField(NodeDAO.FIELD_UID)) {
            serializedNode.put(NodeDAO.FIELD_UID, UidGenerator.generateUid());
        }

        nodeDao.store(serializedNode);
    } catch (Exception e) {
        throw new LogPlayerException("Failed to play command", e);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addRecentQuery(BasicDBObject query, String recent) throws InvalidTimePeriodException {
    long millis = new OddUtil().parseTimePeriod(recent, "m");
    long now = Calendar.getInstance().getTimeInMillis();
    long cutoff = now - millis;
    BasicDBObject subQuery = new BasicDBObject("$gt", Long.toString(cutoff));
    if (query.containsField("timestamp")) {
        ((BasicDBObject) query.get("timestamp")).append("$gt", Long.toString(cutoff));
    } else {//w  w w .  j a  v a  2 s .c  om
        query.append("timestamp", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addCaseRecentQuery(BasicDBObject query, String recent) throws InvalidTimePeriodException {
    long millis = new OddUtil().parseTimePeriod(recent, "m");
    long now = Calendar.getInstance().getTimeInMillis();
    long cutoff = now - millis;
    BasicDBObject subQuery = new BasicDBObject("$gt", cutoff);
    if (query.containsField("case.time")) {
        ((BasicDBObject) query.get("case.time")).append("$gt", cutoff);
    } else {/*  w  ww .  j  a  v a2s  .  co m*/
        query.append("case.time", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addAgoQuery(BasicDBObject query, String ago) throws InvalidTimePeriodException {
    long millis = new OddUtil().parseTimePeriod(ago, "m");
    long now = Calendar.getInstance().getTimeInMillis();
    long cutoff = now - millis;
    BasicDBObject subQuery = new BasicDBObject("$lte", Long.toString(cutoff));
    if (query.containsField("timestamp")) {
        ((BasicDBObject) query.get("timestamp")).append("$lte", Long.toString(cutoff));
    } else {/*from www. j av  a2s .c  o  m*/
        query.append("timestamp", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addCaseAgoQuery(BasicDBObject query, String ago) throws InvalidTimePeriodException {
    long millis = new OddUtil().parseTimePeriod(ago, "m");
    long now = Calendar.getInstance().getTimeInMillis();
    long cutoff = now - millis;
    BasicDBObject subQuery = new BasicDBObject("$lte", cutoff);
    if (query.containsField("case.time")) {
        ((BasicDBObject) query.get("case.time")).append("$lte", cutoff);
    } else {//from   w  w w .ja v  a 2 s  .co m
        query.append("case.time", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addSinceQuery(BasicDBObject query, String since) {
    long cutoff = 0;
    try {/*w  w  w . j av  a  2s  .c om*/
        cutoff = Long.parseLong(since);
    } catch (java.lang.NumberFormatException e) {
    }
    BasicDBObject subQuery = new BasicDBObject("$gte", Long.toString(cutoff));
    if (query.containsField("timestamp")) {
        ((BasicDBObject) query.get("timestamp")).append("$gte", Long.toString(cutoff));
    } else {
        query.append("timestamp", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addCaseSinceQuery(BasicDBObject query, String since) {
    long cutoff = 0;
    try {//from  w  w  w  . j  a  v a2  s. c  o m
        cutoff = Long.parseLong(since);
    } catch (java.lang.NumberFormatException e) {
    }
    BasicDBObject subQuery = new BasicDBObject("$gte", cutoff);
    if (query.containsField("case.time")) {
        ((BasicDBObject) query.get("case.time")).append("$gte", cutoff);
    } else {
        query.append("case.time", subQuery);
    }
}

From source file:uk.co.revsys.oddball.rules.MongoDBHelper.java

private void addBeforeQuery(BasicDBObject query, String before) {
    long cutoff = 0;
    try {/*www.j ava 2s.co m*/
        cutoff = Long.parseLong(before);
    } catch (java.lang.NumberFormatException e) {
    }
    BasicDBObject subQuery = new BasicDBObject("$lt", Long.toString(cutoff));
    if (query.containsField("timestamp")) {
        ((BasicDBObject) query.get("timestamp")).append("$lt", Long.toString(cutoff));
    } else {
        query.append("time", subQuery);
    }
}