0,0 → 1,170 |
package ak.kickup.core.model; |
|
import java.util.*; |
import java.io.UnsupportedEncodingException; |
import net.sf.hibernate.*; |
import net.sf.hibernate.type.Type; |
import ak.kickup.util.HibernateUtil; |
import ak.kickup.util.ModelException; |
import ak.kickup.util.ModelSecurityException; |
|
public class ParticipantManager |
{ |
private static ParticipantManager participantManager = null; |
private static boolean registered = false; |
|
public static ParticipantManager getInstance() |
{ |
return participantManager; |
} |
|
protected static void register() |
{ |
synchronized(ParticipantManager.class) { |
if(registered) return; |
|
registered = true; |
try { |
HibernateUtil.getConfiguration().addResource( |
"ak/kickup/core/model/Participant.hbm.xml"); |
|
participantManager = new ParticipantManager(); |
} |
catch(Exception ex) { |
ex.printStackTrace(); |
throw new RuntimeException(ex.getMessage()); |
} |
} |
} |
|
static { |
register(); |
} |
|
private ParticipantManager() |
{ |
} |
|
public Participant create() |
throws ModelException |
{ |
return new Participant(); |
} |
|
public Participant get(Long id) |
throws ModelException |
{ |
try { |
return (Participant)HibernateUtil.currentSession().load(Participant.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public String generateIdent() |
throws ModelException |
{ |
String ident; |
|
do { |
byte[] buf = new byte[6]; |
|
for(int i = 0; i < buf.length; i++) { |
buf[i] = (byte)(Math.random() * 10 + 48); |
} |
|
try { |
ident = new String(buf, "ascii"); |
} |
catch(UnsupportedEncodingException ex) { |
throw new ModelException(ex); |
} |
} while(identExists(ident)); |
|
return ident; |
} |
|
public boolean identExists(String ident) |
throws ModelException |
{ |
try { |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from Participant where ident = ?", |
ident, Hibernate.STRING) |
.next()).intValue() > 0; |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(Participant participant) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().saveOrUpdate(participant); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void delete(Participant participant) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().delete(participant); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listParticipants(Event event) |
throws ModelException |
{ |
try { |
return HibernateUtil.currentSession().find( |
"from Participant where event = ?", |
event, Hibernate.entity(Event.class)); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public static final Comparator NICK_COMPARATOR = new NickComparator(); |
|
private static class NickComparator |
implements Comparator |
{ |
public int compare(Object o1, Object o2) |
{ |
if(!(o1 instanceof Participant) || !(o2 instanceof Participant)) |
throw new ClassCastException("not a Participant"); |
|
Participant a1 = (Participant)o1; |
Participant a2 = (Participant)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.getNick().compareToIgnoreCase(a2.getNick()); |
} |
|
public boolean equals(Object obj) |
{ |
return (obj instanceof NickComparator); |
} |
} |
} |