0,0 → 1,151 |
package ak.kickup.core.model; |
|
import java.util.*; |
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 ApartmentManager |
{ |
private static ApartmentManager apartmentManager = null; |
private static boolean registered = false; |
|
public static ApartmentManager getInstance() |
{ |
return apartmentManager; |
} |
|
protected static void register() |
{ |
synchronized(ApartmentManager.class) { |
if(registered) return; |
|
registered = true; |
try { |
HibernateUtil.getConfiguration().addResource( |
"ak/kickup/core/model/Apartment.hbm.xml"); |
|
apartmentManager = new ApartmentManager(); |
} |
catch(Exception ex) { |
ex.printStackTrace(); |
throw new RuntimeException(ex.getMessage()); |
} |
} |
} |
|
static { |
register(); |
} |
|
private ApartmentManager() |
{ |
} |
|
public Apartment create() |
throws ModelException |
{ |
return new Apartment(); |
} |
|
public Apartment get(Long id) |
throws ModelException |
{ |
try { |
return (Apartment)HibernateUtil.currentSession().load(Apartment.class, id); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public boolean nameExists(Apartment apartment, String name) |
throws ModelException |
{ |
try { |
if(apartment.getId() == null) |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from Apartment where name = ?", |
name, Hibernate.STRING) |
.next()).intValue() > 0; |
else |
return ((Integer)HibernateUtil.currentSession().iterate( |
"select count(*) from Apartment a where name = ? and a != ?", |
new Object[] { name, apartment }, |
new Type[] { Hibernate.STRING, Hibernate.entity(Apartment.class) } ) |
.next()).intValue() > 0; |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void save(Apartment apartment) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().saveOrUpdate(apartment); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public void delete(Apartment apartment) |
throws ModelException |
{ |
try { |
HibernateUtil.currentSession().delete(apartment); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public Collection listApartments() |
throws ModelException |
{ |
try { |
return HibernateUtil.currentSession().find("from Apartment"); |
} |
catch(HibernateException ex) |
{ |
throw new ModelException(ex); |
} |
} |
|
public static final Comparator NAME_COMPARATOR = new NameComparator(); |
|
private static class NameComparator |
implements Comparator |
{ |
public int compare(Object o1, Object o2) |
{ |
if(!(o1 instanceof Apartment) || !(o2 instanceof Apartment)) |
throw new ClassCastException("not a Apartment"); |
|
Apartment a1 = (Apartment)o1; |
Apartment a2 = (Apartment)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); |
} |
} |
} |