CaseFacadeEjb implements CaseFacade { @Override public boolean doesEpidNumberExist(String epidNumber, String caseUuid, Disease caseDisease) { int suffixSeperatorIndex = epidNumber.lastIndexOf('-'); if (suffixSeperatorIndex == -1) { suffixSeperatorIndex = epidNumber.length() - 1; } String prefixString = epidNumber.substring(0, suffixSeperatorIndex + 1); String suffixString = epidNumber.substring(suffixSeperatorIndex + 1); suffixString = suffixString.replaceAll("[^\\d]", ""); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> cq = cb.createQuery(String.class); Root<Case> caze = cq.from(Case.class); Predicate filter = cb.and(cb.equal(caze.get(Case.DELETED), false), cb.equal(caze.get(Case.DISEASE), caseDisease)); if (!DataHelper.isNullOrEmpty(caseUuid)) { filter = cb.and(filter, cb.notEqual(caze.get(Case.UUID), caseUuid)); } ParameterExpression<String> regexPattern = null, regexReplacement = null, regexFlags = null; if (suffixString.length() > 0) { filter = cb.and(filter, cb.like(caze.get(Case.EPID_NUMBER), prefixString + "%")); int suffixNumber; try { suffixNumber = Integer.parseInt(suffixString); } catch (NumberFormatException e) { throw new IllegalArgumentException( String.format("Invalid suffix for epid number. epidNumber: '%s', suffixString: '%s'", epidNumber, suffixString), e); } regexPattern = cb.parameter(String.class); regexReplacement = cb.parameter(String.class); regexFlags = cb.parameter(String.class); Expression<String> epidNumberSuffixClean = cb.function( "regexp_replace", String.class, cb.substring(caze.get(Case.EPID_NUMBER), suffixSeperatorIndex + 2), regexPattern, regexReplacement, regexFlags); filter = cb.and(filter, cb.equal(cb.concat("0", epidNumberSuffixClean).as(Integer.class), suffixNumber)); } else { filter = cb.and(filter, cb.equal(caze.get(Case.EPID_NUMBER), prefixString)); } cq.where(filter); cq.select(caze.get(Case.EPID_NUMBER)); TypedQuery<String> query = em.createQuery(cq); if (regexPattern != null) { query.setParameter(regexPattern, "\\D"); query.setParameter(regexReplacement, ""); query.setParameter(regexFlags, "g"); } query.setMaxResults(1); return !query.getResultList().isEmpty(); }  @Override List<CaseDataDto> getAllActiveCasesAfter(Date date); @Override List<CaseDataDto> getAllActiveCasesAfter(Date date, boolean includeExtendedChangeDateFilters); @Override List<CaseDataDto> getByUuids(List<String> uuids); @Override String getUuidByUuidEpidNumberOrExternalId(String searchTerm); @Override long count(CaseCriteria caseCriteria); @Override List<CaseIndexDto> getIndexList(CaseCriteria caseCriteria, Integer first, Integer max, List<SortProperty> sortProperties); @Override List<CaseIndexDetailedDto> getIndexDetailedList(CaseCriteria caseCriteria, Integer first, Integer max, List<SortProperty> sortProperties); @Override @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) List<CaseExportDto> getExportList(
		CaseCriteria caseCriteria,
		CaseExportType exportType,
		int first,
		int max,
		ExportConfigurationDto exportConfiguration,
		Language userLanguage); @Override List<String> getAllActiveUuids(); @Override List<DashboardCaseDto> getCasesForDashboard(CaseCriteria caseCriteria); List<DashboardQuarantineDataDto> getQuarantineDataForDashBoard(
		RegionReferenceDto regionRef,
		DistrictReferenceDto districtRef,
		Disease disease,
		Date from,
		Date to); long countCasesConvertedFromContacts(CaseCriteria caseCriteria); @Override List<MapCaseDto> getCasesForMap(RegionReferenceDto regionRef, DistrictReferenceDto districtRef, Disease disease, Date from, Date to); @Override List<CaseDataDto> getAllCasesOfPerson(String personUuid); @Override Map<CaseClassification, Long> getCaseCountPerClassification(
		CaseCriteria caseCriteria,
		boolean excludeSharedCases,
		boolean excludeCasesFromContacts); @Override Map<PresentCondition, Long> getCaseCountPerPersonCondition(
		CaseCriteria caseCriteria,
		boolean excludeSharedCases,
		boolean excludeCasesFromContacts); @Override Map<Disease, Long> getCaseCountByDisease(CaseCriteria caseCriteria, boolean excludeSharedCases, boolean excludeCasesFromContacts); @Override List<CaseReferenceDto> getRandomCaseReferences(CaseCriteria criteria, int count); Map<Disease, District> getLastReportedDistrictByDisease(
		CaseCriteria caseCriteria,
		boolean excludeSharedCases,
		boolean excludeCasesFromContacts); @Override List<CaseIndexDto> getSimilarCases(CaseSimilarityCriteria criteria); @Override List<CaseIndexDto[]> getCasesForDuplicateMerging(CaseCriteria criteria, boolean ignoreRegion); void updateCompleteness(String caseUuid); @Override String getLastReportedDistrictName(CaseCriteria caseCriteria, boolean excludeSharedCases, boolean excludeCasesFromContacts); @Override CaseDataDto getCaseDataByUuid(String uuid); @Override CaseReferenceDto getReferenceByUuid(String uuid); @Override CaseDataDto saveCase(CaseDataDto dto); CaseDataDto saveCase(CaseDataDto dto, boolean handleChanges); @Override void setSampleAssociations(ContactReferenceDto sourceContact, CaseReferenceDto cazeRef); @Override void setSampleAssociations(EventParticipantReferenceDto sourceEventParticipant, CaseReferenceDto cazeRef); @Override void setSampleAssociationsUnrelatedDisease(EventParticipantReferenceDto sourceEventParticipant, CaseReferenceDto cazeRef); @Override void validate(CaseDataDto caze); @Override void archiveOrDearchiveCase(String caseUuid, boolean archive); void onCaseChanged(CaseDataDto existingCase, Case newCase); void setResponsibleSurveillanceOfficer(Case caze); void reassignTasks(Case caze); @Override String generateEpidNumber(CaseReferenceDto caze); String generateEpidNumber(Case caze); @Override void deleteCase(String caseUuid); @Override void deleteCaseAsDuplicate(String caseUuid, String duplicateOfCaseUuid); @Override List<String> getArchivedUuidsSince(Date since); @Override List<String> getDeletedUuidsSince(Date since); Case fillOrBuildEntity(@NotNull CaseDataDto source, Case target); CaseDataDto convertToDto(Case source, Pseudonymizer pseudonymizer); CaseReferenceDto convertToReferenceDto(Case source); static CaseReferenceDto toReferenceDto(Case entity); static CaseDataDto toDto(Case source); void updateInvestigationByStatus(CaseDataDto existingCase, Case caze); void updateInvestigationByTask(Case caze); @Override boolean doesEpidNumberExist(String epidNumber, String caseUuid, Disease caseDisease); @Override List<Pair<DistrictDto, BigDecimal>> getCaseMeasurePerDistrict(Date fromDate, Date toDate, Disease disease, CaseMeasure caseMeasure); @Override Date getOldestCaseOnsetDate(); @Override Date getOldestCaseReportDate(); @Override boolean isArchived(String caseUuid); @Override boolean isDeleted(String caseUuid); @Override void mergeCase(String leadUuid, String otherUuid); @Override CaseDataDto cloneCase(CaseDataDto existingCaseDto); @Override @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) void archiveAllArchivableCases(int daysAfterCaseGetsArchived); @Override void updateArchived(List<String> caseUuids, boolean archived); @Override boolean exists(String uuid); @Override boolean hasPositiveLabResult(String caseUuid); @Override List<CaseFollowUpDto> getCaseFollowUpList(
		CaseCriteria caseCriteria,
		Date referenceDate,
		int interval,
		Integer first,
		Integer max,
		List<SortProperty> sortProperties); Boolean isCaseEditAllowed(String caseUuid);  }
