/** Build the Map record from the Mongo DBObject. */
 public MongoRecord buildRecordFromDBObject(DBObject object) {
   MongoRecord record = new MongoRecord();
   for (Iterator iterator = object.toMap().entrySet().iterator(); iterator.hasNext(); ) {
     Map.Entry entry = (Map.Entry) iterator.next();
     if (entry.getValue() instanceof BasicDBList) {
       List values = new ArrayList();
       for (Iterator valuesIterator = ((BasicDBList) entry.getValue()).iterator();
           valuesIterator.hasNext(); ) {
         Object value = valuesIterator.next();
         if (value instanceof DBObject) {
           values.add(buildRecordFromDBObject((DBObject) value));
         } else {
           values.add(value);
         }
       }
       record.put((String) entry.getKey(), values);
     } else if (entry.getValue() instanceof DBObject) {
       MongoRecord nestedRecord = buildRecordFromDBObject((DBObject) entry.getValue());
       record.put((String) entry.getKey(), nestedRecord);
     } else {
       record.put((String) entry.getKey(), entry.getValue());
     }
   }
   return record;
 }
 public String parseJSONFind(String workspaceID, String jsonString) {
   StringBuffer ret = new StringBuffer();
   ret.append("{\n");
   System.out.println(workspaceID);
   System.out.println(jsonString);
   int counter = 0;
   DB db = m.getDB(Tokens.WORKSPACE_DATABASE);
   DBCollection coll = db.getCollection(workspaceID);
   BasicDBObject query =
       (BasicDBObject) JSON.parse(jsonString); // FixStrings.usr2mongo(jsonString)
   System.out.println("query: " + query.toString());
   DBCursor find = coll.find(query);
   Iterator<DBObject> iter = find.iterator();
   while (iter.hasNext()) {
     counter++;
     if (counter > 1) ret.append(",\n");
     DBObject next = iter.next();
     Map toMap = next.toMap();
     ret.append("\"" + toMap.get("_id").toString() + "\" : ");
     // remove the redundant id
     next.removeField("_id");
     // ret+= "\"kbid" + counter + "\" : ";
     String rec = FixStrings.mongo2usr(next.toString());
     ret.append(rec);
   }
   ret.append("\n}\n");
   // System.out.println(workspaceID);
   return ret.toString();
 }
  @Override
  public User load(final String username) {
    LOG.debug("Loading user {}", username);

    // special case for the locally defined user, we don't store that in MongoDB.
    if (configuration.getRootUsername().equals(username)) {
      LOG.debug("User {} is the built-in admin user", username);
      return new UserImpl.LocalAdminUser(configuration, roleService.getAdminRoleObjectId());
    }

    final DBObject query = new BasicDBObject();
    query.put(UserImpl.USERNAME, username);

    final List<DBObject> result = query(UserImpl.class, query);
    if (result == null || result.isEmpty()) {
      return null;
    }

    if (result.size() > 1) {
      final String msg =
          "There was more than one matching user for username "
              + username
              + ". This should never happen.";
      LOG.error(msg);
      throw new RuntimeException(msg);
    }

    final DBObject userObject = result.get(0);
    final Object userId = userObject.get("_id");

    LOG.debug("Loaded user {}/{} from MongoDB", username, userId);
    return new UserImpl((ObjectId) userId, userObject.toMap());
  }
  @Override
  public IndexRange get(String index) throws NotFoundException {
    DBObject dbo = findOne(IndexRangeImpl.class, new BasicDBObject("index", index));

    if (dbo == null) throw new NotFoundException("Index " + index + " not found.");

    return new IndexRangeImpl((ObjectId) dbo.get("_id"), dbo.toMap());
  }
 @Override
 @SuppressWarnings("unchecked")
 public Map<String, Object> extractEntityTuple(Session session, EntityKey key) {
   MongoDBDatastoreProvider provider = MongoDBTestHelper.getProvider(session.getSessionFactory());
   DBObject finder = new BasicDBObject(MongoDBDialect.ID_FIELDNAME, key.getColumnValues()[0]);
   DBObject result = provider.getDatabase().getCollection(key.getTable()).findOne(finder);
   replaceIdentifierColumnName(result, key);
   return result.toMap();
 }
