/hibernate-ogm-mongodb/src/main/java/org/hibernate/ogm/dialect/mongodb/MongoHelpers.java

https://bitbucket.org/cprenzberg/hibernate-ogm · Java · 106 lines · 68 code · 12 blank · 26 comment · 19 complexity · 52ace91ce722853f205b019a0745b9d9 MD5 · raw file

  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * JBoss, Home of Professional Open Source
  5. * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
  6. * as indicated by the @authors tag. All rights reserved.
  7. * See the copyright.txt in the distribution for a
  8. * full listing of individual contributors.
  9. *
  10. * This copyrighted material is made available to anyone wishing to use,
  11. * modify, copy, or redistribute it subject to the terms and conditions
  12. * of the GNU Lesser General Public License, v. 2.1.
  13. * This program is distributed in the hope that it will be useful, but WITHOUT A
  14. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  15. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  16. * You should have received a copy of the GNU Lesser General Public License,
  17. * v.2.1 along with this distribution; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. */
  21. package org.hibernate.ogm.dialect.mongodb;
  22. import org.hibernate.annotations.common.AssertionFailure;
  23. import org.hibernate.ogm.datastore.mongodb.AssociationStorage;
  24. import org.hibernate.ogm.grid.AssociationKey;
  25. import com.mongodb.BasicDBObject;
  26. import com.mongodb.DBObject;
  27. import org.hibernate.ogm.grid.AssociationKind;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. /**
  31. * @author Alan Fitton <alan at eth0.org.uk>
  32. * @author Emmanuel Bernard <emmanuel@hibernate.org>
  33. */
  34. public class MongoHelpers {
  35. public static DBObject associationKeyToObject(AssociationStorage storage, AssociationKey key) {
  36. if ( isEmbeddedInEntity( key, storage ) ) {
  37. throw new AssertionFailure( MongoHelpers.class.getName()
  38. + ".associationKeyToObject should not be called for associations embedded in entity documents");
  39. }
  40. Object[] columnValues = key.getColumnValues();
  41. DBObject columns = new BasicDBObject( columnValues.length );
  42. int i = 0;
  43. for ( String name : key.getColumnNames() ) {
  44. columns.put( name, columnValues[i++] );
  45. }
  46. BasicDBObject idObject = new BasicDBObject( 1 );
  47. if ( storage == AssociationStorage.GLOBAL_COLLECTION ) {
  48. columns.put( MongoDBDialect.TABLE_FIELDNAME, key.getTable() );
  49. }
  50. idObject.put("_id", columns );
  51. return idObject;
  52. }
  53. public static boolean isEmbeddedInEntity(AssociationKey key, AssociationStorage storage) {
  54. return ( key != null && key.getAssociationKind() == AssociationKind.EMBEDDED )
  55. || storage == AssociationStorage.IN_ENTITY;
  56. }
  57. //only for embedded
  58. public static Collection<DBObject> getAssociationFieldOrNull(AssociationKey key, DBObject entity) {
  59. String[] path = key.getCollectionRole().split( "\\." );
  60. Object field = entity;
  61. for (String node : path) {
  62. field = field != null ? ( (DBObject) field).get( node ) : null;
  63. }
  64. return (Collection<DBObject>) field;
  65. }
  66. public static void addEmptyAssociationField(AssociationKey key, DBObject entity) {
  67. String[] path = key.getCollectionRole().split( "\\." );
  68. Object field = entity;
  69. int size = path.length;
  70. for (int index = 0 ; index < size ; index++) {
  71. String node = path[index];
  72. DBObject parent = (DBObject) field;
  73. field = parent.get( node );
  74. if ( field == null ) {
  75. if ( index == size - 1 ) {
  76. field = Collections.EMPTY_LIST;
  77. }
  78. else {
  79. field = new BasicDBObject();
  80. }
  81. parent.put( node, field );
  82. }
  83. }
  84. }
  85. // Return null if the column is not present
  86. public static Object getValueFromColumns(String column, String[] columns, Object[] values) {
  87. for ( int index = 0 ; index < columns.length ; index++ ) {
  88. if ( columns[index].equals( column ) ) {
  89. return values[index];
  90. }
  91. }
  92. return null;
  93. }
  94. }