@Test public void testDoesEpidNumberExist() { RDCFEntities rdcf = creator.createRDCFEntities(); UserReferenceDto user = creator.createUser(rdcf).toReference(); PersonReferenceDto cazePerson = creator.createPerson("Horst", "Meyer").toReference(); CaseDataDto caze = creator.createCase(user, cazePerson, Disease.CHOLERA, CaseClassification.NOT_CLASSIFIED, InvestigationStatus.PENDING, new Date(), rdcf); assertFalse(getCaseFacade().doesEpidNumberExist(caze.getEpidNumber(), caze.getUuid(), caze.getDisease())); assertTrue(getCaseFacade().doesEpidNumberExist(caze.getEpidNumber(), "abc", caze.getDisease())); assertFalse(getCaseFacade().doesEpidNumberExist("123", "abc", caze.getDisease())); assertFalse(getCaseFacade().doesEpidNumberExist(caze.getEpidNumber(), "abc", Disease.ANTHRAX)); assertFalse(getCaseFacade().doesEpidNumberExist("def", "abc", Disease.ANTHRAX)); }
@Test(expected = IllegalArgumentException.class) public void testDoesEpidNumberExistLargeNumbers() { getCaseFacade().doesEpidNumberExist("NIE-08034912345", "not-a-uuid", Disease.OTHER); }