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:com.petpet.c3po.dao.mongo.MongoFilterSerializer.java

License:Apache License

public DBObject serializeNew(Filter filter) {
    DBObject result = new BasicDBObject();
    if (filter != null) {
        if (filter.getRaw() != null) {
            Object o = com.mongodb.util.JSON.parse(filter.getRaw());
            return (DBObject) o;
        }/*from w ww .  j a  v  a2  s.com*/

        if (filter.getConditions() != null && filter.getConditions().size() > 0)
            return serialize(filter);
        List<PropertyFilterCondition> propertyFilterConditions = filter.getPropertyFilterConditions();

        List<BasicDBObject> and = new ArrayList<BasicDBObject>();

        for (PropertyFilterCondition propertyFilterCondition : propertyFilterConditions) {
            BasicDBObject metadata = new BasicDBObject();
            BasicDBObject elemMatch = new BasicDBObject();
            BasicDBObject elemMatchComponents = new BasicDBObject();

            String propertyName = propertyFilterCondition.getProperty();

            elemMatchComponents.put("property", propertyName);

            //process separate property values
            if (propertyFilterCondition.getValues().size() > 0) {
                List<Object> list = new ArrayList<Object>();
                for (String value : propertyFilterCondition.getValues()) {
                    list.add(normalizeValue(value, propertyName));
                }
                BasicDBObject all = new BasicDBObject("$all", list);
                if (filter.isStrict())
                    all.put("$size", list.size());
                elemMatchComponents.put("sourcedValues.value", all);
            }

            //process separate property value sources
            if (propertyFilterCondition.getSources().size() > 0) {
                List<Object> list = new ArrayList<Object>();
                for (String source : propertyFilterCondition.getSources()) {
                    Source s = null;
                    if (source.contains(":")) {
                        String[] split = source.split(":");
                        s = Configurator.getDefaultConfigurator().getPersistence().getCache()
                                .getSource(split[0], split[1]);
                    } else
                        s = Configurator.getDefaultConfigurator().getPersistence().getCache().getSource(source);
                    list.add(s.getId());
                }
                BasicDBObject all = new BasicDBObject("$all", list);
                if (filter.isStrict())
                    all.put("$size", list.size());
                elemMatchComponents.put("sourcedValues.source", all);
            }

            //process property sourced values
            if (propertyFilterCondition.getSourcedValues().size() > 0) {
                List<Object> list = new ArrayList<Object>();
                List<String> sourceIDs = new ArrayList<String>();
                for (Map.Entry<String, String> stringStringEntry : propertyFilterCondition.getSourcedValues()
                        .entrySet()) {
                    if (stringStringEntry.getValue().equals("UNKNOWN")) {
                        BasicDBObject elemMatchSourcedValue = produceElemMatch(stringStringEntry, propertyName);
                        BasicDBObject tmp_elemMatch = (BasicDBObject) elemMatchSourcedValue.get("$elemMatch");
                        sourceIDs.add(tmp_elemMatch.getString("source"));
                    } else {
                        BasicDBObject elemMatchSourcedValue = produceElemMatch(stringStringEntry, propertyName);
                        list.add(elemMatchSourcedValue);
                    }
                    if (sourceIDs.size() > 0) {
                        BasicDBObject nin = new BasicDBObject("$nin", sourceIDs);
                        elemMatchComponents.put("sourcedValues.source", nin);
                    }
                }
                BasicDBObject all = new BasicDBObject("$all", list);
                if (filter.isStrict())
                    all.put("$size", list.size());
                if (list.size() > 0)
                    elemMatchComponents.put("sourcedValues", all);
            }

            //process separate property statuses
            if (propertyFilterCondition.getStatuses().size() > 0) {
                List<Object> list = new ArrayList<Object>();
                for (String status : propertyFilterCondition.getStatuses()) {
                    list.add(status);
                }
                BasicDBObject all = new BasicDBObject("$in", list);
                elemMatchComponents.put("status", all);
            }

            elemMatch.put("$elemMatch", elemMatchComponents);
            metadata.put("metadata", elemMatch);
            and.add(metadata);
        }

        if (and.size() > 0)
            result.put("$and", and);

        /*if (filter.getConditions()!=null && filter.getConditions().size()>0)
        return serialize(filter);
        List<PropertyFilterCondition> propertyFilterConditions = filter.getPropertyFilterConditions();
        List<BasicDBObject> and = new ArrayList<BasicDBObject>();
                
        for (PropertyFilterCondition propertyFilterCondition : propertyFilterConditions) {
        String propertyName = propertyFilterCondition.getProperty();
        and.addAll(getSourcedValueList(propertyFilterCondition.getSourcedValues(), propertyName));
        and.addAll(getPropertyList(propertyName)) ;
        and.addAll(getStatusList(propertyFilterCondition.getStatuses(), "$or"));
        and.addAll(getSourceList(propertyFilterCondition.getSources(), "$and"));
        and.addAll(getValueList(propertyFilterCondition.getValues(), "$and", propertyName));
                
        }
        if (and.size() > 0)
        result.put("$and", and);*/

    }

    return result;

}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

