Search in sources :

Example 6 with Restriction

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Restriction(org.opennms.core.criteria.restrictions.Restriction) Criteria(org.opennms.core.criteria.Criteria) OnmsNotification(org.opennms.netmgt.model.OnmsNotification)

Example 7 with Restriction

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Order(org.opennms.core.criteria.Order) Set(java.util.Set) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) ArrayList(java.util.ArrayList) Criteria(org.opennms.core.criteria.Criteria) Restriction(org.opennms.core.criteria.restrictions.Restriction) List(java.util.List) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) ArrayList(java.util.ArrayList) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with Restriction

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:
        }
    }
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Restriction(org.opennms.core.criteria.restrictions.Restriction) PrimitiveStatement(org.apache.cxf.jaxrs.ext.search.PrimitiveStatement)

Example 9 with Restriction

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);
    }
}
Also used : BetweenRestriction(org.opennms.core.criteria.restrictions.BetweenRestriction) NullRestriction(org.opennms.core.criteria.restrictions.NullRestriction) AllRestriction(org.opennms.core.criteria.restrictions.AllRestriction) GeRestriction(org.opennms.core.criteria.restrictions.GeRestriction) AnyRestriction(org.opennms.core.criteria.restrictions.AnyRestriction) IlikeRestriction(org.opennms.core.criteria.restrictions.IlikeRestriction) InRestriction(org.opennms.core.criteria.restrictions.InRestriction) NotNullRestriction(org.opennms.core.criteria.restrictions.NotNullRestriction) Restriction(org.opennms.core.criteria.restrictions.Restriction) SqlRestriction(org.opennms.core.criteria.restrictions.SqlRestriction) GtRestriction(org.opennms.core.criteria.restrictions.GtRestriction) IplikeRestriction(org.opennms.core.criteria.restrictions.IplikeRestriction) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) NeRestriction(org.opennms.core.criteria.restrictions.NeRestriction) LikeRestriction(org.opennms.core.criteria.restrictions.LikeRestriction) LeRestriction(org.opennms.core.criteria.restrictions.LeRestriction) NotRestriction(org.opennms.core.criteria.restrictions.NotRestriction) AttributeRestriction(org.opennms.core.criteria.restrictions.AttributeRestriction) LtRestriction(org.opennms.core.criteria.restrictions.LtRestriction)

Aggregations

Restriction (org.opennms.core.criteria.restrictions.Restriction)9 ArrayList (java.util.ArrayList)3 Criteria (org.opennms.core.criteria.Criteria)3 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)3 List (java.util.List)2 InRestriction (org.opennms.core.criteria.restrictions.InRestriction)2 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)2 Component (com.vaadin.ui.Component)1 UI (com.vaadin.ui.UI)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 PrimitiveStatement (org.apache.cxf.jaxrs.ext.search.PrimitiveStatement)1 Test (org.junit.Test)1 Order (org.opennms.core.criteria.Order)1 AllRestriction (org.opennms.core.criteria.restrictions.AllRestriction)1 AnyRestriction (org.opennms.core.criteria.restrictions.AnyRestriction)1