<commit_before>package gov.va.escreening.repository;

import gov.va.escreening.domain.AssessmentStatusEnum;
import gov.va.escreening.dto.SearchAttributes;
import gov.va.escreening.dto.SortDirection;
import gov.va.escreening.dto.dashboard.SearchResult;
import gov.va.escreening.dto.report.Report593ByDayDTO;
import gov.va.escreening.dto.report.Report593ByTimeDTO;
import gov.va.escreening.dto.report.Report594DTO;
import gov.va.escreening.dto.report.Report595DTO;
import gov.va.escreening.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static gov.va.escreening.util.ReportRepositoryUtil.getDateFromString;

@Repository
public class VeteranAssessmentRepositoryImpl extends AbstractHibernateRepository<VeteranAssessment> implements VeteranAssessmentRepository {


    @Autowired
    private MeasureRepository measureRepository;

    public VeteranAssessmentRepositoryImpl() {
		super();

		setClazz(VeteranAssessment.class);
	}

	@Override
	public List<VeteranAssessment> findByVeteranIdAndAssessmentStatusIdList(
			Integer veteranId, List<Integer> assessmentStatusIdList) {

		List<VeteranAssessment> veteranAssessmentList = new ArrayList<VeteranAssessment>();

		String sql = "SELECT va FROM VeteranAssessment va JOIN va.veteran v JOIN va.assessmentStatus ast WHERE va.dateArchived is null AND v.veteranId = :veteranId AND ast.assessmentStatusId IN (:assessmentStatusIdList) ORDER BY va.veteranAssessmentId";

		if (assessmentStatusIdList != null && assessmentStatusIdList.size() > 0) {
			TypedQuery<VeteranAssessment> query = entityManager.createQuery(sql, VeteranAssessment.class);
			query.setParameter("veteranId", veteranId);
			query.setParameter("assessmentStatusIdList", assessmentStatusIdList);
			veteranAssessmentList = query.getResultList();
		}

		return veteranAssessmentList;
	}

	@Override
	public List<VeteranAssessment> findByProgramIdList(
			List<Integer> programIdList) {

		List<VeteranAssessment> veteranAssessmentList = new ArrayList<VeteranAssessment>();

		String sql = "SELECT DISTINCT va FROM VeteranAssessment va JOIN va.program p WHERE va.dateArchived is null AND p.programId IN (:programIdList)";

		if (programIdList != null && programIdList.size() > 0) {
			TypedQuery<VeteranAssessment> query = entityManager.createQuery(sql, VeteranAssessment.class);
			query.setParameter("programIdList", programIdList);
			veteranAssessmentList = query.getResultList();
		}

		return veteranAssessmentList;
	}

