0,0 → 1,162 |
package ak.webcontrol.core.model; |
|
import java.util.*; |
import net.sf.hibernate.*; |
import ak.webcontrol.util.HibernateUtil; |
import ak.webcontrol.util.ModelException; |
|
public class UserManager |
{ |
private static boolean registered = false; |
protected static void register() |
{ |
synchronized(MailboxManager.class) { |
if(registered) return; |
|
registered = true; |
try { |
HibernateUtil.getConfiguration().addResource( |
"/ak/webcontrol/core/model/User.hbm.xml"); |
} |
catch(Exception ex) { |
ex.printStackTrace(); |
throw new RuntimeException(ex.getMessage()); |
} |
} |
} |
|
static { |
register(); |
} |
|
private UserManager() |
{ |
} |
|
public User create() |
{ |
return new User(); |
} |
|
public User get(Long id) |
throws ModelException |
{ |
try { |
return (User)HibernateUtil.currentSession().load(User.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public User findForLogin(String login) |
throws ModelException |
{ |
try { |
List list = HibernateUtil.currentSession().find( |
"from User where login=?", login, Hibernate.STRING); |
|
if(list.size() == 0) |
return null; |
else |
return (User)list.get(0); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(User user) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().saveOrUpdate(user); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void delete(User user) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().delete(user); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listUsers() |
throws ModelException |
{ |
try { |
return HibernateUtil.currentSession().find("from User"); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public User loginUser(String login, String password) |
throws ModelException |
{ |
if(login == null || password == null) |
return null; |
|
User user = findForLogin(login); |
|
if(user != null) { |
if(user.checkPassword(password)) |
return user; |
} |
|
// wrong login or password |
return null; |
} |
|
private static UserManager userManager = null; |
|
public static UserManager getInstance() |
{ |
if(userManager == null) |
userManager = new UserManager(); |
|
return userManager; |
} |
|
public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
|
private static class LoginComparator |
implements Comparator |
{ |
public int compare(Object o1, Object o2) |
{ |
if(!(o1 instanceof User) || !(o2 instanceof User)) |
throw new ClassCastException("not a User"); |
|
User a1 = (User)o1; |
User a2 = (User)o2; |
|
if(a1 == null && a2 == null) |
return 0; |
else if(a1 == null && a2 != null) |
return -1; |
else if(a1 != null && a2 == null) |
return 1; |
else |
return a1.getLogin().compareToIgnoreCase(a2.getLogin()); |
} |
|
public boolean equals(Object obj) |
{ |
return (obj instanceof LoginComparator); |
} |
} |
} |