List of usage examples for com.mongodb BasicDBObject BasicDBObject
public BasicDBObject()
From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java
License:Apache License
private <T> DBObjectId insert(UpdatableSchema schema) throws T2DBException { com.mongodb.BasicDBObject bo = new BasicDBObject(); Surrogate s = schema.getSurrogate(); if (!s.inConstruction()) bo.put(MongoDatabase.FLD_ID, getId(schema)); bo.put(MongoDatabase.FLD_SCHEMA_NAME, schema.getName()); bo.put(MongoDatabase.FLD_SCHEMA_BASE, getIdOrZero(schema.getBase())); bo.put(MongoDatabase.FLD_SCHEMA_ATTRIBS, attributeDefinitions(schema.getAttributeDefinitions())); bo.put(MongoDatabase.FLD_SCHEMA_SERIES, seriesDefinitions(schema.getSeriesDefinitions())); getMongoDB(s).getSchemas().insert(bo); ObjectId ox = getObjectId(bo);/* www. j a v a 2 s. c o m*/ return new MongoDBObjectId(ox); }
From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java
License:Apache License
private BasicDBObject attributeDefinition(AttributeDefinition<?> def) throws T2DBException { com.mongodb.BasicDBObject bo = new BasicDBObject(); bo.put(MongoDatabase.FLD_ATTRIBDEF_NUM, def.getNumber()); bo.put(MongoDatabase.FLD_ATTRIBDEF_ERASING, def.isErasing()); bo.put(MongoDatabase.FLD_ATTRIBDEF_PROP, def.isErasing() ? null : getId(def.getProperty())); bo.put(MongoDatabase.FLD_ATTRIBDEF_VAL, def.isErasing() ? null : def.getProperty().getValueType().toString(def.getValue())); return bo;/* ww w .j a v a2s . co m*/ }
From source file:ch.agent.crnickl.mongodb.WriteMethodsForSchema.java
License:Apache License
private BasicDBObject seriesDefinition(SeriesDefinition def) throws T2DBException { com.mongodb.BasicDBObject bo = new BasicDBObject(); bo.put(MongoDatabase.FLD_SERIESDEF_NUM, def.getNumber()); bo.put(MongoDatabase.FLD_SERIESDEF_ERASING, def.isErasing()); bo.put(MongoDatabase.FLD_SERIESDEF_DESC, def.isErasing() ? null : def.getDescription()); bo.put(MongoDatabase.FLD_SERIESDEF_ATTRIBS, def.isErasing() ? attributeDefinitions(null) : attributeDefinitions(def.getAttributeDefinitions())); return bo;//from w w w. ja v a 2s.co m }
From source file:ch.agent.crnickl.mongodb.WriteMethodsForValueType.java
License:Apache License
private <T> DBObjectId insert(ValueType<T> vt) throws T2DBException { Surrogate s = vt.getSurrogate();//from w w w. j a v a 2 s . c o m com.mongodb.BasicDBObject bo = new BasicDBObject(); if (!s.inConstruction()) bo.put(MongoDatabase.FLD_ID, getId(vt)); bo.put(MongoDatabase.FLD_VT_NAME, vt.getName()); bo.put(MongoDatabase.FLD_VT_TYPE, vt.getExternalRepresentation()); if (vt.isRestricted()) bo.put(MongoDatabase.FLD_VT_VALUES, valueDescriptionsAsMap(vt)); getMongoDB(s).getValueTypes().insert(bo); ObjectId ox = getObjectId(bo); return new MongoDBObjectId(ox); }
From source file:ch.bfh.uniboard.persistence.mongodb.PersistedPost.java
License:GNU General Public License
/** * Method allowing to convert the current PersistedPost to the format supported by the database * @return a DBObject format of the PersistedPost *//*from www . ja va 2s.c o m*/ public BasicDBObject toDBObject() { BasicDBObject doc = new BasicDBObject(); //Save raw message doc.put("message", Base64.encode(message)); //Check if message is a JSON message DBObject jsonMessageContent = null; try { jsonMessageContent = (DBObject) JSON.parse(new String(message, "UTF-8")); } catch (JSONParseException | UnsupportedEncodingException ex) { Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Message is not a JSON string {0}", ex.getMessage()); } if (jsonMessageContent != null) { //save message as JSON content DBObject jsonMessage = new BasicDBObject("searchable-message", jsonMessageContent); doc.putAll(jsonMessage); } //Prepares the Alpha attributes BasicDBObject alphaList = new BasicDBObject(); for (Entry<String, Value> entry : alpha.getEntries()) { alphaList.put(entry.getKey(), entry.getValue().getValue()); } doc.put("alpha", alphaList); //Prepares the Beta attributes BasicDBObject betaList = new BasicDBObject(); for (Entry<String, Value> entry : beta.getEntries()) { betaList.put(entry.getKey(), entry.getValue().getValue()); } doc.put("beta", betaList); return doc; }
From source file:ch.bfh.uniboard.persistence.mongodb.PersistenceService.java
License:GNU General Public License
@Override public ResultContainer get(Query query) { try {//from w ww .j a v a 2 s . co m //Check Database connection if (!this.connectionManager.isConnected()) { Attributes gamma = new Attributes(); gamma.add(Attributes.ERROR, new StringValue("Internal Server Error. Service not available")); logger.log(Level.WARNING, "Database error: unable to connect to database"); return new ResultContainer(new ArrayList<Post>(), gamma); } //TODO Remove this block and its tests //check basic wellformedness of query if (query == null || query.getConstraints() == null || query.getConstraints().isEmpty() || query.getConstraints().contains(null)) { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: Incomplete query")); logger.log(Level.WARNING, "Syntax error: Incomplete query"); return new ResultContainer(new ArrayList<Post>(), gamma); } List<DBObject> constraintsList = new ArrayList<>(); //iterates over the constraints and constructs the corresponding query string for (Constraint c : query.getConstraints()) { //constructs the key String keyString = ""; if (c.getIdentifier() instanceof MessageIdentifier) { //TODO constraint that wants to compare the raw byte[] keyString += "searchable-message"; } else if (c.getIdentifier() instanceof AlphaIdentifier) { keyString += "alpha"; } else if (c.getIdentifier() instanceof BetaIdentifier) { keyString += "beta"; } else { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: Unknown identifier")); logger.log(Level.WARNING, "Syntax error: Unknown identifier"); return new ResultContainer(new ArrayList<Post>(), gamma); } //constructs the hierarchy of the keys for (String key : c.getIdentifier().getParts()) { keyString += "." + key; } //constructs the researched value string by checking the type of constraints and getting the searched values DBObject actualConstraint = new BasicDBObject(); if (c instanceof Equal) { Equal op = (Equal) c; actualConstraint.put(keyString, op.getValue().getValue()); } else if (c instanceof NotEqual) { NotEqual op = (NotEqual) c; actualConstraint.put(keyString, new BasicDBObject("$ne", op.getValue().getValue())); } else if (c instanceof In) { In op = (In) c; List<Object> values = new ArrayList<>(); Class valueClass = op.getSet().get(0).getClass(); for (Value v : op.getSet()) { if (!(v.getClass().equals(valueClass))) { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: not same value type for IN constraint")); logger.log(Level.WARNING, "Syntax error: not same value type for IN constraint"); return new ResultContainer(new ArrayList<Post>(), gamma); } values.add(v.getValue()); } actualConstraint.put(keyString, new BasicDBObject("$in", values)); } else if (c instanceof Between) { Between op = (Between) c; if (!(op.getStart().getClass().equals(op.getEnd().getClass()))) { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: not same value type for BETWEEN constraint")); logger.log(Level.WARNING, "Syntax error: not same value type for BETWEEN constraint"); return new ResultContainer(new ArrayList<Post>(), gamma); } actualConstraint.put(keyString, new BasicDBObject("$gt", op.getStart().getValue()).append("$lt", op.getEnd().getValue())); } else if (c instanceof Greater) { Greater op = (Greater) c; actualConstraint.put(keyString, new BasicDBObject("$gt", op.getValue().getValue())); } else if (c instanceof GreaterEqual) { GreaterEqual op = (GreaterEqual) c; actualConstraint.put(keyString, new BasicDBObject("$gte", op.getValue().getValue())); } else if (c instanceof Less) { Less op = (Less) c; actualConstraint.put(keyString, new BasicDBObject("$lt", op.getValue().getValue())); } else if (c instanceof LessEqual) { LessEqual op = (LessEqual) c; actualConstraint.put(keyString, new BasicDBObject("$lte", op.getValue().getValue())); } else { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: Unknown type of constraint")); logger.log(Level.WARNING, "Syntax error: Unknown type of constraint"); return new ResultContainer(new ArrayList<Post>(), gamma); } constraintsList.add(actualConstraint); } //combine the different constrainst in an AND query DBObject completeQuery = new BasicDBObject(); completeQuery.put("$and", constraintsList); DBCursor cursor; if (query.getOrder().size() > 0) { //Create orderBy BasicDBObject orderBy = new BasicDBObject(); for (Order order : query.getOrder()) { String identifier; if (order.getIdentifier() instanceof MessageIdentifier) { identifier = "message"; } else if (order.getIdentifier() instanceof AlphaIdentifier) { identifier = "alpha"; } else if (order.getIdentifier() instanceof BetaIdentifier) { identifier = "beta"; } else { Attributes gamma = new Attributes(); gamma.add(Attributes.REJECTED, new StringValue("Syntax error: Unknown identifier")); logger.log(Level.WARNING, "Syntax error: Unknown identifier"); return new ResultContainer(new ArrayList<Post>(), gamma); } for (String key : order.getIdentifier().getParts()) { identifier += "." + key; } int ascDesc; if (order.isAscDesc()) { ascDesc = 1; } else { ascDesc = -1; } orderBy.append(identifier, ascDesc); } cursor = this.connectionManager.getCollection().find(completeQuery).sort(orderBy) .limit(query.getLimit()); } else { //apply query on database cursor = this.connectionManager.getCollection().find(completeQuery).limit(query.getLimit()); } //creates the result container with the db result List<Post> list = new ArrayList<>(); while (cursor.hasNext()) { DBObject object = cursor.next(); //convert to PersistedPost list.add(PersistedPost.fromDBObject(object)); } return new ResultContainer(list, new Attributes()); } catch (Exception e) { Attributes gamma = new Attributes(); gamma.add(Attributes.ERROR, new StringValue("General error: " + e.getMessage())); logger.log(Level.WARNING, "General Get error", e); return new ResultContainer(new ArrayList<Post>(), gamma); } }
From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java
License:Open Source License
/** * {@inheritDoc}//w ww. jav a 2 s . com */ @Override protected BasicDBObject toMongoDocument(IAccessEvent event) { final BasicDBObject doc = new BasicDBObject(); doc.append("timeStamp", new Date(event.getTimeStamp())); if (serverName) doc.append("serverName", event.getServerName()); addRemote(doc, event); addRequest(doc, event); addResponse(doc, event); return doc; }
From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java
License:Open Source License
private void addRemote(BasicDBObject parent, IAccessEvent event) { final BasicDBObject remote = new BasicDBObject(); final String host = event.getRemoteHost(); if (remoteHost && host != null) remote.append("host", host); final String remoteUserName = event.getRemoteUser(); if (remoteUser && remoteUserName != null && !remoteUserName.equals("-")) { remote.append("user", remoteUserName); }//from w w w . ja v a 2s.com final String addr = event.getRemoteAddr(); if (remoteAddr && addr != null) remote.append("addr", addr); if (!remote.isEmpty()) parent.put("remote", remote); }
From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java
License:Open Source License
private void addRequest(BasicDBObject parent, IAccessEvent event) { final BasicDBObject request = new BasicDBObject(); final String uri = event.getRequestURI(); if (requestUri && uri != null && !uri.equals("-")) { request.append("uri", uri); }/* ww w . ja v a 2s . c om*/ final String protocol = event.getProtocol(); if (requestProtocol && protocol != null) request.append("protocol", protocol); final String method = event.getMethod(); if (requestMethod && method != null) request.append("method", method); final String requestContent = event.getRequestContent(); if (requestPostContent && requestContent != null && !requestContent.equals("")) { request.append("postContent", requestContent); } final String jSessionId = event.getCookie("JSESSIONID"); if (requestSessionId && jSessionId != null && !jSessionId.equals("-")) request.append("sessionId", jSessionId); final String userAgent = event.getRequestHeader("User-Agent"); if (requestUserAgent && userAgent != null && !userAgent.equals("-")) request.append("userAgent", userAgent); final String referer = event.getRequestHeader("Referer"); if (requestReferer && referer != null && !referer.equals("-")) request.append("referer", referer); if (!request.isEmpty()) parent.put("request", request); }
From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java
License:Open Source License
private void addResponse(BasicDBObject doc, IAccessEvent event) { final BasicDBObject response = new BasicDBObject(); if (responseContentLength) response.append("contentLength", event.getContentLength()); if (responseStatusCode) response.append("statusCode", event.getStatusCode()); if (!response.isEmpty()) doc.append("response", response); }