Subversion Repositories general

Rev

Rev 950 | Rev 1010 | Go to most recent revision | Details | Compare with Previous | 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
946 dev 11
	implements UserBeforeDeleteListener
919 dev 12
{
946 dev 13
	private static InetDomainManager inetDomainManager = null;
919 dev 14
	private static boolean registered = false;
946 dev 15
 
16
	public static InetDomainManager getInstance()
17
	{
18
		return inetDomainManager;
19
	}
20
 
919 dev 21
	protected static void register()
22
	{
23
		synchronized(InetDomainManager.class) {
24
			if(registered) return;
25
 
26
			registered = true;
27
			try {
28
				HibernateUtil.getConfiguration().addResource(
950 dev 29
					"ak/hostadmiral/core/model/InetDomain.hbm.xml");
946 dev 30
 
31
				inetDomainManager = new InetDomainManager();
919 dev 32
			}
33
			catch(Exception ex) {
34
				ex.printStackTrace();
35
				throw new RuntimeException(ex.getMessage());
36
			}
37
		}
38
	}
39
 
40
	static {
41
		register();
42
	}
43
 
949 dev 44
	private Collection beforeDeleteListeners = new ArrayList();
45
 
919 dev 46
	private InetDomainManager()
47
	{
946 dev 48
		UserManager.getInstance().addBeforeDeleteListener(this);
919 dev 49
	}
50
 
51
	public InetDomain create(User editor)
52
		throws ModelException
53
	{
54
		if(!allowedToCreate(editor)) throw new ModelSecurityException();
55
 
56
		return new InetDomain();
57
	}
58
 
59
	public boolean allowedToCreate(User editor)
60
		throws ModelException
61
	{
921 dev 62
		return InetDomain.allowedToCreate(this, editor);
919 dev 63
	}
64
 
65
	public InetDomain get(User editor, Long id)
66
		throws ModelException
67
	{
68
		InetDomain domain;
69
 
70
		try {
71
			domain = (InetDomain)HibernateUtil.currentSession().load(
72
				InetDomain.class, id);
73
		}
74
		catch(HibernateException ex)
75
		{
76
			throw new ModelException(ex);
77
		}
78
 
79
		if(!domain.viewableBy(editor))
80
			throw new ModelSecurityException();
81
 
82
		return domain;
83
	}
84
 
923 dev 85
	public boolean nameExists(User editor, InetDomain domain, String name)
86
		throws ModelException
87
	{
88
		try {
89
			if(domain.getId() == null)
90
				return ((Integer)HibernateUtil.currentSession().iterate(
91
					"select count(*) from InetDomain d where name = ?",
92
					name, Hibernate.STRING)
93
					.next()).intValue() > 0;
94
			else
95
				return ((Integer)HibernateUtil.currentSession().iterate(
96
					"select count(*) from InetDomain d where name = ? and d != ?",
97
					new Object[] { name, domain },
98
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
99
					.next()).intValue() > 0;
100
		}
101
		catch(HibernateException ex)
102
		{
103
			throw new ModelException(ex);
104
		}
105
	}
106
 
919 dev 107
	protected InetDomain findForName(String name)
108
		throws ModelException
109
	{
110
		try {
111
			List list = HibernateUtil.currentSession().find(
112
				"from InetDomain where name=?", name, Hibernate.STRING);
113
 
114
			if(list.size() == 0)
115
				return null;
116
			else
117
				return (InetDomain)list.get(0);
118
		}
119
		catch(HibernateException ex)
120
		{
121
			throw new ModelException(ex);
122
		}
123
	}
124
 
946 dev 125
	public void save(User editor, InetDomain domain)
919 dev 126
		throws ModelException
127
	{
946 dev 128
		if(!domain.editableBy(editor))
919 dev 129
			throw new ModelSecurityException();
130
 
946 dev 131
		domain.setModUser(editor);
919 dev 132
 
133
		try {
946 dev 134
			HibernateUtil.currentSession().saveOrUpdate(domain);
919 dev 135
		}
136
		catch(HibernateException ex)
137
		{
138
			throw new ModelException(ex);
139
		}
140
	}
141
 
949 dev 142
    public void addBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
143
    {
144
    	beforeDeleteListeners.add(listener);
145
    }
146
 
147
    public void removeBeforeDeleteListener(SystemUserBeforeDeleteListener listener)
148
    {
149
    	beforeDeleteListeners.remove(listener);
150
    }
151
 
152
    public Collection beforeDelete(User editor, InetDomain domain, Collection known)
919 dev 153
		throws ModelException
946 dev 154
    {
155
    	Collection cascade = new ArrayList();
949 dev 156
 
946 dev 157
    	for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
158
    		InetDomainBeforeDeleteListener listener = (InetDomainBeforeDeleteListener)i.next();
949 dev 159
			Collection subcascade = listener.inetDomainBeforeDelete(editor, domain, known);
946 dev 160
    		if(subcascade != null)
161
    			cascade.addAll(subcascade);
162
    	}
949 dev 163
 
946 dev 164
    	return cascade;
165
    }
166
 
167
	public void delete(User editor, InetDomain domain)
168
		throws ModelException
919 dev 169
	{
946 dev 170
		if(!domain.deleteableBy(editor))
919 dev 171
			throw new ModelSecurityException();
172
 
173
		try {
174
 
946 dev 175
			HibernateUtil.currentSession().delete(domain);
919 dev 176
		}
177
		catch(HibernateException ex)
178
		{
179
			throw new ModelException(ex);
180
		}
181
	}
182
 
183
	public Collection listInetDomains(User editor)
184
		throws ModelException
185
	{
186
		try {
187
			if(editor.isSuperuser())
188
				return HibernateUtil.currentSession().find("from InetDomain");
189
			else
190
				return HibernateUtil.currentSession().find(
191
					"from InetDomain where owner=?", editor, Hibernate.entity(User.class));
192
		}
193
		catch(HibernateException ex)
194
		{
195
			throw new ModelException(ex);
196
		}
197
	}
198
 
199
	public boolean areInetDomainsAvailable(User editor)
200
		throws ModelException
201
	{
202
		try {
203
			if(editor.isSuperuser())
204
				return true;
205
			else
206
				return ((Integer)HibernateUtil.currentSession().iterate(
207
					"select count(*) from InetDomain where owner=?",
208
					editor, Hibernate.entity(User.class)).next()).intValue() > 0;
209
		}
210
		catch(HibernateException ex)
211
		{
212
			throw new ModelException(ex);
213
		}
214
	}
215
 
949 dev 216
	public Collection userBeforeDelete(User editor, User user, Collection known)
946 dev 217
		throws ModelException
919 dev 218
	{
946 dev 219
        Collection domains;
919 dev 220
 
946 dev 221
		try {
222
			domains = HibernateUtil.currentSession().find(
223
				"from InetDomain where owner = ?",
949 dev 224
				user, Hibernate.entity(User.class) );
946 dev 225
		}
226
		catch(HibernateException ex)
227
		{
228
			throw new ModelException(ex);
229
		}
230
 
231
    	Collection cascade = new ArrayList();
232
		for(Iterator i = domains.iterator(); i.hasNext(); ) {
233
			InetDomain d = (InetDomain)i.next();
234
            if(d.viewableBy(editor)) {
235
				if(d.deleteableBy(editor))
236
					cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.DELETE,
949 dev 237
						this.beforeDelete(editor, d, known)));
946 dev 238
				else
239
					cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.FORBIDDEN, null));
240
			}
241
			else {
242
				cascade.add(new CascadeDeleteElement(InetDomain.createLimitedCopy(d),
243
					CascadeDeleteElement.FORBIDDEN, null));
244
			}
245
		}
246
 
247
    	return cascade;
919 dev 248
	}
249
 
250
	public static final Comparator NAME_COMPARATOR = new NameComparator();
251
 
252
	private static class NameComparator
253
		implements Comparator
254
	{
255
		public int compare(Object o1, Object o2)
256
		{
257
			if(!(o1 instanceof InetDomain) || !(o2 instanceof InetDomain))
258
				throw new ClassCastException("not a InetDomain");
259
 
260
		    InetDomain a1 = (InetDomain)o1;
261
		    InetDomain a2 = (InetDomain)o2;
262
 
263
		    if(a1 == null && a2 == null)
264
		    	return 0;
265
		    else if(a1 == null && a2 != null)
266
		    	return -1;
267
		    else if(a1 != null && a2 == null)
268
		    	return 1;
269
		    else
270
		    	return a1.getName().compareToIgnoreCase(a2.getName());
271
		}
272
 
273
		public boolean equals(Object obj)
274
		{
275
			return (obj instanceof NameComparator);
276
		}
277
	}
278
}