<commit_before>/*
 * The MIT License
 *
 * Copyright 2013 RBC1B.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package uk.org.rbc1b.roms.db.volunteer;

import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;
import uk.org.rbc1b.roms.controller.common.SortDirection;
import uk.org.rbc1b.roms.db.volunteer.department.Assignment;
import uk.org.rbc1b.roms.db.volunteer.interview.VolunteerInterviewSession;
import uk.org.rbc1b.roms.db.volunteer.qualification.VolunteerQualification;
import uk.org.rbc1b.roms.db.volunteer.skill.VolunteerSkill;
import uk.org.rbc1b.roms.db.volunteer.trade.VolunteerTrade;
import uk.org.rbc1b.roms.db.volunteer.trade.VolunteerTradeSearchCriteria;

/**
 * @author rahulsingh
 */
@Repository
public class HibernateVolunteerDao implements VolunteerDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Volunteer findVolunteerById(Integer personId) {
        Volunteer volunteer = (Volunteer) this.sessionFactory.getCurrentSession().get(Volunteer.class, personId);
        if (volunteer == null) {
            return null;
        }
        Hibernate.initialize(volunteer.getPerson());
        return volunteer;
    }

    @Override
    @CacheEvict(value = "person.person", key = "#volunteer.personId")
    public void updateVolunteerByVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        session.merge(volunteer.getPerson());
    }

    @Override
    public Volunteer findVolunteer(Integer volunteerId, Set<VolunteerData> data) {
        Volunteer volunteer = (Volunteer) this.sessionFactory.getCurrentSession().get(Volunteer.class, volunteerId);
        if (volunteer == null) {
            return null;
        }
        Hibernate.initialize(volunteer.getPerson());
        if (CollectionUtils.isNotEmpty(data)) {
            if (data.contains(VolunteerData.SPOUSE)) {
                Hibernate.initialize(volunteer.getSpouse());
            }
            if (data.contains(VolunteerData.EMERGENCY_CONTACT)) {
                Hibernate.initialize(volunteer.getEmergencyContact());
            }
            if (data.contains(VolunteerData.TRADES)) {
                Hibernate.initialize(volunteer.getTrades());
            }
            if (data.contains(VolunteerData.INTERVIEWER)) {
                Hibernate.initialize(volunteer.getInterviewerA());
                Hibernate.initialize(volunteer.getInterviewerB());
            }
        }

        return volunteer;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Volunteer> findVolunteers(VolunteerSearchCriteria searchCriteria) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

        criteria.setFirstResult(searchCriteria.getStartIndex());
        criteria.setMaxResults(searchCriteria.getMaxResults());

        if (searchCriteria.getSortValue() != null) {
            criteria.addOrder(searchCriteria.getSortDirection() == SortDirection.ASCENDING ? Order.asc(searchCriteria
                    .getSortValue()) : Order.desc(searchCriteria.getSortValue()));
        }

        return criteria.list();

    }

    @Override
    public int findVolunteersCount(VolunteerSearchCriteria searchCriteria) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

        criteria.setProjection(Projections.rowCount());

        return ((Long) criteria.uniqueResult()).intValue();
    }

    private Criteria createVolunteerSearchCriteria(VolunteerSearchCriteria searchCriteria, Session session) {

        Criteria criteria = session.createCriteria(Volunteer.class);
        criteria.createAlias("Person", "person");

        if (searchCriteria.getCongregationId() != null) {
            criteria.createAlias("person.congregation", "congregation");
        } else if (searchCriteria.getSearch() != null || "congregation.name".equals(searchCriteria.getSortValue())) {
            criteria.createAlias("person.congregation", "congregation", JoinType.LEFT_OUTER_JOIN);
        }

        if (searchCriteria.getSearch() != null) {
            String searchValue = "%" + searchCriteria.getSearch() + "%";

            criteria.add(Restrictions.or(Restrictions.like("person.forename", searchValue),
                    Restrictions.like("person.middleName", searchValue),
                    Restrictions.like("person.surname", searchValue), Restrictions.like("person.email", searchValue),
                    Restrictions.like("congregation.name", searchValue)));
        }

        if (searchCriteria.getCongregationId() != null) {
            criteria.add(Restrictions.or(Restrictions.eq("person.congregation.congregationId",
                    searchCriteria.getCongregationId())));
        }

        if (searchCriteria.getSkillId() != null) {
            DetachedCriteria skillCriteria = DetachedCriteria.forClass(VolunteerSkill.class);
            skillCriteria.add(Restrictions.eq("skillId", searchCriteria.getSkillId()));
            skillCriteria.setProjection(Projections.property("personId"));

            criteria.add(Property.forName("personId").in(skillCriteria));
        }

        if (searchCriteria.getQualificationId() != null) {
            DetachedCriteria qualificationCriteria = DetachedCriteria.forClass(VolunteerQualification.class);
            qualificationCriteria.add(Restrictions.eq("qualificationId", searchCriteria.getQualificationId()));
            qualificationCriteria.setProjection(Projections.property("personId"));

            criteria.add(Property.forName("personId").in(qualificationCriteria));
        }

        if (searchCriteria.getInterviewSessionId() != null) {
            DetachedCriteria interviewCriteria = DetachedCriteria.forClass(VolunteerInterviewSession.class);
            interviewCriteria.add(Restrictions.eq("interviewSession.interviewSessionId",
                    searchCriteria.getInterviewSessionId()));
            interviewCriteria.setProjection(Projections.property("volunteer.personId"));

            criteria.add(Property.forName("personId").in(interviewCriteria));
        }

        return criteria;
    }

    @Override
    public void createVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        if (volunteer.getPerson().getPersonId() != null) {
            session.merge(volunteer.getPerson());
        }

        session.save(volunteer);
    }

    @Override
    @CacheEvict(value = "person.person", key = "#volunteer.personId")
    public void updateVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        session.merge(volunteer.getPerson());
        session.merge(volunteer);<commit_msg>[#72383336] Restrict Volunteer view so that In-active and DN do not appear in the list. TO DO Unit test for this and alter views for those volunteers.<commit_after>/*
 * The MIT License
 *
 * Copyright 2013 RBC1B.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package uk.org.rbc1b.roms.db.volunteer;

import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;
import uk.org.rbc1b.roms.controller.common.SortDirection;
import uk.org.rbc1b.roms.db.volunteer.department.Assignment;
import uk.org.rbc1b.roms.db.volunteer.interview.VolunteerInterviewSession;
import uk.org.rbc1b.roms.db.volunteer.qualification.VolunteerQualification;
import uk.org.rbc1b.roms.db.volunteer.skill.VolunteerSkill;
import uk.org.rbc1b.roms.db.volunteer.trade.VolunteerTrade;
import uk.org.rbc1b.roms.db.volunteer.trade.VolunteerTradeSearchCriteria;

/**
 * @author rahulsingh
 */
@Repository
public class HibernateVolunteerDao implements VolunteerDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Volunteer findVolunteerById(Integer personId) {
        Volunteer volunteer = (Volunteer) this.sessionFactory.getCurrentSession().get(Volunteer.class, personId);
        if (volunteer == null) {
            return null;
        }
        Hibernate.initialize(volunteer.getPerson());
        return volunteer;
    }

    @Override
    @CacheEvict(value = "person.person", key = "#volunteer.personId")
    public void updateVolunteerByVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        session.merge(volunteer.getPerson());
    }

    @Override
    public Volunteer findVolunteer(Integer volunteerId, Set<VolunteerData> data) {
        Volunteer volunteer = (Volunteer) this.sessionFactory.getCurrentSession().get(Volunteer.class, volunteerId);
        if (volunteer == null) {
            return null;
        }
        Hibernate.initialize(volunteer.getPerson());
        if (CollectionUtils.isNotEmpty(data)) {
            if (data.contains(VolunteerData.SPOUSE)) {
                Hibernate.initialize(volunteer.getSpouse());
            }
            if (data.contains(VolunteerData.EMERGENCY_CONTACT)) {
                Hibernate.initialize(volunteer.getEmergencyContact());
            }
            if (data.contains(VolunteerData.TRADES)) {
                Hibernate.initialize(volunteer.getTrades());
            }
            if (data.contains(VolunteerData.INTERVIEWER)) {
                Hibernate.initialize(volunteer.getInterviewerA());
                Hibernate.initialize(volunteer.getInterviewerB());
            }
        }

        return volunteer;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Volunteer> findVolunteers(VolunteerSearchCriteria searchCriteria) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

        criteria.setFirstResult(searchCriteria.getStartIndex());
        criteria.setMaxResults(searchCriteria.getMaxResults());

        if (searchCriteria.getSortValue() != null) {
            criteria.addOrder(searchCriteria.getSortDirection() == SortDirection.ASCENDING ? Order.asc(searchCriteria
                    .getSortValue()) : Order.desc(searchCriteria.getSortValue()));
        }

        return criteria.list();

    }

    @Override
    public int findVolunteersCount(VolunteerSearchCriteria searchCriteria) {
        Session session = this.sessionFactory.getCurrentSession();
        Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

        criteria.setProjection(Projections.rowCount());

        return ((Long) criteria.uniqueResult()).intValue();
    }

    private Criteria createVolunteerSearchCriteria(VolunteerSearchCriteria searchCriteria, Session session) {

        Criteria criteria = session.createCriteria(Volunteer.class);
        criteria.createAlias("Person", "person");

        if (searchCriteria.getCongregationId() != null) {
            criteria.createAlias("person.congregation", "congregation");
        } else if (searchCriteria.getSearch() != null || "congregation.name".equals(searchCriteria.getSortValue())) {
            criteria.createAlias("person.congregation", "congregation", JoinType.LEFT_OUTER_JOIN);
        }

        if (searchCriteria.getSearch() != null) {
            String searchValue = "%" + searchCriteria.getSearch() + "%";

            criteria.add(Restrictions.or(Restrictions.like("person.forename", searchValue),
                    Restrictions.like("person.middleName", searchValue),
                    Restrictions.like("person.surname", searchValue), Restrictions.like("person.email", searchValue),
                    Restrictions.like("congregation.name", searchValue)));
        }

        if (searchCriteria.getCongregationId() != null) {
            criteria.add(Restrictions.or(Restrictions.eq("person.congregation.congregationId",
                    searchCriteria.getCongregationId())));
        }

        if (searchCriteria.getSkillId() != null) {
            DetachedCriteria skillCriteria = DetachedCriteria.forClass(VolunteerSkill.class);
            skillCriteria.add(Restrictions.eq("skillId", searchCriteria.getSkillId()));
            skillCriteria.setProjection(Projections.property("personId"));

            criteria.add(Property.forName("personId").in(skillCriteria));
        }

        if (searchCriteria.getQualificationId() != null) {
            DetachedCriteria qualificationCriteria = DetachedCriteria.forClass(VolunteerQualification.class);
            qualificationCriteria.add(Restrictions.eq("qualificationId", searchCriteria.getQualificationId()));
            qualificationCriteria.setProjection(Projections.property("personId"));

            criteria.add(Property.forName("personId").in(qualificationCriteria));
        }

        if (searchCriteria.getInterviewSessionId() != null) {
            DetachedCriteria interviewCriteria = DetachedCriteria.forClass(VolunteerInterviewSession.class);
            interviewCriteria.add(Restrictions.eq("interviewSession.interviewSessionId",
                    searchCriteria.getInterviewSessionId()));
            interviewCriteria.setProjection(Projections.property("volunteer.personId"));

            criteria.add(Property.forName("personId").in(interviewCriteria));
        }
        criteria.add(Restrictions.not(Restrictions.eq("rbcStatusCode", "DN")));
        criteria.add(Restrictions.not(Restrictions.eq("rbcStatusCode", "IA")));

        return criteria;
    }

    @Override
    public void createVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        if (volunteer.getPerson().getPersonId() != null) {
            session.merge(volunteer.getPerson());
        }

        session.save(volunteer);
    }

    @Override
    @CacheEvict(value = "person.person", key = "#volunteer.personId")
    public void updateVolunteer(Volunteer volunteer) {
        Session session = this.sessionFactory.getCurrentSession();
        session.merge(volunteer.getPerson());
        session.merge(volunteer);
{'fraction_non_alphanumeric': 0.10184595798854233, 'fraction_numerical': 0.001967478733869568, 'mean_word_length': 3.498178032274857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4112344', 'n_tokens_mistral': 4746, 'n_tokens_neox': 4600, 'n_words': 909}