示例#6
0
 @Override
 @Deprecated
 @SuppressWarnings("unchecked")
 public Query<T> lowerIndexBound(final DBObject lowerBound) {
   if (lowerBound != null) {
     getOptions().modifier("$min", new Document(lowerBound.toMap()));
   }
   return this;
 }
示例#7
0
  @Override
  @Deprecated
  public Query<T> upperIndexBound(final DBObject upperBound) {
    if (upperBound != null) {
      getOptions().getModifiers().put("$max", new BasicDBObject(upperBound.toMap()));
    }

    return this;
  }
  @Override
  public Output load(String streamOutputId) throws NotFoundException {
    DBObject o = get(OutputImpl.class, streamOutputId);

    if (o == null) {
      throw new NotFoundException("Output <" + streamOutputId + "> not found!");
    }

    return new OutputImpl((ObjectId) o.get("_id"), o.toMap());
  }
  @Override
  public Node byNodeId(String nodeId) throws NodeNotFoundException {
    DBObject query = new BasicDBObject("node_id", nodeId);
    DBObject o = findOne(NodeImpl.class, query);

    if (o == null || !o.containsField("node_id")) {
      throw new NodeNotFoundException("Unable to find node " + nodeId);
    }

    return new NodeImpl((ObjectId) o.get("_id"), o.toMap());
  }
  @Override
  public List<User> loadAll() {
    final DBObject query = new BasicDBObject();
    final List<DBObject> result = query(UserImpl.class, query);

    final List<User> users = Lists.newArrayList();
    for (DBObject dbObject : result) {
      users.add(new UserImpl((ObjectId) dbObject.get("_id"), dbObject.toMap()));
    }

    return users;
  }
  protected Set<Output> loadAll(Map<String, Object> additionalQueryOpts) {
    Set<Output> outputs = new HashSet<>();

    DBObject query = new BasicDBObject();

    // putAll() is not working with BasicDBObject.
    for (Map.Entry<String, Object> o : additionalQueryOpts.entrySet()) {
      query.put(o.getKey(), o.getValue());
    }

    List<DBObject> results = query(OutputImpl.class, query);
    for (DBObject o : results) outputs.add(new OutputImpl((ObjectId) o.get("_id"), o.toMap()));

    return outputs;
  }
  @Override
  public Collection<User> loadAllForRole(Role role) {
    final String roleId = role.getId();
    final DBObject query = BasicDBObjectBuilder.start(UserImpl.ROLES, new ObjectId(roleId)).get();

    final List<DBObject> result = query(UserImpl.class, query);
    if (result == null || result.isEmpty()) {
      return Collections.emptySet();
    }
    final Set<User> users = Sets.newHashSetWithExpectedSize(result.size());
    for (DBObject dbObject : result) {
      //noinspection unchecked
      users.add(new UserImpl((ObjectId) dbObject.get("_id"), dbObject.toMap()));
    }
    return users;
  }
  /**
   * Reads the given {@link DBObject} into a {@link Map}. will recursively resolve nested {@link
   * Map}s as well.
   *
   * @param type the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link
   *     DBObject}.
   * @param dbObject must not be {@literal null}
   * @param path must not be {@literal null}
   * @return
   */
  @SuppressWarnings("unchecked")
  protected Map<Object, Object> readMap(
      TypeInformation<?> type, DBObject dbObject, ObjectPath path) {

    Assert.notNull(dbObject, "DBObject must not be null!");
    Assert.notNull(path, "Object path must not be null!");

    Class<?> mapType = typeMapper.readType(dbObject, type).getType();

    TypeInformation<?> keyType = type.getComponentType();
    Class<?> rawKeyType = keyType == null ? null : keyType.getType();

    TypeInformation<?> valueType = type.getMapValueType();
    Class<?> rawValueType = valueType == null ? null : valueType.getType();

    Map<Object, Object> map =
        CollectionFactory.createMap(mapType, rawKeyType, dbObject.keySet().size());
    Map<String, Object> sourceMap = dbObject.toMap();

    for (Entry<String, Object> entry : sourceMap.entrySet()) {
      if (typeMapper.isTypeKey(entry.getKey())) {
        continue;
      }

      Object key = potentiallyUnescapeMapKey(entry.getKey());

      if (rawKeyType != null) {
        key = conversionService.convert(key, rawKeyType);
      }

      Object value = entry.getValue();

      if (value instanceof DBObject) {
        map.put(key, read(valueType, (DBObject) value, path));
      } else if (value instanceof DBRef) {
        map.put(
            key,
            DBRef.class.equals(rawValueType) ? value : read(valueType, readRef((DBRef) value)));
      } else {
        Class<?> valueClass = valueType == null ? null : valueType.getType();
        map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
      }
    }

    return map;
  }
