public
Created

  • Download Gist
User.java
Java
1 2 3 4 5 6 7 8 9 10 11 12
@XmlRootElement
@Document(collection = "user")
@PersistenceCapable
@View
public class User {
 
@Id
@JsonIgnore(true)
private String id;
private String username;
 
}
UserAuth.java
Java
1 2 3 4 5 6 7 8
@XmlRootElement
@Document(collection = "user")
@PersistenceCapable
public class UserAuth extends User {
 
private String password;
 
}
UserContact.java
Java
1 2 3 4 5 6 7 8 9 10
@XmlRootElement
@Document(collection = "user")
@PersistenceCapable
public class UserContact extends User {
 
private String email;
private String phone;
private String address;
 
}
View.java
Java
1 2 3 4 5 6
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface View {
 
}
ViewMongoTemplate.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
 
import java.util.Map;
 
 
public class ViewMongoTemplate extends MongoTemplate {
 
private static final String ID_FIELD = "_id";
private static final String CLASS_FIELD = "_class";
 
public ViewMongoTemplate(Mongo mongo, String databaseName) {
super(mongo, databaseName);
}
 
public ViewMongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
super(mongo, databaseName, userCredentials);
}
 
 
public ViewMongoTemplate(MongoDbFactory mongoDbFactory) {
super(mongoDbFactory);
}
 
public ViewMongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) {
super(mongoDbFactory, mongoConverter);
}
 
@Override
public void save(Object objectToSave, String collectionName) {
if (objectToSave.getClass().isAnnotationPresent(View.class)) {
doViewSave(collectionName, objectToSave, super.getConverter());
} else {
super.save(objectToSave, collectionName);
}
}
 
protected <T> void doViewSave(final String collectionName, final T objectToSave,
final MongoWriter<T> writer) {
final BasicDBObject dbDoc = new BasicDBObject();
 
maybeEmitEvent(new BeforeConvertEvent<T>(objectToSave));
writer.write(objectToSave, dbDoc);
 
// remove class field, views collection have many classes
dbDoc.remove(CLASS_FIELD);
 
final Object idValue = getDBObjectIdValue(dbDoc);
if (idValue == null) {
saveNewEntity(collectionName, objectToSave, dbDoc);
} else {
updateViewEntity(collectionName, objectToSave, dbDoc, idValue);
}
}
 
private <T> void updateViewEntity(String collectionName, T objectToSave, BasicDBObject dbDoc, Object idValue) {
final Query query = new Query().addCriteria(new Criteria(ID_FIELD).is(idValue));
final Update update = new Update();
for (Map.Entry<String, Object> entry : dbDoc.entrySet()) {
final String field = entry.getKey();
if (ID_FIELD.equals(field)) {
continue;
}
final Object value = entry.getValue();
if (value == null) {
continue;
}
update.set(field, value);
}
// may check result
doUpdate(collectionName, query, update, objectToSave.getClass(), true, false);
}
 
private <T> void saveNewEntity(String collectionName, T objectToSave, BasicDBObject dbDoc) {
maybeEmitEvent(new BeforeSaveEvent<T>(objectToSave, dbDoc));
final Object id = saveDBObject(collectionName, dbDoc, objectToSave.getClass());
 
populateIdIfNecessary(objectToSave, id);
maybeEmitEvent(new AfterSaveEvent<T>(objectToSave, dbDoc));
}
 
private Object getDBObjectIdValue(final DBObject dbDoc) {
return dbDoc.get(ID_FIELD);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.