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

https://github.com/anotherJay/grails-data-mapping · Java · 135 lines · 95 code · 21 blank · 19 comment · 10 complexity · a42dc035ee8fa2bde461faa38238ebbd 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 org.grails.datastore.mapping.config.AbstractGormMappingFactory;
  17. import org.grails.datastore.mapping.document.config.Collection;
  18. import org.grails.datastore.mapping.document.config.DocumentMappingContext;
  19. import org.grails.datastore.mapping.document.config.DocumentPersistentEntity;
  20. import org.grails.datastore.mapping.model.*;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. /**
  26. * Models a {@link org.grails.datastore.mapping.model.MappingContext} for Mongo.
  27. *
  28. * @author Graeme Rocher
  29. */
  30. public class MongoMappingContext extends DocumentMappingContext {
  31. public static final Set<String> MONGO_SIMPLE_TYPES;
  32. static {
  33. MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
  34. org.bson.types.ObjectId.class.getName(),
  35. org.bson.types.Binary.class.getName(),
  36. com.mongodb.DBObject.class.getName()
  37. )));
  38. }
  39. private final class MongoDocumentMappingFactory extends
  40. AbstractGormMappingFactory<MongoCollection, MongoAttribute> {
  41. @Override
  42. protected Class<MongoAttribute> getPropertyMappedFormType() {
  43. return MongoAttribute.class;
  44. }
  45. @Override
  46. protected Class<MongoCollection> getEntityMappedFormType() {
  47. return MongoCollection.class;
  48. }
  49. @Override
  50. protected IdentityMapping getIdentityMappedForm(final ClassMapping classMapping, final MongoAttribute property) {
  51. if(property != null) {
  52. return new IdentityMapping() {
  53. public String[] getIdentifierName() {
  54. if(property.getName() != null)
  55. return new String[] { property.getName()};
  56. else {
  57. return new String[] { MappingFactory.IDENTITY_PROPERTY };
  58. }
  59. }
  60. public ClassMapping getClassMapping() {
  61. return classMapping;
  62. }
  63. public Object getMappedForm() {
  64. return property;
  65. }
  66. };
  67. }
  68. return super.getIdentityMappedForm(classMapping, property);
  69. }
  70. @Override
  71. public boolean isSimpleType(Class propType) {
  72. if (propType == null) return false;
  73. if (propType.isArray()) {
  74. return isSimpleType(propType.getComponentType()) || super.isSimpleType(propType);
  75. }
  76. final String typeName = propType.getName();
  77. return MONGO_SIMPLE_TYPES.contains(typeName) || super.isSimpleType(propType);
  78. }
  79. }
  80. public MongoMappingContext(String defaultDatabaseName) {
  81. super(defaultDatabaseName);
  82. }
  83. @SuppressWarnings("rawtypes")
  84. @Override
  85. protected MappingFactory createDocumentMappingFactory() {
  86. return new MongoDocumentMappingFactory();
  87. }
  88. @Override
  89. public PersistentEntity createEmbeddedEntity(Class type) {
  90. return new DocumentEmbeddedPersistentEntity(type, this);
  91. }
  92. class DocumentEmbeddedPersistentEntity extends EmbeddedPersistentEntity {
  93. private DocumentCollectionMapping classMapping ;
  94. public DocumentEmbeddedPersistentEntity(Class type, MappingContext ctx) {
  95. super(type, ctx);
  96. classMapping = new DocumentCollectionMapping(this, ctx);
  97. }
  98. @Override
  99. public ClassMapping getMapping() {
  100. return classMapping;
  101. }
  102. public class DocumentCollectionMapping extends AbstractClassMapping<Collection> {
  103. private Collection mappedForm;
  104. public DocumentCollectionMapping(PersistentEntity entity, MappingContext context) {
  105. super(entity, context);
  106. this.mappedForm = (Collection) context.getMappingFactory().createMappedForm(DocumentEmbeddedPersistentEntity.this);
  107. }
  108. @Override
  109. public Collection getMappedForm() {
  110. return mappedForm ;
  111. }
  112. }
  113. }
  114. }