示例#14
0
  @Override
  public Map<String, Node> allActive(NodeImpl.Type type) {
    Map<String, Node> nodes = Maps.newHashMap();

    BasicDBObject query = new BasicDBObject();
    query.put("last_seen", new BasicDBObject("$gte", Tools.getUTCTimestamp() - pingTimeout));
    query.put("type", type.toString());

    for (DBObject obj : query(NodeImpl.class, query)) {
      Node node = new NodeImpl((ObjectId) obj.get("_id"), obj.toMap());
      String nodeId = (String) obj.get("node_id");

      nodes.put(nodeId, node);
    }

    return nodes;
  }
  private void getTweetCountGroupyByUser() {
    Mongo mongo = null;
    try {
      mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT);
      DB db = mongo.getDB(MONGO_DB_NAME);
      DBCollection collection = db.getCollection(MONGO_DB_COLLECTION);

      GroupCommand groupCommad =
          new GroupCommand(
              collection,
              new BasicDBObject("user", true),
              null,
              new BasicDBObject("count", 0),
              "function(key,val){ val.count++;}",
              null);

      DBObject obj = collection.group(groupCommad);

      Map<Object, Object> map = obj.toMap();

      Iterator<Entry<Object, Object>> i = map.entrySet().iterator();
      Entry<Object, Object> entry;

      System.out.println("\n\n========================================");
      System.out.println("Displaying summary Tweet count per User");

      while (i.hasNext()) {
        entry = i.next();
        System.out.println(entry.getValue());
      }

      System.out.println("========================================\n\n");

    } catch (UnknownHostException e) {
      e.printStackTrace();
    } finally {
      if (mongo != null) mongo.close();
    }
  }
