Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 2 → Rev 3

/it-ru/trunk/src/ak/itru/core/model/EventManager.java
0,0 → 1,190
package ak.itru.core.model;
 
import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.itru.util.HibernateUtil;
import ak.itru.util.ModelException;
import ak.itru.util.ModelSecurityException;
 
public class EventManager
{
private static EventManager eventManager = null;
private static boolean registered = false;
 
public static EventManager getInstance()
{
return eventManager;
}
 
protected static void register()
{
synchronized(EventManager.class) {
if(registered) return;
 
registered = true;
try {
HibernateUtil.getConfiguration().addResource(
"ak/itru/core/model/Event.hbm.xml");
HibernateUtil.getConfiguration().addResource(
"ak/itru/core/model/EventAct.hbm.xml");
 
eventManager = new EventManager();
}
catch(Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
}
 
static {
register();
}
 
private EventManager()
{
}
 
public Event create()
throws ModelException
{
Event e = new Event();
e.init();
return e;
}
 
public Event get(Long id)
throws ModelException
{
try {
return (Event)HibernateUtil.currentSession().load(Event.class, id);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public boolean nameExists(Event event, String name)
throws ModelException
{
try {
if(event.getId() == null)
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Event where name = ?",
name, Hibernate.STRING)
.next()).intValue() > 0;
else
return ((Integer)HibernateUtil.currentSession().iterate(
"select count(*) from Event e where name = ? and e != ?",
new Object[] { name, event },
new Type[] { Hibernate.STRING, Hibernate.entity(Event.class) } )
.next()).intValue() > 0;
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void save(Event event)
throws ModelException
{
try {
HibernateUtil.currentSession().saveOrUpdate(event);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public void delete(Event event)
throws ModelException
{
try {
HibernateUtil.currentSession().delete(event);
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public Collection listEvents(boolean enabledOnly)
throws ModelException
{
try {
if(enabledOnly) {
return HibernateUtil.currentSession().find(
"from Event where enabled=?",
Boolean.TRUE, Hibernate.BOOLEAN);
}
else {
return HibernateUtil.currentSession().find("from Event");
}
}
catch(HibernateException ex)
{
throw new ModelException(ex);
}
}
 
public static final Comparator NAME_COMPARATOR = new NameComparator();
public static final Comparator START_COMPARATOR = new StartComparator();
 
private static class NameComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof Event) || !(o2 instanceof Event))
throw new ClassCastException("not a Event");
 
Event a1 = (Event)o1;
Event a2 = (Event)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.getName().compareToIgnoreCase(a2.getName());
}
 
public boolean equals(Object obj)
{
return (obj instanceof NameComparator);
}
}
 
private static class StartComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
if(!(o1 instanceof Event) || !(o2 instanceof Event))
throw new ClassCastException("not a Event");
 
Event a1 = (Event)o1;
Event a2 = (Event)o2;
 
if((a1 == null || a1.getStart() == null) && (a2 == null || a2.getStart() == null))
return 0;
else if((a1 == null || a1.getStart() == null) && (a2 != null && a2.getStart() != null))
return -1;
else if((a1 != null && a1.getStart() != null) && (a2 == null || a2.getStart() == null))
return 1;
else
return a1.getStart().compareTo(a2.getStart());
}
 
public boolean equals(Object obj)
{
return (obj instanceof StartComparator);
}
}
}