/**
 * Parses the histogram results out of a {@link DBObject}. This method assumes
 * that the passed db object contains a list of {@link DBObject}s with every
 * result. (As it is outputted by the map reduce job).
 *
 * @param object the object to parse./*from w w w.j  a va 2s . com*/
 * @return the histogram map.
 */
private Map<String, Long> parseHistogramResults(DBObject object) {
    Map<String, Long> histogram = new HashMap<String, Long>();

    if (object == null) {
        return histogram;
    }

    List<BasicDBObject> results = (List<BasicDBObject>) object;
    for (final BasicDBObject dbo : results) {
        histogram.put(DataHelper.removeTrailingZero(dbo.getString("_id")), dbo.getLong("value"));
    }

    return histogram;
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public Map<String, Long> parseAggregation(List<BasicDBObject> aggregation, String propert, Boolean getStats) {
    Map<String, Long> histogram = new HashMap<String, Long>();

    if (aggregation == null) {
        return histogram;
    }//from  w w  w .ja v  a  2s  . co m
    for (BasicDBObject basicDBObject : aggregation) {
        String id = basicDBObject.getString("_id");
        try {
            if (!getStats) {
                long count = basicDBObject.getLong("count");
                histogram.put(id, count);
            } else {
                long sum, min, max, avg, std, var, count;
                try {
                    count = basicDBObject.getLong("count");
                } catch (Exception ex) {
                    count = 0;
                }
                try {
                    sum = basicDBObject.getLong("sum");
                } catch (Exception ex) {
                    sum = 0;
                }
                try {
                    min = basicDBObject.getLong("min");
                } catch (Exception ex) {
                    min = 0;
                }
                try {
                    max = basicDBObject.getLong("max");
                } catch (Exception ex) {
                    max = 0;
                }
                try {
                    avg = basicDBObject.getLong("avg");
                } catch (Exception ex) {
                    avg = 0;
                }
                try {
                    std = basicDBObject.getLong("stdDev");
                } catch (Exception ex) {
                    std = 0;
                }
                try {
                    var = std * std;
                } catch (Exception ex) {
                    var = 0;
                }
                histogram.put("sum", sum);
                histogram.put("min", min);
                histogram.put("max", max);
                histogram.put("avg", avg);
                histogram.put("std", std);
                histogram.put("var", var);
                histogram.put("count", count);
            }
        } catch (Exception e) {
        }
    }
    return histogram;
}

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

License:Open Source License

/**
 * Return a list of the reports associated with the concern 
 **  each report will have an id and a order 
 *//*from w ww  .jav a 2s .  c  o m*/
public int getReports(int concernId) {
    int reportCount;
    ArrayList<String[]> reportList = new ArrayList<String[]>();
    checkExistingId(concernId);

    try {
        currentConcernDoc.get("Reports");

        ListIterator<Object> scriptObjects = ((BasicDBList) currentConcernDoc.get("Reports")).listIterator();
        while (scriptObjects.hasNext()) {
            String[] t = new String[2];
            BasicDBObject nextItem = (BasicDBObject) scriptObjects.next();
            t[0] = nextItem.getString("reportId");
            t[1] = nextItem.getString("reportOrder");
            reportList.add(t);

        }
        if (reportList.isEmpty()) {
            return 0;
        } else {
            reportCount = reportList.size();
            reports = new int[reportCount][2];
            int i;
            for (i = 0; i < reportCount; i++) {
                String[] t = new String[2];
                t = reportList.get(i);
                reports[i][0] = Integer.parseInt(t[0]);
                reports[i][1] = Integer.parseInt(t[1]);
            }
        }
    } catch (Exception e) {
        return 0;
    }

    return reportCount;
}

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

License:Open Source License

/**
 * Return a list of the scripts associated with the concern 
 **  each script will have an id and a order 
 *///from  w w w. jav  a2s.  c om
public int getScripts(int concernId) {

    int scriptCount;
    ArrayList<String[]> scriptList = new ArrayList<String[]>();
    checkExistingId(concernId);

    try {
        currentConcernDoc.get("scripts");

        ListIterator<Object> scriptObjects = ((BasicDBList) currentConcernDoc.get("Scripts")).listIterator();
        while (scriptObjects.hasNext()) {
            String[] t = new String[2];
            BasicDBObject nextItem = (BasicDBObject) scriptObjects.next();
            t[0] = nextItem.getString("scriptId");
            t[1] = nextItem.getString("scriptOrder");
            scriptList.add(t);

        }
        if (scriptList.isEmpty()) {
            return 0;
        } else {
            scriptCount = scriptList.size();
            scripts = new int[scriptCount][2];
            int i;
            for (i = 0; i < scriptCount; i++) {
                String[] t = new String[2];
                t = scriptList.get(i);
                scripts[i][0] = Integer.parseInt(t[0]);
                scripts[i][1] = Integer.parseInt(t[1]);
            }
        }
    } catch (Exception e) {
        return 0;
    }

    return scriptCount;
}

From source file:com.streamreduce.core.service.EventServiceImpl.java

License:Apache License

/**
 * Helper method that returns all metadata for a {@link InventoryItem}.
 *
 * @param inventoryItem the cloud inventory item to retrieve metadata for/about
 * @return the metadata/*from w  w  w . j  a v  a 2 s  .co  m*/
 */
private Map<String, Object> getMetadataFromInventoryItem(InventoryItem inventoryItem) {
    // NOTE: We're not using CloudService methods here for performance reasons
    Map<String, Object> civMetadata = new HashMap<>();

    // Right now, we are only creating extended metadata for AWS EC2 instance items
    if (inventoryItem.getConnection().getProviderId().equals(ProviderIdConstants.AWS_PROVIDER_ID)
            && inventoryItem.getType().equals(Constants.COMPUTE_INSTANCE_TYPE)) {
        DBObject cMetadata = genericCollectionDAO.getById(DAODatasourceType.BUSINESS,
                Constants.INVENTORY_ITEM_METADATA_COLLECTION_NAME, inventoryItem.getMetadataId());

        if (cMetadata == null) {
            // Fill in the metadata based on the last event for this target
            Event previousEvent = getLastEventForTarget(inventoryItem.getId());

            if (previousEvent != null) {
                Map<String, Object> peMetadata = previousEvent.getMetadata();

                if (peMetadata != null) {
                    civMetadata.put("targetIP", peMetadata.get("targetIP"));
                    civMetadata.put("targetOS", peMetadata.get("targetOS"));
                    civMetadata.put("targetISO3166Code", peMetadata.get("targetISO3166Code"));
                    civMetadata.put("targetRegion", peMetadata.get("targetRegion"));
                    civMetadata.put("targetZone", peMetadata.get("targetZone"));
                }
            }
        } else {
            // Fill in the metadata from the available node metadata

            // Get the IP address
            if (cMetadata.containsField("publicAddresses")) {
                BasicDBList publicAddresses = (BasicDBList) cMetadata.get("publicAddresses");

                // TODO: How do we want to handle multiple IP addresses?
                if (publicAddresses.size() > 0) {
                    civMetadata.put("targetIP", publicAddresses.get(0));
                }
            }

            // Get location information (ISO 3166 code, region and availability zone)
            if (cMetadata.containsField("location") && cMetadata.get("location") != null) {
                BasicDBObject location = (BasicDBObject) cMetadata.get("location");
                boolean regionProcessed = false;
                boolean zoneProcessed = false;

                while (location != null) {
                    if (regionProcessed && zoneProcessed) {
                        break;
                    }

                    String locationScope = location.containsField("scope") ? location.getString("scope") : null;

                    if (locationScope != null) {
                        LocationScope scope = LocationScope.valueOf(locationScope);

                        switch (scope) {
                        case REGION:
                            civMetadata.put("targetRegion", location.get("id"));
                            regionProcessed = true;
                            break;
                        case ZONE:
                            BasicDBList iso3166Codes = (BasicDBList) location.get("iso3166Codes");

                            civMetadata.put("targetISO3166Code", iso3166Codes.get(0));
                            civMetadata.put("targetZone", location.get("id"));
                            zoneProcessed = true;
                            break;
                        }
                    }

                    location = location.containsField("parent") && location.get("parent") != null
                            ? (BasicDBObject) location.get("parent")
                            : null;
                }
            }

            // Get OS name
            if (cMetadata.containsField("operatingSystem")) {
                BasicDBObject operatingSystem = (BasicDBObject) cMetadata.get("operatingSystem");

                if (operatingSystem != null) {
                    if (operatingSystem.containsField("family")) {
                        civMetadata.put("targetOS", operatingSystem.get("family"));
                    }
                }
            }
        }
    }

    return civMetadata;
}

From source file:com.streamreduce.core.service.InventoryServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from  ww w. j ava  2 s  .  c om*/
 */
@Override
public void rebootComputeInstance(InventoryItem inventoryItem)
        throws CommandNotAllowedException, InvalidCredentialsException {
    Preconditions.checkNotNull(inventoryItem, "inventoryItem cannot be null.");
    Preconditions.checkArgument(inventoryItem.getType().equals(Constants.COMPUTE_INSTANCE_TYPE),
            "Inventory item of type '" + inventoryItem.getType() + "' cannot be rebooted.");

    AWSClient client = (AWSClient) getClient(inventoryItem.getConnection());

    logger.debug("Rebooting node: " + inventoryItem.getExternalId());

    BasicDBObject payload = getInventoryItemPayload(inventoryItem);
    String jcloudsNodeId = payload.getString("id");
    NodeMetadata nodeMetadata = client.getEC2Instance(jcloudsNodeId);

    if (nodeMetadata.getStatus().equals(NodeMetadata.Status.TERMINATED)) {
        throw new CommandNotAllowedException("You cannot reboot a terminated node.");
    }

    EventId eventId;

    if (client.rebootEC2Instance(jcloudsNodeId)) {
        eventId = EventId.CLOUD_INVENTORY_ITEM_REBOOT;
    } else {
        // TODO: Handle this issue but it can be a false positive if the time it takes surpasses the time we wait
        eventId = EventId.CLOUD_INVENTORY_ITEM_REBOOT_FAILURE;
    }

    // Create the event
    Event event = eventService.createEvent(eventId, inventoryItem, null);
    // Create the message
    messageService.sendInventoryMessage(event, inventoryItem);
}

From source file:com.streamreduce.core.service.InventoryServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}//w  ww  . j  a  va2s .  c  o  m
 */
