Subversion Repositories general

Rev

Rev 1041 | Go to most recent revision | Details | Compare with Previous | 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.ModelStoreException;
1042 dev 14
import ak.hostadmiral.util.hibernate.HibernateUtil;
1041 dev 15
import ak.hostadmiral.core.model.User;
16
import ak.hostadmiral.core.model.UserLogin;
17
import ak.hostadmiral.core.model.UserManager;
18
import ak.hostadmiral.core.model.store.UserStore;
19
 
20
public class UserHibernate
21
	implements UserStore
22
{
23
	public UserHibernate()
24
		throws ModelStoreException
25
	{
26
		initSortKeys();
27
		register();
28
	}
29
 
30
	public User get(Long id)
31
		throws ModelStoreException
32
	{
33
		try {
34
			return (User)HibernateUtil.currentSession().load(User.class, id);
35
		}
36
		catch(HibernateException ex) {
37
			throw new ModelStoreException(ex);
38
		}
39
	}
40
 
41
	public boolean loginExists(User user, String login)
42
		throws ModelStoreException
43
	{
44
		try {
45
			if(user.getId() == null)
46
				return ((Integer)HibernateUtil.currentSession().iterate(
47
					"select count(*) from User u where login = ?",
48
					login, Hibernate.STRING)
49
					.next()).intValue() > 0;
50
			else
51
				return ((Integer)HibernateUtil.currentSession().iterate(
52
					"select count(*) from User u where login = ? and u != ?",
53
					new Object[] { login, user },
54
					new Type[] { Hibernate.STRING, Hibernate.entity(User.class) } )
55
					.next()).intValue() > 0;
56
		}
57
		catch(HibernateException ex) {
58
			throw new ModelStoreException(ex);
59
		}
60
	}
61
 
62
	public User findForLogin(String login)
63
		throws ModelStoreException
64
	{
65
		try {
66
			List list = HibernateUtil.currentSession().find(
67
				"select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?",
68
				new Object[] { login, Boolean.TRUE },
69
				new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } );
70
 
71
			if(list.size() == 0)
72
				return null;
73
			else
74
				return (User)list.get(0);
75
		}
76
		catch(HibernateException ex) {
77
			throw new ModelStoreException(ex);
78
		}
79
	}
80
 
81
	public void save(User user)
82
		throws ModelStoreException
83
	{
84
		try {
85
			HibernateUtil.currentSession().saveOrUpdate(user);
86
		}
87
		catch(HibernateException ex) {
88
			throw new ModelStoreException(ex);
89
		}
90
	}
91
 
92
	public void delete(User user)
93
		throws ModelStoreException
94
	{
95
		try {
96
			HibernateUtil.currentSession().delete(user);
97
		}
98
		catch(HibernateException ex) {
99
			throw new ModelStoreException(ex);
100
		}
101
	}
102
 
103
	public Collection listAllUsers(CollectionInfo info, int rowsPerPage, int pageNumber,
104
			Integer[] sortingKeys)
105
		throws ModelStoreException
106
	{
107
		try {
108
			if(info != null) {
109
				info.init(((Integer)HibernateUtil.currentSession().iterate(
110
					"select count(*) from User").next()).intValue(),
111
					pageNumber, rowsPerPage);
112
			}
113
 
114
			return HibernateUtil.pageableList(rowsPerPage, pageNumber,
115
				"select u from User u left join fetch u.boss"
116
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
117
		}
118
		catch(HibernateException ex) {
119
			throw new ModelStoreException(ex);
120
		}
121
	}
122
 
123
	public Collection listUsers(CollectionInfo info, int rowsPerPage, int pageNumber,
124
			Integer[] sortingKeys, User user)
125
		throws ModelStoreException
126
	{
127
		try {
128
			if(info != null) {
129
				info.init(((Integer)HibernateUtil.currentSession().iterate(
130
					"select count(*) from User u where u = ? or u.boss = ?",
131
					new Object[] { user, user},
132
					new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }
133
					).next()).intValue(),
134
					pageNumber, rowsPerPage);
135
			}
136
 
137
			return HibernateUtil.pageableList(rowsPerPage, pageNumber,
138
				"select u from User u left join fetch u.boss where u = ? or u.boss = ?"
139
				+ HibernateUtil.formOrderClause(sortingKeys, sortKeys),
140
				new Object[] { user, user},
141
				new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) } );
142
		}
143
		catch(HibernateException ex) {
144
			throw new ModelStoreException(ex);
145
		}
146
	}
147
 
148
	public void saveUserLogin(UserLogin userLogin)
149
		throws ModelStoreException
150
	{
151
		try {
152
			HibernateUtil.currentSession().saveOrUpdate(userLogin);
153
		}
154
		catch(HibernateException ex) {
155
			throw new ModelStoreException(ex);
156
		}
157
	}
158
 
159
	public Collection listFailedLogins()
160
		throws ModelStoreException
161
	{
162
		try {
163
			return HibernateUtil.currentSession().find(
164
				"select l from UserLogin l left join fetch l.user where l.success = ?",
165
				Boolean.FALSE, Hibernate.BOOLEAN);
166
		}
167
		catch(HibernateException ex) {
168
            throw new ModelStoreException(ex);
169
		}
170
	}
171
 
172
	public Collection listSubusers(User user)
173
		throws ModelStoreException
174
	{
175
		try {
176
			return HibernateUtil.currentSession().find(
177
				"select u from User u where u.boss = ?",
178
				user, Hibernate.entity(User.class) );
179
		}
180
		catch(HibernateException ex) {
181
			throw new ModelStoreException(ex);
182
		}
183
	}
184
 
185
	protected static Map     sortKeys            = new HashMap();
186
	private   static boolean sortKeysInitialized = false;
187
 
188
	private static void initSortKeys()
189
	{
190
		if(!sortKeysInitialized) {
191
			sortKeys.put(UserManager.SORT_LOGIN, "u.login");
192
			sortKeysInitialized = true;
193
		}
194
	}
195
 
196
	private static boolean registered = false;
197
	protected static void register()
198
		throws ModelStoreException
199
	{
200
		synchronized(UserHibernate.class) {
201
			if(registered) return;
202
 
203
			registered = true;
204
			try {
205
				HibernateUtil.getConfiguration().addResource(
206
					"ak/hostadmiral/core/model/User.hbm.xml");
207
				HibernateUtil.getConfiguration().addResource(
208
					"ak/hostadmiral/core/model/UserLogin.hbm.xml");
209
				HibernateUtil.getConfiguration().addResource(
210
					"ak/hostadmiral/core/model/PasswordStoreAbstract.hbm.xml");
211
			}
212
			catch(Exception ex) {
213
				throw new ModelStoreException(ex);
214
			}
215
		}
216
	}
217
}