EventCodeDao { @SuppressWarnings("unchecked") public List<EventCode> eventCodeQuery(List<SlotType1> slots) { List<EventCode> eventCodes = null; List<String> eventCodesList = new ArrayList<>(); List<String> eventCodeSchemeList = new ArrayList<>(); Session sess = null; try { sess = getSession(); if (sess != null) { Criteria criteria = sess.createCriteria(EventCode.class); DetachedCriteria subCriteria = DetachedCriteria.forClass(EventCode.class); Criterion andCondition = Restrictions.conjunction(); Disjunction orCondition = Restrictions.disjunction(); String[] alias = new String[1]; alias[0] = "documentid"; Type[] types = new Type[1]; types[0] = org.hibernate.type.StandardBasicTypes.INTEGER; List<String> classCodes; List<String> orValues; int eventCodeSlotSize = 0; HashMap<String, String> hashMap = new HashMap<>(); if (slots != null) { for (SlotType1 slot : slots) { boolean slotContainsName = slot.getName() != null && slot.getName().length() > 0; boolean slotHasValues = slot.getValueList() != null && slot.getValueList().getValue() != null && !slot.getValueList().getValue().isEmpty(); if (slotContainsName && slotHasValues && slot.getName().equals(EBXML_EVENT_CODE_LIST)) { eventCodeSlotSize++; ValueListType valueListType = slot.getValueList(); List<String> slotValues = valueListType.getValue(); classCodes = new ArrayList<>(); for (int j = 0; j < slotValues.size(); j++) { parseParamFormattedString(slotValues.get(j), classCodes); if (slotValues.get(j).contains(",")) { orValues = Arrays.asList(slotValues.get(j).split("\\,")); for (int l = 0; l < orValues.size(); l++) { String innereventCode = getEventCode(classCodes.get(l), "eventCode"); String innereventCodeScheme = getEventCode(classCodes.get(l), "eventCodeScheme"); andCondition = Restrictions.and(Restrictions.eq("eventCode", innereventCode), Restrictions.eq("eventCodeScheme", innereventCodeScheme)); orCondition.add(andCondition); eventCodesList.add(innereventCode); eventCodeSchemeList.add(innereventCodeScheme); hashMap.put(innereventCode + "^^" + innereventCodeScheme, Integer.toString(eventCodeSlotSize)); } } else { String eventCode = getEventCode(classCodes.get(j), "eventCode"); String eventCodeScheme = getEventCode(classCodes.get(j), "eventCodeScheme"); orCondition.add(Restrictions.and(Restrictions.eq("eventCode", eventCode), Restrictions.eq("eventCodeScheme", eventCodeScheme))); eventCodesList.add(eventCode); eventCodeSchemeList.add(eventCodeScheme); hashMap.put(eventCode + "^^" + eventCodeScheme, Integer.toString(eventCodeSlotSize)); } } } } } String groupBy = "documentid having count(*) >= " + eventCodeSlotSize; subCriteria.add(orCondition); subCriteria.setProjection(Projections.projectionList() .add(Projections.sqlGroupProjection("documentid", groupBy, alias, types))); criteria.add(Subqueries.propertyIn("document", subCriteria)); criteria.addOrder(Order.asc("document")); eventCodes = criteria.list(); List<Long> documentIds; documentIds = getDocumentIds(eventCodes); List<Long> uniqueDocumentIds; uniqueDocumentIds = getUniqueDocumentIds(documentIds); boolean present; List<Long> documentNotPresent = new ArrayList<>(); for (int i = 0; i < uniqueDocumentIds.size(); i++) { present = documentInAllSlots(eventCodes, eventCodeSlotSize, hashMap, uniqueDocumentIds.get(i)); if (!present) { documentNotPresent.add(uniqueDocumentIds.get(i)); } } eventCodes = resultEventCodesList(documentNotPresent, eventCodes); eventCodes = eventCodes == null ? new ArrayList<EventCode>() : eventCodes; } else { LOG.error("Session was null"); } } finally { GenericDBUtils.closeSession(sess); } return eventCodes; }  @SuppressWarnings("unchecked") List<EventCode> eventCodeQuery(List<SlotType1> slots); void parseParamFormattedString(String paramFormattedString, List<String> resultCollection); List<EventCode> findAll(); List<EventCode> findAllBy(Long documentId); boolean save(EventCode eventCode); EventCode findById(Long eventCodeId); boolean delete(EventCode eventCode);  }
@Test public void testEventCodeQuery() { List<EventCode> eventCodeList = new ArrayList<>(); EventCode eventCode = new EventCode(); final long EVENT_CODE_ID = 12345; eventCode.setEventCodeId(EVENT_CODE_ID); eventCodeList.add(eventCode); EventCodeParam eventCodeParam = new EventCodeParam(); eventCodeParam.setEventCode("Event Code"); eventCodeParam.setEventCodeScheme("Event Code Scheme"); List<SlotType1> slots = null; Criteria criteria = mock(Criteria.class); when(session.createCriteria(EventCode.class)).thenReturn(criteria); when(criteria.list()).thenReturn(eventCodeList); List<EventCode> resultEventCodes = eventCodeDao.eventCodeQuery(slots); assertEquals(resultEventCodes.size(), 1); assertEquals(resultEventCodes.get(0).getEventCodeId(), (Long) EVENT_CODE_ID); }