示例#16
0
  // --------------------------------------------------------------------------------
  private void drawTreeRoot(Tree _tree, DBObject data) {
    // reset tree
    _tree.removeAll();

    if (data != null) {
      TreeItem root = new TreeItem(_tree, SWT.NONE);
      root.setText(data.get("_id") + "");
      root.setImage(documentImage);
      root.setData("fieldName", "");
      root.setData("value", data.get("_id"));
      fieldNameTreeItemMap.put("", root);

      boolean expand = (data.keySet().size() < 35);
      drawItem("", root, data.toMap(), expand);
    }

    nameText.setText("");
    typeCombo.select(0);
    typeCombo.setEnabled(false);
    valueText.setText("");
    updateButton.setEnabled(false);
  }
  @Override
  public List<IndexRange> getFrom(int timestamp) {
    List<IndexRange> ranges = Lists.newArrayList();

    BasicDBObject query = new BasicDBObject();
    query.put("start", new BasicDBObject("$gte", timestamp));

    for (DBObject dbo : query(IndexRangeImpl.class, query)) {
      ranges.add(new IndexRangeImpl((ObjectId) dbo.get("_id"), dbo.toMap()));
    }

    Collections.sort(
        ranges,
        new Comparator<IndexRange>() {
          @Override
          public int compare(IndexRange o1, IndexRange o2) {
            return o2.getStart().compareTo(o1.getStart());
          }
        });

    return ranges;
  }
  @Override
  public void buildEntity(DBObject dbObject, Object instance, Class<?> clazz, SessionImpl session)
      throws IllegalArgumentException, IllegalAccessException {

    for (Field eachField : session.getGraMongoSerializableFields(clazz)) {

      DBObject fieldValue = (DBObject) dbObject.get(eachField.getName());

      if (fieldValue != null) {

        @SuppressWarnings("unchecked")
        GraMongoBasicEntity graMongoBasicEntity = new GraMongoBasicEntity(fieldValue.toMap());

        GraMongoSerializable<?> fieldInstance;
        try {
          fieldInstance = (GraMongoSerializable<?>) eachField.getType().newInstance();
          eachField.set(instance, fieldInstance.deserialize(graMongoBasicEntity));
        } catch (InstantiationException e) {
          new GraMongoException(e);
        }
      }
    }
  }
  public Double[] countAndAmountCdr(
      Account account,
      List<String> restrictedMerchants,
      String pin,
      String serial,
      String status,
      List<Integer> amounts,
      List<String> cardTypes,
      String searchMerchant,
      List<String> merchants,
      List<String> providers,
      Date fromTime,
      Date toTime) {
    if (account == null) return null;
    if (account.checkRole("staff")) ;
    boolean isAdmin =
        account.checkRoles(
            new String[] {
              "admin",
              "operation_manager",
              "biz_supporter",
              "reporter",
              "share_holder",
              "customer_care"
            });

    Double count = Double.valueOf(0.0D);
    Double amount = Double.valueOf(0.0D);
    DBCollection collection = this.mongoTemplate.getCollection(this.mongoCollection);
    Query query =
        getQuery(
            account,
            restrictedMerchants,
            pin,
            serial,
            status,
            amounts,
            cardTypes,
            searchMerchant,
            merchants,
            providers,
            fromTime,
            toTime);
    if (query == null) return new Double[] {count, amount};
    try {
      BasicDBObject initial = new BasicDBObject("count", Integer.valueOf(0));
      initial.put("amount", Integer.valueOf(0));
      GroupCommand cmd =
          new GroupCommand(
              collection,
              null,
              query.getQueryObject(),
              initial,
              "function(obj,prev) {prev.count++; prev.amount += obj.amount;}",
              null);
      DBObject obj = collection.group(cmd);
      JSONObject jsonObject = null;
      jsonObject = new JSONObject(JSON.serialize(obj.toMap()));
      System.out.println("RESULT:" + jsonObject.toString());
      count = Double.valueOf(((JSONObject) jsonObject.get("0")).getDouble("count"));
      amount = Double.valueOf(((JSONObject) jsonObject.get("0")).getDouble("amount"));

      if ((isAdmin) && (SharedConstants.MBIZ_RATE > 0.0D) && (SharedConstants.MBIZ_RATE < 1.0D)) {
        count = Double.valueOf(count.doubleValue() * SharedConstants.MBIZ_RATE);
        amount = Double.valueOf(amount.doubleValue() * SharedConstants.MBIZ_RATE);
      }
    } catch (Exception localException) {
    }
    return new Double[] {count, amount};
  }
示例#20
0
 @Override
 @Deprecated
 public DBObject getSortObject() {
   DBObject sort = getOptions().getSortDBObject();
   return (sort == null) ? null : new BasicDBObject(sort.toMap());
 }
示例#21
0
 /**
  * Sets query structure directly
  *
  * @param query the DBObject containing the query
  */
 public void setQueryObject(final DBObject query) {
   baseQuery = new BasicDBObject(query.toMap());
 }
示例#22
0
 protected BasicDBObject copy(final DBObject dbObject) {
   return dbObject == null ? null : new BasicDBObject(dbObject.toMap());
 }