Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
924 | dev | 1 | package ak.hostadmiral.core.model; |
919 | dev | 2 | |
3 | import java.util.*; |
||
4 | import net.sf.hibernate.*; |
||
923 | dev | 5 | import net.sf.hibernate.type.Type; |
924 | dev | 6 | import ak.hostadmiral.util.HibernateUtil; |
7 | import ak.hostadmiral.util.ModelException; |
||
8 | import ak.hostadmiral.util.ModelSecurityException; |
||
919 | dev | 9 | |
10 | public class InetDomainManager |
||
11 | { |
||
12 | private static boolean registered = false; |
||
13 | protected static void register() |
||
14 | { |
||
15 | synchronized(InetDomainManager.class) { |
||
16 | if(registered) return; |
||
17 | |||
18 | registered = true; |
||
19 | try { |
||
20 | HibernateUtil.getConfiguration().addResource( |
||
924 | dev | 21 | "/ak/hostadmiral/core/model/InetDomain.hbm.xml"); |
919 | dev | 22 | } |
23 | catch(Exception ex) { |
||
24 | ex.printStackTrace(); |
||
25 | throw new RuntimeException(ex.getMessage()); |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | |||
30 | static { |
||
31 | register(); |
||
32 | } |
||
33 | |||
34 | private InetDomainManager() |
||
35 | { |
||
36 | } |
||
37 | |||
38 | public InetDomain create(User editor) |
||
39 | throws ModelException |
||
40 | { |
||
41 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
||
42 | |||
43 | return new InetDomain(); |
||
44 | } |
||
45 | |||
46 | public boolean allowedToCreate(User editor) |
||
47 | throws ModelException |
||
48 | { |
||
921 | dev | 49 | return InetDomain.allowedToCreate(this, editor); |
919 | dev | 50 | } |
51 | |||
52 | public InetDomain get(User editor, Long id) |
||
53 | throws ModelException |
||
54 | { |
||
55 | InetDomain domain; |
||
56 | |||
57 | try { |
||
58 | domain = (InetDomain)HibernateUtil.currentSession().load( |
||
59 | InetDomain.class, id); |
||
60 | } |
||
61 | catch(HibernateException ex) |
||
62 | { |
||
63 | throw new ModelException(ex); |
||
64 | } |
||
65 | |||
66 | if(!domain.viewableBy(editor)) |
||
67 | throw new ModelSecurityException(); |
||
68 | |||
69 | return domain; |
||
70 | } |
||
71 | |||
923 | dev | 72 | public boolean nameExists(User editor, InetDomain domain, String name) |
73 | throws ModelException |
||
74 | { |
||
75 | try { |
||
76 | if(domain.getId() == null) |
||
77 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
78 | "select count(*) from InetDomain d where name = ?", |
||
79 | name, Hibernate.STRING) |
||
80 | .next()).intValue() > 0; |
||
81 | else |
||
82 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
83 | "select count(*) from InetDomain d where name = ? and d != ?", |
||
84 | new Object[] { name, domain }, |
||
85 | new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } ) |
||
86 | .next()).intValue() > 0; |
||
87 | } |
||
88 | catch(HibernateException ex) |
||
89 | { |
||
90 | throw new ModelException(ex); |
||
91 | } |
||
92 | } |
||
93 | |||
919 | dev | 94 | protected InetDomain findForName(String name) |
95 | throws ModelException |
||
96 | { |
||
97 | try { |
||
98 | List list = HibernateUtil.currentSession().find( |
||
99 | "from InetDomain where name=?", name, Hibernate.STRING); |
||
100 | |||
101 | if(list.size() == 0) |
||
102 | return null; |
||
103 | else |
||
104 | return (InetDomain)list.get(0); |
||
105 | } |
||
106 | catch(HibernateException ex) |
||
107 | { |
||
108 | throw new ModelException(ex); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | public void save(User editor, InetDomain inetDomain) |
||
113 | throws ModelException |
||
114 | { |
||
115 | if(!inetDomain.editableBy(editor)) |
||
116 | throw new ModelSecurityException(); |
||
117 | |||
118 | inetDomain.setModUser(editor); |
||
119 | |||
120 | try { |
||
121 | HibernateUtil.currentSession().saveOrUpdate(inetDomain); |
||
122 | } |
||
123 | catch(HibernateException ex) |
||
124 | { |
||
125 | throw new ModelException(ex); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public void delete(User editor, InetDomain inetDomain) |
||
130 | throws ModelException |
||
131 | { |
||
132 | if(!inetDomain.deleteableBy(editor)) |
||
133 | throw new ModelSecurityException(); |
||
134 | |||
135 | try { |
||
136 | |||
137 | HibernateUtil.currentSession().delete(inetDomain); |
||
138 | } |
||
139 | catch(HibernateException ex) |
||
140 | { |
||
141 | throw new ModelException(ex); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public Collection listInetDomains(User editor) |
||
146 | throws ModelException |
||
147 | { |
||
148 | try { |
||
149 | if(editor.isSuperuser()) |
||
150 | return HibernateUtil.currentSession().find("from InetDomain"); |
||
151 | else |
||
152 | return HibernateUtil.currentSession().find( |
||
153 | "from InetDomain where owner=?", editor, Hibernate.entity(User.class)); |
||
154 | } |
||
155 | catch(HibernateException ex) |
||
156 | { |
||
157 | throw new ModelException(ex); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | public boolean areInetDomainsAvailable(User editor) |
||
162 | throws ModelException |
||
163 | { |
||
164 | try { |
||
165 | if(editor.isSuperuser()) |
||
166 | return true; |
||
167 | else |
||
168 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
169 | "select count(*) from InetDomain where owner=?", |
||
170 | editor, Hibernate.entity(User.class)).next()).intValue() > 0; |
||
171 | } |
||
172 | catch(HibernateException ex) |
||
173 | { |
||
174 | throw new ModelException(ex); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | private static InetDomainManager inetDomainManager = null; |
||
179 | |||
180 | public static InetDomainManager getInstance() |
||
181 | { |
||
182 | if(inetDomainManager == null) |
||
183 | inetDomainManager = new InetDomainManager(); |
||
184 | |||
185 | return inetDomainManager; |
||
186 | } |
||
187 | |||
188 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
||
189 | |||
190 | private static class NameComparator |
||
191 | implements Comparator |
||
192 | { |
||
193 | public int compare(Object o1, Object o2) |
||
194 | { |
||
195 | if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain)) |
||
196 | throw new ClassCastException("not a InetDomain"); |
||
197 | |||
198 | InetDomain a1 = (InetDomain)o1; |
||
199 | InetDomain a2 = (InetDomain)o2; |
||
200 | |||
201 | if(a1 == null && a2 == null) |
||
202 | return 0; |
||
203 | else if(a1 == null && a2 != null) |
||
204 | return -1; |
||
205 | else if(a1 != null && a2 == null) |
||
206 | return 1; |
||
207 | else |
||
208 | return a1.getName().compareToIgnoreCase(a2.getName()); |
||
209 | } |
||
210 | |||
211 | public boolean equals(Object obj) |
||
212 | { |
||
213 | return (obj instanceof NameComparator); |
||
214 | } |
||
215 | } |
||
216 | } |