@Override
public void destroyComputeInstance(InventoryItem inventoryItem) throws InvalidCredentialsException {
    Preconditions.checkNotNull(inventoryItem, "inventoryItem cannot be null.");
    Preconditions.checkArgument(inventoryItem.getType().equals(Constants.COMPUTE_INSTANCE_TYPE),
            "Inventory item of type '" + inventoryItem.getType() + "' cannot be destroyed.");

    AWSClient client = (AWSClient) getClient(inventoryItem.getConnection());

    logger.debug("Terminating node: " + inventoryItem.getExternalId());

    BasicDBObject payload = getInventoryItemPayload(inventoryItem);
    String jcloudsNodeId = payload.getString("id");
    NodeMetadata nodeMetadata = client.getEC2Instance(jcloudsNodeId);

    if (nodeMetadata.getStatus().equals(NodeMetadata.Status.TERMINATED)) {
        return;
    }

    EventId eventId;

    if (client.destroyEC2Instance(jcloudsNodeId)) {
        eventId = EventId.CLOUD_INVENTORY_ITEM_TERMINATE;
    } else {
        // TODO: Handle this issue but it can be a false positive if the time it takes surpasses the time we wait
        eventId = EventId.CLOUD_INVENTORY_ITEM_TERMINATE_FAILURE;
    }

    // Create the event
    Event event = eventService.createEvent(eventId, inventoryItem, null);
    // Create the message
    messageService.sendInventoryMessage(event, inventoryItem);
}

