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; |
||
914 | dev | 7 | import ak.hostcaptain.util.ModelSecurityException; |
899 | dev | 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( |
||
904 | dev | 20 | "/ak/hostcaptain/core/model/InetDomain.hbm.xml"); |
899 | dev | 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 | |||
914 | dev | 37 | public InetDomain create(User editor) |
38 | throws ModelException |
||
899 | dev | 39 | { |
914 | dev | 40 | if(!allowedToCreate(editor)) throw new ModelSecurityException(); |
41 | |||
899 | dev | 42 | return new InetDomain(); |
43 | } |
||
44 | |||
914 | dev | 45 | public boolean allowedToCreate(User editor) |
899 | dev | 46 | throws ModelException |
47 | { |
||
914 | dev | 48 | return editor.isSuperuser(); |
49 | } |
||
50 | |||
51 | public InetDomain get(User editor, Long id) |
||
52 | throws ModelException |
||
53 | { |
||
54 | InetDomain domain; |
||
55 | |||
899 | dev | 56 | try { |
914 | dev | 57 | domain = (InetDomain)HibernateUtil.currentSession().load( |
58 | InetDomain.class, id); |
||
899 | dev | 59 | } |
60 | catch(HibernateException ex) |
||
61 | { |
||
62 | throw new ModelException(ex); |
||
63 | } |
||
914 | dev | 64 | |
65 | if(!domain.viewableBy(editor)) |
||
66 | throw new ModelSecurityException(); |
||
67 | |||
68 | return domain; |
||
899 | dev | 69 | } |
70 | |||
914 | dev | 71 | protected InetDomain findForName(String name) |
899 | dev | 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 | |||
914 | dev | 89 | public void save(User editor, InetDomain inetDomain) |
899 | dev | 90 | throws ModelException |
91 | { |
||
914 | dev | 92 | if(!inetDomain.editableBy(editor)) |
93 | throw new ModelSecurityException(); |
||
94 | |||
918 | dev | 95 | inetDomain.setModUser(editor); |
96 | |||
899 | dev | 97 | try { |
98 | HibernateUtil.currentSession().saveOrUpdate(inetDomain); |
||
99 | } |
||
100 | catch(HibernateException ex) |
||
101 | { |
||
102 | throw new ModelException(ex); |
||
103 | } |
||
104 | } |
||
105 | |||
914 | dev | 106 | public void delete(User editor, InetDomain inetDomain) |
899 | dev | 107 | throws ModelException |
108 | { |
||
914 | dev | 109 | if(!inetDomain.deleteableBy(editor)) |
110 | throw new ModelSecurityException(); |
||
111 | |||
899 | dev | 112 | try { |
914 | dev | 113 | |
899 | dev | 114 | HibernateUtil.currentSession().delete(inetDomain); |
115 | } |
||
116 | catch(HibernateException ex) |
||
117 | { |
||
118 | throw new ModelException(ex); |
||
119 | } |
||
120 | } |
||
121 | |||
914 | dev | 122 | public Collection listInetDomains(User editor) |
899 | dev | 123 | throws ModelException |
124 | { |
||
125 | try { |
||
914 | dev | 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)); |
||
899 | dev | 131 | } |
132 | catch(HibernateException ex) |
||
133 | { |
||
134 | throw new ModelException(ex); |
||
135 | } |
||
136 | } |
||
137 | |||
914 | dev | 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 | |||
899 | dev | 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 | } |