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 SystemUserManager |
||
9 | { |
||
10 | private static boolean registered = false; |
||
11 | protected static void register() |
||
12 | { |
||
13 | synchronized(SystemUserManager.class) { |
||
14 | if(registered) return; |
||
15 | |||
16 | registered = true; |
||
17 | try { |
||
18 | HibernateUtil.getConfiguration().addResource( |
||
904 | dev | 19 | "/ak/hostcaptain/core/model/SystemUser.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 SystemUserManager() |
||
33 | { |
||
34 | } |
||
35 | |||
36 | public SystemUser create() |
||
37 | { |
||
38 | return new SystemUser(); |
||
39 | } |
||
40 | |||
41 | public SystemUser get(Long id) |
||
42 | throws ModelException |
||
43 | { |
||
44 | try { |
||
45 | return (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id); |
||
46 | } |
||
47 | catch(HibernateException ex) |
||
48 | { |
||
49 | throw new ModelException(ex); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public SystemUser findForName(String name) |
||
54 | throws ModelException |
||
55 | { |
||
56 | try { |
||
57 | List list = HibernateUtil.currentSession().find( |
||
58 | "from SystemUser where name=?", name, Hibernate.STRING); |
||
59 | |||
60 | if(list.size() == 0) |
||
61 | return null; |
||
62 | else |
||
63 | return (SystemUser)list.get(0); |
||
64 | } |
||
65 | catch(HibernateException ex) |
||
66 | { |
||
67 | throw new ModelException(ex); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public SystemUser findForUid(Integer uid) |
||
72 | throws ModelException |
||
73 | { |
||
74 | try { |
||
75 | List list = HibernateUtil.currentSession().find( |
||
76 | "from SystemUser where uid=?", uid, Hibernate.INTEGER); |
||
77 | |||
78 | if(list.size() == 0) |
||
79 | return null; |
||
80 | else |
||
81 | return (SystemUser)list.get(0); |
||
82 | } |
||
83 | catch(HibernateException ex) |
||
84 | { |
||
85 | throw new ModelException(ex); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public void save(SystemUser systemUser) |
||
90 | throws ModelException |
||
91 | { |
||
92 | try { |
||
93 | HibernateUtil.currentSession().saveOrUpdate(systemUser); |
||
94 | } |
||
95 | catch(HibernateException ex) |
||
96 | { |
||
97 | throw new ModelException(ex); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public void delete(SystemUser systemUser) |
||
102 | throws ModelException |
||
103 | { |
||
104 | try { |
||
105 | HibernateUtil.currentSession().delete(systemUser); |
||
106 | } |
||
107 | catch(HibernateException ex) |
||
108 | { |
||
109 | throw new ModelException(ex); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | public Collection listSystemUsers() |
||
114 | throws ModelException |
||
115 | { |
||
116 | try { |
||
117 | return HibernateUtil.currentSession().find("from SystemUser"); |
||
118 | } |
||
119 | catch(HibernateException ex) |
||
120 | { |
||
121 | throw new ModelException(ex); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | private static SystemUserManager systemUserManager = null; |
||
126 | |||
127 | public static SystemUserManager getInstance() |
||
128 | { |
||
129 | if(systemUserManager == null) |
||
130 | systemUserManager = new SystemUserManager(); |
||
131 | |||
132 | return systemUserManager; |
||
133 | } |
||
134 | |||
135 | public static final Comparator UID_COMPARATOR = new UidComparator(); |
||
136 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
||
137 | |||
138 | private static class UidComparator |
||
139 | implements Comparator |
||
140 | { |
||
141 | public int compare(Object o1, Object o2) |
||
142 | { |
||
143 | if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser)) |
||
144 | throw new ClassCastException("not a SystemUser"); |
||
145 | |||
146 | SystemUser a1 = (SystemUser)o1; |
||
147 | SystemUser a2 = (SystemUser)o2; |
||
148 | |||
149 | if(a1 == null && a2 == null) |
||
150 | return 0; |
||
151 | else if(a1 == null && a2 != null) |
||
152 | return -1; |
||
153 | else if(a1 != null && a2 == null) |
||
154 | return 1; |
||
155 | else |
||
156 | return a1.getUid().compareTo(a2.getUid()); |
||
157 | } |
||
158 | |||
159 | public boolean equals(Object obj) |
||
160 | { |
||
161 | return (obj instanceof UidComparator); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | private static class NameComparator |
||
166 | implements Comparator |
||
167 | { |
||
168 | public int compare(Object o1, Object o2) |
||
169 | { |
||
170 | if(!(o1 instanceof SystemUser) || !(o2 instanceof SystemUser)) |
||
171 | throw new ClassCastException("not a SystemUser"); |
||
172 | |||
173 | SystemUser a1 = (SystemUser)o1; |
||
174 | SystemUser a2 = (SystemUser)o2; |
||
175 | |||
176 | if(a1 == null && a2 == null) |
||
177 | return 0; |
||
178 | else if(a1 == null && a2 != null) |
||
179 | return -1; |
||
180 | else if(a1 != null && a2 == null) |
||
181 | return 1; |
||
182 | else |
||
183 | return a1.getName().compareToIgnoreCase(a2.getName()); |
||
184 | } |
||
185 | |||
186 | public boolean equals(Object obj) |
||
187 | { |
||
188 | return (obj instanceof NameComparator); |
||
189 | } |
||
190 | } |
||
191 | } |