Subversion Repositories WebE

Rev

Rev 34 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

1
package ch.ffhs.webE.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import ch.ffhs.webE.domain.Term;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

/**
 * Implements the Database Access Object for terms
 *
 * @author Thomas Lahn
 */

public class TermDAO
{
  /**
   * Database session
   */

  @SessionTarget
  Session session;

  /**
   * Database transaction
   */

  @TransactionTarget
  Transaction transaction;

  /**
   * Returns a list of all terms
   *
   * @return an ArrayList with all the terms - in case of a problem, an empty
   *         list is returned
   */

  @SuppressWarnings("unchecked")
  public List<Term> getList()
  {
    List<Term> term = null;
    try
    {
      term = this.session.createQuery("from Term").list(); //$NON-NLS-1$
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    /*
     * If no term was checked, return an empty list to mitigate null pointer
     * exceptions
     */

    if (term == null)
    {
      term = new ArrayList<Term>();
    }

    return term;
  }

  /**
   * Executes the query to save the term
   *
   * @param term
   *          Domain object to be saved
   * @return <code>true</code> if successful, <code>false</code> otherwise
   */

  public boolean saveOrUpdate(Term term)
  {
    try
    {
      term.setObjectId(term.getObjectId());
      this.session.saveOrUpdate(term);
      return true;
    }
    catch (Exception e)
    {
      this.transaction.rollback();
      e.printStackTrace();
      return false;
    }
  }

  /**
   * Delete a term
   *
   * @param termId
   *          Term ID
   */

  public void delete(int termId)
  {
    try
    {
      Term term = (Term) this.session.get(Term.class, termId);
      this.session.delete(term);
    }
    catch (Exception e)
    {
      this.transaction.rollback();
      e.printStackTrace();
    }
  }

  /**
   * Retrieves a term by ID
   *
   * @param termId
   * @return
   */

  public Term getById(int termId)
  {
    Term term = null;

    try
    {
      term = (Term) this.session.get(Term.class, termId);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    return term;
  }
}