use of org.opennms.core.criteria.restrictions.Restriction in project opennms by OpenNMS.
the class DefaultSurveillanceViewService method getNotificationsWithCriterias.
/**
* Returns a list of notifications for a given list of nodes.
*
* @param rowCategories the row catgories
* @param colCategories the column categories
* @param customSeverity the custom severity mapping for notifications
* @param severity the severity for these nodes
* @param criterias the restrictions to use
* @return the list of notifications
*/
private List<OnmsNotification> getNotificationsWithCriterias(final Set<OnmsCategory> rowCategories, final Set<OnmsCategory> colCategories, final Map<OnmsNotification, String> customSeverity, final String severity, final Restriction... criterias) {
CriteriaBuilder criteriaBuilder = new CriteriaBuilder(OnmsNotification.class);
criteriaBuilder.alias("node", "node");
// Restrict on OnmsNotification.nodeId
criteriaBuilder.sql(createQuery("{alias}.nodeId", rowCategories, colCategories));
criteriaBuilder.ne("node.type", "D");
criteriaBuilder.orderBy("pageTime", false);
Criteria myCriteria = criteriaBuilder.toCriteria();
for (Restriction criteria : criterias) {
myCriteria.addRestriction(criteria);
}
List<OnmsNotification> notifications = m_notificationDao.findMatching(myCriteria);
for (OnmsNotification onmsNotification : notifications) {
customSeverity.put(onmsNotification, severity);
}
return notifications;
}
use of org.opennms.core.criteria.restrictions.Restriction in project opennms by OpenNMS.
the class NodeRestService method getNodes.
/**
* <p>getNodes</p>
*
* @return a {@link org.opennms.netmgt.model.OnmsNodeList} object.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
public OnmsNodeList getNodes(@Context final UriInfo uriInfo) {
final MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
final String type = params.getFirst("type");
final CriteriaBuilder builder = getCriteriaBuilder(params);
Criteria crit = null;
if (params.size() == 1 && params.getFirst("nodeId") != null && params.getFirst("nodeId").contains(",")) {
// we've been specifically asked for a list of nodes by ID
final List<Integer> nodeIds = new ArrayList<Integer>();
for (final String id : params.getFirst("nodeId").split(",")) {
nodeIds.add(Integer.valueOf(id));
}
crit = filterForNodeIds(builder, nodeIds).toCriteria();
} else if (params.getFirst("filterRule") != null) {
final Set<Integer> filteredNodeIds = m_filterDao.getNodeMap(params.getFirst("filterRule")).keySet();
if (filteredNodeIds.size() < 1) {
// The "in" criteria fails if the list of node ids is empty
final OnmsNodeList coll = new OnmsNodeList(Collections.emptyList());
coll.setTotalCount(0);
return coll;
}
// Apply the criteria without the filter rule
params.remove("filterRule");
final CriteriaBuilder filterRuleCriteriaBuilder = getCriteriaBuilder(params);
crit = filterForNodeIds(filterRuleCriteriaBuilder, filteredNodeIds).toCriteria();
} else {
applyQueryFilters(params, builder);
builder.orderBy("label").asc();
crit = builder.toCriteria();
if (type == null) {
final List<Restriction> restrictions = new ArrayList<Restriction>(crit.getRestrictions());
restrictions.add(Restrictions.ne("type", "D"));
crit.setRestrictions(restrictions);
}
}
final OnmsNodeList coll = new OnmsNodeList(m_nodeDao.findMatching(crit));
crit.setLimit(null);
crit.setOffset(null);
crit.setOrders(new ArrayList<Order>());
coll.setTotalCount(m_nodeDao.countMatching(crit));
return coll;
}
use of org.opennms.core.criteria.restrictions.Restriction 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:
}
}
}
use of org.opennms.core.criteria.restrictions.Restriction in project opennms by OpenNMS.
the class BeanWrapperRestrictionVisitor method visitAny.
@Override
public void visitAny(final AnyRestriction restriction) {
boolean matched = false;
for (final Restriction r : restriction.getRestrictions()) {
try {
r.visit(this);
matched = true;
break;
} catch (final Exception e) {
}
}
if (!matched) {
fail(restriction);
}
}
Aggregations