Rev 27 |
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.User;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
public class UserDAO
{
@SessionTarget
Session session
;
@TransactionTarget
Transaction transaction
;
/**
* 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();
}
// If no user was checked, return an empty list to mitigate null pointer
// exceptions
if (user ==
null)
{
user =
new ArrayList<User
>();
}
return user
;
}
/**
* 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();
}
}
/**
* 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
;
}
}