use of org.apache.cxf.jaxrs.ext.search.PrimitiveStatement in project opennms by OpenNMS.
the class CriteriaBuilderSearchVisitor method visit.
@Override
public void visit(SearchCondition<Q> sc) {
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
String name = getRealPropertyName(statement.getProperty());
// TODO: Figure out how to use validators at some point
// validatePropertyValue(name, originalValue);
// Introspect the property type
ClassValue clsValue = getPrimitiveFieldClass(statement, name, statement.getValue().getClass(), statement.getValueType(), statement.getValue());
// If the property value is a String
boolean isWildcard = false;
if (String.class.equals(clsValue.getCls())) {
// And if it's a FIQL wildcard
if (SearchUtils.containsWildcard((String) clsValue.getValue())) {
// Then mark it as a wildcard and replace the * wildcards with % wildcards
isWildcard = true;
clsValue.setValue(SearchUtils.toSqlWildcardString((String) clsValue.getValue(), false));
}
}
final Object value;
// Check to see if we have any criteria behaviors for this search term
if (m_criteriaBehaviors != null && m_criteriaBehaviors.containsKey(name)) {
// TODO: Change CriteriaBehaviors so that they can remap prefixes
// so that we don't have to put every joined property into m_criteriaMapping
CriteriaBehavior<?> behavior = m_criteriaBehaviors.get(name);
// Convert the query bean property name to the Criteria property name
// if necessary
name = behavior.getPropertyName() == null ? name : behavior.getPropertyName();
// If we're using CriteriaBehaviors, assume that the value is a String
// and convert it to the value that will be used in the Criteria
value = NULL_VALUE.equals((String) clsValue.getValue()) ? null : behavior.convert((String) clsValue.getValue());
// Execute any beforeVisit() actions for this query term such as adding
// additional JOIN aliases
behavior.beforeVisit(m_criteriaBuilder, value, sc.getConditionType(), isWildcard);
// If the behavior indicates that we should skip this search term, then return
if (behavior.shouldSkipProperty(sc.getConditionType(), isWildcard)) {
return;
}
} else {
value = clsValue.getValue();
}
switch(sc.getConditionType()) {
case EQUALS:
if (isWildcard) {
m_criteriaBuilder.like(name, value);
} else {
if (value == null || NULL_VALUE.equals(value) || NULL_DATE_VALUE.equals(value)) {
m_criteriaBuilder.isNull(name);
} else {
m_criteriaBuilder.eq(name, value);
}
}
break;
case NOT_EQUALS:
if (isWildcard) {
m_criteriaBuilder.not().like(name, value);
} else {
if (value == null || NULL_VALUE.equals(value) || NULL_DATE_VALUE.equals(value)) {
m_criteriaBuilder.isNotNull(name);
} else {
// Match any rows that do not match the value or are null
m_criteriaBuilder.or(Restrictions.ne(name, value), Restrictions.isNull(name));
}
}
break;
case LESS_THAN:
// TODO: Check for null?
m_criteriaBuilder.lt(name, value);
break;
case GREATER_THAN:
// TODO: Check for null?
m_criteriaBuilder.gt(name, value);
break;
case LESS_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.le(name, value);
break;
case GREATER_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.ge(name, value);
break;
case OR:
case AND:
case CUSTOM:
default:
}
}
} else {
List<Restriction> subRestrictions = new ArrayList<>();
for (SearchCondition<Q> condition : sc.getSearchConditions()) {
// Create a new CriteriaBuilder
CriteriaBuilder builder = null;
try {
// Try to use the same class as the outside CriteriaBuilder
builder = m_criteriaBuilder.getClass().getConstructor(Class.class).newInstance(m_class);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
LOG.warn("Could not create " + m_criteriaBuilder.getClass().getSimpleName() + "; falling back to CriteriaBuilder: " + e.getClass().getSimpleName() + ": " + e.getMessage());
builder = new CriteriaBuilder(m_class);
}
// Create a new visitor for the SearchCondition
CriteriaBuilderSearchVisitor<T, Q> newVisitor = new CriteriaBuilderSearchVisitor<T, Q>(builder, m_class, m_criteriaBehaviors);
// Visit the children
condition.accept(newVisitor);
Criteria newCriteria = newVisitor.getQuery().toCriteria();
// Add any aliases from the subcriteria
Collection<Alias> aliases = newCriteria.getAliases();
if (aliases != null) {
for (Alias alias : aliases) {
m_criteriaBuilder.alias(alias);
}
}
// Fetch the rendered restrictions
Collection<Restriction> restrictions = newCriteria.getRestrictions();
// If there are restrictions...
if (restrictions != null && restrictions.size() > 0) {
final Restriction subRestriction;
// If there are multiple restrictions...
if (restrictions.size() > 1) {
// Wrap them in an AND restriction
subRestriction = Restrictions.all(restrictions);
} else {
subRestriction = restrictions.iterator().next();
}
LOG.info(subRestriction.toString());
subRestrictions.add(subRestriction);
}
}
switch(sc.getConditionType()) {
case OR:
LOG.info("OR criteria");
// .or() with current Criteria
m_criteriaBuilder.or(subRestrictions.toArray(new Restriction[0]));
break;
case AND:
LOG.info("AND criteria");
// .and() with current Criteria
m_criteriaBuilder.and(subRestrictions.toArray(new Restriction[0]));
break;
default:
}
}
}
use of org.apache.cxf.jaxrs.ext.search.PrimitiveStatement in project cxf by apache.
the class HBaseQueryVisitor method visit.
public void visit(SearchCondition<T> sc) {
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
queryStack.peek().add(buildSimpleQuery(sc.getConditionType(), statement.getProperty(), statement.getValue()));
}
} else {
queryStack.push(new ArrayList<>());
for (SearchCondition<T> condition : sc.getSearchConditions()) {
condition.accept(this);
}
boolean orCondition = sc.getConditionType() == ConditionType.OR;
List<Filter> queries = queryStack.pop();
queryStack.peek().add(createCompositeQuery(queries, orCondition));
}
}
use of org.apache.cxf.jaxrs.ext.search.PrimitiveStatement in project cxf by apache.
the class LdapQueryVisitor method visit.
public void visit(SearchCondition<T> sc) {
StringBuilder sb = getStringBuilder();
if (sb == null) {
sb = new StringBuilder();
}
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
String name = getRealPropertyName(statement.getProperty());
String rvalStr = getPropertyValue(name, statement.getValue());
validatePropertyValue(name, rvalStr);
sb.append('(');
if (sc.getConditionType() == ConditionType.NOT_EQUALS) {
sb.append('!');
}
String ldapOperator = conditionTypeToLdapOperator(sc.getConditionType());
String encodedRValStr = encodeQueryValues ? Util.doRFC2254Encoding(rvalStr) : rvalStr;
sb.append(name).append(ldapOperator).append(encodedRValStr);
sb.append(')');
}
} else {
sb.append('(');
if (sc.getConditionType() == ConditionType.AND) {
sb.append('&');
} else {
sb.append('|');
}
for (SearchCondition<T> condition : sc.getSearchConditions()) {
saveStringBuilder(sb);
condition.accept(this);
sb = getStringBuilder();
}
sb.append(')');
}
saveStringBuilder(sb);
}
use of org.apache.cxf.jaxrs.ext.search.PrimitiveStatement in project cxf by apache.
the class SQLPrinterVisitor method visit.
public void visit(SearchCondition<T> sc) {
StringBuilder sb = getStringBuilder();
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
String property = statement.getProperty();
String[] properties = property.split("\\.");
if (properties.length > 2) {
throw new SearchParseException("SQL Visitor supports only a single JOIN");
} else if (properties.length == 2) {
if (joinDone) {
throw new SearchParseException("SQL Visitor has already created JOIN");
}
joinDone = true;
String joinTable = getRealPropertyName(properties[0]);
// Joining key can be pre-configured
String joiningKey = primaryTable;
if (joiningKey.endsWith("s")) {
joiningKey = joiningKey.substring(0, joiningKey.length() - 1);
}
joiningKey += "_id";
topBuilder.append(" left join ").append(joinTable);
topBuilder.append(" on ").append(primaryTable).append(".id").append(" = ").append(joinTable).append('.').append(joiningKey);
property = joinTable + "." + getRealPropertyName(properties[1]);
}
String name = getRealPropertyName(property);
String originalValue = getPropertyValue(name, statement.getValue());
validatePropertyValue(name, originalValue);
String value = SearchUtils.toSqlWildcardString(originalValue, isWildcardStringMatch());
value = SearchUtils.duplicateSingleQuoteIfNeeded(value);
if (tableAlias != null) {
name = tableAlias + "." + name;
}
sb.append(name).append(' ').append(SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(), value, originalValue)).append(' ').append("'").append(value).append(// NOPMD
"'");
}
} else {
boolean first = true;
for (SearchCondition<T> condition : sc.getSearchConditions()) {
if (!first) {
sb.append(' ').append(sc.getConditionType().toString()).append(' ');
} else {
first = false;
}
sb.append('(');
saveStringBuilder(sb);
condition.accept(this);
sb = getStringBuilder();
sb.append(')');
}
}
saveStringBuilder(sb);
}
use of org.apache.cxf.jaxrs.ext.search.PrimitiveStatement in project opennms by OpenNMS.
the class CriteriaBuilderSearchVisitor method visit.
@Override
public void visit(SearchCondition<T> sc) {
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
String name = getRealPropertyName(statement.getProperty());
// TODO: Figure out how to use validators at some point
//validatePropertyValue(name, originalValue);
// Introspect the property type
ClassValue clsValue = getPrimitiveFieldClass(statement, name, statement.getValue().getClass(), statement.getValueType(), statement.getValue());
// If the property value is a String
boolean isWildcard = false;
if (String.class.equals(clsValue.getCls())) {
// And if it's a FIQL wildcard
if (SearchUtils.containsWildcard((String) clsValue.getValue())) {
// Then mark it as a wildcard and replace the * wildcards with % wildcards
isWildcard = true;
clsValue.setValue(SearchUtils.toSqlWildcardString((String) clsValue.getValue(), isWildcardStringMatch()));
}
}
switch(sc.getConditionType()) {
case EQUALS:
if (isWildcard) {
m_criteriaBuilder.like(name, clsValue.getValue());
} else {
if (clsValue.getValue() == null || NULL_VALUE.equals(clsValue.getValue()) || NULL_DATE_VALUE.equals(clsValue.getValue())) {
m_criteriaBuilder.isNull(name);
} else {
m_criteriaBuilder.eq(name, clsValue.getValue());
}
}
break;
case NOT_EQUALS:
if (isWildcard) {
m_criteriaBuilder.not().like(name, clsValue.getValue());
} else {
if (clsValue.getValue() == null || NULL_VALUE.equals(clsValue.getValue()) || NULL_DATE_VALUE.equals(clsValue.getValue())) {
m_criteriaBuilder.isNotNull(name);
} else {
// Match any rows that do not match the value or are null
m_criteriaBuilder.or(Restrictions.ne(name, clsValue.getValue()), Restrictions.isNull(name));
}
}
break;
case LESS_THAN:
// TODO: Check for null?
m_criteriaBuilder.lt(name, clsValue.getValue());
break;
case GREATER_THAN:
// TODO: Check for null?
m_criteriaBuilder.gt(name, clsValue.getValue());
break;
case LESS_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.le(name, clsValue.getValue());
break;
case GREATER_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.ge(name, clsValue.getValue());
break;
case OR:
case AND:
case CUSTOM:
default:
}
}
} else {
List<Restriction> subRestrictions = new ArrayList<Restriction>();
for (SearchCondition<T> condition : sc.getSearchConditions()) {
// Create a new CriteriaBuilder
CriteriaBuilder builder = null;
try {
// Try to use the same class as the outside CriteriaBuilder
builder = m_criteriaBuilder.getClass().getConstructor(Class.class).newInstance(m_class);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
LOG.warn("Could not create " + m_criteriaBuilder.getClass().getSimpleName() + "; falling back to CriteriaBuilder: " + e.getClass().getSimpleName() + ": " + e.getMessage());
builder = new CriteriaBuilder(m_class);
}
// Create a new visitor for the SearchCondition
CriteriaBuilderSearchVisitor<T> newVisitor = new CriteriaBuilderSearchVisitor<T>(builder, m_class);
// Visit the children
condition.accept(newVisitor);
// Fetch the rendered restrictions
Collection<Restriction> restrictions = newVisitor.getQuery().toCriteria().getRestrictions();
// If there are restrictions...
if (restrictions != null && restrictions.size() > 0) {
final Restriction subRestriction;
// If there are multiple restrictions...
if (restrictions.size() > 1) {
// Wrap them in an AND restriction
subRestriction = Restrictions.all(restrictions);
} else {
subRestriction = restrictions.iterator().next();
}
LOG.info(subRestriction.toString());
subRestrictions.add(subRestriction);
}
}
switch(sc.getConditionType()) {
case OR:
LOG.info("OR criteria");
// .or() with current Criteria
m_criteriaBuilder.or(subRestrictions.toArray(new Restriction[0]));
break;
case AND:
LOG.info("AND criteria");
// .and() with current Criteria
m_criteriaBuilder.and(subRestrictions.toArray(new Restriction[0]));
break;
default:
}
}
}
Aggregations