/grails-datastore-mongo/src/main/groovy/org/grails/datastore/mapping/mongo/config/MongoMappingContext.java

https://github.com/chongivan/grails-data-mapping · Java · 179 lines · 131 code · 21 blank · 27 comment · 9 complexity · b09ebe9aeabe0623b75edab3d54c252e MD5 · raw file

  1. /* Copyright (C) 2010 SpringSource
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package org.grails.datastore.mapping.mongo.config;
  16. import com.mongodb.DBObject;
  17. import groovy.lang.Closure;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Date;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. import java.util.UUID;
  24. import java.util.regex.Pattern;
  25. import org.bson.BSONObject;
  26. import org.bson.types.BSONTimestamp;
  27. import org.bson.types.Code;
  28. import org.bson.types.CodeWScope;
  29. import org.bson.types.Symbol;
  30. import org.grails.datastore.mapping.config.AbstractGormMappingFactory;
  31. import org.grails.datastore.mapping.config.Property;
  32. import org.grails.datastore.mapping.document.config.Collection;
  33. import org.grails.datastore.mapping.document.config.DocumentMappingContext;
  34. import org.grails.datastore.mapping.model.AbstractClassMapping;
  35. import org.grails.datastore.mapping.model.ClassMapping;
  36. import org.grails.datastore.mapping.model.EmbeddedPersistentEntity;
  37. import org.grails.datastore.mapping.model.IdentityMapping;
  38. import org.grails.datastore.mapping.model.MappingContext;
  39. import org.grails.datastore.mapping.model.MappingFactory;
  40. import org.grails.datastore.mapping.model.PersistentEntity;
  41. import com.mongodb.DBRef;
  42. /**
  43. * Models a {@link org.grails.datastore.mapping.model.MappingContext} for Mongo.
  44. *
  45. * @author Graeme Rocher
  46. */
  47. @SuppressWarnings("rawtypes")
  48. public class MongoMappingContext extends DocumentMappingContext {
  49. /**
  50. * Java types supported as mongo property types.
  51. */
  52. private static final Set<String> MONGO_NATIVE_TYPES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
  53. Double.class.getName(),
  54. String.class.getName(),
  55. com.mongodb.DBObject.class.getName(),
  56. org.bson.types.Binary.class.getName(),
  57. org.bson.types.ObjectId.class.getName(),
  58. DBRef.class.getName(),
  59. Boolean.class.getName(),
  60. Date.class.getName(),
  61. Pattern.class.getName(),
  62. Symbol.class.getName(),
  63. Integer.class.getName(),
  64. BSONTimestamp.class.getName(),
  65. Code.class.getName(),
  66. CodeWScope.class.getName(),
  67. Long.class.getName(),
  68. UUID.class.getName(),
  69. byte[].class.getName(),
  70. Byte.class.getName()
  71. )));
  72. /**
  73. * Check whether a type is a native mongo type that can be stored by the mongo driver without conversion.
  74. * @param clazz The class to check.
  75. * @return true if no conversion is required and the type can be stored natively.
  76. */
  77. public static boolean isMongoNativeType(Class clazz) {
  78. return MongoMappingContext.MONGO_NATIVE_TYPES.contains(clazz.getName()) ||
  79. DBObject.class.isAssignableFrom(clazz.getClass());
  80. }
  81. private final class MongoDocumentMappingFactory extends
  82. AbstractGormMappingFactory<MongoCollection, MongoAttribute> {
  83. @Override
  84. protected Class<MongoAttribute> getPropertyMappedFormType() {
  85. return MongoAttribute.class;
  86. }
  87. @Override
  88. protected Class<MongoCollection> getEntityMappedFormType() {
  89. return MongoCollection.class;
  90. }
  91. @Override
  92. protected IdentityMapping getIdentityMappedForm(final ClassMapping classMapping, final MongoAttribute property) {
  93. if (property == null) {
  94. return super.getIdentityMappedForm(classMapping, property);
  95. }
  96. return new IdentityMapping() {
  97. public String[] getIdentifierName() {
  98. if (property.getName() == null) {
  99. return new String[] { MappingFactory.IDENTITY_PROPERTY };
  100. }
  101. return new String[] { property.getName()};
  102. }
  103. public ClassMapping getClassMapping() {
  104. return classMapping;
  105. }
  106. public Property getMappedForm() {
  107. return property;
  108. }
  109. };
  110. }
  111. @Override
  112. public boolean isSimpleType(Class propType) {
  113. if (propType == null) return false;
  114. if (propType.isArray()) {
  115. return isSimpleType(propType.getComponentType()) || super.isSimpleType(propType);
  116. }
  117. return isMongoNativeType(propType) || super.isSimpleType(propType);
  118. }
  119. }
  120. public MongoMappingContext(String defaultDatabaseName) {
  121. super(defaultDatabaseName);
  122. }
  123. public MongoMappingContext(String defaultDatabaseName, Closure defaultMapping) {
  124. super(defaultDatabaseName, defaultMapping);
  125. }
  126. @Override
  127. protected MappingFactory createDocumentMappingFactory(Closure defaultMapping) {
  128. MongoDocumentMappingFactory mongoDocumentMappingFactory = new MongoDocumentMappingFactory();
  129. mongoDocumentMappingFactory.setDefaultMapping(defaultMapping);
  130. return mongoDocumentMappingFactory;
  131. }
  132. @Override
  133. public PersistentEntity createEmbeddedEntity(Class type) {
  134. return new DocumentEmbeddedPersistentEntity(type, this);
  135. }
  136. class DocumentEmbeddedPersistentEntity extends EmbeddedPersistentEntity {
  137. private DocumentCollectionMapping classMapping ;
  138. public DocumentEmbeddedPersistentEntity(Class type, MappingContext ctx) {
  139. super(type, ctx);
  140. classMapping = new DocumentCollectionMapping(this, ctx);
  141. }
  142. @Override
  143. public ClassMapping getMapping() {
  144. return classMapping;
  145. }
  146. public class DocumentCollectionMapping extends AbstractClassMapping<Collection> {
  147. private Collection mappedForm;
  148. public DocumentCollectionMapping(PersistentEntity entity, MappingContext context) {
  149. super(entity, context);
  150. this.mappedForm = (Collection) context.getMappingFactory().createMappedForm(DocumentEmbeddedPersistentEntity.this);
  151. }
  152. @Override
  153. public Collection getMappedForm() {
  154. return mappedForm ;
  155. }
  156. }
  157. }
  158. }