Package org.nutz.mongo.entity

Examples of org.nutz.mongo.entity.MongoEntity


      Class<?> eleType = this.field.getType().getComponentType();
      if (!Mirror.me(eleType).isPojo()) {
        return val;
      }
      if (eleType.isAssignableFrom(val.getClass())) {
        MongoEntity en = Mongos.entity(eleType);
        if (en instanceof StaticMongoEntity)
          return en.toDBObject(val);
        return Mongos.obj2dbo(val);
      }
      // 否则 ...
      if (!check)
        return val;
      throw Lang.makeThrow"ArrayAdaptor can only adaptForGet array, but is is '%s'",
                  val.getClass().getName());
    }
    // 首先,建立一个新数组
    Object[] array = new Object[Array.getLength(val)];

    // 将每个项目都转换成 DBObject
    for (int i = 0; i < array.length; i++) {
      Object ele = Array.get(val, i);
      // 如果是POJO或者容器
      if (Mirror.me(ele.getClass()).isObj()) {
        if (field.isRef()) {
          MongoEntity en = Mongos.entity(val.getClass().getComponentType());
          DBObject dbo = en.toDBObject(ele);
          if (dbo.containsField("_id")) {
            array[i] = new DBRef(null, en.getCollectionName(null), dbo.get("_id"));
            continue;
          } else {
            if (log.isWarnEnabled())
              log.warn("!!obj without _id but using as ref field value!! fallback to embed doc!!");
          }
        } else {
          MongoEntity en = Mongos.entity(val.getClass().getComponentType());
          if (en instanceof StaticMongoEntity)
            array[i] = en.toDBObject(ele);
          else
            array[i] = Mongos.obj2dbo(ele);
        }
      }
      // 否则直接设置
View Full Code Here


      Object v = list.get(i);
      Object o = null;
      if (null != v) {
        // 如果是 DBObject
        if (v instanceof DBObject) {
          MongoEntity en = Mongos.entity(eleType);
          if (en instanceof StaticMongoEntity) {
            o = en.toObject((DBObject) v);
          } else
            o = Lang.map2Object(((DBObject) v).toMap(), eleType);
        }
        // 否则就强制转换一下
        else {
View Full Code Here

 
  @Override
  public Object adaptForGet(Object val, boolean check) {
    if (val == null)
      return null;
    MongoEntity en = Mongos.entity(val);
    DBObject dbo = en.toDBObject(val);
    if (!field.isRef())
      return dbo;

    if (!dbo.containsField("_id")) {
      if (log.isWarnEnabled())
        log.warn("!!obj without _id but using as ref field value!! fallback to embed doc!!");
      return dbo;
    }
    return new DBRef(null, en.getCollectionName(null), dbo.get("_id"));
  }
View Full Code Here

  @Override
  public Object adaptForSet(Object val) {
    if (val instanceof DBRef && field.isRef()) {
      return unpackRef((DBRef)val, field.getType());
    }
    MongoEntity en = Mongos.entity(field.getMirror().getType());
    if (en instanceof StaticMongoEntity)
      return en.toObject((DBObject) val);
    Map<String, Object> map = (Map<String, Object>) ((DBObject) val).toMap();
    return Lang.map2Object(map, field.getType());
  }
View Full Code Here

   * @param q
   *            查询条件
   * @return 修改结果
   */
  public WriteResult remove(Class<?> type, Object q) {
    MongoEntity moe = Mongos.entity(type);
    // 获得集合
    String collName = moe.getCollectionName(q);
    if (db.collectionExists(collName)) {
      DBCollection coll = db.getCollection(collName);
      // 将 ref 对象转换成 DBObject
      DBObject dbRef = moe.formatObject(q);

      // 执行删除
      return coll.remove(dbRef);
    }
    return null;
View Full Code Here

   * @param obj
   *            对象
   * @return 保存后的对象,如果为 null,表示集合不存在,保存失败
   */
  public <T extends Object> T save(T obj) {
    MongoEntity moe = Mongos.entity(obj);
    String collName = moe.getCollectionName(obj);
    if (db.collectionExists(collName)) {
      moe.fillIdIfNoexits(obj);
      DBObject dbo = moe.toDBObject(obj);
      DBCollection coll = db.getCollection(collName);
      coll.save(dbo);
      return obj;
    }
    return null;
View Full Code Here

   * @param o
   *            要更新的字段
   * @return 修改结果
   */
  public WriteResult update(Object enref, Object q, Object o) {
    MongoEntity moe = (MongoEntity) Mongos.entity(enref);
    String collName = moe.getCollectionName(q);
    if (db.collectionExists(collName)) {
      DBCollection coll = db.getCollection(collName);
      DBObject dbq = moe.formatObject(q);
      DBObject dbo = moe.formatObject(o);
      return coll.updateMulti(dbq, dbo);
    }
    return null;
  }
View Full Code Here

   *            可更新的对象
   * @return 匹配的对象
   */
  @SuppressWarnings("unchecked")
  public <T> T findAndModify(Class<T> type, Object q, MCur cur, Object obj) {
    MongoEntity moe = Mongos.entity(type);
    return (T) findAndModify(moe.getCollectionName(q), moe, q, cur, obj);
  }
View Full Code Here

   * @param obj
   *            可更新的对象
   * @return 匹配的对象
   */
  public NutMap findAndModify(String collName, Object q, MCur cur, Object obj) {
    MongoEntity moe = Mongos.entity(NutMap.class);
    return (NutMap) findAndModify(collName, moe, q, cur, obj);
  }
View Full Code Here

   *            查询对象
   * @return 匹配的对象
   */
  @SuppressWarnings("unchecked")
  public <T> T findAndRemove(Class<T> type, Object q) {
    MongoEntity moe = Mongos.entity(type);
    return (T) findAndRemove(moe.getCollectionName(q), moe, q);
  }
View Full Code Here

TOP

Related Classes of org.nutz.mongo.entity.MongoEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.