AsyncMsgRecordDao { public List<AsyncMsgRecord> queryByCriteria(QueryDeferredQueueRequestType queryCriteria) { LOG.debug("Performing database record retrieve using AsyncMsgRecord instance to hold criteria."); List<AsyncMsgRecord> asyncMsgRecs = null; Session sess = null; try { sess = getSession(); if (sess != null) { boolean criteriaPopulated = false; Criteria criteria = sess.createCriteria(AsyncMsgRecord.class); if (queryCriteria.getCreationBeginTime() != null) { Date date = XMLDateUtil.gregorian2date(queryCriteria.getCreationBeginTime()); criteria.add(Restrictions.ge("CreationTime", date)); criteriaPopulated = true; } if (queryCriteria.getCreationEndTime() != null) { Date date = XMLDateUtil.gregorian2date(queryCriteria.getCreationEndTime()); criteria.add(Restrictions.le("CreationTime", date)); criteriaPopulated = true; } if (queryCriteria.getResponseBeginTime() != null) { Date date = XMLDateUtil.gregorian2date(queryCriteria.getResponseBeginTime()); criteria.add(Restrictions.ge("ResponseTime", date)); criteriaPopulated = true; } if (queryCriteria.getResponseEndTime() != null) { Date date = XMLDateUtil.gregorian2date(queryCriteria.getResponseEndTime()); criteria.add(Restrictions.le("ResponseTime", date)); criteriaPopulated = true; } if (queryCriteria.getServiceName() != null && !queryCriteria.getServiceName().isEmpty()) { criteria.add(Restrictions.in("ServiceName", queryCriteria.getServiceName())); criteriaPopulated = true; } if (queryCriteria.getDirection() != null) { criteria.add(Restrictions.eq("Direction", queryCriteria.getDirection())); criteriaPopulated = true; } if (queryCriteria.getCommunityId() != null && !queryCriteria.getCommunityId().isEmpty()) { criteria.add(Restrictions.in("CommunityId", queryCriteria.getCommunityId())); criteriaPopulated = true; } if (queryCriteria.getStatus() != null && !queryCriteria.getStatus().isEmpty()) { criteria.add(Restrictions.in("Status", queryCriteria.getStatus())); criteriaPopulated = true; } if (queryCriteria.getResponseType() != null) { criteria.add(Restrictions.eq("ResponseType", queryCriteria.getResponseType())); criteriaPopulated = true; } if (criteriaPopulated) { asyncMsgRecs = criteria.list(); } else { LOG.error("No query criteria defined. At least one criteria value must be defined."); } } else { LOG.error("Failed to obtain a session from the sessionFactory"); } if (LOG.isDebugEnabled()) { LOG.debug("Completed database record retrieve by criteria. Results found: " + (asyncMsgRecs == null ? "0" : Integer.toString(asyncMsgRecs.size()))); } } finally { if (sess != null) { try { sess.close(); } catch (HibernateException he) { LOG.error("Failed to close session: " + he.getLocalizedMessage(), he); } } } return asyncMsgRecs; } AsyncMsgRecordDao();  AsyncMsgRecordDao(PropertyAccessor accessor); List<AsyncMsgRecord> queryByMessageIdAndDirection(String messageId, String direction); List<AsyncMsgRecord> queryByMessageIdAndServiceName(String messageId, String serviceName); List<AsyncMsgRecord> queryByTime(Date timestamp); List<AsyncMsgRecord> queryForExpired(Date timestamp); List<AsyncMsgRecord> queryForDeferredQueueProcessing(); List<AsyncMsgRecord> queryForDeferredQueueSelected(); List<AsyncMsgRecord> queryByCriteria(QueryDeferredQueueRequestType queryCriteria); boolean insertRecords(List<AsyncMsgRecord> asyncMsgRecs); void save(AsyncMsgRecord asyncMsgRecord); void save(List<AsyncMsgRecord> asyncMsgRecs); void delete(AsyncMsgRecord asyncMsgRecord); void checkExpiration(); static final String QUEUE_DIRECTION_INBOUND; static final String QUEUE_DIRECTION_OUTBOUND; static final String QUEUE_RESPONSE_TYPE_AUTO; static final String QUEUE_RESPONSE_TYPE_EXTERNAL; static final String QUEUE_STATUS_EXPIRED; static final String QUEUE_STATUS_REQPROCESS; static final String QUEUE_STATUS_REQRCVD; static final String QUEUE_STATUS_REQRCVDACK; static final String QUEUE_STATUS_REQRCVDERR; static final String QUEUE_STATUS_REQSENT; static final String QUEUE_STATUS_REQSENTACK; static final String QUEUE_STATUS_REQSENTERR; static final String QUEUE_STATUS_RSPSELECT; static final String QUEUE_STATUS_RSPPROCESS; static final String QUEUE_STATUS_RSPRCVD; static final String QUEUE_STATUS_RSPRCVDACK; static final String QUEUE_STATUS_RSPRCVDERR; static final String QUEUE_STATUS_RSPSENT; static final String QUEUE_STATUS_RSPSENTACK; static final String QUEUE_STATUS_RSPSENTERR; }
@Test public void testQueryByCriteria() { System.out.println("queryByCriteria"); QueryDeferredQueueRequestType queryCriteria = new QueryDeferredQueueRequestType(); queryCriteria.setCreationBeginTime(long2Gregorian(new Date().getTime())); queryCriteria.setCreationEndTime(long2Gregorian(new Date().getTime())); queryCriteria.setDirection("Test"); queryCriteria.setResponseBeginTime(long2Gregorian(new Date().getTime())); queryCriteria.setResponseEndTime(long2Gregorian(new Date().getTime())); queryCriteria.setResponseType("DQ"); QueryDeferredQueueRequestType queryCriteria1 = new QueryDeferredQueueRequestType(); Query query = mock(Query.class); when(session.getNamedQuery(Mockito.anyString())).thenReturn(query); List<AsyncMsgRecord> asyncRecords = new ArrayList<>(); AsyncMsgRecord asyncMsgRecord1 = new AsyncMsgRecord(); AsyncMsgRecord asyncMsgRecord2 = new AsyncMsgRecord(); asyncRecords.add(asyncMsgRecord1); asyncRecords.add(asyncMsgRecord2); Criteria criteria = mock(Criteria.class); when(session.createCriteria(AsyncMsgRecord.class)).thenReturn(criteria); when(criteria.list()).thenReturn(asyncRecords); List<AsyncMsgRecord> result = asyncMsgRecordDao.queryByCriteria(queryCriteria); assertEquals(asyncRecords, result); result = asyncMsgRecordDao.queryByCriteria(queryCriteria1); assertNull(result); }