package edu.northwestern.bioinformatics.studycalendar.dao;

import edu.northwestern.bioinformatics.studycalendar.StudyCalendarSystemException;
import edu.northwestern.bioinformatics.studycalendar.domain.*;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Hibernate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Date;

public class SubjectDao extends StudyCalendarMutableDomainObjectDao<Subject> {
    @Override
    public Class<Subject> domainClass() {
        return Subject.class;
    }

    public List<Subject> getAll() {
        return getHibernateTemplate().find("from Subject p order by p.lastName, p.firstName");
    }

    public StudySubjectAssignment getAssignment(final Subject subject, final Study study, final Site site) {
        return (StudySubjectAssignment) CollectionUtils.firstElement(getHibernateTemplate().find(
                "from StudySubjectAssignment a where a.subject = ? and a.studySite.site = ? and a.studySite.study = ?",
                new Object[]{subject, site, study}));
    }

    @SuppressWarnings("unchecked")
    public Subject findSubjectByPersonId(final String mrn) {
        List<Subject> results = getHibernateTemplate().find("from Subject s left join fetch s.assignments where s.personId= ?", mrn);
        if (!results.isEmpty()) {
            Subject subject = results.get(0);
            return subject;
        }
        String message = "No subject exist with the given mrn :" + mrn;
        logger.info(message);

        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Subject> findSubjectByFirstNameLastNameAndDoB(final String firstName, final String lastName, Date dateOfBirth) {
        List<Subject> results = getHibernateTemplate().find("from Subject s left join fetch s.assignments where s.firstName= ? and s.lastName= ? and s.dateOfBirth= ?", new Object[] {firstName, lastName, dateOfBirth});
        if (!results.isEmpty()) {
            return results;
        }
        String message = "No subject exist with the given firstName : " + firstName + " , lastName : " + lastName + " ,dateOfBirth : " + dateOfBirth;
        logger.info(message);

        return null;
    }

    @Transactional(readOnly = false)
    public void delete(Subject subject) {
        getHibernateTemplate().delete(subject);
    }
}

@@ -112,36 +112,19 @@
 dar.
-StudyCalendarSystemException
+domain.Site
 ;%0D%0Ai
@@ -186,9 +186,13 @@
 ain.
-*
+Study
 ;%0D%0Ai
@@ -206,10 +206,19 @@
 du.n
-wu
+orthwestern
 .bio
@@ -233,199 +233,177 @@
 ics.
-commons.CollectionUtils;%0D%0Aimport org.hibernate.HibernateException;%0D%0Aimport org.hibernate.Session;%0D%0Aimport org.hibernate.Hibernate;%0D%0Aimport org.springframework.orm.hibernate3.HibernateCallback
+studycalendar.domain.StudySubjectAssignment;%0D%0Aimport edu.northwestern.bioinformatics.studycalendar.domain.Subject;%0D%0Aimport edu.nwu.bioinformatics.commons.CollectionUtils
 ;%0D%0Ai
@@ -473,39 +473,8 @@
 %0D%0A%0D%0A
-import java.sql.SQLException;%0D%0A
 impo
@@ -486,20 +486,20 @@
 va.util.
-List
+Date
 ;%0D%0Aimpor
@@ -510,20 +510,20 @@
 va.util.
-Date
+List
 ;%0D%0A%0D%0Apub
@@ -689,32 +689,137 @@
 lass;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Finds all the subjects%0D%0A     *%0D%0A     * @return      a list of all the subjects%0D%0A     */%0D%0A
     public List%3C
@@ -940,24 +940,409 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Finds the subject assignment for the given subject, study, and site%0D%0A     *%0D%0A     * @param  subject the subject to search with for the assignment%0D%0A     * @param  study the study to search with for the assignment%0D%0A     * @param  site the site to search with for the assignment%0D%0A     * @return      the subject assignment for the given subject, study, and site%0D%0A     */%0D%0A
     public S
@@ -1715,32 +1715,251 @@
 y%7D));%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Finds the subject for the given mrn (person id)%0D%0A     *%0D%0A     * @param  mrn the mrn (person id) to search for the subject with%0D%0A     * @return      the subject that correspnds to the given mrn%0D%0A     */%0D%0A
     @SuppressWar
@@ -2430,24 +2430,442 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Finds all the subjects for the given first name, last name, and birth date.%0D%0A     *%0D%0A     * @param  firstName the first name to search for the subject with%0D%0A     * @param  lastName the lastName to search for the subject with%0D%0A     * @param  dateOfBirth the birth date to search for the subject with%0D%0A     * @return      finds the subject for the given first name, last name and date of birth%0D%0A     */%0D%0A
     @Suppres

Add javadoc to study subject dao.