added Enum support for Update and Query

This commit is contained in:
Thomas Risberg
2011-02-14 16:58:02 -05:00
parent e12ac23131
commit 202837e349
2 changed files with 32 additions and 9 deletions

View File

@@ -132,7 +132,7 @@ public class Criteria implements CriteriaDefinition {
for (String k : this.criteria.keySet()) { for (String k : this.criteria.keySet()) {
if (not) { if (not) {
DBObject notDbo = new BasicDBObject(); DBObject notDbo = new BasicDBObject();
notDbo.put(k, this.criteria.get(k)); notDbo.put(k, convertValueIfNecessary(this.criteria.get(k)));
dbo.put("$not", notDbo); dbo.put("$not", notDbo);
not = false; not = false;
} }
@@ -141,13 +141,13 @@ public class Criteria implements CriteriaDefinition {
not = true; not = true;
} }
else { else {
dbo.put(k, this.criteria.get(k)); dbo.put(k, convertValueIfNecessary(this.criteria.get(k)));
} }
} }
} }
DBObject queryCriteria = new BasicDBObject(); DBObject queryCriteria = new BasicDBObject();
if (isValue != null) { if (isValue != null) {
queryCriteria.put(this.key, this.isValue); queryCriteria.put(this.key, convertValueIfNecessary(this.isValue));
queryCriteria.putAll(dbo); queryCriteria.putAll(dbo);
} }
else { else {
@@ -156,4 +156,11 @@ public class Criteria implements CriteriaDefinition {
return queryCriteria; return queryCriteria;
} }
private Object convertValueIfNecessary(Object value) {
if (value instanceof Enum) {
return ((Enum<?>)value).name();
}
return value;
}
} }

View File

@@ -31,7 +31,7 @@ public class Update {
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
public Update set(String key, Object value) { public Update set(String key, Object value) {
criteria.put("$set", Collections.singletonMap(key, value)); criteria.put("$set", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@@ -46,19 +46,23 @@ public class Update {
} }
public Update push(String key, Object value) { public Update push(String key, Object value) {
criteria.put("$push", Collections.singletonMap(key, value)); criteria.put("$push", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
public Update pushAll(String key, Object[] values) { 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(); DBObject keyValue = new BasicDBObject();
keyValue.put(key, values); keyValue.put(key, convertedValues);
criteria.put("$pushAll", keyValue); criteria.put("$pushAll", keyValue);
return this; return this;
} }
public Update addToSet(String key, Object value) { public Update addToSet(String key, Object value) {
criteria.put("$addToSet", Collections.singletonMap(key, value)); criteria.put("$addToSet", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
@@ -68,13 +72,17 @@ public class Update {
} }
public Update pull(String key, Object value) { public Update pull(String key, Object value) {
criteria.put("$pull", Collections.singletonMap(key, value)); criteria.put("$pull", Collections.singletonMap(key, convertValueIfNecessary(value)));
return this; return this;
} }
public Update pullAll(String key, Object[] values) { 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(); DBObject keyValue = new BasicDBObject();
keyValue.put(key, values); keyValue.put(key, convertedValues);
criteria.put("$pullAll", keyValue); criteria.put("$pullAll", keyValue);
return this; return this;
} }
@@ -92,4 +100,12 @@ public class Update {
return dbo; return dbo;
} }
private Object convertValueIfNecessary(Object value) {
System.out.println("-> " + value);
if (value instanceof Enum) {
return ((Enum<?>)value).name();
}
return value;
}
} }