package edu.northwestern.bioinformatics.studycalendar.dao;

import edu.northwestern.bioinformatics.studycalendar.domain.Site;
import edu.nwu.bioinformatics.commons.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author Padmaja Vedula
 * @author Rhett Sutphin
 */
@Transactional(readOnly = true)
public class SiteDao extends StudyCalendarMutableDomainObjectDao<Site> implements Serializable {

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

    public List<Site> getAll() {
        return getHibernateTemplate().find("from Site");
    }

    // TODO: this method assumes success
    public Site getByName(final String name) {
        List<Site> results = getHibernateTemplate().find("from Site where name= ?", name);
        return results.get(0);
    }

    public Site getByAssignedIdentifier(final String assignedIdentifier) {
        List<Site> results = getHibernateTemplate().find("from Site where assignedIdentifier= ? or (assignedIdentifier=null and name=?)", new String[]{assignedIdentifier,assignedIdentifier});
        return CollectionUtils.firstElement(results);
    }

    public int getCount() {
        Long count = (Long) getHibernateTemplate().find("select COUNT(s) from Site s").get(0);
        return count.intValue();
    }

    public void delete(final Site site) {
        getHibernateTemplate().delete(site);
    }
}

@@ -598,145 +598,404 @@
     
-public List%3CSite%3E getAll() %7B%0D%0A        return getHibernateTemplate().find(%22from Site%22);%0D%0A    %7D%0D%0A%0D%0A    // TODO: this method assumes success
+/**%0D%0A     * Finds all the sites available%0D%0A     *%0D%0A     * @return      a list of all the available sites%0D%0A     */%0D%0A    public List%3CSite%3E getAll() %7B%0D%0A        return getHibernateTemplate().find(%22from Site%22);%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Finds the site with the given name%0D%0A     *%0D%0A     * @param  name the name of the site to find%0D%0A     * @return      a site that corresponds to the name given%0D%0A     */
 %0D%0A  
@@ -1048,37 +1048,58 @@
         
-List%3CSite%3E results = 
+return CollectionUtils.%3CSite%3EfirstElement(
 getHiber
@@ -1154,50 +1154,266 @@
 ame)
+)
 ;%0D%0A    
-    return results.get(0);
+%7D%0D%0A%0D%0A    /**%0D%0A     * Finds a site by it's assigned identifier%0D%0A     *%0D%0A     * @param  assignedIdentifier the assigned identifier for the site we want to search for%0D%0A     * @return      a site that corresponds to the given assigned identifier
 %0D%0A    
-%7D%0D%0A
+ */
 %0D%0A  
@@ -1735,32 +1735,170 @@
 lts);%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Returns the number of sites available%0D%0A     *%0D%0A     * @return      an integer for the number of sites available%0D%0A     */%0D%0A
     public int g
@@ -2045,24 +2045,113 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Deletes a site%0D%0A     *%0D%0A     * @param  site the site to delete%0D%0A     */%0D%0A
     public v

Add javadoc to site dao.