ActDAO extends AbstractJPADAO<Act, Long> { public int findNumberOfActByPeriodAndIp(Contract contract, Date limitDate, String ip) { Query query = entityManager.createQuery("SELECT count(a.id) FROM " + getEntityClass().getName() + " a" + " left join a.audit au" + " WHERE a.clientIp = :clientIp" + " AND a.contract = :contract" + " AND (a.status = :completedStatus OR a.status = :errorStatus)" + " AND a.endDate > :limitDate" + " AND au.status = :auditStatus"); query.setParameter("clientIp", ip); query.setParameter("contract", contract); query.setParameter("completedStatus", ActStatus.COMPLETED); query.setParameter("errorStatus", ActStatus.ERROR); query.setParameter("auditStatus", AuditStatus.COMPLETED); query.setParameter("limitDate", limitDate, TemporalType.TIMESTAMP); return ((Long)query.getSingleResult()).intValue(); } ActDAO(); int findNumberOfAct(Contract contract); @SuppressWarnings("unchecked") Collection<Act> findAllActsByContract(Contract contract); @SuppressWarnings("unchecked") Collection<Act> findActsByContract(
            Contract contract,
            int nbOfActs,
            int sortDirection,
            ScopeEnum scopeEnum,
            boolean onlyCompleted); @SuppressWarnings("unchecked") Collection<Act> findRunningActsByContract(Contract contract); Act findActFromAudit(Long auditId); int findNumberOfActByPeriodAndIp(Contract contract, Date limitDate, String ip); int findNumberOfActByScope(Contract contract, Collection<ScopeEnum> scopes);  }
@Test public void testFindNumberOfActByPeriodAndIp() { Contract contract = contractDAO.read((1L)); DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date limitDate = null; try { limitDate = dfm.parse("2011-01-01 00:00:00"); } catch (ParseException ex) { Logger.getLogger(ActDAOTest.class.getName()).log(Level.SEVERE, null, ex); } assertEquals(2, actDAO.findNumberOfActByPeriodAndIp(contract, limitDate, "127.0.0.1")); assertEquals(0, actDAO.findNumberOfActByPeriodAndIp(contract, limitDate, "127.0.0.2")); try { limitDate = dfm.parse("2011-01-01 01:00:00"); } catch (ParseException ex) { Logger.getLogger(ActDAOTest.class.getName()).log(Level.SEVERE, null, ex); } assertEquals(1, actDAO.findNumberOfActByPeriodAndIp(contract, limitDate, "127.0.0.1")); assertEquals(0, actDAO.findNumberOfActByPeriodAndIp(contract, limitDate, "127.0.0.2")); }