Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
904 | dev | 1 | package ak.hostcaptain.core.model; |
899 | dev | 2 | |
3 | import java.util.*; |
||
4 | import net.sf.hibernate.*; |
||
904 | dev | 5 | import ak.hostcaptain.util.HibernateUtil; |
6 | import ak.hostcaptain.util.ModelException; |
||
899 | dev | 7 | |
8 | public class InetDomainManager |
||
9 | { |
||
10 | private static boolean registered = false; |
||
11 | protected static void register() |
||
12 | { |
||
13 | synchronized(InetDomainManager.class) { |
||
14 | if(registered) return; |
||
15 | |||
16 | registered = true; |
||
17 | try { |
||
18 | HibernateUtil.getConfiguration().addResource( |
||
904 | dev | 19 | "/ak/hostcaptain/core/model/InetDomain.hbm.xml"); |
899 | dev | 20 | } |
21 | catch(Exception ex) { |
||
22 | ex.printStackTrace(); |
||
23 | throw new RuntimeException(ex.getMessage()); |
||
24 | } |
||
25 | } |
||
26 | } |
||
27 | |||
28 | static { |
||
29 | register(); |
||
30 | } |
||
31 | |||
32 | private InetDomainManager() |
||
33 | { |
||
34 | } |
||
35 | |||
36 | public InetDomain create() |
||
37 | { |
||
38 | return new InetDomain(); |
||
39 | } |
||
40 | |||
41 | public InetDomain get(Long id) |
||
42 | throws ModelException |
||
43 | { |
||
44 | try { |
||
45 | return (InetDomain)HibernateUtil.currentSession().load(InetDomain.class, id); |
||
46 | } |
||
47 | catch(HibernateException ex) |
||
48 | { |
||
49 | throw new ModelException(ex); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public InetDomain findForName(String name) |
||
54 | throws ModelException |
||
55 | { |
||
56 | try { |
||
57 | List list = HibernateUtil.currentSession().find( |
||
58 | "from InetDomain where name=?", name, Hibernate.STRING); |
||
59 | |||
60 | if(list.size() == 0) |
||
61 | return null; |
||
62 | else |
||
63 | return (InetDomain)list.get(0); |
||
64 | } |
||
65 | catch(HibernateException ex) |
||
66 | { |
||
67 | throw new ModelException(ex); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public void save(InetDomain inetDomain) |
||
72 | throws ModelException |
||
73 | { |
||
74 | try { |
||
75 | HibernateUtil.currentSession().saveOrUpdate(inetDomain); |
||
76 | } |
||
77 | catch(HibernateException ex) |
||
78 | { |
||
79 | throw new ModelException(ex); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public void delete(InetDomain inetDomain) |
||
84 | throws ModelException |
||
85 | { |
||
86 | try { |
||
87 | HibernateUtil.currentSession().delete(inetDomain); |
||
88 | } |
||
89 | catch(HibernateException ex) |
||
90 | { |
||
91 | throw new ModelException(ex); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | public Collection listInetDomains() |
||
96 | throws ModelException |
||
97 | { |
||
98 | try { |
||
99 | return HibernateUtil.currentSession().find("from InetDomain"); |
||
100 | } |
||
101 | catch(HibernateException ex) |
||
102 | { |
||
103 | throw new ModelException(ex); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | private static InetDomainManager inetDomainManager = null; |
||
108 | |||
109 | public static InetDomainManager getInstance() |
||
110 | { |
||
111 | if(inetDomainManager == null) |
||
112 | inetDomainManager = new InetDomainManager(); |
||
113 | |||
114 | return inetDomainManager; |
||
115 | } |
||
116 | |||
117 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
||
118 | |||
119 | private static class NameComparator |
||
120 | implements Comparator |
||
121 | { |
||
122 | public int compare(Object o1, Object o2) |
||
123 | { |
||
124 | if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain)) |
||
125 | throw new ClassCastException("not a InetDomain"); |
||
126 | |||
127 | InetDomain a1 = (InetDomain)o1; |
||
128 | InetDomain a2 = (InetDomain)o2; |
||
129 | |||
130 | if(a1 == null && a2 == null) |
||
131 | return 0; |
||
132 | else if(a1 == null && a2 != null) |
||
133 | return -1; |
||
134 | else if(a1 != null && a2 == null) |
||
135 | return 1; |
||
136 | else |
||
137 | return a1.getName().compareToIgnoreCase(a2.getName()); |
||
138 | } |
||
139 | |||
140 | public boolean equals(Object obj) |
||
141 | { |
||
142 | return (obj instanceof NameComparator); |
||
143 | } |
||
144 | } |
||
145 | } |