List of usage examples for com.mongodb Block Block
Block
From source file:entities.Usuario.java
public static Usuario getUsuarioByName(String name) { Usuario user = new Usuario(); MongoManager mongo = MongoManager.getInstance(); FindIterable<Document> iterable = mongo.db.getCollection("usuario").find(new Document("nombre", name)); iterable.forEach(new Block<Document>() { @Override// www .jav a2 s . c om public void apply(final Document document) { user.idUsuario = (ObjectId) document.get("_id"); user.nombre = document.get("nombre").toString(); user.password = document.get("password").toString(); user.tipo = (Integer) document.get("tipo"); user.email = document.get("email").toString(); user.leyendaTipo = user.returnLeyendaTipo(user.tipo); } }); return user; }
From source file:entities.Usuario.java
public static Usuario getUsuarioById(ObjectId id) { Usuario user = new Usuario(); MongoManager mongo = MongoManager.getInstance(); FindIterable<Document> iterable = mongo.db.getCollection("usuario").find(new Document("_id", id)); iterable.forEach(new Block<Document>() { @Override// w w w. j a va2 s . co m public void apply(final Document document) { user.idUsuario = (ObjectId) document.get("_id"); user.nombre = document.get("nombre").toString(); user.password = document.get("password").toString(); user.tipo = (Integer) document.get("tipo"); user.email = document.get("email").toString(); user.leyendaTipo = user.returnLeyendaTipo(user.tipo); } }); return user; }
From source file:entities.Usuario.java
public static List<Usuario> getAllUsuarios() { List<Usuario> res = new ArrayList<>(); MongoManager mongo = MongoManager.getInstance(); FindIterable<Document> iterable = mongo.db.getCollection("usuario").find(); iterable.forEach(new Block<Document>() { @Override//w w w .j a v a2 s . c o m public void apply(final Document document) { Usuario user = new Usuario(); user.idUsuario = (ObjectId) document.get("_id"); user.nombre = document.get("nombre").toString(); user.password = document.get("password").toString(); user.tipo = (Integer) document.get("tipo"); user.email = document.get("email").toString(); user.leyendaTipo = user.returnLeyendaTipo(user.tipo); res.add(user); } }); return res; }
From source file:eu.vital.vitalcep.collector.Collector.java
private void getCollectorList() { try {//from w w w. j a v a 2 s.c o m MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); final MongoDatabase db = mongo.getDatabase(mongoDB); BasicDBObject clause1 = new BasicDBObject("cepType", "CONTINUOUS").append("status", "OK"); BasicDBObject clause2 = new BasicDBObject("cepType", "CEPICO"); BasicDBObject clause3 = new BasicDBObject("cepType", "ALERT"); BasicDBList or = new BasicDBList(); or.add(clause1); or.add(clause2); or.add(clause3); BasicDBObject query = new BasicDBObject("$or", or); FindIterable<Document> coll; coll = db.getCollection("cepinstances").find(query); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { JSONObject oCollector = new JSONObject(); oCollector.put("id", document.getObjectId("_id").toString()); oCollector.put("mqin", document.getString("mqin")); oCollector.put("mqout", document.getString("mqout")); oCollector.put("cepType", document.getString("cepType")); if (document.getString("cepType").equals("CONTINUOUS")) { final Document doc = new Document("sources", document.get("sources")); final String jsonStringSources = doc.toJson(); JSONObject sources = new JSONObject(jsonStringSources); final Document docproperties = new Document("properties", document.get("properties")); final String jsonStringproperties = docproperties.toJson(); JSONObject sourcesproperties = new JSONObject(jsonStringproperties); oCollector.put("sources", sources.getJSONArray("sources")); oCollector.put("properties", sourcesproperties.getJSONArray("properties")); oCollector.put("username", document.getString("username")); oCollector.put("password", document.getString("password")); } else { final Document doc = new Document("requests", document.get("requests")); final String jsonStringRequests = doc.toJson(); JSONObject requestsObject = new JSONObject(jsonStringRequests); oCollector.put("requests", requestsObject.getJSONArray("requests")); oCollector.put("username", document.getString("username")); oCollector.put("password", document.getString("password")); } oCollector.put("lastRequest", document.getString("lastRequest")); sensors.put(oCollector); } }); } catch (Exception e) { String a = "a"; } finally { if (db != null) db = null; if (mongo != null) { mongo.close(); mongo = null; } } }
From source file:eu.vital.vitalcep.restApp.alert.Alerts.java
/** * Gets the filters.//from w ww. j a va2 s.c o m * * @return the filters */ @GET @Path("getalerts") @Produces(MediaType.APPLICATION_JSON) public String getAlerts() { MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("alerts").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { AllJson.put(document); } }); if (db != null) db = null; if (mongo != null) { mongo.close(); mongo = null; } return AllJson.toString(); }
From source file:eu.vital.vitalcep.restApp.cepRESTApi.CEPICO.java
/** * Gets the filters./*from w w w . ja v a 2s . co m*/ * * @return the filters */ @GET @Path("getcepicos") @Produces(MediaType.APPLICATION_JSON) public Response getCEPICOs() { MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false).append("cepinstance", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("cepicos").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { AllJson.put(document); } }); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.OK).entity(AllJson.toString()).build(); }
From source file:eu.vital.vitalcep.restApp.filteringApi.ContinuosFiltering.java
/** * Gets the filters./*from w ww .ja v a2 s . co m*/ * * @return the filters */ @GET @Path("getcontinuousfilters") @Produces(MediaType.APPLICATION_JSON) public Response getFilterings(@Context HttpServletRequest req) { // StringBuilder ck = new StringBuilder(); // Security slogin = new Security(); // // Boolean token = slogin.login(req.getHeader("name") // ,req.getHeader("password"),false,ck); // if (!token){ // return Response.status(Response.Status.UNAUTHORIZED).build(); // } // this.cookie = ck.toString(); MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("continuousfilters"); } catch (Exception e) { //System.out.println("Mongo is down"); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } // create an empty query BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("continuousfilters").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); AllJson.put(sensoraux); } }); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.OK).entity(AllJson.toString()).build(); }
From source file:eu.vital.vitalcep.restApp.filteringApi.StaticFiltering.java
/** * Gets the filters.//from ww w. jav a 2 s . co m * * @return the filters */ @GET @Path("getstaticdatafilters") @Produces(MediaType.APPLICATION_JSON) public Response getStaticDataFilters(@Context HttpServletRequest req) { StringBuilder ck = new StringBuilder(); Security slogin = new Security(); Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck); if (!token) { return Response.status(Response.Status.UNAUTHORIZED).build(); } this.cookie = ck.toString(); MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("staticdatafilters"); } catch (Exception e) { //System.out.println("Mongo is down"); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } // create an empty query BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("staticdatafilters").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); AllJson.put(sensoraux); } }); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.OK).entity(AllJson.toString()).build(); }
From source file:eu.vital.vitalcep.restApp.filteringApi.StaticFiltering.java
/** * Gets the filters.//from w w w.jav a 2 s .c o m * * @return the filters */ @GET @Path("getstaticqueryfilters") @Produces(MediaType.APPLICATION_JSON) public Response getStaticQueryFilters(@Context HttpServletRequest req) { StringBuilder ck = new StringBuilder(); Security slogin = new Security(); Boolean token = slogin.login(req.getHeader("name"), req.getHeader("password"), false, ck); if (!token) { return Response.status(Response.Status.UNAUTHORIZED).build(); } this.cookie = ck.toString(); MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { db.getCollection("staticqueryfilters"); } catch (Exception e) { //System.out.println("Mongo is down"); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } // create an empty query BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("staticqueryfilters").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); AllJson.put(sensoraux); } }); db = null; if (mongo != null) { mongo.close(); mongo = null; } return Response.status(Response.Status.OK).entity(AllJson.toString()).build(); }
From source file:eu.vital.vitalcep.restApp.vuaippi.Sensor.java
/** * Gets sensors metadata .//from w w w.j a va 2 s . c o m * * @param info * @param req * @return the metadata of the sensors * @throws java.io.FileNotFoundException */ @POST @Path("metadata") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response getSensorMetadata(String info, @Context HttpServletRequest req) throws FileNotFoundException, IOException { // StringBuilder ck = new StringBuilder(); // Security slogin = new Security(); // // Boolean token = slogin.login(req.getHeader("name") // ,req.getHeader("password"),false,ck); // if (!token){ // return Response.status(Response.Status.UNAUTHORIZED).build(); // } MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); try { JSONObject filter = new JSONObject(info); if (!filter.has("type") && !filter.has("id")) { final JSONArray sensorspool1 = new JSONArray(); // create an empty query BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("continuousfilters").find(query).projection(fields); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool1.put(sensoraux); } }); BasicDBObject query3 = new BasicDBObject(); BasicDBObject fields3 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll3 = db.getCollection("staticdatafilters").find(query3) .projection(fields3); coll3.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool1.put(sensoraux); } }); BasicDBObject query4 = new BasicDBObject(); BasicDBObject fields4 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll4 = db.getCollection("staticqueryfilters").find(query4) .projection(fields4); coll4.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool1.put(sensoraux); } }); // create an empty query BasicDBObject fieldscep = new BasicDBObject().append("_id", false).append("source", false) .append("query", false).append("complexEvent", false).append("data", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll2 = db.getCollection("cepicos").find(query).projection(fieldscep); coll2.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool1.put(sensoraux); } }); JSONObject monSensor = new JSONObject(); monSensor.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); monSensor.put("id", host + "/sensor/1"); monSensor.put("type", "vital:MonitoringSensor"); monSensor.put("name", "CEP System Monitoring Sensor"); monSensor.put("description", "A virtual sensor that monitors the operationalstate of the CEP system"); JSONObject observesItem1 = new JSONObject(); observesItem1.put("type", "vital:OperationalState"); observesItem1.put("id", host + "/sensor/1/operationalState"); JSONObject observesItem2 = new JSONObject(); observesItem2.put("type", "vital:SysUptime"); observesItem2.put("id", host + "/sensor/1/sysUptime"); JSONObject observesItem3 = new JSONObject(); observesItem3.put("type", "vital:SysLoad"); observesItem3.put("id", host + "/sensor/1/sysLoad"); JSONObject observesItem4 = new JSONObject(); observesItem4.put("type", "vital:errors"); observesItem4.put("id", host + "/sensor/1/errors"); JSONArray observes = new JSONArray(); observes.put(observesItem1); observes.put(observesItem2); observes.put(observesItem3); //observes.put(observesItem4); monSensor.put("ssn:observes", observes); monSensor.put("status", "vital:Running"); sensorspool1.put(monSensor); return Response.status(Response.Status.OK).entity(sensorspool1.toString()).build(); } else if (filter.has("type")) { JSONArray filteredSensors = new JSONArray(); final JSONArray sensorspool = new JSONArray(); JSONArray types = filter.getJSONArray("type"); for (int i = 0; i < types.length(); i++) { String type = types.getString(i); switch (type) { case CEPICOSENSOR_TYPE: case "vital:CEPSensor": case "CEPSensor": // create an empty query BasicDBObject fieldscep = new BasicDBObject().append("_id", false).append("source", false) .append("query", false).append("complexEvent", false).append("data", false) .append("event", false).append("dolceSpecification", false); BasicDBObject query = new BasicDBObject(); FindIterable<Document> coll2 = db.getCollection("cepicoss").find(query) .projection(fieldscep); coll2.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool.put(sensoraux); } }); break; case MONITORINGSENSOR_TYPE: JSONObject monSensor = new JSONObject(); monSensor.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); monSensor.put("id", host + "/sensor/1"); monSensor.put("type", "vital:MonitoringSensor"); monSensor.put("name", "CEP System Monitoring Sensor"); monSensor.put("description", "A virtual sensor that monitors the operationalstate of the CEP system"); JSONObject observesItem1 = new JSONObject(); observesItem1.put("type", "vital:OperationalState"); observesItem1.put("id", host + "/sensor/1/operationalState"); JSONObject observesItem2 = new JSONObject(); observesItem2.put("type", "vital:SysUptime"); observesItem2.put("id", host + "/sensor/1/sysUptime"); JSONObject observesItem3 = new JSONObject(); observesItem3.put("type", "vital:SysLoad"); observesItem3.put("id", host + "/sensor/1/sysLoad"); JSONObject observesItem4 = new JSONObject(); observesItem4.put("type", "vital:errors"); observesItem4.put("id", host + "/sensor/1/errors"); JSONArray observes = new JSONArray(); observes.put(observesItem1); observes.put(observesItem2); observes.put(observesItem3); //observes.put(observesItem4); monSensor.put("ssn:observes", observes); monSensor.put("status", "vital:Running"); sensorspool.put(monSensor); filteredSensors.put(monSensor); break; case CEPFILTERSTATICDATASENSOR_TYPE: case "vital:CEPFilterStaticDataSensor": case "CEPFilterStaticDataSensor": BasicDBObject query3 = new BasicDBObject(); BasicDBObject fields3 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll3 = db.getCollection("staticdatafilters").find(query3) .projection(fields3); coll3.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool.put(sensoraux); } }); break; case CEPFILTERSTATICQUERYSENSOR_TYPE: case "vital:CEPFilterStaticQuerySensor": case "CEPFilterStaticQuerySensor": BasicDBObject query4 = new BasicDBObject(); BasicDBObject fields4 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll4 = db.getCollection("staticqueryfilters").find(query4) .projection(fields4); coll4.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool.put(sensoraux); } }); break; case CEPFILTERSENSOR_TYPE: case "vital:CEPFilterSensor": case "CEPFilterSensor": // create an empty query BasicDBObject query2 = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("continuousfilters").find(query2) .projection(fields); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool.put(sensoraux); } }); break; default: return Response.status(Response.Status.BAD_REQUEST).build(); } } return Response.status(Response.Status.OK).entity(sensorspool.toString()).build(); } else if (filter.has("id")) { final JSONArray sensorspool2 = new JSONArray(); JSONArray ids = filter.getJSONArray("id"); for (int i = 0; i < ids.length(); i++) { String id = ids.getString(i); // create an empty query BasicDBObject query = new BasicDBObject("id", id); BasicDBObject fields = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("filters").find(query).projection(fields); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool2.put(sensoraux); } }); BasicDBObject fields3 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll3 = db.getCollection("staticdatafilters").find(query) .projection(fields3); coll3.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool2.put(sensoraux); } }); BasicDBObject fields4 = new BasicDBObject().append("_id", false).append("query", false) .append("data", false).append("complexEvent", false).append("source", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll4 = db.getCollection("staticqueryfilters").find(query) .projection(fields4); coll4.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool2.put(sensoraux); } }); // create an empty query BasicDBObject fieldscep = new BasicDBObject().append("_id", false).append("source", false) .append("query", false).append("complexEvent", false).append("data", false) .append("event", false).append("dolceSpecification", false); FindIterable<Document> coll2 = db.getCollection("ceps").find(query).projection(fieldscep); coll2.forEach(new Block<Document>() { @Override public void apply(final Document document) { String aux = document.toJson(); JSONObject sensoraux = new JSONObject(aux); sensoraux.put("status", "vital:Running"); sensorspool2.put(sensoraux); } }); if (id.equals(host + "/sensor/1")) { JSONObject monSensor = new JSONObject(); monSensor.put("@context", "http://vital-iot.eu/contexts/sensor.jsonld"); monSensor.put("id", host + "/sensor/1"); monSensor.put("type", "vital:MonitoringSensor"); monSensor.put("name", "CEP System Monitoring Sensor"); monSensor.put("description", "A virtual sensor that monitors the operationalstate of the CEP system"); JSONObject observesItem1 = new JSONObject(); observesItem1.put("type", "vital:OperationalState"); observesItem1.put("id", host + "/sensor/1/operationalState"); JSONObject observesItem2 = new JSONObject(); observesItem2.put("type", "vital:SysUptime"); observesItem2.put("id", host + "/sensor/1/sysUptime"); JSONObject observesItem3 = new JSONObject(); observesItem3.put("type", "vital:SysLoad"); observesItem3.put("id", host + "/sensor/1/sysLoad"); JSONObject observesItem4 = new JSONObject(); observesItem4.put("type", "vital:errors"); observesItem4.put("id", host + "/sensor/1/errors"); JSONArray observes = new JSONArray(); observes.put(observesItem1); observes.put(observesItem2); observes.put(observesItem3); //observes.put(observesItem4); monSensor.put("ssn:observes", observes); monSensor.put("status", "vital:Running"); sensorspool2.put(monSensor); } } return Response.status(Response.Status.OK).entity(sensorspool2.toString()).build(); } } catch (JSONException ex) { return Response.status(Response.Status.BAD_REQUEST).build(); } finally { db = null; if (mongo != null) { mongo.close(); mongo = null; } } return Response.status(Response.Status.BAD_REQUEST).build(); }