1,37 → 1,66 |
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; |
|
/** |
* Defines methods all term DAO implementations must implement |
* Implements the Database Access Object for terms |
* |
* @author pelinux |
* @author Thomas Lahn |
*/ |
public interface TermDAO |
public class TermDAO |
{ |
/** |
* @return |
* Database session |
*/ |
List<Term> getTerms(); |
@SessionTarget |
Session session; |
|
/** |
* Delete a term |
* |
* @param termId |
* Term ID |
* Database transaction |
*/ |
void deleteTerm(int termId); |
@TransactionTarget |
Transaction transaction; |
|
/** |
* Retrieves a term by ID |
* Returns a list of all terms |
* |
* @param termId |
* @return |
* @return an ArrayList with all the terms - in case of a problem, an empty |
* list is returned |
*/ |
Term getTermById(int termId); |
@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 |
* |
39,5 → 68,61 |
* Domain object to be saved |
* @return <code>true</code> if successful, <code>false</code> otherwise |
*/ |
boolean saveOrUpdate(Term term); |
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; |
} |
} |