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 getList() { List 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(); } return term; } /** * Executes the query to save the term * * @param term * Domain object to be saved * @return true if successful, false 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; } }