List of usage examples for com.mongodb BasicDBList get
public Object get(final String key)
From source file:org.springframework.data.mongodb.core.convert.GeoConverters.java
License:Apache License
/** * Converts a coordinate pairs nested in in {@link BasicDBList} into {@link GeoJsonPolygon}. * //w w w . j a va 2 s . c o m * @param dbList * @return * @since 1.7 */ static GeoJsonPolygon toGeoJsonPolygon(BasicDBList dbList) { return new GeoJsonPolygon(toListOfPoint((BasicDBList) dbList.get(0))); }
From source file:org.springframework.data.mongodb.core.DBObjectTestUtils.java
License:Apache License
/** * Expects the list element with the given index to be a non-{@literal null} {@link DBObject} and returns it. * //w w w. j a v a 2 s. c om * @param source the {@link BasicDBList} to look up the {@link DBObject} element in * @param index the index of the element expected to contain a {@link DBObject} * @return */ public static DBObject getAsDBObject(BasicDBList source, int index) { assertThat(source.size(), greaterThanOrEqualTo(index + 1)); Object value = source.get(index); assertThat(value, is(instanceOf(DBObject.class))); return (DBObject) value; }
From source file:org.teiid.translator.mongodb.MongoDBExecutionFactory.java
License:Open Source License
/** * @param field/*w ww. jav a2 s. c om*/ * @param expectedClass * @return * @throws TranslatorException */ public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException { if (value == null) { return null; } if (value.getClass().equals(expectedClass)) { return value; } if (value instanceof DBRef) { Object obj = ((DBRef) value).getId(); if (obj instanceof BasicDBObject) { BasicDBObject bdb = (BasicDBObject) obj; return bdb.get(colName); } return obj; } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) { return new java.sql.Date(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) { return new java.sql.Timestamp(((java.util.Date) value).getTime()); } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) { return new java.sql.Time(((java.util.Date) value).getTime()); } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) { return new BigDecimal((String) value); } else if (value instanceof String && expectedClass.equals(BigInteger.class)) { return new BigInteger((String) value); } else if (value instanceof String && expectedClass.equals(Character.class)) { return new Character(((String) value).charAt(0)); } else if (value instanceof String && expectedClass.equals(BinaryType.class)) { return new BinaryType(((String) value).getBytes()); } else if (value instanceof String && expectedClass.equals(Blob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof String && expectedClass.equals(Clob.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new ClobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }, -1); } else if (value instanceof String && expectedClass.equals(SQLXML.class)) { GridFS gfs = new GridFS(mongoDB, fqn); final GridFSDBFile resource = gfs.findOne((String) value); if (resource == null) { return null; } return new SQLXMLImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return resource.getInputStream(); } }); } else if (value instanceof BasicDBList) { BasicDBList arrayValues = (BasicDBList) value; //array if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) { Class arrayType = expectedClass.getComponentType(); Object array = Array.newInstance(arrayType, arrayValues.size()); for (int i = 0; i < arrayValues.size(); i++) { Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName); Array.set(array, i, arrayItem); } value = array; } } else if (value instanceof org.bson.types.ObjectId) { org.bson.types.ObjectId id = (org.bson.types.ObjectId) value; value = id.toStringBabble(); } else { Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass); if (transform != null) { try { value = transform.transform(value, expectedClass); } catch (TransformationException e) { throw new TranslatorException(e); } } } return value; }
From source file:org.teiid.translator.mongodb.MongoDBUpdateExecution.java
License:Open Source License
private void buildUpdate(MongoDocument doc, DBCollection collection, BasicDBObject row, List<String> parentKeys, int level, RowInfo rowInfo, List<WriteResult> executionResults, UpdateOperation operation) throws TranslatorException { String parentKeyName = parentKeys.get(level); boolean top = parentKeyName.equals(doc.getTargetDocument().getQualifiedName(false)); Object parentBlock = row.get(top ? "_id" : parentKeyName); //$NON-NLS-1$ // the parent-child must have been one-2-one relationship if (parentBlock == null) { parentBlock = rowInfo.PK;/*from ww w. j a v a 2 s .co m*/ } String mergeTableName = doc.getTable().getName(); if (parentKeys.size() != (level + 1)) { mergeTableName = parentKeys.get(level + 1); } if (parentBlock instanceof BasicDBList) { // so parent is an array document BasicDBList parentRows = (BasicDBList) parentBlock; //parentRows = (BasicDBList)((BasicDBObject)parentRows.get(0)).get("_id"); //$NON-NLS-1$ for (int i = 0; i < parentRows.size(); i++) { RowInfo info = RowInfo.build(parentKeyName, mergeTableName, parentRows.get(i), i, rowInfo); if (parentKeys.size() == (level + 1)) { String aliasDocumentName = doc.getQualifiedName(false).replace('.', '_'); BasicDBList dataRows = (BasicDBList) row.get(aliasDocumentName); if (dataRows != null && dataRows.size() > i) { operation.execute(doc, collection, row, (DBObject) dataRows.get(i), info, executionResults); } } else { buildUpdate(doc, collection, row, parentKeys, level + 1, info, executionResults, operation); } } } else { // here the _id is same as parent RowInfo info = RowInfo.build(parentKeyName, mergeTableName, parentBlock, -1, rowInfo); if (parentKeys.size() == (level + 1)) { //Leaf, no more down String aliasDocumentName = doc.getQualifiedName(false).replace('.', '_'); DBObject dataRows = (DBObject) row.get(aliasDocumentName); if (dataRows != null) { operation.execute(doc, collection, row, dataRows, info, executionResults); } } else { buildUpdate(doc, collection, row, parentKeys, level + 1, info, executionResults, operation); } } }
From source file:org.teiid.translator.mongodb.MongoDBUpdateVisitor.java
License:Open Source License
public boolean updateMerge(BasicDBList previousRows, RowInfo parentKey, BasicDBList updated) throws TranslatorException { boolean update = false; for (int i = 0; i < previousRows.size(); i++) { BasicDBObject row = (BasicDBObject) previousRows.get(i); if (this.match == null && getPullQuery() == null || ExpressionEvaluator.matches(this.executionFactory, this.mongoDB, this.condition, row, parentKey)) { update = true;/*from ww w. jav a 2s . co m*/ for (String key : this.columnValues.keySet()) { Object obj = this.columnValues.get(key); if (obj instanceof MergeDetails) { MergeDetails ref = ((MergeDetails) obj); row.put(key, ref.getValue()); } else { row.put(key, obj); } } } updated.add(row); } return update; }
From source file:org.teiid.translator.mongodb.MongoDBUpdateVisitor.java
License:Open Source License
public boolean updateDelete(BasicDBList previousRows, RowInfo parentKey, BasicDBList updated) throws TranslatorException { for (int i = 0; i < previousRows.size(); i++) { BasicDBObject row = (BasicDBObject) previousRows.get(i); if (this.match == null && getPullQuery() == null || ExpressionEvaluator.matches(this.executionFactory, this.mongoDB, this.condition, row, parentKey)) { //do not add } else {/*from www .j a v a 2s . co m*/ updated.add(row); } } return updated.size() != previousRows.size(); }
From source file:org.tinygroup.mongodb.db.MongodbPersistence.java
License:GNU General Public License
/** * // ww w .ja va2s .c om groupbynull?? * * @param select * @param where * @param group * @param having * @param sort * @return */ public BSONObject[] find(DBObject select, DBObject where, DBObject group, DBObject having, DBObject sort) { if (group == null) { return find(select, where); } else { BasicDBList dbList = aggregate(select, where, group, having, sort); if (dbList != null && dbList.size() > 0) { BSONObject[] objects = new BSONObject[dbList.size()]; objects = new BSONObject[dbList.size()]; for (int i = 0; i < dbList.size(); i++) { BSONObject bson = (BSONObject) dbList.get(i); BSONObject _id = (BSONObject) bson.get("_id"); bson.removeField("_id"); bson.putAll(_id); objects[i] = bson; } return objects; } else { return new BSONObject[0]; } } }
From source file:org.tinygroup.mongodb.db.MongodbPersistence.java
License:GNU General Public License
/** * ?/*from w w w. j a va 2 s . co m*/ * * @param dbList * ? * @param objects * */ private void fillObjects(BasicDBList source, BSONObject[] tageter) { if (source != null && source.size() > 0) { tageter = new BSONObject[source.size()]; for (int i = 0; i < source.size(); i++) { BSONObject bson = (BSONObject) source.get(i); BSONObject _id = (BSONObject) bson.get("_id"); bson.removeField("_id"); bson.putAll(_id); tageter[i] = bson; } } }
From source file:org.tinygroup.mongodb.engine.view.MongoViewContext.java
License:GNU General Public License
/** * //from ww w . j a v a 2s. c om * * * @return */ public PageInfo nestedArrayQuery() { PageInfo pageInfo = new PageInfo(); int pageSize = checkPageSize(); String pageNumStr = context.get(ParameterBuilder.PAGE_NUMBER); int pageNum = 1; if (pageNumStr != null) { pageNum = Integer.parseInt(pageNumStr); } int size = 0; if (view != null) { DBObject conditionObject = generateConditionObject(); DBObject havingObject = generateHavingConditionObject(); DBObject orderObject = generateOrderObject(); DBObject displayObject = generateNestedDisplayObject(); BSONObject[] records = persistence.find(displayObject, conditionObject, groupObject, havingObject, orderObject); if (records != null && records.length == 1) { BSONObject record = records[0]; filterArrayRecords(record);// ?? Object object = record.get(nestedObject); if (object != null) { if (object instanceof BasicDBList) { BasicDBList nestedObjects = (BasicDBList) object; size = nestedObjects.size(); pageInfo.pageAttributeSet(pageSize, pageNum, size); int start = pageInfo.getStart(); int length = start + pageInfo.getPageSize(); int pageLength = pageInfo.getPageSize(); ; if (size < length) { pageLength = size - start; } BSONObject[] pageObjects = new BSONObject[pageLength]; for (int i = start, j = 0; j < pageLength; i++, j++) { BSONObject bsonObject = (BSONObject) nestedObjects.get(i); pageObjects[j] = bsonObject; } pageInfo.setObjects(pageObjects); return pageInfo; } } } else { pageInfo.pageAttributeSet(pageSize, pageNum, size); pageInfo.setObjects(new BSONObject[0]); } } return pageInfo; }
From source file:org.tymoonnext.bot.module.athenaeum.AthenaeumSource.java
License:GNU General Public License
@Override public ResultSet get(String volume, int from, int to, String user) throws SourceException, InexistentVolumeException { try {/*w w w. j a v a2 s. c om*/ DataModel mod = resolveModel(volume); if (mod == null) throw new InexistentVolumeException("No results for '" + volume + "'."); BasicDBList pages = mod.get("pages"); if (to > pages.size()) to = pages.size(); if (to == -1) to = pages.size(); if (from > to) from = to; Result[] res = new Result[to - from]; for (int i = 0; i < res.length; i++) { res[i] = new Result(pages.get(i + from).toString()); } return new ResultSet(res, pages.size()); } catch (MongoException ex) { Commons.log.log(Level.WARNING, "Failed to retrieve record from the athenaeum collection!", ex); throw new SourceException("Error in MongoDB", ex); } }