<commit_before>package br.net.mirante.singular.server.commons.persistence.dao.form;


import br.net.mirante.singular.flow.core.TaskType;
import br.net.mirante.singular.server.commons.persistence.dto.PeticaoDTO;
import br.net.mirante.singular.server.commons.persistence.entity.form.PetitionEntity;
import br.net.mirante.singular.server.commons.persistence.filter.QuickFilter;
import br.net.mirante.singular.server.commons.util.JPAQueryUtil;
import br.net.mirante.singular.support.persistence.BaseDAO;
import br.net.mirante.singular.support.persistence.enums.SimNao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.AliasToEntityMapResultTransformer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PetitionDAO<T extends PetitionEntity> extends BaseDAO<T, Long> {

    public PetitionDAO() {
        super((Class<T>) PetitionEntity.class);
    }

    public PetitionDAO(Class<T> tipo) {
        super(tipo);
    }

    @SuppressWarnings("unchecked")
    public List<T> list(String type) {
        Criteria crit = getSession().createCriteria(tipo);
        crit.add(Restrictions.eq("type", type));
        return crit.list();
    }

    public Long countQuickSearch(QuickFilter filtro, List<String> siglasProcesso, List<String> formNames) {
        return (Long) createQuery(filtro, siglasProcesso, true, formNames).uniqueResult();
    }

    public List<PeticaoDTO> quickSearch(QuickFilter filtro, List<String> siglasProcesso, List<String> formNames) {
        final Query query = createQuery(filtro, siglasProcesso, false, formNames);
        query.setFirstResult(filtro.getFirst());
        query.setMaxResults(filtro.getCount());
        query.setResultTransformer(new AliasToBeanResultTransformer(getResultClass()));
        return query.list();
    }

    protected Class<? extends PeticaoDTO> getResultClass() {
        return PeticaoDTO.class;
    }

    public List<Map<String, Object>> quickSearchMap(QuickFilter filter, List<String> processesAbbreviation, List<String> formNames) {
        final Query query = createQuery(filter, processesAbbreviation, false, formNames);
        query.setFirstResult(filter.getFirst());
        query.setMaxResults(filter.getCount());
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }

    private void buildSelectClause(StringBuilder hql, boolean count) {
        if (count) {
            hql.append("SELECT count(p) ");
        } else {
            hql.append(" SELECT p.cod as codPeticao ");
            hql.append(" , p.description as description ");
            hql.append(" , task.name as situation ");
            hql.append(" , processDefinitionEntity.name as processName ");
            hql.append(" , case when currentFormDraftVersionEntity is null then currentFormVersion.inclusionDate else currentFormDraftVersionEntity.inclusionDate end as creationDate ");
            hql.append(" , case when formDraftType is null then formType.abbreviation else formDraftType.abbreviation end as type ");
            hql.append(" , processDefinitionEntity.key as processType ");
            hql.append(" , ta.beginDate as situationBeginDate ");
            hql.append(" , pie.beginDate as processBeginDate ");
            hql.append(" , currentDraftEntity.editionDate as editionDate ");
            hql.append(" , pie.cod as processInstanceId ");
            appendCustomSelectClauses(hql);
        }
    }

    /**
     * Append Custom Select Clauses
     */
    protected void appendCustomSelectClauses(StringBuilder hql) {
    }

    private void buildFromClause(StringBuilder hql) {
        hql.append(" FROM ").append(tipo.getName()).append(" p ");
        hql.append(" LEFT JOIN p.processInstanceEntity pie ");
        hql.append(" LEFT JOIN p.currentDraftEntity currentDraftEntity ");
        hql.append(" LEFT JOIN p.petitioner petitioner ");
        hql.append(" LEFT JOIN currentDraftEntity.form formDraftEntity");
        hql.append(" LEFT JOIN formDraftEntity.currentFormVersionEntity currentFormDraftVersionEntity");
        hql.append(" LEFT JOIN p.formPetitionEntities formPetitionEntity on formPetitionEntity.mainForm = :sim ");
        hql.append(" LEFT JOIN formPetitionEntity.form formEntity ");
        hql.append(" LEFT JOIN formEntity.currentFormVersionEntity currentFormVersion ");
        hql.append(" LEFT JOIN p.processDefinitionEntity processDefinitionEntity ");
        hql.append(" LEFT JOIN formEntity.formType formType  ");
        hql.append(" LEFT JOIN formDraftEntity.formType formDraftType  ");
        hql.append(" LEFT JOIN pie.tasks ta ");
        hql.append(" LEFT JOIN ta.task task ");
    }

    private void buildWhereClause(StringBuilder hql,
                                  Map<String, Object> params,
                                  QuickFilter filtro,
                                  List<String> siglasProcesso,
                                  List<String> formNames) {

        params.put("sim", SimNao.SIM);

        hql.append(" WHERE 1=1 ");

        if (StringUtils.isNotBlank(filtro.getIdPessoa())) {
            hql.append(" AND petitioner.idPessoa = :idPessoa ");
            params.put("idPessoa", filtro.getIdPessoa());
        }

        if (siglasProcesso != null && !siglasProcesso.isEmpty()) {
            hql.append(" AND ( processDefinitionEntity.key  in (:siglasProcesso) ");
            params.put("siglasProcesso", siglasProcesso);
            if (formNames != null && !formNames.isEmpty()) {<commit_msg>[SINGULAR-PET-SERVER] alterando categoria de GGTOX para Toxicologia, adicionando número da transação na listagem de acompanhamento<commit_after>package br.net.mirante.singular.server.commons.persistence.dao.form;


import br.net.mirante.singular.flow.core.TaskType;
import br.net.mirante.singular.server.commons.persistence.dto.PeticaoDTO;
import br.net.mirante.singular.server.commons.persistence.entity.form.PetitionEntity;
import br.net.mirante.singular.server.commons.persistence.filter.QuickFilter;
import br.net.mirante.singular.server.commons.util.JPAQueryUtil;
import br.net.mirante.singular.support.persistence.BaseDAO;
import br.net.mirante.singular.support.persistence.enums.SimNao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.AliasToEntityMapResultTransformer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PetitionDAO<T extends PetitionEntity> extends BaseDAO<T, Long> {

    public PetitionDAO() {
        super((Class<T>) PetitionEntity.class);
    }

    public PetitionDAO(Class<T> tipo) {
        super(tipo);
    }

    @SuppressWarnings("unchecked")
    public List<T> list(String type) {
        Criteria crit = getSession().createCriteria(tipo);
        crit.add(Restrictions.eq("type", type));
        return crit.list();
    }

    public Long countQuickSearch(QuickFilter filtro, List<String> siglasProcesso, List<String> formNames) {
        return (Long) createQuery(filtro, siglasProcesso, true, formNames).uniqueResult();
    }

    public List<PeticaoDTO> quickSearch(QuickFilter filtro, List<String> siglasProcesso, List<String> formNames) {
        final Query query = createQuery(filtro, siglasProcesso, false, formNames);
        query.setFirstResult(filtro.getFirst());
        query.setMaxResults(filtro.getCount());
        query.setResultTransformer(new AliasToBeanResultTransformer(getResultClass()));
        return query.list();
    }

    protected Class<? extends PeticaoDTO> getResultClass() {
        return PeticaoDTO.class;
    }

    public List<Map<String, Object>> quickSearchMap(QuickFilter filter, List<String> processesAbbreviation, List<String> formNames) {
        final Query query = createQuery(filter, processesAbbreviation, false, formNames);
        query.setFirstResult(filter.getFirst());
        query.setMaxResults(filter.getCount());
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }

    private void buildSelectClause(StringBuilder hql, boolean count) {
        if (count) {
            hql.append("SELECT count(p) ");
        } else {
            hql.append(" SELECT p.cod as codPeticao ");
            hql.append(" , p.description as description ");
            hql.append(" , task.name as situation ");
            hql.append(" , processDefinitionEntity.name as processName ");
            hql.append(" , case when currentFormDraftVersionEntity is null then currentFormVersion.inclusionDate else currentFormDraftVersionEntity.inclusionDate end as creationDate ");
            hql.append(" , case when formDraftType is null then formType.abbreviation else formDraftType.abbreviation end as type ");
            hql.append(" , processDefinitionEntity.key as processType ");
            hql.append(" , ta.beginDate as situationBeginDate ");
            hql.append(" , pie.beginDate as processBeginDate ");
            hql.append(" , currentDraftEntity.editionDate as editionDate ");
            hql.append(" , pie.cod as processInstanceId ");
            appendCustomSelectClauses(hql);
        }
    }

    /**
     * Append Custom Select Clauses
     */
    protected void appendCustomSelectClauses(StringBuilder hql) {
    }

    private void buildFromClause(StringBuilder hql) {
        hql.append(" FROM ").append(tipo.getName()).append(" p ");
        hql.append(" LEFT JOIN p.processInstanceEntity pie ");
        hql.append(" LEFT JOIN p.currentDraftEntity currentDraftEntity ");
        hql.append(" LEFT JOIN p.petitioner petitioner ");
        hql.append(" LEFT JOIN currentDraftEntity.form formDraftEntity");
        hql.append(" LEFT JOIN formDraftEntity.currentFormVersionEntity currentFormDraftVersionEntity");
        hql.append(" LEFT JOIN p.formPetitionEntities formPetitionEntity on formPetitionEntity.mainForm = :sim ");
        hql.append(" LEFT JOIN formPetitionEntity.form formEntity ");
        hql.append(" LEFT JOIN formEntity.currentFormVersionEntity currentFormVersion ");
        hql.append(" LEFT JOIN p.processDefinitionEntity processDefinitionEntity ");
        hql.append(" LEFT JOIN formEntity.formType formType  ");
        hql.append(" LEFT JOIN formDraftEntity.formType formDraftType  ");
        hql.append(" LEFT JOIN pie.tasks ta ");
        hql.append(" LEFT JOIN ta.task task ");
        appendCustomFromClauses(hql);
    }

    /**
     * Append Custom From Clauses
     */
    protected void appendCustomFromClauses(StringBuilder hql) {
    }


    private void buildWhereClause(StringBuilder hql,
                                  Map<String, Object> params,
                                  QuickFilter filtro,
                                  List<String> siglasProcesso,
                                  List<String> formNames) {

        params.put("sim", SimNao.SIM);

        hql.append(" WHERE 1=1 ");

        if (StringUtils.isNotBlank(filtro.getIdPessoa())) {
            hql.append(" AND petitioner.idPessoa = :idPessoa ");
            params.put("idPessoa", filtro.getIdPessoa());
        }

        if (siglasProcesso != null && !siglasProcesso.isEmpty()) {
            hql.append(" AND ( processDefinitionEntity.key  in (:siglasProcesso) ");
            params.put("siglasProcesso", siglasProcesso);
            if (formNames != null && !formNames.isEmpty()) {
{'fraction_non_alphanumeric': 0.10645679952610645, 'fraction_numerical': 0.0005077430820005078, 'mean_word_length': 3.2041977943792244, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2503978', 'n_tokens_mistral': 3414, 'n_tokens_neox': 3268, 'n_words': 677}