Example usage for com.mongodb BasicDBObject getString

List of usage examples for com.mongodb BasicDBObject getString

Introduction

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

Prototype

public String getString(final String key) 

Source Link

Document

Returns the value of a field as a string

Usage

From source file:uk.ac.ncl.aries.entanglement.graph.AbstractGraphEntityDAO.java

License:Apache License

@Override
public void store(BasicDBObject item) throws GraphModelException {
    try {/*from   w w  w . ja  v  a2  s . c  o m*/
        //      logger.log(Level.INFO, "Storing node: {0}", node);
        if (insertModeHint == InsertMode.INSERT_CONSISTENCY) {
            String uid = item.getString(FIELD_UID);
            String name = item.getString(FIELD_NAME);
            String type = item.getString(FIELD_TYPE);
            //        logger.info("Running in consistency mode");
            if (existsByUid(uid)) {
                throw new GraphModelException(
                        "Failed to store item - an entity with this unique ID already exists: " + uid);
            }
            if (name != null && existsByName(type, name)) {
                throw new GraphModelException(
                        "Failed to store item - an entity with the same 'well known' name already exists: "
                                + name);
            }
        }

        col.insert(item);

        /////// DEBUG (Performance info)
        if (printPeriodicPerformanceInfo) {
            insertCount++;
            if (timestampOfLastPerformanceMessage < 0) {
                //First ever insert
                long now = System.currentTimeMillis();
                timestampOfLastPerformanceMessage = now;
                timestampOfFirstInsert = now;
                return;
            }
            if (insertCount % PRINT_PERF_INFO_EVERY == 0) {
                long now = System.currentTimeMillis();
                double secondsPerBlock = (now - timestampOfLastPerformanceMessage);
                secondsPerBlock = secondsPerBlock / 1000;
                double totalSeconds = (now - timestampOfFirstInsert);
                totalSeconds = totalSeconds / 1000;
                logger.log(Level.INFO,
                        "Inserted a total of\t{0}\t" + getClass().getSimpleName() + " documents. "
                                + "Total time\t{1}\t seconds. Seconds since last block: {2}",
                        new Object[] { insertCount, totalSeconds, secondsPerBlock });
                timestampOfLastPerformanceMessage = now;
            }
        }
        /////// DEBUG (Performance info) (end)
    } catch (Exception e) {
        throw new GraphModelException("Failed to store item: " + item, e);
    }
}

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  2s  .c  o  m
        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 {//from  w ww  .j  a v  a2 s  .c  o m
        /******************
         * 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:Vistas.PanelFoto.java

@Override
public void rellenarCampos(BasicDBObject obj, int posi, int l) {
    if (obj.getString("nombre") == null) {
        Nombre.setText("-");
    } else {//  w  w  w  . ja va  2  s .c o  m
        Nombre.setText(obj.getString("nombre"));
    }
    if (obj.getString("marca") == null) {
        Marca.setText("-");
    } else {
        Marca.setText(obj.getString("marca"));
    }
    if (obj.getString("modelo") == null) {
        Modelo.setText("-");
    } else {
        Modelo.setText(obj.getString("modelo"));
    }
    if (obj.getString("Flash") == null) {
        Flash.setText("-");
    } else if (obj.getBoolean("Flash")) {
        Flash.setText("Si");
    } else {
        Flash.setText("No");
    }
    if (obj.getString("Photoshop") == null) {
        Photoshop.setText("-");
    } else if (obj.getBoolean("Photoshop")) {
        Photoshop.setText("Si");
    } else {
        Photoshop.setText("No");
    }
    if (obj.getString("fecha") == null) {
        Fecha.setText("-");
    } else {
        DateFormat dateFormatHora = new SimpleDateFormat("HH:mm");
        DateFormat dateFormatFecha = new SimpleDateFormat("dd/MM/yyyy");
        Date fechaTime = obj.getDate("fecha");
        String fecha = dateFormatFecha.format(fechaTime);
        String hora = dateFormatHora.format(fechaTime);
        Fecha.setText(fecha + " - " + hora);
    }

    BufferedImage originalImage = null;
    if (obj.getString("ruta") == null) {
        Foto.setText("-");
    } else {
        try {
            File f = new File(obj.getString("ruta") + "\\" + obj.getString("nombre"));
            if (f.isFile()) {
                originalImage = ImageIO.read(f);
                Image scaledImage = originalImage.getScaledInstance(250, 200, Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(scaledImage);
                Foto.setIcon(icon);
            } else {
                Foto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagen/imageNotFound-2.jpg")));
            }
        } catch (IOException ex) {
            Logger.getLogger(PanelFoto.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    pos.setText((posi + 1) + "/" + (l));

}