List of usage examples for com.mongodb BasicDBObject get
public Object get(final String key)
From source file:org.graylog2.streams.StreamRuleServiceImpl.java
License:Open Source License
@Override public StreamRule load(String id) throws NotFoundException { BasicDBObject o = (BasicDBObject) get(StreamRuleImpl.class, new ObjectId(id)); if (o == null) { throw new NotFoundException(); }/*from w w w . j av a 2 s . c o m*/ return new StreamRuleImpl((ObjectId) o.get("_id"), o.toMap()); }
From source file:org.graylog2.streams.StreamServiceImpl.java
License:Open Source License
@Override public AlertCondition getAlertCondition(Stream stream, String conditionId) throws NotFoundException { if (stream.getFields().containsKey(StreamImpl.EMBEDDED_ALERT_CONDITIONS)) { for (BasicDBObject conditionFields : (List<BasicDBObject>) stream.getFields() .get(StreamImpl.EMBEDDED_ALERT_CONDITIONS)) { try { if (conditionFields.get("id").equals(conditionId)) { return alertService.fromPersisted(conditionFields, stream); }//www . j av a 2 s .c om } catch (AbstractAlertCondition.NoSuchAlertConditionTypeException e) { LOG.error("Skipping unknown alert condition type.", e); continue; } catch (Exception e) { LOG.error("Skipping alert condition.", e); continue; } } } throw new org.graylog2.database.NotFoundException(); }
From source file:org.gsafeproject.audit.dao.EventDao.java
License:Open Source License
public Event[] save(Event... events) { if (events == null || events.length <= 0) { return events; }//from w ww . java 2s. co m for (Event event : events) { if (event == null) { continue; } DBCollection coll = db.getCollection(COLLECTION_NAME); BasicDBObject doc = new BasicDBObject("action", event.getAction())// .append("actor", event.getActor())// .append("timestamp", event.getDate())// .append("resource", event.getResource())// .append("description", event.getDescription())// .append("metadata", event.getMetadata()); coll.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); event.setId(id.toString()); } return events; }
From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java
License:LGPL
private BasicDBObject getSubQuery(String operator, BasicDBObject query) { return query.get(operator) != null ? (BasicDBObject) query.get(operator) : new BasicDBObject(); }
From source file:org.icgc.dcc.submission.dictionary.model.validation.CheckRestrictionValidator.java
License:Open Source License
@Override public boolean isValid(Restriction restriction, ConstraintValidatorContext constraintContext) { // TODO: address properly instead of nesting (DCC-904) if (restriction != null) { RestrictionType type = restriction.getType(); if (type != null) { if (type == RestrictionType.REGEX) { BasicDBObject config = restriction.getConfig(); Object object = config.get("pattern"); if (object instanceof String) { String pattern = (String) object; try { compile(pattern); } catch (PatternSyntaxException e) { // must be a pattern that compiles return false; }/*from ww w. ja va2s.co m*/ return true; } else { // must be a String if "pattern" return false; } } else { // TODO: other types return true; } } else { // type is null return false; } } else { // no restrictions return true; } }
From source file:org.keycloak.connections.mongo.updater.impl.updates.AbstractMigrateUserFedToComponent.java
License:Apache License
public void portUserFedToComponent(String providerId) { DBCollection realms = db.getCollection("realms"); DBCursor cursor = realms.find();//from w w w .ja va 2s . c om while (cursor.hasNext()) { BasicDBObject realm = (BasicDBObject) cursor.next(); String realmId = realm.getString("_id"); Set<String> removedProviders = new HashSet<>(); BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); for (Object obj : federationProviders) { BasicDBObject fedProvider = (BasicDBObject) obj; if (fedProvider.getString("providerName").equals(providerId)) { String id = fedProvider.getString("id"); removedProviders.add(id); int priority = fedProvider.getInt("priority"); String displayName = fedProvider.getString("displayName"); int fullSyncPeriod = fedProvider.getInt("fullSyncPeriod"); int changedSyncPeriod = fedProvider.getInt("changedSyncPeriod"); int lastSync = fedProvider.getInt("lastSync"); BasicDBObject component = new BasicDBObject(); component.put("id", id); component.put("name", displayName); component.put("providerType", UserStorageProvider.class.getName()); component.put("providerId", providerId); component.put("parentId", realmId); BasicDBObject config = new BasicDBObject(); config.put("priority", Collections.singletonList(Integer.toString(priority))); config.put("fullSyncPeriod", Collections.singletonList(Integer.toString(fullSyncPeriod))); config.put("changedSyncPeriod", Collections.singletonList(Integer.toString(changedSyncPeriod))); config.put("lastSync", Collections.singletonList(Integer.toString(lastSync))); BasicDBObject fedConfig = (BasicDBObject) fedProvider.get("config"); if (fedConfig != null) { for (Map.Entry<String, Object> attr : new HashSet<>(fedConfig.entrySet())) { String attrName = attr.getKey(); String attrValue = attr.getValue().toString(); config.put(attrName, Collections.singletonList(attrValue)); } } component.put("config", config); componentEntities.add(component); } } Iterator<Object> it = federationProviders.iterator(); while (it.hasNext()) { BasicDBObject fedProvider = (BasicDBObject) it.next(); String id = fedProvider.getString("id"); if (removedProviders.contains(id)) { it.remove(); } } realms.update(new BasicDBObject().append("_id", realmId), realm); } }
From source file:org.keycloak.connections.mongo.updater.impl.updates.AbstractMigrateUserFedToComponent.java
License:Apache License
public void portUserFedMappersToComponent(String providerId, String mapperType) { //logger.info("*** port mappers"); DBCollection realms = db.getCollection("realms"); DBCursor cursor = realms.find();// ww w . ja v a 2 s.c om while (cursor.hasNext()) { BasicDBObject realm = (BasicDBObject) cursor.next(); String realmId = realm.getString("_id"); Set<String> removedProviders = new HashSet<>(); BasicDBList componentEntities = (BasicDBList) realm.get("componentEntities"); BasicDBList federationProviders = (BasicDBList) realm.get("userFederationProviders"); BasicDBList fedMappers = (BasicDBList) realm.get("userFederationMappers"); for (Object obj : federationProviders) { BasicDBObject fedProvider = (BasicDBObject) obj; String providerName = fedProvider.getString("providerName"); //logger.info("looking for mappers of fed provider: " + providerName); if (providerName.equals(providerId)) { String id = fedProvider.getString("id"); //logger.info("found fed provider: " + id + ", looking at mappers"); for (Object obj2 : fedMappers) { BasicDBObject fedMapper = (BasicDBObject) obj2; String federationProviderId = fedMapper.getString("federationProviderId"); //logger.info("looking at mapper with federationProviderId: " + federationProviderId); if (federationProviderId.equals(id)) { String name = fedMapper.getString("name"); String mapperId = fedMapper.getString("id"); removedProviders.add(mapperId); String mapperProviderId = fedMapper.getString("federationMapperType"); BasicDBObject component = new BasicDBObject(); component.put("id", mapperId); component.put("name", name); component.put("providerType", mapperType); component.put("providerId", mapperProviderId); component.put("parentId", id); BasicDBObject fedConfig = (BasicDBObject) fedMapper.get("config"); BasicDBObject config = new BasicDBObject(); if (fedConfig != null) { for (Map.Entry<String, Object> attr : new HashSet<>(fedConfig.entrySet())) { String attrName = attr.getKey(); String attrValue = attr.getValue().toString(); config.put(attrName, Collections.singletonList(attrValue)); } } component.put("config", config); componentEntities.add(component); } } } } Iterator<Object> it = fedMappers.iterator(); while (it.hasNext()) { BasicDBObject fedMapper = (BasicDBObject) it.next(); String id = fedMapper.getString("id"); if (removedProviders.contains(id)) { it.remove(); } } realms.update(new BasicDBObject().append("_id", realmId), realm); } }
From source file:org.keycloak.connections.mongo.updater.impl.updates.Update2_3_0.java
License:Apache License
@Override public void update(KeycloakSession session) { DBCollection realms = db.getCollection("realms"); DBCursor cursor = realms.find();//from www .j av a2 s. co m while (cursor.hasNext()) { BasicDBObject realm = (BasicDBObject) cursor.next(); String realmId = realm.getString("_id"); String privateKeyPem = realm.getString("privateKeyPem"); String certificatePem = realm.getString("certificatePem"); BasicDBList entities = (BasicDBList) realm.get("componentEntities"); BasicDBObject component = new BasicDBObject(); component.put("id", KeycloakModelUtils.generateId()); component.put("name", "rsa"); component.put("providerType", KeyProvider.class.getName()); component.put("providerId", "rsa"); component.put("parentId", realmId); BasicDBObject config = new BasicDBObject(); config.put("priority", Collections.singletonList("100")); config.put("privateKey", Collections.singletonList(privateKeyPem)); config.put("certificate", Collections.singletonList(certificatePem)); component.put("config", config); entities.add(component); realm.remove("privateKeyPem"); realm.remove("certificatePem"); realm.remove("publicKeyPem"); realm.remove("codeSecret"); realms.update(new BasicDBObject().append("_id", realmId), realm); } }
From source file:org.kiaan.Main.java
private static void getBuy() { try {//from w w w .j a v a 2 s . c o m MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("kiaan"); DBCollection coll = db.getCollection("buy"); //aggregate DBObject unwind = new BasicDBObject("$unwind", "$items"); //$group DBObject group_id = new BasicDBObject("_id", "$_id"); group_id.put("num", "$num"); group_id.put("person_id", "$person_id"); group_id.put("discount", "$discount"); group_id.put("increase", "$increase"); //$group -> $multiply BasicDBList args = new BasicDBList(); args.add("$items.value"); args.add("$items.price"); DBObject multiply = new BasicDBObject("$multiply", args); //$group -> $sum // DBObject group_sum = new BasicDBObject("$sum", multiply); DBObject group_field = new BasicDBObject(); group_field.put("_id", group_id); group_field.put("total", new BasicDBObject("$sum", multiply)); DBObject group = new BasicDBObject("$group", group_field); //$project DBObject project_field = new BasicDBObject("_id", "$_id._id"); project_field.put("person_id", "$_id.person_id"); project_field.put("num", "$_id.num"); BasicDBList arr = new BasicDBList(); arr.add("$total"); arr.add("$_id.discount"); arr.add("$_id.increase"); DBObject field_add = new BasicDBObject("$add", arr); project_field.put("sum", field_add); DBObject project = new BasicDBObject("$project", project_field); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1)); List<DBObject> pipeline = Arrays.asList(unwind, group, project, sort); // AggregationOutput output = coll.aggregate(pipeline); // for (DBObject result : output.results()) { // System.out.println(result); // } AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); BasicDBObject dbo = new BasicDBObject(); BasicDBList dbl = new BasicDBList(); Cursor cursor = coll.aggregate(pipeline, aggregationOptions); // DBCollection person_col = db.getCollection("persons"); // BasicDBObject query = new BasicDBObject("items.personId",1); BasicDBObject fields = new BasicDBObject("items.$", 1).append("_id", false); // BasicDBList l_per = (BasicDBList) person_col.findOne(query, fields).get("items"); // BasicDBObject[] lightArr = l_per.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("_id")); // System.out.println(lightArr[0].get("first_name")); BasicDBList result = new BasicDBList(); while (cursor.hasNext()) { dbo = (BasicDBObject) cursor.next(); // System.out.println(dbo.toString()); DBObject query = new BasicDBObject("items._id", (ObjectId) dbo.get("person_id")); BasicDBList lst_person = (BasicDBList) person_col.findOne(query, fields).get("items"); BasicDBObject[] lightArr = lst_person.toArray(new BasicDBObject[0]); // System.out.println(lightArr[0].get("first_name")); Date date = ((ObjectId) lightArr[0].get("_id")).getDate(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); persianCalendar persianCalendar = new persianCalendar(calendar); dbo.put("date", persianCalendar.getNumericDateFormatWithTime()); dbo.put("personId", lightArr[0].get("personId").toString()); dbo.put("first_name", lightArr[0].get("first_name").toString()); dbo.put("last_name", lightArr[0].get("last_name").toString()); dbo.removeField("person_id"); result.add(dbo); // System.out.println(dbo.get("num")); } System.out.println(result.toString()); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:org.knowrob.knowrob_robcog.MongoRobcogQueries.java
License:Open Source License
public double[] GetActorPoseAt(String actorName, double timestamp) { // $and list for querying the $match in the aggregation BasicDBList time_and_name = new BasicDBList(); // add the timestamp and the actor name time_and_name.add(new BasicDBObject("timestamp", new BasicDBObject("$lte", timestamp))); time_and_name.add(new BasicDBObject("actors.name", actorName)); // create the pipeline operations, first the $match DBObject match_time_and_name = new BasicDBObject("$match", new BasicDBObject("$and", time_and_name)); // sort the results in descending order on the timestamp (keep most recent result first) DBObject sort_desc = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1)); // $limit the result to 1, we only need one pose DBObject limit_result = new BasicDBObject("$limit", 1); // $unwind actors in order to output only the queried actor DBObject unwind_actors = new BasicDBObject("$unwind", "$actors"); // $match for the given actor name from the unwinded actors DBObject match_actor = new BasicDBObject("$match", new BasicDBObject("actors.name", actorName)); // build the $projection operation DBObject proj_fields = new BasicDBObject("_id", 0); proj_fields.put("timestamp", 1); proj_fields.put("pos", "$actors.pos"); proj_fields.put("rot", "$actors.rot"); DBObject project = new BasicDBObject("$project", proj_fields); // run aggregation List<DBObject> pipeline = Arrays.asList(match_time_and_name, sort_desc, limit_result, unwind_actors, match_actor, project);//from ww w. j a va2s. c om AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100) .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build(); // get results Cursor cursor = this.MongoRobcogConn.coll.aggregate(pipeline, aggregationOptions); // if query has a response, return the pose if (cursor.hasNext()) { // get the first document as the next cursor and append the metadata to it BasicDBObject first_doc = (BasicDBObject) cursor.next(); // close cursor cursor.close(); // get the pose return new double[] { ((BasicDBObject) first_doc.get("pos")).getDouble("x"), ((BasicDBObject) first_doc.get("pos")).getDouble("y"), ((BasicDBObject) first_doc.get("pos")).getDouble("z"), ((BasicDBObject) first_doc.get("rot")).getDouble("w"), ((BasicDBObject) first_doc.get("rot")).getDouble("x"), ((BasicDBObject) first_doc.get("rot")).getDouble("y"), ((BasicDBObject) first_doc.get("rot")).getDouble("z") }; } else { System.out.println("Java - GetActorPose - No results found, returning empty list.."); return new double[0]; } }