1,19 → 1,134 |
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.User; |
|
public interface UserDAO |
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget; |
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget; |
|
public class UserDAO |
{ |
|
List<User> listUser(); |
@SessionTarget |
Session session; |
@TransactionTarget |
Transaction transaction; |
|
User searchUsername(String username); |
/** |
* Creates a list of all the registered users |
* |
* @return an ArrayList with all the users - in case of a problem, an empty |
* list is returned |
*/ |
@SuppressWarnings("unchecked") |
public List<User> getList() |
{ |
List<User> user = null; |
try |
{ |
user = this.session.createQuery("from User").list(); |
} |
catch (Exception e) |
{ |
e.printStackTrace(); |
} |
|
void deleteUser(int userId); |
// If no user was checked, return an empty list to mitigate null pointer |
// exceptions |
if (user == null) |
{ |
user = new ArrayList<User>(); |
} |
return user; |
} |
|
User listUserById(int userId); |
/** |
* Executes the query to save the user |
* |
* @param user |
* Domain object to be saved |
* @return void |
*/ |
public void saveOrUpdate(User user) |
{ |
try |
{ |
this.session.saveOrUpdate(user); |
} |
catch (Exception e) |
{ |
this.transaction.rollback(); |
e.printStackTrace(); |
} |
} |
|
void saveOrUpdateUser(User user); |
} |
/** |
* Used to delete a user. |
* |
* @param userId |
*/ |
public void delete(int userId) |
{ |
try |
{ |
User user = (User) this.session.get(User.class, userId); |
this.session.delete(user); |
} |
catch (Exception e) |
{ |
this.transaction.rollback(); |
e.printStackTrace(); |
} |
} |
|
/** |
* Returns a single user with this user name (used for login) |
* |
* @param username |
* : String - entire user name |
* @return User: Returns a user object if something is found. If not, null is |
* returned |
*/ |
public User getByUsername(String username) |
{ |
User user = null; |
|
// Exec query |
try |
{ |
user = (User) this.session |
.createQuery("FROM User " + "WHERE username = :username") |
.setParameter("username", username).uniqueResult(); |
} |
catch (Exception e) |
{ |
// TODO: Log error |
} |
return user; |
} |
|
/** |
* Used to list a single user by Id. |
* |
* @param userId |
* @return |
*/ |
public User getById(int userId) |
{ |
User user = null; |
try |
{ |
user = (User) this.session.get(User.class, userId); |
} |
catch (Exception e) |
{ |
e.printStackTrace(); |
} |
return user; |
} |
} |