From source file:com.streamreduce.core.transformer.message.AgentMessageTransformer.java

License:Apache License

/**
 * {@inheritDoc}//from  www .j  a  v a 2  s.  c o  m
 */
@Override
public String doTransform(Event event) {
    EventId eventId = event.getEventId();
    Map<String, Object> eventMetadata = event.getMetadata();
    String msg = "";

    switch (eventId) {
    case ACTIVITY:
        BasicDBObject payload = (BasicDBObject) eventMetadata.get("payload");
        StringBuilder sb = new StringBuilder();

        sb.append("Current system overview at ").append(eventMetadata.get("activityGenerated")) // Should we format this?
                .append("\n\n");

        sb.append("Uptime: ").append(payload.getString("uptime")).append("s\n").append("Disk usage:\n");

        BasicDBObject partitionsObj = (BasicDBObject) payload.get("partitions");
        Set<String> partitions = new TreeSet<>(partitionsObj.keySet());

        for (String key : partitions) {
            BasicDBObject partition = (BasicDBObject) partitionsObj.get(key);
            double totalAsKb = partition.getDouble("total");

            // Certain devices show as 0.00GB and should be pruned
            if (totalAsKb == 0) {
                continue;
            }

            double totalAsGB = MessageUtils.kbToGB(totalAsKb);
            double usedAsGB = MessageUtils.kbToGB(partition.getDouble("used"));
            double freeAsGB = MessageUtils.kbToGB(partition.getDouble("free"));

            sb.append("  ").append(key).append(": Total ").append(MessageUtils.roundAndTruncate(totalAsGB, 2))
                    .append("GB, Used ").append(MessageUtils.roundAndTruncate(usedAsGB, 2)).append("GB, Free ")
                    .append(MessageUtils.roundAndTruncate(freeAsGB, 2)).append("GB\n");
        }

        sb.append("Disk I/O:\n");

        BasicDBObject diskIO = (BasicDBObject) payload.get("disk_io");
        Set<String> disks = new TreeSet<>(diskIO.keySet());

        if (disks.size() == 0) {
            sb.append("  Unavailable\n");
        } else {
            for (String key : disks) {
                BasicDBObject disk = (BasicDBObject) diskIO.get(key);
                long reads = disk.getLong("read_count");
                long writes = disk.getLong("write_count");
                double gbRead = MessageUtils.kbToGB(disk.getLong("read_kbytes"));
                double gbWrite = MessageUtils.kbToGB(disk.getLong("write_kbytes"));
                long readSecs = disk.getLong("read_time");
                long writeSecs = disk.getLong("write_time");

                sb.append("  ").append(key).append(": Reads ").append(reads).append(", Writes ").append(writes)
                        .append(", GB Read ").append(MessageUtils.roundAndTruncate(gbRead, 2))
                        .append(", GB Written ").append(MessageUtils.roundAndTruncate(gbWrite, 2))
                        .append(", Read Time ").append(readSecs).append("s, Write Time ").append(writeSecs)
                        .append("s\n");
            }
        }

        sb.append("Network I/O:\n");

        BasicDBObject netIO = (BasicDBObject) payload.get("network_io");
        Set<String> nics = new TreeSet<>(netIO.keySet());
        int nicsDisplayed = 0;

        for (String key : nics) {
            BasicDBObject nic = (BasicDBObject) netIO.get(key);
            long packetsIn = nic.getInt("packets_in");
            long packetsOut = nic.getInt("packets_out");

            // Certain devices show 0 packets in/out and should be pruned
            if (packetsIn == 0 && packetsOut == 0) {
                continue;
            }

            double gbIn = MessageUtils.kbToGB(nic.getLong("kbytes_in"));
            double gbOut = MessageUtils.kbToGB(nic.getLong("kbytes_out"));

            sb.append("  ").append(key).append(": Packets In ").append(packetsIn).append(", Packets Out ")
                    .append(packetsOut).append(", GB In ").append(MessageUtils.roundAndTruncate(gbIn, 2))
                    .append(", GB Out ").append(MessageUtils.roundAndTruncate(gbOut, 2)).append("\n");

            nicsDisplayed++;
        }

        if (nicsDisplayed == 0) {
            sb.append("  Unavailable\n");
        }

        sb.append("Load: 1m ").append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_0"), 2))
                .append(", ").append("5m ")
                .append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_1"), 2)).append(", ")
                .append("15m ").append(MessageUtils.roundAndTruncate(payload.getDouble("load_avg_2"), 2))
                .append("\n");

        float gbTotalRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_total"));
        float gbUsedRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_used"));
        float gbFreeRAM = (float) MessageUtils.kbToGB(payload.getLong("phy_ram_free"));

        sb.append("Real Mem: Total ").append(MessageUtils.roundAndTruncate(gbTotalRAM, 2)).append("GB, Used ")
                .append(MessageUtils.roundAndTruncate(gbUsedRAM, 2)).append("GB, Free ")
                .append(MessageUtils.roundAndTruncate(gbFreeRAM, 2)).append("GB\n");

        double gbTotalVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_total"));
        double gbUsedVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_used"));
        double gbFreeVRAM = MessageUtils.kbToGB(payload.getLong("vir_ram_free"));

        sb.append("Virt Mem: Total ").append(MessageUtils.roundAndTruncate(gbTotalVRAM, 2)).append("GB, Used ")
                .append(MessageUtils.roundAndTruncate(gbUsedVRAM, 2)).append("GB, Free ")
                .append(MessageUtils.roundAndTruncate(gbFreeVRAM, 2)).append("GB\n");

        sb.append("Processes: ").append(payload.getInt("processes")).append("\n");

        sb.append("Users: Total ").append(payload.getInt("users_total")).append(", Unique ")
                .append(payload.getInt("users_unique")).append("\n");

        msg = sb.toString();
        break;
    default:
        super.doTransform(event);
        break;
    }
    return msg;
}

