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