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 | |||
899 | dev | 95 | try { |
96 | HibernateUtil.currentSession().saveOrUpdate(inetDomain); |
||
97 | } |
||
98 | catch(HibernateException ex) |
||
99 | { |
||
100 | throw new ModelException(ex); |
||
101 | } |
||
102 | } |
||
103 | |||
914 | dev | 104 | public void delete(User editor, InetDomain inetDomain) |
899 | dev | 105 | throws ModelException |
106 | { |
||
914 | dev | 107 | if(!inetDomain.deleteableBy(editor)) |
108 | throw new ModelSecurityException(); |
||
109 | |||
899 | dev | 110 | try { |
914 | dev | 111 | |
899 | dev | 112 | HibernateUtil.currentSession().delete(inetDomain); |
113 | } |
||
114 | catch(HibernateException ex) |
||
115 | { |
||
116 | throw new ModelException(ex); |
||
117 | } |
||
118 | } |
||
119 | |||
914 | dev | 120 | public Collection listInetDomains(User editor) |
899 | dev | 121 | throws ModelException |
122 | { |
||
123 | try { |
||
914 | dev | 124 | if(editor.isSuperuser()) |
125 | return HibernateUtil.currentSession().find("from InetDomain"); |
||
126 | else |
||
127 | return HibernateUtil.currentSession().find( |
||
128 | "from InetDomain where owner=?", editor, Hibernate.entity(User.class)); |
||
899 | dev | 129 | } |
130 | catch(HibernateException ex) |
||
131 | { |
||
132 | throw new ModelException(ex); |
||
133 | } |
||
134 | } |
||
135 | |||
914 | dev | 136 | public boolean areInetDomainsAvailable(User editor) |
137 | throws ModelException |
||
138 | { |
||
139 | try { |
||
140 | if(editor.isSuperuser()) |
||
141 | return true; |
||
142 | else |
||
143 | return ((Integer)HibernateUtil.currentSession().iterate( |
||
144 | "select count(*) from InetDomain where owner=?", |
||
145 | editor, Hibernate.entity(User.class)).next()).intValue() > 0; |
||
146 | } |
||
147 | catch(HibernateException ex) |
||
148 | { |
||
149 | throw new ModelException(ex); |
||
150 | } |
||
151 | } |
||
152 | |||
899 | dev | 153 | private static InetDomainManager inetDomainManager = null; |
154 | |||
155 | public static InetDomainManager getInstance() |
||
156 | { |
||
157 | if(inetDomainManager == null) |
||
158 | inetDomainManager = new InetDomainManager(); |
||
159 | |||
160 | return inetDomainManager; |
||
161 | } |
||
162 | |||
163 | public static final Comparator NAME_COMPARATOR = new NameComparator(); |
||
164 | |||
165 | private static class NameComparator |
||
166 | implements Comparator |
||
167 | { |
||
168 | public int compare(Object o1, Object o2) |
||
169 | { |
||
170 | if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain)) |
||
171 | throw new ClassCastException("not a InetDomain"); |
||
172 | |||
173 | InetDomain a1 = (InetDomain)o1; |
||
174 | InetDomain a2 = (InetDomain)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 | } |