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.Relationship; 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 RelationshipDAO { /** * Database session */ @SessionTarget Session session; /** * Database transaction */ @TransactionTarget Transaction transaction; /** * Creates a list of all relationships * * @return an ArrayList with all the relationshops - in case of a problem, an * empty list is returned */ @SuppressWarnings("unchecked") public List getList() { List relationship = null; try { relationship = this.session.createQuery("from Relationship").list(); //$NON-NLS-1$ } catch (Exception e) { e.printStackTrace(); } /* * If no relationship was checked, return an empty list to mitigate null * pointer exceptions */ if (relationship == null) { relationship = new ArrayList(); } return relationship; } /** * Executes the query to save the relationship * * @param relationship * Domain object to be saved * @return true if successful, false otherwise */ public boolean saveOrUpdate(Relationship relationship) { try { relationship.setObjectId(relationship.getObjectId()); this.session.saveOrUpdate(relationship); return true; } catch (Exception e) { this.transaction.rollback(); e.printStackTrace(); return false; } } /** * Delete a relationship * * @param id * Relationship ID */ public void delete(int id) { try { Relationship relationship = (Relationship) this.session.get( Relationship.class, id); this.session.delete(relationship); } catch (Exception e) { this.transaction.rollback(); e.printStackTrace(); } } /** * Retrieves a relationship by ID * * @param id * Term ID * @return The relationship with this id */ public Relationship getById(int id) { Relationship relationship = null; try { relationship = (Relationship) this.session.get(Relationship.class, id); } catch (Exception e) { e.printStackTrace(); } return relationship; } }