Search in sources :

Example 1 with Disjunction

use of org.hibernate.criterion.Disjunction in project opennms by OpenNMS.

the class DefaultNodeListService method addCriteriaForMaclike.

private static void addCriteriaForMaclike(final OnmsCriteria criteria, final String macLike) {
    final String macLikeStripped = macLike.replaceAll("[:-]", "");
    criteria.createAlias("node.snmpInterfaces", "snmpInterface", OnmsCriteria.LEFT_JOIN);
    final Disjunction physAddrDisjunction = Restrictions.disjunction();
    physAddrDisjunction.add(Restrictions.ilike("snmpInterface.physAddr", macLikeStripped, MatchMode.ANYWHERE));
    criteria.add(physAddrDisjunction);
// This is an alternative to the above code if we need to use the out-of-the-box DetachedCriteria which doesn't let us specify the join type 
/*
        String propertyName = "nodeId";
        String value = "%" + macLikeStripped + "%";
        
        Disjunction physAddrDisjuction = Restrictions.disjunction();
        physAddrDisjuction.add(Restrictions.sqlRestriction("{alias}." + propertyName + " IN (SELECT nodeid FROM snmpinterface WHERE snmpphysaddr LIKE ? )", value, new StringType()));
        criteria.add(physAddrDisjuction);
        */
}
Also used : Disjunction(org.hibernate.criterion.Disjunction)

Example 2 with Disjunction

use of org.hibernate.criterion.Disjunction in project opennms by OpenNMS.

the class DefaultRtcService method getNodeListForCriteria.

/** {@inheritDoc} */
@Override
public RtcNodeModel getNodeListForCriteria(OnmsCriteria serviceCriteria, OnmsCriteria outageCriteria) {
    serviceCriteria.addOrder(Order.asc("node.label"));
    serviceCriteria.addOrder(Order.asc("node.id"));
    serviceCriteria.addOrder(Order.asc("ipInterface.ipAddress"));
    serviceCriteria.addOrder(Order.asc("serviceType.name"));
    Date periodEnd = new Date(System.currentTimeMillis());
    Date periodStart = new Date(periodEnd.getTime() - (24 * 60 * 60 * 1000));
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(Restrictions.isNull("ifRegainedService"));
    disjunction.add(Restrictions.ge("ifLostService", periodStart));
    disjunction.add(Restrictions.ge("ifRegainedService", periodStart));
    outageCriteria.add(disjunction);
    outageCriteria.addOrder(Order.asc("monitoredService"));
    outageCriteria.addOrder(Order.asc("ifLostService"));
    List<OnmsMonitoredService> services = m_monitoredServiceDao.findMatching(serviceCriteria);
    List<OnmsOutage> outages = m_outageDao.findMatching(outageCriteria);
    Map<OnmsMonitoredService, Long> serviceDownTime = calculateServiceDownTime(periodEnd, periodStart, outages);
    RtcNodeModel model = new RtcNodeModel();
    OnmsNode lastNode = null;
    int serviceCount = 0;
    int serviceDownCount = 0;
    long downMillisCount = 0;
    for (OnmsMonitoredService service : services) {
        if (!service.getIpInterface().getNode().equals(lastNode) && lastNode != null) {
            Double availability = calculateAvailability(serviceCount, downMillisCount, periodEnd.getTime() - periodStart.getTime());
            model.addNode(new RtcNode(lastNode, serviceCount, serviceDownCount, availability));
            serviceCount = 0;
            serviceDownCount = 0;
            downMillisCount = 0;
        }
        serviceCount++;
        if (service.isDown()) {
            serviceDownCount++;
        }
        Long downMillis = serviceDownTime.get(service);
        if (downMillis == null) {
        // This service had 100% availability over the period, no downtime
        } else {
            downMillisCount += downMillis;
        }
        lastNode = service.getIpInterface().getNode();
    }
    if (lastNode != null) {
        Double availability = calculateAvailability(serviceCount, downMillisCount, periodEnd.getTime() - periodStart.getTime());
        model.addNode(new RtcNode(lastNode, serviceCount, serviceDownCount, availability));
    }
    return model;
}
Also used : OnmsOutage(org.opennms.netmgt.model.OnmsOutage) RtcNodeModel(org.opennms.web.svclayer.model.RtcNodeModel) OnmsNode(org.opennms.netmgt.model.OnmsNode) RtcNode(org.opennms.web.svclayer.model.RtcNode) Date(java.util.Date) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Disjunction(org.hibernate.criterion.Disjunction)

Example 3 with Disjunction

use of org.hibernate.criterion.Disjunction in project hibernate-orm by hibernate.

the class CompositeIdTest method testQueryInAndComposite.

@Test
public void testQueryInAndComposite() {
    Session s = openSession();
    Transaction transaction = s.beginTransaction();
    createData(s);
    s.flush();
    List ids = new ArrayList<SomeEntityId>(2);
    ids.add(new SomeEntityId(1, 12));
    ids.add(new SomeEntityId(10, 23));
    Criteria criteria = s.createCriteria(SomeEntity.class);
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(Restrictions.in("id", ids));
    criteria.add(disjunction);
    List list = criteria.list();
    assertEquals(2, list.size());
    transaction.rollback();
    s.close();
}
Also used : Disjunction(org.hibernate.criterion.Disjunction) Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with Disjunction

