|
1 |
| -/* |
2 |
| - * Copyright 2011-2016 the original author or authors. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| -package org.springframework.data.mongodb.crossstore; |
17 |
| - |
18 |
| -import javax.persistence.EntityManagerFactory; |
19 |
| - |
20 |
| -import org.slf4j.Logger; |
21 |
| -import org.slf4j.LoggerFactory; |
22 |
| -import org.springframework.dao.DataAccessException; |
23 |
| -import org.springframework.dao.DataAccessResourceFailureException; |
24 |
| -import org.springframework.dao.DataIntegrityViolationException; |
25 |
| -import org.springframework.data.crossstore.ChangeSet; |
26 |
| -import org.springframework.data.crossstore.ChangeSetBacked; |
27 |
| -import org.springframework.data.crossstore.ChangeSetPersister; |
28 |
| -import org.springframework.data.mongodb.core.CollectionCallback; |
29 |
| -import org.springframework.data.mongodb.core.MongoTemplate; |
30 |
| -import org.springframework.util.ClassUtils; |
31 |
| - |
32 |
| -import com.mongodb.BasicDBObject; |
33 |
| -import com.mongodb.DBCollection; |
34 |
| -import com.mongodb.DBObject; |
35 |
| -import com.mongodb.MongoException; |
36 |
| - |
37 |
| -/** |
38 |
| - * @author Thomas Risberg |
39 |
| - * @author Oliver Gierke |
40 |
| - * @author Alex Vengrovsk |
41 |
| - * @author Mark Paluch |
42 |
| - */ |
43 |
| -public class MongoChangeSetPersister implements ChangeSetPersister<Object> { |
44 |
| - |
45 |
| - private static final String ENTITY_CLASS = "_entity_class"; |
46 |
| - private static final String ENTITY_ID = "_entity_id"; |
47 |
| - private static final String ENTITY_FIELD_NAME = "_entity_field_name"; |
48 |
| - private static final String ENTITY_FIELD_CLASS = "_entity_field_class"; |
49 |
| - |
50 |
| - private final Logger log = LoggerFactory.getLogger(getClass()); |
51 |
| - |
52 |
| - private MongoTemplate mongoTemplate; |
53 |
| - private EntityManagerFactory entityManagerFactory; |
54 |
| - |
55 |
| - public void setMongoTemplate(MongoTemplate mongoTemplate) { |
56 |
| - this.mongoTemplate = mongoTemplate; |
57 |
| - } |
58 |
| - |
59 |
| - public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { |
60 |
| - this.entityManagerFactory = entityManagerFactory; |
61 |
| - } |
62 |
| - |
63 |
| - /* |
64 |
| - * (non-Javadoc) |
65 |
| - * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet) |
66 |
| - */ |
67 |
| - public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet) |
68 |
| - throws DataAccessException, NotFoundException { |
69 |
| - |
70 |
| - if (id == null) { |
71 |
| - log.debug("Unable to load MongoDB data for null id"); |
72 |
| - return; |
73 |
| - } |
74 |
| - |
75 |
| - String collName = getCollectionNameForEntity(entityClass); |
76 |
| - |
77 |
| - final DBObject dbk = new BasicDBObject(); |
78 |
| - dbk.put(ENTITY_ID, id); |
79 |
| - dbk.put(ENTITY_CLASS, entityClass.getName()); |
80 |
| - if (log.isDebugEnabled()) { |
81 |
| - log.debug("Loading MongoDB data for {}", dbk); |
82 |
| - } |
83 |
| - mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
84 |
| - public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
85 |
| - for (DBObject dbo : collection.find(dbk)) { |
86 |
| - String key = (String) dbo.get(ENTITY_FIELD_NAME); |
87 |
| - if (log.isDebugEnabled()) { |
88 |
| - log.debug("Processing key: {}", key); |
89 |
| - } |
90 |
| - if (!changeSet.getValues().containsKey(key)) { |
91 |
| - String className = (String) dbo.get(ENTITY_FIELD_CLASS); |
92 |
| - if (className == null) { |
93 |
| - throw new DataIntegrityViolationException( |
94 |
| - "Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); |
95 |
| - } |
96 |
| - Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); |
97 |
| - Object value = mongoTemplate.getConverter().read(clazz, dbo); |
98 |
| - if (log.isDebugEnabled()) { |
99 |
| - log.debug("Adding to ChangeSet: {}", key); |
100 |
| - } |
101 |
| - changeSet.set(key, value); |
102 |
| - } |
103 |
| - } |
104 |
| - return null; |
105 |
| - } |
106 |
| - }); |
107 |
| - } |
108 |
| - |
109 |
| - /* |
110 |
| - * (non-Javadoc) |
111 |
| - * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) |
112 |
| - */ |
113 |
| - public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { |
114 |
| - if (log.isDebugEnabled()) { |
115 |
| - log.debug("getPersistentId called on {}", entity); |
116 |
| - } |
117 |
| - if (entityManagerFactory == null) { |
118 |
| - throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null"); |
119 |
| - } |
120 |
| - |
121 |
| - return entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); |
122 |
| - } |
123 |
| - |
124 |
| - /* |
125 |
| - * (non-Javadoc) |
126 |
| - * @see org.springframework.data.crossstore.ChangeSetPersister#persistState(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) |
127 |
| - */ |
128 |
| - public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { |
129 |
| - if (cs == null) { |
130 |
| - log.debug("Flush: changeset was null, nothing to flush."); |
131 |
| - return 0L; |
132 |
| - } |
133 |
| - |
134 |
| - if (log.isDebugEnabled()) { |
135 |
| - log.debug("Flush: changeset: {}", cs.getValues()); |
136 |
| - } |
137 |
| - |
138 |
| - String collName = getCollectionNameForEntity(entity.getClass()); |
139 |
| - if (mongoTemplate.getCollection(collName) == null) { |
140 |
| - mongoTemplate.createCollection(collName); |
141 |
| - } |
142 |
| - |
143 |
| - for (String key : cs.getValues().keySet()) { |
144 |
| - if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) { |
145 |
| - Object value = cs.getValues().get(key); |
146 |
| - final DBObject dbQuery = new BasicDBObject(); |
147 |
| - dbQuery.put(ENTITY_ID, getPersistentId(entity, cs)); |
148 |
| - dbQuery.put(ENTITY_CLASS, entity.getClass().getName()); |
149 |
| - dbQuery.put(ENTITY_FIELD_NAME, key); |
150 |
| - DBObject dbId = mongoTemplate.execute(collName, new CollectionCallback<DBObject>() { |
151 |
| - public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
152 |
| - return collection.findOne(dbQuery); |
153 |
| - } |
154 |
| - }); |
155 |
| - if (value == null) { |
156 |
| - if (log.isDebugEnabled()) { |
157 |
| - log.debug("Flush: removing: {}", dbQuery); |
158 |
| - } |
159 |
| - mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
160 |
| - public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
161 |
| - collection.remove(dbQuery); |
162 |
| - return null; |
163 |
| - } |
164 |
| - }); |
165 |
| - } else { |
166 |
| - final DBObject dbDoc = new BasicDBObject(); |
167 |
| - dbDoc.putAll(dbQuery); |
168 |
| - if (log.isDebugEnabled()) { |
169 |
| - log.debug("Flush: saving: {}", dbQuery); |
170 |
| - } |
171 |
| - mongoTemplate.getConverter().write(value, dbDoc); |
172 |
| - dbDoc.put(ENTITY_FIELD_CLASS, value.getClass().getName()); |
173 |
| - if (dbId != null) { |
174 |
| - dbDoc.put("_id", dbId.get("_id")); |
175 |
| - } |
176 |
| - mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
177 |
| - public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
178 |
| - collection.save(dbDoc); |
179 |
| - return null; |
180 |
| - } |
181 |
| - }); |
182 |
| - } |
183 |
| - } |
184 |
| - } |
185 |
| - return 0L; |
186 |
| - } |
187 |
| - |
188 |
| - /** |
189 |
| - * Returns the collection the given entity type shall be persisted to. |
190 |
| - * |
191 |
| - * @param entityClass must not be {@literal null}. |
192 |
| - * @return |
193 |
| - */ |
194 |
| - private String getCollectionNameForEntity(Class<? extends ChangeSetBacked> entityClass) { |
195 |
| - return mongoTemplate.getCollectionName(entityClass); |
196 |
| - } |
197 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2011-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.crossstore; |
| 17 | + |
| 18 | +import javax.persistence.EntityManagerFactory; |
| 19 | + |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.springframework.dao.DataAccessException; |
| 23 | +import org.springframework.dao.DataAccessResourceFailureException; |
| 24 | +import org.springframework.dao.DataIntegrityViolationException; |
| 25 | +import org.springframework.data.crossstore.ChangeSet; |
| 26 | +import org.springframework.data.crossstore.ChangeSetBacked; |
| 27 | +import org.springframework.data.crossstore.ChangeSetPersister; |
| 28 | +import org.springframework.data.mongodb.core.CollectionCallback; |
| 29 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | + |
| 32 | +import com.mongodb.BasicDBObject; |
| 33 | +import com.mongodb.DBCollection; |
| 34 | +import com.mongodb.DBObject; |
| 35 | +import com.mongodb.MongoException; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Thomas Risberg |
| 39 | + * @author Oliver Gierke |
| 40 | + * @author Alex Vengrovsk |
| 41 | + * @author Mark Paluch |
| 42 | + */ |
| 43 | +public class MongoChangeSetPersister implements ChangeSetPersister<Object> { |
| 44 | + |
| 45 | + private static final String ENTITY_CLASS = "_entity_class"; |
| 46 | + private static final String ENTITY_ID = "_entity_id"; |
| 47 | + private static final String ENTITY_FIELD_NAME = "_entity_field_name"; |
| 48 | + private static final String ENTITY_FIELD_CLASS = "_entity_field_class"; |
| 49 | + |
| 50 | + private final Logger log = LoggerFactory.getLogger(getClass()); |
| 51 | + |
| 52 | + private MongoTemplate mongoTemplate; |
| 53 | + private EntityManagerFactory entityManagerFactory; |
| 54 | + |
| 55 | + public void setMongoTemplate(MongoTemplate mongoTemplate) { |
| 56 | + this.mongoTemplate = mongoTemplate; |
| 57 | + } |
| 58 | + |
| 59 | + public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) { |
| 60 | + this.entityManagerFactory = entityManagerFactory; |
| 61 | + } |
| 62 | + |
| 63 | + /* |
| 64 | + * (non-Javadoc) |
| 65 | + * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentState(java.lang.Class, java.lang.Object, org.springframework.data.crossstore.ChangeSet) |
| 66 | + */ |
| 67 | + public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet) |
| 68 | + throws DataAccessException, NotFoundException { |
| 69 | + |
| 70 | + if (id == null) { |
| 71 | + log.debug("Unable to load MongoDB data for null id"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + String collName = getCollectionNameForEntity(entityClass); |
| 76 | + |
| 77 | + final DBObject dbk = new BasicDBObject(); |
| 78 | + dbk.put(ENTITY_ID, id); |
| 79 | + dbk.put(ENTITY_CLASS, entityClass.getName()); |
| 80 | + if (log.isDebugEnabled()) { |
| 81 | + log.debug("Loading MongoDB data for {}", dbk); |
| 82 | + } |
| 83 | + mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
| 84 | + public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 85 | + for (DBObject dbo : collection.find(dbk)) { |
| 86 | + String key = (String) dbo.get(ENTITY_FIELD_NAME); |
| 87 | + if (log.isDebugEnabled()) { |
| 88 | + log.debug("Processing key: {}", key); |
| 89 | + } |
| 90 | + if (!changeSet.getValues().containsKey(key)) { |
| 91 | + String className = (String) dbo.get(ENTITY_FIELD_CLASS); |
| 92 | + if (className == null) { |
| 93 | + throw new DataIntegrityViolationException( |
| 94 | + "Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); |
| 95 | + } |
| 96 | + Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); |
| 97 | + Object value = mongoTemplate.getConverter().read(clazz, dbo); |
| 98 | + if (log.isDebugEnabled()) { |
| 99 | + log.debug("Adding to ChangeSet: {}", key); |
| 100 | + } |
| 101 | + changeSet.set(key, value); |
| 102 | + } |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * (non-Javadoc) |
| 111 | + * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) |
| 112 | + */ |
| 113 | + public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { |
| 114 | + if (log.isDebugEnabled()) { |
| 115 | + log.debug("getPersistentId called on {}", entity); |
| 116 | + } |
| 117 | + if (entityManagerFactory == null) { |
| 118 | + throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null"); |
| 119 | + } |
| 120 | + |
| 121 | + return entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity); |
| 122 | + } |
| 123 | + |
| 124 | + /* |
| 125 | + * (non-Javadoc) |
| 126 | + * @see org.springframework.data.crossstore.ChangeSetPersister#persistState(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) |
| 127 | + */ |
| 128 | + public Object persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { |
| 129 | + if (cs == null) { |
| 130 | + log.debug("Flush: changeset was null, nothing to flush."); |
| 131 | + return 0L; |
| 132 | + } |
| 133 | + |
| 134 | + if (log.isDebugEnabled()) { |
| 135 | + log.debug("Flush: changeset: {}", cs.getValues()); |
| 136 | + } |
| 137 | + |
| 138 | + String collName = getCollectionNameForEntity(entity.getClass()); |
| 139 | + if (mongoTemplate.getCollection(collName) == null) { |
| 140 | + mongoTemplate.createCollection(collName); |
| 141 | + } |
| 142 | + |
| 143 | + for (String key : cs.getValues().keySet()) { |
| 144 | + if (key != null && !key.startsWith("_") && !key.equals(ChangeSetPersister.ID_KEY)) { |
| 145 | + Object value = cs.getValues().get(key); |
| 146 | + final DBObject dbQuery = new BasicDBObject(); |
| 147 | + dbQuery.put(ENTITY_ID, getPersistentId(entity, cs)); |
| 148 | + dbQuery.put(ENTITY_CLASS, entity.getClass().getName()); |
| 149 | + dbQuery.put(ENTITY_FIELD_NAME, key); |
| 150 | + DBObject dbId = mongoTemplate.execute(collName, new CollectionCallback<DBObject>() { |
| 151 | + public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 152 | + return collection.findOne(dbQuery); |
| 153 | + } |
| 154 | + }); |
| 155 | + if (value == null) { |
| 156 | + if (log.isDebugEnabled()) { |
| 157 | + log.debug("Flush: removing: {}", dbQuery); |
| 158 | + } |
| 159 | + mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
| 160 | + public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 161 | + collection.remove(dbQuery); |
| 162 | + return null; |
| 163 | + } |
| 164 | + }); |
| 165 | + } else { |
| 166 | + final DBObject dbDoc = new BasicDBObject(); |
| 167 | + dbDoc.putAll(dbQuery); |
| 168 | + if (log.isDebugEnabled()) { |
| 169 | + log.debug("Flush: saving: {}", dbQuery); |
| 170 | + } |
| 171 | + mongoTemplate.getConverter().write(value, dbDoc); |
| 172 | + dbDoc.put(ENTITY_FIELD_CLASS, value.getClass().getName()); |
| 173 | + if (dbId != null) { |
| 174 | + dbDoc.put("_id", dbId.get("_id")); |
| 175 | + } |
| 176 | + mongoTemplate.execute(collName, new CollectionCallback<Object>() { |
| 177 | + public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 178 | + collection.save(dbDoc); |
| 179 | + return null; |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return 0L; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns the collection the given entity type shall be persisted to. |
| 190 | + * |
| 191 | + * @param entityClass must not be {@literal null}. |
| 192 | + * @return |
| 193 | + */ |
| 194 | + private String getCollectionNameForEntity(Class<? extends ChangeSetBacked> entityClass) { |
| 195 | + return mongoTemplate.getCollectionName(entityClass); |
| 196 | + } |
| 197 | +} |
0 commit comments