/**
 * @author dgeorge
 * 
 * $Id: PersonManagerImpl.java,v 1.5 2005-09-23 14:55:12 georgeda Exp $
 * 
 * $Log: not supported by cvs2svn $
 * 
 */
package gov.nih.nci.camod.service.impl;

import gov.nih.nci.camod.domain.Person;
import gov.nih.nci.camod.service.PersonManager;
import gov.nih.nci.common.persistence.Persist;
import gov.nih.nci.common.persistence.Search;
import gov.nih.nci.common.persistence.exception.PersistenceException;
import gov.nih.nci.common.persistence.hibernate.HibernateUtil;
import gov.nih.nci.common.persistence.hibernate.eqbe.Evaluation;
import gov.nih.nci.common.persistence.hibernate.eqbe.Evaluator;

import java.util.List;

/**
 * Class that handles saving/fetching Person objects
 */
public class PersonManagerImpl extends BaseManager implements PersonManager {

    /**
     * Get all people in the system
     * 
     * @returns a list of all people in the system
     * 
     * @exception Exception
     *                If an error occurrs fetching the users
     */
    public List getAll() throws Exception {
        List persons = null;

        try {
            persons = Search.query(Person.class);
        } catch (Exception e) {
            log.error("Exception in getAll", e);
            throw e;
        }

        return persons;
    }

    /**
     * Get a person object by unique id
     * 
     * @param id
     *            The username of the person to be fetched
     * 
     * @returns the Person object corresponding to the id
     * 
     * @exception Exception
     *                If an error occurrs fetching the user
     */
    public Person get(String id) throws Exception {
        Person person = null;

        try {
            person = (Person) Search.queryById(Person.class, new Long(id));
        } catch (PersistenceException pe) {
            log.error("PersistenceException in get", pe);
            throw pe;
        } catch (Exception e) {
            log.error("Exception in get", e);
            throw e;
        }

        return person;
    }

    /**
     * Get a person object by username
     * 
     * @param inUsername
     *            The username of the person to be fetched
     * 
     * @returns the Person object corresponding to the username
     * 
     * @exception Exception
     *                If an error occurrs fetching the user
     */
    public Person getByUsername(String inUsername) throws Exception {

        Person person = null;

        try {

            // The following two objects are needed for eQBE.
            Person thePerson = new Person();
            thePerson.setUsername(inUsername);

            // Apply evaluators to object properties
            Evaluation theEvaluation = new Evaluation();
            theEvaluation.addEvaluator("person.username", Evaluator.EQUAL);

            List thePersonList = Search.query(thePerson, theEvaluation);

            if (thePersonList != null && thePersonList.size() > 0) {
                person = (Person) thePersonList.get(0);
            }

        } catch (PersistenceException pe) {
            log.error("PersistenceException in getByUsername", pe);
            throw pe;
        } catch (Exception e) {
            log.error("Exception in getByUsername", e);
            throw e;
        }

        return person;
    }

    /**
     * Save the person object
     * 
     * @param person
     *            The person to be saved
     * 
     * @exception Exception
     *                If we're unable to save the person
     */
    public void save(Person person) throws Exception {

        log.trace("Entering save");

        try {

            // Begin an transaction
            HibernateUtil.beginTransaction();

            // Save the object
            Persist.save(person);

            // Commit an transaction
            HibernateUtil.commitTransaction();

        } catch (Exception e) {

            log.error("Exception in save", e);

            // Rollback a transaction
            HibernateUtil.rollbackTransaction();

            throw e;
        }

        log.trace("Exiting save");
    }

    /**
     * Remove the person based on the id
     * 
     * @param id
     *            The unique id for the Person
     * 
     * @exception Exception
     *                If it was unable to remove the person
     */
    public void remove(String id) throws Exception {
        try {
            // Begin an transaction
            HibernateUtil.beginTransaction();

            Persist.deleteById(Person.class, new Long(id));

            // Commit an transaction
            HibernateUtil.commitTransaction();

        } catch (Exception e) {
            log.error("Exception in getByUsername", e);

            // Rollback a transaction
            HibernateUtil.rollbackTransaction();

            throw e;
        }
    }
}

@@ -59,29 +59,29 @@
 v 1.
-5
+6
  2005-
-09-23 14:55:12
+10-11 18:13:36
  geo
@@ -132,16 +132,108 @@
 s2svn $%0A
+ * Revision 1.5  2005/09/23 14:55:12  georgeda%0A * Made SexDistribution a reference table%0A *%0A
  * %0A */%0A
@@ -2519,32 +2519,97 @@
 person = null;%0A%0A
+        if (inUsername != null && inUsername.length() %3E 0) %7B%0A    
         try %7B%0A%0A 
@@ -2599,32 +2599,36 @@
          try %7B%0A%0A
+    
             // T
@@ -2681,24 +2681,28 @@
             
+  
+  
 Person thePe
@@ -2718,24 +2718,28 @@
 w Person();%0A
+    
             
@@ -2786,16 +2786,20 @@
         
+   
+ 
 // Apply
@@ -2831,16 +2831,20 @@
 perties%0A
+    
         
@@ -2888,24 +2888,28 @@
 aluation();%0A
+    
             
@@ -2981,24 +2981,28 @@
             
+    
 List thePers
@@ -3059,24 +3059,28 @@
             
+    
 if (thePerso
@@ -3132,32 +3132,36 @@
                 
+ 
+   
 person = (Person
@@ -3188,32 +3188,36 @@
 0);%0A            
+    
 %7D%0A%0A        %7D cat
@@ -3203,32 +3203,36 @@
      %7D%0A%0A        
+  
+  
 %7D catch (Persist
@@ -3243,32 +3243,36 @@
 Exception pe) %7B%0A
+    
             log.
@@ -3327,32 +3327,36 @@
 e);%0A            
+   
+ 
 throw pe;%0A      
@@ -3341,32 +3341,36 @@
       throw pe;%0A
+    
         %7D catch 
@@ -3377,32 +3377,36 @@
 (Exception e) %7B%0A
+    
             log.
@@ -3449,32 +3449,36 @@
 e);%0A            
+    
 throw e;%0A       
@@ -3470,34 +3470,47 @@
 hrow e;%0A        
+    %7D%0A        
 %7D
-%0A
 %0A        return 

Return a null pointer if the username is null