use of org.hibernate.criterion.Disjunction in project dhis2-core by dhis2.

the class HibernateGenericStore method getSharingDetachedCriteria.

private DetachedCriteria getSharingDetachedCriteria(UserInfo user, String access) {
    DetachedCriteria criteria = DetachedCriteria.forClass(getClazz(), "c");
    preProcessDetachedCriteria(criteria);
    if (!sharingEnabled(user) || user == null) {
        return criteria;
    }
    Assert.notNull(user, "User argument can't be null.");
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(Restrictions.like("c.publicAccess", access));
    disjunction.add(Restrictions.isNull("c.publicAccess"));
    disjunction.add(Restrictions.isNull("c.user.id"));
    disjunction.add(Restrictions.eq("c.user.id", user.getId()));
    DetachedCriteria userGroupDetachedCriteria = DetachedCriteria.forClass(getClazz(), "ugdc");
    userGroupDetachedCriteria.createCriteria("ugdc.userGroupAccesses", "uga");
    userGroupDetachedCriteria.createCriteria("uga.userGroup", "ug");
    userGroupDetachedCriteria.createCriteria("ug.members", "ugm");
    userGroupDetachedCriteria.add(Restrictions.eqProperty("ugdc.id", "c.id"));
    userGroupDetachedCriteria.add(Restrictions.eq("ugm.id", user.getId()));
    userGroupDetachedCriteria.add(Restrictions.like("uga.access", access));
    userGroupDetachedCriteria.setProjection(Property.forName("uga.id"));
    disjunction.add(Subqueries.exists(userGroupDetachedCriteria));
    DetachedCriteria userDetachedCriteria = DetachedCriteria.forClass(getClazz(), "udc");
    userDetachedCriteria.createCriteria("udc.userAccesses", "ua");
    userDetachedCriteria.createCriteria("ua.user", "u");
    userDetachedCriteria.add(Restrictions.eqProperty("udc.id", "c.id"));
    userDetachedCriteria.add(Restrictions.eq("u.id", user.getId()));
    userDetachedCriteria.add(Restrictions.like("ua.access", access));
    userDetachedCriteria.setProjection(Property.forName("ua.id"));
    disjunction.add(Subqueries.exists(userDetachedCriteria));
    criteria.add(disjunction);
    return criteria;
}
Also used : Disjunction(org.hibernate.criterion.Disjunction) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 5 with Disjunction

use of org.hibernate.criterion.Disjunction in project dhis2-core by dhis2.

the class HibernateDeletedObjectStore method query.

@Override
@SuppressWarnings("unchecked")
public List<DeletedObject> query(DeletedObjectQuery query) {
    Criteria criteria = getCurrentSession().createCriteria(DeletedObject.class);
    if (query.getKlass().isEmpty()) {
        Disjunction disjunction = Restrictions.disjunction();
        if (!query.getUid().isEmpty()) {
            disjunction.add(Restrictions.in("uid", query.getUid()));
        }
        if (!query.getCode().isEmpty()) {
            disjunction.add(Restrictions.in("code", query.getCode()));
        }
        criteria.add(disjunction);
    } else if (query.getUid().isEmpty() && query.getCode().isEmpty()) {
        criteria.add(Restrictions.in("klass", query.getKlass()));
    } else {
        Disjunction disjunction = Restrictions.disjunction();
        if (!query.getUid().isEmpty()) {
            Conjunction conjunction = Restrictions.conjunction();
            conjunction.add(Restrictions.in("klass", query.getKlass()));
            conjunction.add(Restrictions.in("uid", query.getUid()));
            disjunction.add(conjunction);
        }
        if (!query.getCode().isEmpty()) {
            Conjunction conjunction = Restrictions.conjunction();
            conjunction.add(Restrictions.in("klass", query.getKlass()));
            conjunction.add(Restrictions.in("code", query.getCode()));
            disjunction.add(conjunction);
        }
        criteria.add(disjunction);
    }
    if (query.getDeletedAt() != null) {
        criteria.add(Restrictions.ge("deletedAt", query.getDeletedAt()));
    }
    if (!query.isSkipPaging()) {
        Pager pager = query.getPager();
        criteria.setFirstResult(pager.getOffset());
        criteria.setMaxResults(pager.getPageSize());
    }
    return criteria.list();
}
Also used : Disjunction(org.hibernate.criterion.Disjunction) Pager(org.hisp.dhis.common.Pager) Conjunction(org.hibernate.criterion.Conjunction) Criteria(org.hibernate.Criteria)

Aggregations

Disjunction (org.hibernate.criterion.Disjunction)5 Criteria (org.hibernate.Criteria)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 Conjunction (org.hibernate.criterion.Conjunction)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 Pager (org.hisp.dhis.common.Pager)1 Test (org.junit.Test)1 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)1 RtcNode (org.opennms.web.svclayer.model.RtcNode)1 RtcNodeModel (org.opennms.web.svclayer.model.RtcNodeModel)1