/nutz-plugins-mongodb/src/main/java/org/nutz/mongo/adaptor/ZMoCollectionAdaptor.java

https://github.com/nutzam/nutzmore · Java · 110 lines · 79 code · 14 blank · 17 comment · 26 complexity · c8c3cfd3324510bc2d7f5cd48b7fff02 MD5 · raw file

  1. package org.nutz.mongo.adaptor;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. import org.bson.types.ObjectId;
  6. import org.nutz.lang.Lang;
  7. import org.nutz.lang.Mirror;
  8. import org.nutz.mongo.ZMo;
  9. import org.nutz.mongo.ZMoAdaptor;
  10. import org.nutz.mongo.entity.ZMoEntity;
  11. import org.nutz.mongo.entity.ZMoField;
  12. import com.mongodb.BasicDBList;
  13. import com.mongodb.DBObject;
  14. public class ZMoCollectionAdaptor implements ZMoAdaptor {
  15. ZMoCollectionAdaptor() {}
  16. @SuppressWarnings("unchecked")
  17. @Override
  18. public Object toJava(ZMoField fld, Object obj) {
  19. if (obj instanceof BasicDBList) {
  20. BasicDBList list = (BasicDBList) obj;
  21. // 获取元素的实体
  22. ZMoEntity en = null;
  23. // 创建数组
  24. Collection<Object> coll = null;
  25. if (fld == null) {
  26. coll = new ArrayList<Object>(list.size());
  27. } else {
  28. coll = (Collection<Object>) fld.getBorning().born();
  29. }
  30. // 开始循环数组
  31. Iterator<Object> it = list.iterator();
  32. while (it.hasNext()) {
  33. Object eleMongo = it.next();
  34. Object elePojo;
  35. // 如果元素是个 Mongo 类型
  36. if (eleMongo instanceof DBObject) {
  37. // 确保已经获得过实体过了,这里这个代码考虑到效率
  38. // 就是说一个集合或者数组,映射方式总是一样的
  39. // 如果有不一样的,那么就完蛋了
  40. if (null == en) {
  41. en = ZMo.me().getEntity(fld.getEleType());
  42. }
  43. // 转换
  44. elePojo = ZMo.me().fromDoc((DBObject) eleMongo, en);
  45. }
  46. // 如果 fld 有 adaptor
  47. else if (null != fld && null != fld.getEleAdaptor()) {
  48. elePojo = fld.getEleAdaptor().toJava(null, eleMongo);
  49. }
  50. // 其他情况,直接上 smart 咯
  51. else {
  52. elePojo = ZMoAs.smart().toJava(null, eleMongo);
  53. }
  54. // 加入到数组中
  55. coll.add(elePojo);
  56. }
  57. return coll;
  58. }
  59. throw Lang.makeThrow("toJava error: %s", obj.getClass());
  60. }
  61. @Override
  62. public Object toMongo(ZMoField fld, Object obj) {
  63. if (Mirror.me(obj).isCollection()) {
  64. Collection<?> coll = (Collection<?>) obj;
  65. BasicDBList list = new BasicDBList();
  66. for (Object objPojo : coll) {
  67. Object objMongo;
  68. Mirror<?> mi = Mirror.me(objPojo);
  69. // null
  70. if (null == objPojo) {
  71. objMongo = null;
  72. }
  73. // 对于 ObjectId
  74. else if (objPojo instanceof ObjectId) {
  75. objMongo = objPojo;
  76. }
  77. // 普通的 DBObject
  78. else if (objPojo instanceof DBObject) {
  79. objMongo = objPojo;
  80. }
  81. // Map 或者 POJO
  82. else if (mi.isMap() || mi.isPojo()) {
  83. objMongo = ZMo.me().toDoc(objPojo);
  84. }
  85. // 其他类型用 smart 转一下咯
  86. else {
  87. objMongo = ZMoAs.smart().toMongo(null, objPojo);
  88. }
  89. // 加入到列表
  90. list.add(objMongo);
  91. }
  92. return list;
  93. }
  94. throw Lang.makeThrow("toMongo error: %s", obj.getClass());
  95. }
  96. }