added Enum support for Update and Query
This commit is contained in:
@@ -132,7 +132,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
for (String k : this.criteria.keySet()) {
|
||||
if (not) {
|
||||
DBObject notDbo = new BasicDBObject();
|
||||
notDbo.put(k, this.criteria.get(k));
|
||||
notDbo.put(k, convertValueIfNecessary(this.criteria.get(k)));
|
||||
dbo.put("$not", notDbo);
|
||||
not = false;
|
||||
}
|
||||
@@ -141,13 +141,13 @@ public class Criteria implements CriteriaDefinition {
|
||||
not = true;
|
||||
}
|
||||
else {
|
||||
dbo.put(k, this.criteria.get(k));
|
||||
dbo.put(k, convertValueIfNecessary(this.criteria.get(k)));
|
||||
}
|
||||
}
|
||||
}
|
||||
DBObject queryCriteria = new BasicDBObject();
|
||||
if (isValue != null) {
|
||||
queryCriteria.put(this.key, this.isValue);
|
||||
queryCriteria.put(this.key, convertValueIfNecessary(this.isValue));
|
||||
queryCriteria.putAll(dbo);
|
||||
}
|
||||
else {
|
||||
@@ -155,5 +155,12 @@ public class Criteria implements CriteriaDefinition {
|
||||
}
|
||||
return queryCriteria;
|
||||
}
|
||||
|
||||
private Object convertValueIfNecessary(Object value) {
|
||||
if (value instanceof Enum) {
|
||||
return ((Enum<?>)value).name();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class Update {
|
||||
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
|
||||
|
||||
public Update set(String key, Object value) {
|
||||
criteria.put("$set", Collections.singletonMap(key, value));
|
||||
criteria.put("$set", Collections.singletonMap(key, convertValueIfNecessary(value)));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -46,19 +46,23 @@ public class Update {
|
||||
}
|
||||
|
||||
public Update push(String key, Object value) {
|
||||
criteria.put("$push", Collections.singletonMap(key, value));
|
||||
criteria.put("$push", Collections.singletonMap(key, convertValueIfNecessary(value)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Update pushAll(String key, Object[] values) {
|
||||
Object[] convertedValues = new Object[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
convertedValues[i] = convertValueIfNecessary(values[i]);
|
||||
}
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
keyValue.put(key, convertedValues);
|
||||
criteria.put("$pushAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Update addToSet(String key, Object value) {
|
||||
criteria.put("$addToSet", Collections.singletonMap(key, value));
|
||||
criteria.put("$addToSet", Collections.singletonMap(key, convertValueIfNecessary(value)));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -68,13 +72,17 @@ public class Update {
|
||||
}
|
||||
|
||||
public Update pull(String key, Object value) {
|
||||
criteria.put("$pull", Collections.singletonMap(key, value));
|
||||
criteria.put("$pull", Collections.singletonMap(key, convertValueIfNecessary(value)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Update pullAll(String key, Object[] values) {
|
||||
Object[] convertedValues = new Object[values.length];
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
convertedValues[i] = convertValueIfNecessary(values[i]);
|
||||
}
|
||||
DBObject keyValue = new BasicDBObject();
|
||||
keyValue.put(key, values);
|
||||
keyValue.put(key, convertedValues);
|
||||
criteria.put("$pullAll", keyValue);
|
||||
return this;
|
||||
}
|
||||
@@ -92,4 +100,12 @@ public class Update {
|
||||
return dbo;
|
||||
}
|
||||
|
||||
private Object convertValueIfNecessary(Object value) {
|
||||
System.out.println("-> " + value);
|
||||
if (value instanceof Enum) {
|
||||
return ((Enum<?>)value).name();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user