001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.helper;
023:
024: import org.hibernate.Criteria;
025: import org.hibernate.HibernateException;
026: import org.hibernate.Query;
027: import org.hibernate.Session;
028: import org.hibernate.criterion.Restrictions;
029: import org.jboss.portal.migration.exception.ModuleException;
030: import org.jboss.portal.migration.exception.NoSuchUserException;
031: import org.jboss.portal.migration.model20.impl.role.Role20Impl;
032: import org.jboss.portal.migration.model20.impl.user.User20Impl;
033: import org.jboss.portal.migration.model22.impl.role.Role22Impl;
034: import org.jboss.portal.migration.model22.impl.user.User22Impl;
035: import org.jboss.portal.migration.model24.identity.Role24Impl;
036: import org.jboss.portal.migration.model24.identity.User24Impl;
037:
038: import java.util.Iterator;
039: import java.util.Set;
040:
041: /**
042: * Class grouping reusable queries for migration
043: *
044: * @author <a href="mailto:boleslaw.dawidowicz@jboss.org">Boleslaw Dawidowicz</a>
045: * @version $Revision: 8784 $
046: */
047: public class Finder {
048:
049: public static Set findUsers20(Session session, int offset, int limit)
050: throws ModuleException {
051: try {
052: //Session session = getCurrentFromSession();
053: Query query = session.createQuery("from User20Impl");
054: query.setFirstResult(offset);
055: query.setMaxResults(limit);
056: Iterator iterator = query.iterate();
057: return Tools.toSet(iterator);
058: } catch (HibernateException e) {
059: String message = "Cannot find user range [" + offset + ","
060: + limit + "]";
061: //log.error(message, e);
062: throw new ModuleException(message, e);
063: }
064: }
065:
066: public static User20Impl findUser20ById(Session session, String id)
067: throws IllegalArgumentException, ModuleException,
068: NoSuchUserException {
069: if (id == null) {
070: throw new IllegalArgumentException("The id is null");
071: }
072: try {
073: return findUser20ById(session, new Integer(id));
074: } catch (NumberFormatException e) {
075: throw new IllegalArgumentException(
076: "Cannot parse id into an integer " + id);
077: }
078: }
079:
080: public static User20Impl findUser20ById(Session session, Object id)
081: throws IllegalArgumentException, ModuleException,
082: NoSuchUserException {
083: if (id instanceof Integer) {
084: try {
085: //Session session = getCurrentFromSession();
086: User20Impl user = (User20Impl) session.get(
087: User20Impl.class, (Integer) id);
088: if (user == null) {
089: throw new NoSuchUserException("No user found for "
090: + id);
091: }
092: return user;
093: } catch (HibernateException e) {
094: String message = "Cannot find user by id " + id;
095: //log.error(message, e);
096: throw new ModuleException(message, e);
097: }
098: } else {
099: throw new IllegalArgumentException(
100: "The id is not an Integer : " + id);
101: }
102: }
103:
104: public static Set findUsers20Ids(Session session)
105: throws ModuleException {
106: try {
107: //Session session = getCurrentFromSession();
108: Query query = session
109: .createQuery("select ID from User20Impl");
110: //query.setFirstResult(offset);
111: //query.setMaxResults(limit);
112: Iterator iterator = query.iterate();
113: return Tools.toSet(iterator);
114: } catch (HibernateException e) {
115: String message = "Cannot find user ids";
116: //log.error(message, e);
117: throw new ModuleException(message, e);
118: }
119: }
120:
121: public static Set findUsers22(Session session, int offset, int limit)
122: throws ModuleException {
123: try {
124: //Session session = getCurrentToSession();
125: Query query = session.createQuery("from User22Impl");
126: query.setFirstResult(offset);
127: query.setMaxResults(limit);
128: Iterator iterator = query.iterate();
129: return Tools.toSet(iterator);
130: } catch (HibernateException e) {
131: String message = "Cannot find user range [" + offset + ","
132: + limit + "]";
133: //log.error(message, e);
134: throw new ModuleException(message, e);
135: }
136: }
137:
138: public static User22Impl findUser22ById(Session session, String id)
139: throws IllegalArgumentException, ModuleException,
140: NoSuchUserException {
141: if (id == null) {
142: throw new IllegalArgumentException("The id is null");
143: }
144: try {
145: return findUser22ById(session, new Long(id));
146: } catch (NumberFormatException e) {
147: throw new IllegalArgumentException(
148: "Cannot parse id into an integer " + id);
149: }
150: }
151:
152: public static User22Impl findUser22ById(Session session, Object id)
153: throws IllegalArgumentException, ModuleException,
154: NoSuchUserException {
155: if (id instanceof Long) {
156: try {
157: //Session session = getCurrentToSession();
158: User22Impl user = (User22Impl) session.get(
159: User22Impl.class, (Long) id);
160: if (user == null) {
161: throw new NoSuchUserException("No user found for "
162: + id);
163: }
164: return user;
165: } catch (HibernateException e) {
166: String message = "Cannot find user by id " + id;
167: //log.error(message, e);
168: throw new ModuleException(message, e);
169: }
170: } else {
171: throw new IllegalArgumentException(
172: "The id is not an Long : " + id);
173: }
174: }
175:
176: public static User20Impl findUser20ByUserName(Session session,
177: String userName) throws ModuleException {
178: if (userName != null) {
179: try {
180: //Session session = getCurrentFromSession();
181: Query query = session
182: .createQuery("from User20Impl where userName=:userName");
183: query.setParameter("userName", userName);
184: //query.setCacheable(true);
185: User20Impl user = (User20Impl) query.uniqueResult();
186: if (user == null) {
187: throw new NoSuchUserException("No such user "
188: + userName);
189: }
190: return user;
191: } catch (HibernateException e) {
192: String message = "Cannot find user by name " + userName;
193: //log.error(message, e);
194: throw new ModuleException(message, e);
195: }
196: } else {
197: throw new IllegalArgumentException(
198: "user name cannot be null");
199: }
200: }
201:
202: public static User22Impl findUser22ByUserName(Session session,
203: String userName) throws ModuleException {
204: if (userName != null) {
205: try {
206: //Session session = getCurrentToSession();
207: Query query = session
208: .createQuery("from User22Impl where userName=:userName");
209: query.setParameter("userName", userName);
210: //query.setCacheable(true);
211: User22Impl user = (User22Impl) query.uniqueResult();
212: if (user == null) {
213: throw new NoSuchUserException("No such user "
214: + userName);
215: }
216: return user;
217: } catch (HibernateException e) {
218: String message = "Cannot find user by name " + userName;
219: //log.error(message, e);
220: throw new ModuleException(message, e);
221: }
222: } else {
223: throw new IllegalArgumentException(
224: "user name cannot be null");
225: }
226: }
227:
228: public static User24Impl findUser24ByUserName(Session session,
229: String userName) throws ModuleException {
230: if (userName != null) {
231: try {
232: //Session session = getCurrentToSession();
233: Query query = session
234: .createQuery("from User24Impl where userName=:userName");
235: query.setParameter("userName", userName);
236: //query.setCacheable(true);
237: User24Impl user = (User24Impl) query.uniqueResult();
238: if (user == null) {
239: throw new NoSuchUserException("No such user "
240: + userName);
241: }
242: return user;
243: } catch (HibernateException e) {
244: String message = "Cannot find user by name " + userName;
245: //log.error(message, e);
246: throw new ModuleException(message, e);
247: }
248: } else {
249: throw new IllegalArgumentException(
250: "user name cannot be null");
251: }
252: }
253:
254: public static Set findRoles20(Session session)
255: throws ModuleException {
256: try {
257: //Session session = getCurrentFromSession();
258: Query query = session.createQuery("from Role20Impl");
259: Iterator iterator = query.iterate();
260: return Tools.toSet(iterator);
261: } catch (HibernateException e) {
262: String message = "Cannot find roles";
263: //log.error(message, e);
264: throw new ModuleException(message, e);
265: }
266: }
267:
268: public static Role20Impl findRole20ByName(Session session,
269: String name) throws ModuleException {
270: if (name != null) {
271: try {
272: //Session session = getCurrentFromSession();
273: Criteria criteria = session
274: .createCriteria(Role20Impl.class);
275: criteria
276: .add(Restrictions.naturalId().set("name", name));
277: criteria.setCacheable(true);
278: Role20Impl role = (Role20Impl) criteria.uniqueResult();
279: if (role == null) {
280: throw new ModuleException("No such role " + name);
281: }
282: return role;
283: } catch (HibernateException e) {
284: String message = "Cannot find role by name " + name;
285: //log.error(message, e);
286: throw new ModuleException(message, e);
287: }
288: } else {
289: throw new IllegalArgumentException("name cannot be null");
290: }
291: }
292:
293: public static Set findRoles20ByNames(Session session, String[] names)
294: throws ModuleException {
295: if (names != null) {
296: try {
297: //Session session = getCurrentFromSession();
298: StringBuffer queryString = new StringBuffer(
299: "from Role20Impl as g where g.name=?");
300: for (int i = 1; i < names.length; i++) {
301: queryString.append(" or g.name=?");
302: }
303: Query query = session.createQuery(queryString
304: .toString());
305: for (int i = 0; i < names.length; i++) {
306: query.setString(i, names[i]);
307: }
308: Iterator iterator = query.iterate();
309: return Tools.toSet(iterator);
310: } catch (HibernateException e) {
311: String message = "Cannot find roles";
312: //log.error(message, e);
313: throw new ModuleException(message, e);
314: }
315: } else {
316: throw new IllegalArgumentException("name cannot be null");
317: }
318: }
319:
320: public static Set findRoles22(Session session)
321: throws ModuleException {
322: try {
323: //Session session = getCurrentToSession();
324: Query query = session.createQuery("from Role22Impl");
325: Iterator iterator = query.iterate();
326: return Tools.toSet(iterator);
327: } catch (HibernateException e) {
328: String message = "Cannot find roles";
329: //log.error(message, e);
330: throw new ModuleException(message, e);
331: }
332: }
333:
334: public static Role22Impl findRole22ByName(Session session,
335: String name) throws ModuleException {
336: if (name != null) {
337: try {
338: //Session session = getCurrentToSession();
339: Criteria criteria = session
340: .createCriteria(Role22Impl.class);
341: criteria
342: .add(Restrictions.naturalId().set("name", name));
343: criteria.setCacheable(true);
344: Role22Impl role = (Role22Impl) criteria.uniqueResult();
345: if (role == null) {
346: throw new ModuleException("No such role " + name);
347: }
348: return role;
349: } catch (HibernateException e) {
350: String message = "Cannot find role by name " + name;
351: //log.error(message, e);
352: throw new ModuleException(message, e);
353: }
354: } else {
355: throw new IllegalArgumentException("name cannot be null");
356: }
357: }
358:
359: public static Role24Impl findRole24ByName(Session session,
360: String name) throws ModuleException {
361: if (name != null) {
362: try {
363: //Session session = getCurrentToSession();
364: Criteria criteria = session
365: .createCriteria(Role24Impl.class);
366: criteria
367: .add(Restrictions.naturalId().set("name", name));
368: //criteria.setCacheable(true);
369: Role24Impl role = (Role24Impl) criteria.uniqueResult();
370: if (role == null) {
371: throw new ModuleException("No such role " + name);
372: }
373: return role;
374: } catch (HibernateException e) {
375: String message = "Cannot find role by name " + name;
376: //log.error(message, e);
377: throw new ModuleException(message, e);
378: }
379: } else {
380: throw new IllegalArgumentException("name cannot be null");
381: }
382: }
383:
384: public static Set findRoles22ByNames(Session session, String[] names)
385: throws ModuleException {
386: if (names != null) {
387: try {
388: //Session session = getCurrentFromSession();
389: StringBuffer queryString = new StringBuffer(
390: "from Role22Impl as g where g.name=?");
391: for (int i = 1; i < names.length; i++) {
392: queryString.append(" or g.name=?");
393: }
394: Query query = session.createQuery(queryString
395: .toString());
396: for (int i = 0; i < names.length; i++) {
397: query.setString(i, names[i]);
398: }
399: Iterator iterator = query.iterate();
400: return Tools.toSet(iterator);
401: } catch (HibernateException e) {
402: String message = "Cannot find roles";
403: //log.error(message, e);
404: throw new ModuleException(message, e);
405: }
406: } else {
407: throw new IllegalArgumentException("name cannot be null");
408: }
409: }
410:
411: }
|