1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.jobs.dao.hibernate;
18
19 import org.bejug.javacareers.common.exception.DuplicateUserNameException;
20 import org.bejug.javacareers.common.exception.LastAdminException;
21 import org.bejug.javacareers.jobs.dao.UserDao;
22 import org.bejug.javacareers.jobs.model.Organisation;
23 import org.bejug.javacareers.jobs.model.Resume;
24 import org.bejug.javacareers.jobs.model.User;
25 import org.springframework.dao.DataAccessException;
26 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
27
28 import java.util.List;
29
30 /***
31 * The User Data Access Object implementation.
32 *
33 * @author Sven Schauwvliege (Last modified by $Author: shally $)
34 * @version $Revision: 1.4 $ - $Date: 2005/12/20 15:36:46 $
35 */
36 public class UserDaoHibernateImpl
37 extends HibernateDaoSupport
38 implements UserDao {
39
40
41 static final String COUNT_ADMIN =
42 "select count(user)from User user where role = 'ROLE_ADMIN'";
43
44 /***
45 * Add a user.
46 * {@inheritDoc}
47 */
48 public synchronized void store(User user)
49 throws DataAccessException,
50 DuplicateUserNameException,
51 LastAdminException {
52
53 if (user.getId() == null &&
54 getUserByUserName(user.getUserName()) != null) {
55
56
57 throw new DuplicateUserNameException("username already in use");
58 }
59
60 Organisation org = user.getOrganisation();
61
62 if (org != null) {
63 org.setModificationDate(user.getModificationDate());
64 if (org.getAddress() != null) {
65 org.getAddress().setModificationDate(user.getModificationDate());
66 }
67 }
68
69 Resume resume = user.getResume();
70
71 if (resume != null) {
72 resume.setModificationDate(user.getModificationDate());
73 }
74
75 if (user.getAddress() != null) {
76 user.getAddress().setModificationDate(user.getModificationDate());
77 }
78 user.getContact().setModificationDate(user.getModificationDate());
79 if (user.isRoleChanged()) {
80 if (user.getPreviousRole().equals("ROLE_ADMIN") &&
81 !user.getRole().equals("ROLE_ADMIN")) {
82
83 Integer i = (Integer)getHibernateTemplate().find(COUNT_ADMIN).get(0);
84 if (i.intValue() < 2) {
85 throw new LastAdminException(
86 "You can not delete the last administrator");
87 }
88 }
89 }
90 getHibernateTemplate().saveOrUpdate(user);
91 }
92
93 /***
94 * {@inheritDoc}
95 */
96 public User getUser(Integer id) throws DataAccessException {
97 return (User) getHibernateTemplate().load(User.class, id);
98 }
99
100 /***
101 * {@inheritDoc}
102 */
103 public List getUsers() throws DataAccessException {
104 return getHibernateTemplate().loadAll(User.class);
105 }
106
107 /***
108 * {@inheritDoc}
109 */
110 public void deleteUser(Integer userId)
111 throws DataAccessException, LastAdminException {
112 User user = getUser(userId);
113 deleteUser(user);
114 }
115
116 /***
117 * {@inheritDoc}
118 */
119 public synchronized void deleteUser(User user)
120 throws DataAccessException, LastAdminException {
121 if (user.getRole().equals("ROLE_ADMIN") ) {
122 Integer i = (Integer)getHibernateTemplate().find(COUNT_ADMIN).get(0);
123 if (i.intValue() < 2) {
124 throw new LastAdminException(
125 "You can not delete the last administrator");
126 }
127 }
128 getHibernateTemplate().delete(user);
129 }
130
131 /***
132 * {@inheritDoc}
133 */
134 public User getUserByUserName(String userName) throws DataAccessException {
135
136 List users = getHibernateTemplate().findByNamedQueryAndNamedParam(
137 "findUserByName", "name", userName);
138 User user = null;
139 if (users.size() != 0) {
140 user = (User) users.get(0);
141 }
142 return user;
143 }
144 }
145 /***
146 * $Log: UserDaoHibernateImpl.java,v $
147 * Revision 1.4 2005/12/20 15:36:46 shally
148 * CheckStyle and PMD changes.
149 *
150 * Revision 1.3 2005/10/11 08:47:55 stephan_janssen
151 * Reformat code added todo.
152 *
153 * Revision 1.2 2005/09/13 08:11:17 schauwvliege
154 * organize imports
155 *
156 * Revision 1.1 2005/08/26 07:58:30 ge0ffrey
157 * split up the sources in service, serviceimpl and webclient
158 *
159 * Revision 1.10 2005/08/24 09:37:57 schauwvliege
160 * addded resume
161 *
162 * Revision 1.9 2005/08/11 10:26:32 bavo_jcs
163 * Role change rule
164 *
165 * Revision 1.8 2005/08/10 09:04:48 bavo_jcs
166 * Optimized imports according to checkstyle
167 *
168 * Revision 1.7 2005/08/09 12:59:55 bavo_jcs
169 * Optimized imports
170 *
171 * Revision 1.6 2005/08/03 13:14:09 bme_jcs
172 * getDao's removed and storeObject renamed to store
173 *
174 * Revision 1.5 2005/07/05 15:13:21 schauwvliege
175 * added person/contact and location to model
176 *
177 * Revision 1.4 2005/06/14 13:40:04 schauwvliege
178 * Renamed add to store
179 *
180 * Revision 1.3 2005/06/14 12:05:53 schauwvliege
181 * CheckStyle and fixing tests
182 *
183 * Revision 1.2 2005/06/09 08:18:44 bejug_cc
184 * Fix initial import
185 *
186 * Revision 1.14 2005/06/07 12:12:56 ssc
187 * Delete last admin exception
188 *
189 * Revision 1.13 2005/05/30 09:31:47 bme
190 * updated for the introduction of HQL in the hbm-files
191 *
192 * Revision 1.12 2005/05/26 14:15:31 PSONG09
193 * added org.getAddress() != null check
194 *
195 * Revision 1.11 2005/05/17 11:59:56 ssc
196 * Refactored User and Publisher class to User Class added cvAdded Boolean, added Address to user, Fixed test to work with this setup
197 *
198 * Revision 1.10 2005/05/12 08:23:55 ssc
199 * Checkstyle errors
200 *
201 * Revision 1.9 2005/05/11 17:44:15 sja
202 * Moved all dao Hibernate queries variables to HQLConstants.
203 *
204 * Revision 1.8 2005/05/11 17:35:39 sja
205 * Moved Hibernate queries variables to HQLConstants.
206 *
207 * Revision 1.7 2005/05/11 16:50:20 sja
208 * Uses DuplicateUserNameException from correct package location.
209 *
210 * Revision 1.6 2005/05/11 10:14:52 ssc
211 * Checstyle errors
212 *
213 * Revision 1.5 2005/05/10 09:54:02 ssc
214 * Removed AbstractPersistableObject
215 *
216 * Revision 1.4 2005/05/10 07:10:18 ssc
217 * mqde add user synchronized
218 *
219 * Revision 1.3 2005/05/09 12:31:29 ssc
220 * Added duplicate username exception
221 *
222 * Revision 1.2 2005/05/04 09:47:56 bme
223 * modified for introduction AOP
224 *
225 * Revision 1.1 2005/05/02 13:26:27 ssc
226 * First release
227 *
228 *
229 */