use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.
the class MongoPlatform method buildInteractionSpec.
/**
* Allow the platform to build the interaction spec based on properties defined in the interaction.
*/
@Override
public InteractionSpec buildInteractionSpec(EISInteraction interaction) {
InteractionSpec spec = interaction.getInteractionSpec();
if (spec == null) {
MongoInteractionSpec mongoSpec = new MongoInteractionSpec();
Object operation = interaction.getProperty(OPERATION);
if (interaction.isQueryStringCall()) {
mongoSpec.setCode(((QueryStringCall) interaction).getQueryString());
operation = MongoOperation.EVAL;
}
if (operation == null) {
throw new EISException("'" + OPERATION + "' property must be set on the query's interation.");
}
if (operation instanceof String) {
operation = MongoOperation.valueOf((String) operation);
}
mongoSpec.setOperation((MongoOperation) operation);
Object collection = interaction.getProperty(COLLECTION);
if (collection != null) {
mongoSpec.setCollection((String) collection);
}
// Allows setting of read preference as a property.
Object preference = interaction.getProperty(READ_PREFERENCE);
if (preference instanceof ReadPreference) {
mongoSpec.setReadPreference((ReadPreference) preference);
} else if (preference instanceof String) {
String constant = (String) preference;
if (constant.equals("PRIMARY")) {
mongoSpec.setReadPreference(ReadPreference.primary());
} else if (constant.equals("SECONDARY")) {
mongoSpec.setReadPreference(ReadPreference.secondary());
} else {
throw new EISException("Invalid read preference property value: " + constant);
}
}
// Allows setting of write concern as a property.
Object concern = interaction.getProperty(WRITE_CONCERN);
if (concern instanceof WriteConcern) {
mongoSpec.setWriteConcern((WriteConcern) concern);
} else if (concern instanceof String) {
String constant = (String) concern;
if (constant.equals("FSYNC_SAFE")) {
mongoSpec.setWriteConcern(WriteConcern.FSYNC_SAFE);
} else if (constant.equals("JOURNAL_SAFE")) {
mongoSpec.setWriteConcern(WriteConcern.JOURNAL_SAFE);
} else if (constant.equals("MAJORITY")) {
mongoSpec.setWriteConcern(WriteConcern.MAJORITY);
} else if (constant.equals("NONE")) {
mongoSpec.setWriteConcern(/* WriteConcern.NONE */
new WriteConcern("none"));
} else if (constant.equals("NORMAL")) {
mongoSpec.setWriteConcern(WriteConcern.NORMAL);
} else if (constant.equals("REPLICAS_SAFE")) {
mongoSpec.setWriteConcern(WriteConcern.REPLICAS_SAFE);
} else if (constant.equals("SAFE")) {
mongoSpec.setWriteConcern(WriteConcern.SAFE);
} else {
throw new EISException("Invalid read preference property value: " + constant);
}
}
// Allows setting of options as a property.
Object options = interaction.getProperty(OPTIONS);
if (options instanceof Number) {
mongoSpec.setOptions(((Number) options).intValue());
} else if (options instanceof String) {
mongoSpec.setOptions(Integer.parseInt(((String) options)));
}
// Allows setting of skip as a property.
Object skip = interaction.getProperty(SKIP);
if (skip instanceof Number) {
mongoSpec.setSkip(((Number) skip).intValue());
} else if (skip instanceof String) {
mongoSpec.setSkip(Integer.parseInt(((String) skip)));
}
// Allows setting of limit as a property.
Object limit = interaction.getProperty(LIMIT);
if (limit instanceof Number) {
mongoSpec.setLimit(((Number) limit).intValue());
} else if (skip instanceof String) {
mongoSpec.setLimit(Integer.parseInt(((String) limit)));
}
// Allows setting of batchSize as a property.
Object batchSize = interaction.getProperty(BATCH_SIZE);
if (batchSize instanceof Number) {
mongoSpec.setBatchSize(((Number) batchSize).intValue());
} else if (skip instanceof String) {
mongoSpec.setBatchSize(Integer.parseInt(((String) batchSize)));
}
spec = mongoSpec;
}
return spec;
}
use of org.eclipse.persistence.eis.EISException in project eclipselink by eclipse-ee4j.
the class MongoPlatform method appendExpressionToQueryRow.
/**
* Append the expression and recursively to the query row.
*/
protected void appendExpressionToQueryRow(Expression expression, AbstractRecord row, DatabaseQuery query) {
if (expression.isRelationExpression()) {
RelationExpression relation = (RelationExpression) expression;
Object left = extractValueFromExpression(relation.getFirstChild(), query);
Object right = extractValueFromExpression(relation.getSecondChild(), query);
if (relation.getOperator().getSelector() == ExpressionOperator.Equal) {
row.put(left, right);
} else {
DatabaseRecord nested = new DatabaseRecord();
if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThan) {
nested.put("$gt", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.LessThan) {
nested.put("$lt", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.LessThanEqual) {
nested.put("$lte", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.GreaterThanEqual) {
nested.put("$gte", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.NotEqual) {
nested.put("$ne", right);
} else if (relation.getOperator().getSelector() == ExpressionOperator.In) {
nested.put("$in", right);
row.put(left, nested);
} else if (relation.getOperator().getSelector() == ExpressionOperator.NotIn) {
nested.put("$nin", right);
row.put(left, nested);
} else {
throw new EISException("Query too complex for Mongo translation, relation [" + expression + "] not supported in query: " + query);
}
row.put(left, nested);
}
} else if (expression.isLogicalExpression()) {
LogicalExpression logic = (LogicalExpression) expression;
DatabaseRecord first = new DatabaseRecord();
DatabaseRecord second = new DatabaseRecord();
appendExpressionToQueryRow(logic.getFirstChild(), first, query);
appendExpressionToQueryRow(logic.getSecondChild(), second, query);
List<DatabaseRecord> nested = new Vector<>();
nested.add(first);
nested.add(second);
if (logic.getOperator().getSelector() == ExpressionOperator.And) {
row.put("$and", nested);
} else if (logic.getOperator().getSelector() == ExpressionOperator.Or) {
row.put("$or", nested);
} else {
throw new EISException("Query too complex for Mongo translation, logic [" + expression + "] not supported in query: " + query);
}
} else if (expression.isFunctionExpression()) {
FunctionExpression function = (FunctionExpression) expression;
if (function.getOperator().getSelector() == ExpressionOperator.Like) {
Object left = extractValueFromExpression(function.getChildren().get(0), query);
Object right = extractValueFromExpression(function.getChildren().get(1), query);
if (!(right instanceof String)) {
throw new EISException("Query too complex for Mongo translation, like with [" + right + "] not supported in query: " + query);
}
String pattern = (String) right;
DatabaseRecord nested = new DatabaseRecord();
if (!this.isLikeRegex) {
pattern = Helper.convertLikeToRegex(pattern);
}
nested.put("$regex", pattern);
row.put(left, nested);
} else if (function.getOperator().getSelector() == ExpressionOperator.Not) {
// Detect situation 'not(a = b)' and change it to '(a != b)'
Expression expr = function.getChildren().get(0);
if (expr.isRelationExpression()) {
RelationExpression relation = (RelationExpression) expr;
Object left = extractValueFromExpression(relation.getFirstChild(), query);
Object right = extractValueFromExpression(relation.getSecondChild(), query);
DatabaseRecord nested = new DatabaseRecord();
if (expr.getOperator().getSelector() == ExpressionOperator.Equal) {
nested.put("$ne", right);
} else {
nested.put("not", right);
}
row.put(left, nested);
} else {
throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
}
} else {
throw new EISException("Query too complex for Mongo translation, function [" + expression + "] not supported in query: " + query);
}
} else {
throw new EISException("Query too complex for Mongo translation, expression [" + expression + "] not supported in query: " + query);
}
}
Aggregations