List of usage examples for com.mongodb DBObject toMap
Map toMap();
From source file:com.guestful.bus.MongoScheduler.java
License:Apache License
private Map next() { long now = System.currentTimeMillis(); DBObject object = collection.findAndModify( new BasicDBObject().append("scheduledTime", new BasicDBObject("$lte", new Date(now))).append("$or", Arrays.asList(new BasicDBObject("status", "PENDING"), new BasicDBObject("status", "LOADING") .append("updatedDate", new BasicDBObject("$lte", new Date(now - MINS_5))))), null, null, false,//from w w w . j a v a2 s . c o m new BasicDBObject("$set", new BasicDBObject().append("updatedDate", new Date(now)).append("status", "LOADING")), true, false); return object == null ? null : object.toMap(); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
@Override public AssetList retrieveAllAssets() { List<Map<String, Object>> mapList = new ArrayList<>(); try (DBCursor cursor = getAssetCollection().find()) { if (logger.isLoggable(Level.FINE)) { logger.fine("retrieveAllAssets: found " + cursor.count() + " assets."); }/*w w w. ja v a2 s. c o m*/ for (DBObject obj : cursor) { convertObjectIdToHexString(obj); // BSON spec says that all keys have to be strings // so this should be safe. @SuppressWarnings("unchecked") Map<String, Object> assetMap = obj.toMap(); mapList.add(assetMap); } } return AssetList.createAssetListFromMaps(mapList); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
/** {@inheritDoc} */ @Override/*from w ww. j a v a 2 s. c o m*/ public AssetList retrieveAllAssets(Map<String, List<Condition>> filters, String searchTerm, PaginationOptions pagination, SortOptions sortOptions) { if (filters.size() == 0 && searchTerm == null && pagination == null && sortOptions == null) { return retrieveAllAssets(); } BasicDBObject filterObject = createFilterObject(filters, searchTerm); DBObject sortObject = null; DBObject projectionObject = null; boolean textScoreAdded = false; if (sortOptions != null) { // If sort options are provided, use them to sort the results int sortOrder = getMongoSortOrder(sortOptions.getSortOrder()); sortObject = new BasicDBObject(sortOptions.getField(), sortOrder); } else { // If no sort options are provided but there is a search term, sort on relevance to the search term if (searchTerm != null) { sortObject = new BasicDBObject("score", new BasicDBObject("$meta", "textScore")); projectionObject = sortObject; textScoreAdded = true; } } List<DBObject> results = query(filterObject, sortObject, projectionObject, pagination); List<Map<String, Object>> assets = new ArrayList<Map<String, Object>>(); for (DBObject result : results) { // BSON spec says that all keys have to be strings // so this should be safe. @SuppressWarnings("unchecked") Map<String, Object> resultMap = result.toMap(); if (textScoreAdded) { resultMap.remove("score"); } assets.add(resultMap); } return AssetList.createAssetListFromMaps(assets); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
/** * Retrieve a single asset by its id./* w w w.ja va2 s. c om*/ * * @return The requested asset * @throws NonExistentArtefactException if the asset doesn't exist */ private Asset retrieveAsset(ObjectId assetId) throws NonExistentArtefactException { BasicDBObject query = new BasicDBObject(ID, assetId); DBObject resultObj = getAssetCollection().findOne(query); if (resultObj == null) { throw new NonExistentArtefactException(assetId.toString(), "asset"); } convertObjectIdToHexString(resultObj); // All entries in a Mongo document have string keys, this is part of // the BSON spec, so this should be safe. Not very nice though. @SuppressWarnings("unchecked") Map<String, Object> asset = resultObj.toMap(); return Asset.createAssetFromMap(asset); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
@Override public Attachment retrieveAttachmentMetadata(String attachmentId) throws NonExistentArtefactException { BasicDBObject query = new BasicDBObject(ID, new ObjectId(attachmentId)); DBObject resultObj = getAttachmentCollection().findOne(query); if (resultObj == null) { throw new NonExistentArtefactException(attachmentId, "attachment"); }//from ww w. j a v a 2s . c o m convertObjectIdToHexString(resultObj); // All entries in a Mongo document have string keys, this is part of // the BSON spec, so this should be safe. Not very nice though. @SuppressWarnings("unchecked") Map<String, Object> map = resultObj.toMap(); return Attachment.createAttachmentFromMap(map); }
From source file:com.ibm.ws.lars.rest.PersistenceBean.java
License:Apache License
@Override public AttachmentList findAttachmentsForAsset(String assetId) { BasicDBObject query = new BasicDBObject("assetId", assetId); ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); try (DBCursor cursor = getAttachmentCollection().find(query)) { if (logger.isLoggable(Level.FINE)) { logger.fine(/* www. ja v a 2 s . c om*/ "findAttachmentsForAsset: found " + cursor.count() + " attachments for asset " + assetId); } for (DBObject attachment : cursor) { convertObjectIdToHexString(attachment); @SuppressWarnings("unchecked") Map<String, Object> oneResult = attachment.toMap(); results.add(oneResult); } } return AttachmentList.createAttachmentListFromMaps(results); }
From source file:com.icoin.trading.webui.init.SystemController.java
License:Apache License
@RequestMapping(value = "/collection/{id}", method = RequestMethod.GET) public String collection(@PathVariable("id") String collectionName, @RequestParam(value = "page", defaultValue = "1") int pageNumber, @RequestParam(value = "itemsperpage", defaultValue = "5") int itemsPerPage, Model model) { DBCursor dbCursor = springTemplate.getCollection(collectionName).find(); List<DBObject> dbObjects = dbCursor.skip((pageNumber - 1) * itemsPerPage).limit(itemsPerPage).toArray(); List<Map> items = new ArrayList<Map>(dbCursor.length()); for (DBObject dbObject : dbObjects) { items.add(dbObject.toMap()); }//from w ww . j av a2 s . c om model.addAttribute("items", items); int totalItems = dbCursor.count(); int numPages = ((int) Math.floor(totalItems / itemsPerPage)) + 1; model.addAttribute("numPages", numPages); model.addAttribute("page", pageNumber); model.addAttribute("itemsPerPage", itemsPerPage); model.addAttribute("collectionName", collectionName); return "data/collection"; }
From source file:com.ikanow.infinit.e.api.knowledge.aliases.AliasLookupTable.java
License:Open Source License
private void populateAliasTableFromShare(SharePojo share) { String json = share.getShare(); if (null != json) { try {// w ww . j a v a2 s .co m DBObject dbo = (DBObject) JSON.parse(json); if (null != dbo) { for (Object entryObj : dbo.toMap().entrySet()) { @SuppressWarnings("unchecked") Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryObj; String masterAlias = entry.getKey(); EntityPojo masterAliasEntity = new EntityPojo(); masterAliasEntity.setIndex(masterAlias); BasicDBObject entityFeatureObj = (BasicDBObject) entry.getValue(); EntityFeaturePojo aliasInfo = null; try { aliasInfo = EntityFeaturePojo.fromDb(entityFeatureObj, EntityFeaturePojo.class); } catch (Exception e) { logger.debug("Failed to deserialize aliasInfo", e); } if (masterAlias.endsWith("/")) { // it's a regex try { Pattern key = Pattern.compile(masterAlias.substring(0, masterAlias.length() - 1)); if (null == _entityRegexes) { _entityRegexes = new HashMap<Pattern, EntityFeaturePojo>(); } _entityRegexes.put(key, aliasInfo); } catch (Exception e) { } // ignore regex compile fails continue; } if ((null != aliasInfo) && (null != aliasInfo.getAlias())) { aliasInfo.setIndex(masterAlias); if ((null == aliasInfo.getDimension()) && (null != aliasInfo.getType())) { aliasInfo.setDimension(DimensionUtility.getDimensionByType(aliasInfo.getType())); } //TESTED logger.debug("aliasTable entry: " + aliasInfo.getIndex() + " vs " + Arrays.toString(aliasInfo.getAlias().toArray())); // This is going to collide in an ugly fashion across multiple communities, // we just have to live with that for (String aliasIndex : aliasInfo.getAlias()) { _aliasTable.put(aliasIndex, aliasInfo); } _aliasTable.put(aliasInfo.getIndex(), aliasInfo); EntityFeaturePojo currAlias = _reverseAliasTable.get(aliasInfo.getIndex()); if (null == currAlias) { _reverseAliasTable.put(aliasInfo.getIndex(), aliasInfo); } else { // Collision ... this we can handle a little-bit more elegantly currAlias.getAlias().addAll(aliasInfo.getAlias()); _reverseAliasTable.put(aliasInfo.getIndex(), currAlias); } } } } } catch (Exception e) { logger.debug("General Aliasing Error: ", e); } // not Json, just carry on... } }
From source file:com.ikanow.infinit.e.data_model.driver.InfiniteDriver.java
License:Apache License
private void populateAliasTableFromShare(SharePojo share, HashMap<String, EntityFeaturePojo> masterAliases, Map<String, List<SharePojo>> aliasMapping) { String json = share.getShare(); if (null != json) { try {// w w w. ja va 2 s.c om DBObject dbo = (DBObject) JSON.parse(json); if (null != dbo) { for (Object entryObj : dbo.toMap().entrySet()) { @SuppressWarnings("unchecked") Map.Entry<String, Object> entry = (Map.Entry<String, Object>) entryObj; String masterAlias = entry.getKey(); BasicDBObject entityFeatureObj = (BasicDBObject) entry.getValue(); EntityFeaturePojo aliasInfo = null; try { aliasInfo = EntityFeaturePojo.fromDb(entityFeatureObj, EntityFeaturePojo.class); } catch (Exception e) { continue; } if (null == aliasInfo.getIndex()) { aliasInfo.setIndex(masterAlias); } if (null != aliasInfo) { // (allow getAlias to be null/empty) //(overwrite duplicates) masterAliases.put(aliasInfo.getIndex(), aliasInfo); if (null != aliasMapping) { List<SharePojo> shareList = aliasMapping.get(aliasInfo.getIndex()); if (null == shareList) { shareList = new LinkedList<SharePojo>(); aliasMapping.put(aliasInfo.getIndex(), shareList); } shareList.add(share); } } } } } catch (Exception e) { } // not Json, just carry on... } }
From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java
License:Apache License
@SuppressWarnings("unchecked") public static Record toRecord(DBObject dbObject) { Record record = new Record(); record.setColumns(dbObject.toMap()); return record; }