Search in sources :

Example 1 with LikeRestriction

use of org.opennms.core.criteria.restrictions.LikeRestriction in project opennms by OpenNMS.

the class EventUtilDaoImpl method getHardwareFieldValue.

@Override
public String getHardwareFieldValue(String parm, long nodeId) {
    final Matcher matcher = HW_PARM_PATTERN.matcher(parm);
    if (!matcher.matches()) {
        LOG.warn("Unsupported hardware field parameter '{}'.", parm);
        return null;
    }
    final String hwFieldSelector = matcher.group(1);
    final String hwField = matcher.group(2).toLowerCase();
    // Retrieve the entity with a like query
    if (hwFieldSelector.startsWith("~")) {
        final String likeQuery = hwFieldSelector.substring(1);
        LOG.debug("Retrieving hardware field value {} on {} with like query {}", parm, nodeId, likeQuery);
        Criteria criteria = new Criteria(OnmsHwEntity.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", (int) nodeId)).addRestriction(new LikeRestriction("entPhysicalName", likeQuery)).setOrders(Arrays.asList(new Order[] { Order.desc("id") }));
        List<OnmsHwEntity> hwEntities = hwEntityDao.findMatching(criteria);
        System.err.println(hwEntities);
        if (hwEntities.size() < 1) {
            return null;
        }
        return getStringPropertyByName(hwField, hwEntities.get(0), hwEntityDescriptorsByName);
    }
    // Retrieve the entity by index if the select is an integer
    try {
        int index = Integer.parseInt(hwFieldSelector);
        OnmsHwEntity hwEntity = hwEntityDao.findEntityByIndex((int) nodeId, index);
        if (hwEntity == null) {
            // No entry with this index
            return null;
        }
        return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
    } catch (NumberFormatException e) {
    // pass
    }
    // Retrieve the entity by name
    OnmsHwEntity hwEntity = hwEntityDao.findEntityByName((int) nodeId, hwFieldSelector);
    if (hwEntity == null) {
        // No entry with this name
        return null;
    }
    return getStringPropertyByName(hwField, hwEntity, hwEntityDescriptorsByName);
}
Also used : Order(org.opennms.core.criteria.Order) OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) Matcher(java.util.regex.Matcher) Alias(org.opennms.core.criteria.Alias) LikeRestriction(org.opennms.core.criteria.restrictions.LikeRestriction) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria)

Aggregations

Matcher (java.util.regex.Matcher)1 Alias (org.opennms.core.criteria.Alias)1 Criteria (org.opennms.core.criteria.Criteria)1 Order (org.opennms.core.criteria.Order)1 EqRestriction (org.opennms.core.criteria.restrictions.EqRestriction)1 LikeRestriction (org.opennms.core.criteria.restrictions.LikeRestriction)1 OnmsHwEntity (org.opennms.netmgt.model.OnmsHwEntity)1