0,0 → 1,158 |
package ak.webcontrol.core.model; |
|
import java.util.*; |
import net.sf.hibernate.*; |
import ak.webcontrol.util.HibernateUtil; |
import ak.webcontrol.util.ModelException; |
|
public class MailboxManager |
{ |
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/Mailbox.hbm.xml"); |
} |
catch(Exception ex) { |
ex.printStackTrace(); |
throw new RuntimeException(ex.getMessage()); |
} |
} |
} |
|
static { |
register(); |
} |
|
private MailboxManager() |
{ |
} |
|
public Mailbox create() |
{ |
return new Mailbox(); |
} |
|
public Mailbox get(Long id) |
throws ModelException |
{ |
try { |
return (Mailbox)HibernateUtil.currentSession().load(Mailbox.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Mailbox findForLogin(String login) |
throws ModelException |
{ |
try { |
List list = HibernateUtil.currentSession().find( |
"from Mailbox where login=?", login, Hibernate.STRING); |
|
if(list.size() == 0) |
return null; |
else |
return (Mailbox)list.get(0); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(Mailbox mailbox) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().saveOrUpdate(mailbox); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void delete(Mailbox mailbox) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().delete(mailbox); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listMailboxes() |
throws ModelException |
{ |
try { |
return HibernateUtil.currentSession().find("from Mailbox"); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listMailboxes(User owner) |
throws ModelException |
{ |
try { |
return HibernateUtil.currentSession().find( |
"from Mailbox where owner=?", owner, Hibernate.entity(User.class)); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
private static MailboxManager mailboxManager = null; |
|
public static MailboxManager getInstance() |
{ |
if(mailboxManager == null) |
mailboxManager = new MailboxManager(); |
|
return mailboxManager; |
} |
|
public static final Comparator LOGIN_COMPARATOR = new LoginComparator(); |
|
private static class LoginComparator |
implements Comparator |
{ |
public int compare(Object o1, Object o2) |
{ |
if(!(o1 instanceof Mailbox) || !(o2 instanceof Mailbox)) |
throw new ClassCastException("not a Mailbox"); |
|
Mailbox a1 = (Mailbox)o1; |
Mailbox a2 = (Mailbox)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); |
} |
} |
} |