From source file:com.streamreduce.storm.bolts.InternalConnectionInventoryBolt.java

License:Apache License

/**
 * {@inheritDoc}//w ww  . jav a 2  s . c o  m
 */
@Override
public void execute(Tuple tuple) {
    try {
        BasicDBObject connection = (BasicDBObject) tuple.getValue(0);
        String id = connection.getString("_id");
        String type = connection.getString("type");
        List<BasicDBObject> inventoryItems = new ArrayList<>();

        if (type.equals(ConnectionTypeConstants.FEED_TYPE)) {
            // Do nothing, just here for posterity
        } else if (type.equals(ConnectionTypeConstants.PROJECT_HOSTING_TYPE)) {
            inventoryItems = mongoClient.getProjectHostingInventoryItems(id);
        } else if (type.equals(ConnectionTypeConstants.CLOUD_TYPE)) {
            inventoryItems = mongoClient.getCloudInventoryItems(id);
        } else {
            // TODO: We need to figure out how we want to handle this
            logger.warn(type + " is an unsupported connection type.");
        }

        for (BasicDBObject inventoryItem : inventoryItems) {
            outputCollector.emit(new Values("internal", connection, inventoryItem));
        }
    } catch (Exception e) {
        logger.error("Unknown exception type in InternalConnectionInventoryBolt " + e.getMessage());
    }
}