package edu.northwestern.bioinformatics.studycalendar.dao;

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

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

/**
 * @author Rhett Sutphin
 */
@Transactional(readOnly = true)
public class StudyDao extends StudyCalendarMutableDomainObjectDao<Study> {

    @Override
    public Class<Study> domainClass() {
        return Study.class;
    }

    @SuppressWarnings( { "unchecked" })
    public List<Study> getAll() {
        return getHibernateTemplate().find("from Study");
    }

    @SuppressWarnings( { "unchecked" })
    public List<StudySubjectAssignment> getAssignmentsForStudy(final Integer studyId) {
        return getHibernateTemplate()
            .find("select a from StudySubjectAssignment a inner join a.studySite ss inner join a.subject p where ss.study.id = ? order by p.lastName, p.firstName",
                studyId);
    }

    @Deprecated // use getByAssignedIdentifier
    public Study getStudyByAssignedIdentifier(final String assignedIdentifier) {
        return getByAssignedIdentifier(assignedIdentifier);
    }

    @SuppressWarnings({"unchecked"})
    public Study getByAssignedIdentifier(String assignedIdentifier) {
        List<Study> results = getHibernateTemplate().find("from Study where assignedIdentifier= ?", assignedIdentifier);
        return CollectionUtils.firstElement(results);
    }

    public String getStudyIdByAssignedIdentifier(final String assignedIdentifier) {
        Object studyId = getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createSQLQuery(
                        "select max(id) from studies where  assigned_identifier='" + assignedIdentifier + "'")
                        .uniqueResult();
            }
        });


        if (studyId != null)
            return studyId.toString();
        return null;
    }


    @Transactional(readOnly = false)
    public void delete(Study study) {
        getHibernateTemplate().delete(study);
    }
}

@@ -733,32 +733,150 @@
 y.class;%0A    %7D%0A%0A
+    /**%0A     * Finds all the studies available%0A     *%0A     * @return      a list of all the available studies%0A     */%0A
     @SuppressWar
@@ -990,32 +990,236 @@
 Study%22);%0A    %7D%0A%0A
+    /**%0A     * Finds the assignments for a particular study%0A     *%0A     * @param  studyId the study to get the assignments for%0A     * @return      a list of the assignments for the study id given%0A     */%0A
     @SuppressWar
@@ -1756,24 +1756,267 @@
 er);%0A    %7D%0A%0A
+    /**%0A     * Finds a study based on the assignment identifer given%0A     *%0A     * @param  assignedIdentifier the assignment identifier for the study to search for%0A     * @return      the study that has the given assignment identifier%0A     */%0A
     @Suppres
@@ -2288,24 +2288,313 @@
 ts);%0A    %7D%0A%0A
+    /**%0A     * Gets the study id that corresponds to the given assignment identifier%0A     *%0A     * @param  assignedIdentifier the assignment identifier to search for the study to search for%0A     * @return      the study id that corresponds to the study assignment identifier given%0A     */%0A
     public S
@@ -3169,16 +3169,106 @@
 %0A    %7D%0A%0A
+    /**%0A     * Deletes a study%0A     *%0A     * @param  study the study to be deleted%0A     */
 %0A    @Tr

Add javadoc to study dao.