	@Override
	public List<VeteranAssessment> searchVeteranAssessmentForExport(Integer clinicanId,
			Integer createdByUserId, Integer programId,
			Date fromAssessmentDate, Date toAssessmentDate, Integer veteranId,
			List<Integer> programIdList) {

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class);

		Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class);
		Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus");

		List<Predicate> criteriaList = new ArrayList<Predicate>();

		// only include assessments in the appropriate state
		Expression<Integer> statusExp = assessmentStatusJoin.get("assessmentStatusId");
		Predicate statusPredicate = statusExp.in(getExportAssessmentStates());
		criteriaList.add(statusPredicate);

		if (clinicanId != null) {
			Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician");
			criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId));
		}

		if (createdByUserId != null) {
			Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser");
			criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId));
		}

		if (fromAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.greaterThan(veteranAssessmentRoot.<Date> get("dateCreated"), fromAssessmentDate));
		}

		if (toAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.lessThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateCreated"), toAssessmentDate));
		}

		if (veteranId != null) {
			Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran");
			criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId));
		}

		if (programId != null) {
			// criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId));
			if (programIdList == null) {
				programIdList = new ArrayList<Integer>();
			}

			programIdList.add(programId);
		}

		if (programIdList != null && programIdList.size() > 0) {
			Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program");
			Expression<Integer> exp = programJoin.get("programId");
			Predicate programIdPredicate = exp.in(programIdList);
			criteriaList.add(programIdPredicate);
		}

		criteriaQuery.select(veteranAssessmentRoot);
		criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));

		@SuppressWarnings("rawtypes")
		Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId");

		criteriaQuery.orderBy(criteriaBuilder.desc(orderByPath));

		// Generate the query based on the criteria.
		TypedQuery<VeteranAssessment> query = entityManager.createQuery(criteriaQuery);
		List<VeteranAssessment> veteranAssessments = query.getResultList();

		return veteranAssessments;
	}

	@Override
	public SearchResult<VeteranAssessment> searchVeteranAssessment(
			Integer veteranAssessmentId, Integer veteranId, Integer programId,
			Integer clinicanId, Integer createdByUserId,
			Date fromAssessmentDate, Date toAssessmentDate,
			List<Integer> programIdList, SearchAttributes searchAttributes) {

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class);

		Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class);
		Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus");
		Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran");
		Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program");
		Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician");
		Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser");

		List<Predicate> criteriaList = new ArrayList<Predicate>();

		if (veteranAssessmentId != null) {
			criteriaList.add(criteriaBuilder.equal(veteranAssessmentRoot.get("veteranAssessmentId"), veteranAssessmentId));
		}

		if (veteranId != null) {
			criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId));
		}

		if (programId != null) {
			criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId));
		}

		if (clinicanId != null) {
			criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId));
		}

		if (createdByUserId != null) {
			criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId));
		}

		if (fromAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.greaterThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateCreated"), fromAssessmentDate));
		}

		if (toAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.lessThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateCreated"), toAssessmentDate));
		}

		if (programIdList != null && programIdList.size() > 0) {
			Expression<Integer> exp = programJoin.get("programId");
			Predicate programIdPredicate = exp.in(programIdList);
			criteriaList.add(programIdPredicate);
		}

		criteriaQuery.select(veteranAssessmentRoot);
		criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));

		// Set default order by field and then check if one was passed to us.
		@SuppressWarnings("rawtypes")
		Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId");

		if (StringUtils.isNotBlank(searchAttributes.getSortColumn())) {
			if (searchAttributes.getSortColumn().equalsIgnoreCase("programName")) {
				orderByPath = programJoin.get("name");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("clinicianName")) {
				orderByPath = clinicianJoin.get("lastName");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("createdBy")) {
				orderByPath = createdByUserJoin.get("lastName");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("createDate")) {
				orderByPath = veteranAssessmentRoot.get("dateCreated");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("assessmentDate")) {
				orderByPath = veteranAssessmentRoot.get("dateUpdated");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("completeDate")) {
				orderByPath = veteranAssessmentRoot.get("dateCompleted");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranId")) {
				orderByPath = veteranJoin.get("veteranId");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranName")) {<commit_msg>t721 the search sql uses dateUpdated instead of dateCreated<commit_after>package gov.va.escreening.repository;

import gov.va.escreening.domain.AssessmentStatusEnum;
import gov.va.escreening.dto.SearchAttributes;
import gov.va.escreening.dto.SortDirection;
import gov.va.escreening.dto.dashboard.SearchResult;
import gov.va.escreening.dto.report.Report593ByDayDTO;
import gov.va.escreening.dto.report.Report593ByTimeDTO;
import gov.va.escreening.dto.report.Report594DTO;
import gov.va.escreening.dto.report.Report595DTO;
import gov.va.escreening.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static gov.va.escreening.util.ReportRepositoryUtil.getDateFromString;

@Repository
public class VeteranAssessmentRepositoryImpl extends AbstractHibernateRepository<VeteranAssessment> implements VeteranAssessmentRepository {


    @Autowired
    private MeasureRepository measureRepository;

    public VeteranAssessmentRepositoryImpl() {
		super();

		setClazz(VeteranAssessment.class);
	}

	@Override
	public List<VeteranAssessment> findByVeteranIdAndAssessmentStatusIdList(
			Integer veteranId, List<Integer> assessmentStatusIdList) {

		List<VeteranAssessment> veteranAssessmentList = new ArrayList<VeteranAssessment>();

		String sql = "SELECT va FROM VeteranAssessment va JOIN va.veteran v JOIN va.assessmentStatus ast WHERE va.dateArchived is null AND v.veteranId = :veteranId AND ast.assessmentStatusId IN (:assessmentStatusIdList) ORDER BY va.veteranAssessmentId";

		if (assessmentStatusIdList != null && assessmentStatusIdList.size() > 0) {
			TypedQuery<VeteranAssessment> query = entityManager.createQuery(sql, VeteranAssessment.class);
			query.setParameter("veteranId", veteranId);
			query.setParameter("assessmentStatusIdList", assessmentStatusIdList);
			veteranAssessmentList = query.getResultList();
		}

		return veteranAssessmentList;
	}

	@Override
	public List<VeteranAssessment> findByProgramIdList(
			List<Integer> programIdList) {

		List<VeteranAssessment> veteranAssessmentList = new ArrayList<VeteranAssessment>();

		String sql = "SELECT DISTINCT va FROM VeteranAssessment va JOIN va.program p WHERE va.dateArchived is null AND p.programId IN (:programIdList)";

		if (programIdList != null && programIdList.size() > 0) {
			TypedQuery<VeteranAssessment> query = entityManager.createQuery(sql, VeteranAssessment.class);
			query.setParameter("programIdList", programIdList);
			veteranAssessmentList = query.getResultList();
		}

		return veteranAssessmentList;
	}

	@Override
	public List<VeteranAssessment> searchVeteranAssessmentForExport(Integer clinicanId,
			Integer createdByUserId, Integer programId,
			Date fromAssessmentDate, Date toAssessmentDate, Integer veteranId,
			List<Integer> programIdList) {

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class);

		Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class);
		Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus");

		List<Predicate> criteriaList = new ArrayList<Predicate>();

		// only include assessments in the appropriate state
		Expression<Integer> statusExp = assessmentStatusJoin.get("assessmentStatusId");
		Predicate statusPredicate = statusExp.in(getExportAssessmentStates());
		criteriaList.add(statusPredicate);

		if (clinicanId != null) {
			Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician");
			criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId));
		}

		if (createdByUserId != null) {
			Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser");
			criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId));
		}

		if (fromAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.greaterThan(veteranAssessmentRoot.<Date> get("dateCreated"), fromAssessmentDate));
		}

		if (toAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.lessThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateCreated"), toAssessmentDate));
		}

		if (veteranId != null) {
			Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran");
			criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId));
		}

		if (programId != null) {
			// criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId));
			if (programIdList == null) {
				programIdList = new ArrayList<Integer>();
			}

			programIdList.add(programId);
		}

		if (programIdList != null && programIdList.size() > 0) {
			Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program");
			Expression<Integer> exp = programJoin.get("programId");
			Predicate programIdPredicate = exp.in(programIdList);
			criteriaList.add(programIdPredicate);
		}

		criteriaQuery.select(veteranAssessmentRoot);
		criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));

		@SuppressWarnings("rawtypes")
		Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId");

		criteriaQuery.orderBy(criteriaBuilder.desc(orderByPath));

		// Generate the query based on the criteria.
		TypedQuery<VeteranAssessment> query = entityManager.createQuery(criteriaQuery);
		List<VeteranAssessment> veteranAssessments = query.getResultList();

		return veteranAssessments;
	}

	@Override
	public SearchResult<VeteranAssessment> searchVeteranAssessment(
			Integer veteranAssessmentId, Integer veteranId, Integer programId,
			Integer clinicanId, Integer createdByUserId,
			Date fromAssessmentDate, Date toAssessmentDate,
			List<Integer> programIdList, SearchAttributes searchAttributes) {

		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<VeteranAssessment> criteriaQuery = criteriaBuilder.createQuery(VeteranAssessment.class);

		Root<VeteranAssessment> veteranAssessmentRoot = criteriaQuery.from(VeteranAssessment.class);
		Join<VeteranAssessment, AssessmentStatus> assessmentStatusJoin = veteranAssessmentRoot.join("assessmentStatus");
		Join<VeteranAssessment, Veteran> veteranJoin = veteranAssessmentRoot.join("veteran");
		Join<VeteranAssessment, Program> programJoin = veteranAssessmentRoot.join("program");
		Join<VeteranAssessment, User> clinicianJoin = veteranAssessmentRoot.join("clinician");
		Join<VeteranAssessment, User> createdByUserJoin = veteranAssessmentRoot.join("createdByUser");

		List<Predicate> criteriaList = new ArrayList<Predicate>();

		if (veteranAssessmentId != null) {
			criteriaList.add(criteriaBuilder.equal(veteranAssessmentRoot.get("veteranAssessmentId"), veteranAssessmentId));
		}

		if (veteranId != null) {
			criteriaList.add(criteriaBuilder.equal(veteranJoin.get("veteranId"), veteranId));
		}

		if (programId != null) {
			criteriaList.add(criteriaBuilder.equal(programJoin.get("programId"), programId));
		}

		if (clinicanId != null) {
			criteriaList.add(criteriaBuilder.equal(clinicianJoin.get("userId"), clinicanId));
		}

		if (createdByUserId != null) {
			criteriaList.add(criteriaBuilder.equal(createdByUserJoin.get("userId"), createdByUserId));
		}

		if (fromAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.greaterThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateUpdated"), fromAssessmentDate));
		}

		if (toAssessmentDate != null) {
			criteriaList.add(criteriaBuilder.lessThanOrEqualTo(veteranAssessmentRoot.<Date> get("dateUpdated"), toAssessmentDate));
		}

		if (programIdList != null && programIdList.size() > 0) {
			Expression<Integer> exp = programJoin.get("programId");
			Predicate programIdPredicate = exp.in(programIdList);
			criteriaList.add(programIdPredicate);
		}

		criteriaQuery.select(veteranAssessmentRoot);
		criteriaQuery.where(criteriaBuilder.and(criteriaList.toArray(new Predicate[0])));

		// Set default order by field and then check if one was passed to us.
		@SuppressWarnings("rawtypes")
		Expression orderByPath = veteranAssessmentRoot.get("veteranAssessmentId");

		if (StringUtils.isNotBlank(searchAttributes.getSortColumn())) {
			if (searchAttributes.getSortColumn().equalsIgnoreCase("programName")) {
				orderByPath = programJoin.get("name");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("clinicianName")) {
				orderByPath = clinicianJoin.get("lastName");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("createdBy")) {
				orderByPath = createdByUserJoin.get("lastName");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("createDate")) {
				orderByPath = veteranAssessmentRoot.get("dateCreated");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("assessmentDate")) {
				orderByPath = veteranAssessmentRoot.get("dateUpdated");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("completeDate")) {
				orderByPath = veteranAssessmentRoot.get("dateCompleted");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranId")) {
				orderByPath = veteranJoin.get("veteranId");
			} else if (searchAttributes.getSortColumn().equalsIgnoreCase("veteranName")) {
{'fraction_non_alphanumeric': 0.10858400586940573, 'fraction_numerical': 0.0021486217377633373, 'mean_word_length': 6.785801713586292, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492997', 'n_tokens_mistral': 6148, 'n_tokens_neox': 5628, 'n_words': 930}