Subversion Repositories general

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1041 dev 1
package ak.hostadmiral.core.model.store.hibernate;
2
 
3
import java.util.Collection;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.HashMap;
7
 
8
import net.sf.hibernate.Hibernate;
9
import net.sf.hibernate.HibernateException;
10
import net.sf.hibernate.type.Type;
11
 
12
import ak.hostadmiral.util.CollectionInfo;
13
import ak.hostadmiral.util.HibernateUtil;
14
import ak.hostadmiral.util.ModelStoreException;
15
import ak.hostadmiral.core.model.User;
16
import ak.hostadmiral.core.model.InetDomain;
17
import ak.hostadmiral.core.model.InetDomainManager;
18
import ak.hostadmiral.core.model.store.InetDomainStore;
19
 
20
public class InetDomainHibernate
21
	implements InetDomainStore
22
{
23
	public InetDomainHibernate()
24
		throws ModelStoreException
25
	{
26
		initSortKeys();
27
		register();
28
	}
29
 
30
	public InetDomain get(Long id)
31
		throws ModelStoreException
32
	{
33
		try {
34
			return (InetDomain)HibernateUtil.currentSession().load(
35
				InetDomain.class, id);
36
		}
37
		catch(HibernateException ex)
38
		{
39
			throw new ModelStoreException(ex);
40
		}
41
	}
42
 
43
	public boolean nameExists(InetDomain domain, String name)
44
		throws ModelStoreException
45
	{
46
		try {
47
			if(domain.getId() == null)
48
				return ((Integer)HibernateUtil.currentSession().iterate(
49
					"select count(*) from InetDomain d where name = ?",
50
					name, Hibernate.STRING)
51
					.next()).intValue() > 0;
52
			else
53
				return ((Integer)HibernateUtil.currentSession().iterate(
54
					"select count(*) from InetDomain d where name = ? and d != ?",
55
					new Object[] { name, domain },
56
					new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
57
					.next()).intValue() > 0;
58
		}
59
		catch(HibernateException ex)
60
		{
61
			throw new ModelStoreException(ex);
62
		}
63
	}
64
 
65
	public InetDomain findForName(String name)
66
		throws ModelStoreException
67
	{
68
		try {
69
			List list = HibernateUtil.currentSession().find(
70
				"select d from InetDomain d left join fetch d.owner where d.name=?",
71
				name, Hibernate.STRING);
72
 
73
			if(list.size() == 0)
74
				return null;
75
			else
76
				return (InetDomain)list.get(0);
77
		}
78
		catch(HibernateException ex)
79
		{
80
			throw new ModelStoreException(ex);
81
		}
82
	}
83
 
84
	public void save(InetDomain domain)
85
		throws ModelStoreException
86
	{
87
		try {
88
			HibernateUtil.currentSession().saveOrUpdate(domain);
89
		}
90
		catch(HibernateException ex)
91
		{
92
			throw new ModelStoreException(ex);
93
		}
94
	}
95
 
96
	public void delete(InetDomain domain)
97
		throws ModelStoreException
98
	{
99
		try {
100
			HibernateUtil.currentSession().delete(domain);
101
		}
102
		catch(HibernateException ex)
103
		{
104
			throw new ModelStoreException(ex);
105
		}
106
	}
107
 
108
	public Collection listAllInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
109
			Integer[] sortingKeys)
110
		throws ModelStoreException
111
	{
112
		try {
113
			if(info != null) {
114
				info.init(((Integer)HibernateUtil.currentSession().iterate(
115
					"select count(*) from InetDomain").next()).intValue(),
116
					pageNumber, rowsPerPage);
117
			}
118
 
119
			return HibernateUtil.pageableList(rowsPerPage, pageNumber,
120
				"select d from InetDomain d left join fetch d.owner"
121
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
122
		}
123
		catch(HibernateException ex)
124
		{
125
			throw new ModelStoreException(ex);
126
		}
127
	}
128
 
129
	public Collection listInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
130
			Integer[] sortingKeys, User user)
131
		throws ModelStoreException
132
	{
133
		try {
134
			if(info != null) {
135
				info.init(((Integer)HibernateUtil.currentSession().iterate(
136
					"select count(*) from InetDomain d where d.owner=?",
137
					user, Hibernate.entity(User.class)).next()).intValue(),
138
					pageNumber, rowsPerPage);
139
			}
140
 
141
			return HibernateUtil.pageableList(rowsPerPage, pageNumber,
142
				"select d from InetDomain d where d.owner=?"
143
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
144
				new Object[] { user }, new Type[] { Hibernate.entity(User.class) } );
145
		}
146
		catch(HibernateException ex)
147
		{
148
			throw new ModelStoreException(ex);
149
		}
150
	}
151
 
152
	public int countInetDomainsAvailable(User user)
153
		throws ModelStoreException
154
	{
155
		try {
156
			return ((Integer)HibernateUtil.currentSession().iterate(
157
				"select count(*) from InetDomain where owner=?",
158
				user, Hibernate.entity(User.class)).next()).intValue();
159
		}
160
		catch(HibernateException ex)
161
		{
162
			throw new ModelStoreException(ex);
163
		}
164
	}
165
 
166
	public Collection listOwnInetDomains(User user)
167
		throws ModelStoreException
168
	{
169
		try {
170
			return HibernateUtil.currentSession().find(
171
				"select d from InetDomain d where d.owner = ?",
172
				user, Hibernate.entity(User.class) );
173
		}
174
		catch(HibernateException ex)
175
		{
176
			throw new ModelStoreException(ex);
177
		}
178
	}
179
 
180
	protected static Map     sortKeys            = new HashMap();
181
	private   static boolean sortKeysInitialized = false;
182
 
183
	private static void initSortKeys()
184
	{
185
		if(!sortKeysInitialized) {
186
			sortKeys.put(InetDomainManager.SORT_NAME, "d.name");
187
			sortKeysInitialized = true;
188
		}
189
	}
190
 
191
	private static boolean registered = false;
192
	protected static void register()
193
		throws ModelStoreException
194
	{
195
		synchronized(InetDomainHibernate.class) {
196
			if(registered) return;
197
 
198
			registered = true;
199
			try {
200
				HibernateUtil.getConfiguration().addResource(
201
					"ak/hostadmiral/core/model/InetDomain.hbm.xml");
202
			}
203
			catch(Exception ex) {
204
				throw new ModelStoreException(ex);
205
			}
206
		}
207
	}
208
}