001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.identity.hibernate;
023:
024: import java.util.*;
025:
026: import org.apache.commons.logging.*;
027: import org.hibernate.*;
028: import org.jbpm.db.JbpmSession;
029: import org.jbpm.identity.*;
030: import org.jbpm.identity.assignment.*;
031: import org.jbpm.identity.security.*;
032:
033: public class IdentitySession implements IdentityService,
034: ExpressionSession {
035:
036: Session session = null;
037: private Transaction transaction;
038:
039: public IdentitySession(Session session) {
040: this .session = session;
041: }
042:
043: public IdentitySession() {
044: JbpmSession currentJbpmSession = JbpmSession
045: .getCurrentJbpmSession();
046: if ((currentJbpmSession == null)
047: || (currentJbpmSession.getSession() == null)
048: || (!currentJbpmSession.getSession().isOpen())) {
049: throw new RuntimeException(
050: "no active JbpmSession to create an identity session");
051: }
052: session = currentJbpmSession.getSession();
053: }
054:
055: // IdentityService methods //////////////////////////////////////////////////
056:
057: public Object verify(String userName, String pwd) {
058: Object userId = null;
059: Query query = session.createQuery("select user.id "
060: + "from org.jbpm.identity.User as user "
061: + "where user.name = :userName "
062: + " and user.password = :password");
063: query.setString("userName", userName);
064: query.setString("password", pwd);
065: userId = (Long) query.uniqueResult();
066: return userId;
067: }
068:
069: public User getUserById(Object userId) {
070: return (User) session.load(User.class, (Long) userId);
071: }
072:
073: // transaction convenience methods //////////////////////////////////////////
074:
075: public Session getSession() {
076: return session;
077: }
078:
079: public Transaction getTransaction() {
080: return transaction;
081: }
082:
083: public void beginTransaction() {
084: try {
085: transaction = session.beginTransaction();
086: } catch (HibernateException e) {
087: log.error(e);
088: throw new RuntimeException("couldn't begin a transaction",
089: e);
090: }
091: }
092:
093: public void commitTransaction() {
094: if (transaction == null) {
095: throw new RuntimeException(
096: "can't commit : no transaction started");
097: }
098: try {
099: session.flush();
100: transaction.commit();
101: } catch (HibernateException e) {
102: log.error(e);
103: throw new RuntimeException("couldn't commit transaction", e);
104: }
105: transaction = null;
106: }
107:
108: public void rollbackTransaction() {
109: if (transaction == null) {
110: throw new RuntimeException(
111: "can't rollback : no transaction started");
112: }
113: try {
114: transaction.rollback();
115: } catch (HibernateException e) {
116: log.error(e);
117: throw new RuntimeException("couldn't rollback transaction",
118: e);
119: }
120: transaction = null;
121: }
122:
123: public void commitTransactionAndClose() {
124: commitTransaction();
125: close();
126: }
127:
128: public void rollbackTransactionAndClose() {
129: rollbackTransaction();
130: close();
131: }
132:
133: public void close() {
134: try {
135: session.close();
136: } catch (HibernateException e) {
137: log.error(e);
138: throw new RuntimeException(
139: "couldn't close the hibernate connection", e);
140: }
141: }
142:
143: // identity methods /////////////////////////////////////////////////////////
144:
145: public void saveUser(User user) {
146: session.save(user);
147: }
148:
149: public void saveGroup(Group group) {
150: session.save(group);
151: }
152:
153: public void saveEntity(Entity entity) {
154: session.save(entity);
155: }
156:
157: public void saveMembership(Membership membership) {
158: session.save(membership);
159: }
160:
161: public User loadUser(long userId) {
162: return (User) session.load(User.class, new Long(userId));
163: }
164:
165: public Group loadGroup(long groupId) {
166: return (Group) session.load(Group.class, new Long(groupId));
167: }
168:
169: public User getUserByName(String userName) {
170: User user = null;
171: Query query = session.createQuery("select u "
172: + "from org.jbpm.identity.User as u "
173: + "where u.name = :userName");
174: query.setString("userName", userName);
175: List users = query.list();
176: if ((users != null) && (users.size() > 0)) {
177: user = (User) users.get(0);
178: }
179: return user;
180: }
181:
182: public Group getGroupByName(String groupName) {
183: Group group = null;
184: Query query = session.createQuery("select g "
185: + "from org.jbpm.identity.Group as g "
186: + "where g.name = :groupName");
187: query.setString("groupName", groupName);
188: List groups = query.list();
189: if ((groups != null) && (groups.size() > 0)) {
190: group = (Group) groups.get(0);
191: }
192: return group;
193: }
194:
195: public List getUsers() {
196: Query query = session.createQuery("select u "
197: + "from org.jbpm.identity.User as u");
198: return query.list();
199: }
200:
201: public List getGroupNamesByUserAndGroupType(String userName,
202: String groupType) {
203: Query query = session
204: .createQuery("select membership.group.name "
205: + "from org.jbpm.identity.Membership as membership "
206: + "where membership.user.name = :userName "
207: + " and membership.group.type = :groupType");
208: query.setString("userName", userName);
209: query.setString("groupType", groupType);
210: return query.list();
211: }
212:
213: public User getUserByGroupAndRole(String groupName, String role) {
214: User user = null;
215: Query query = session.createQuery("select m.user "
216: + "from org.jbpm.identity.Membership as m "
217: + "where m.group.name = :groupName "
218: + " and m.role = :role");
219: query.setString("groupName", groupName);
220: query.setString("role", role);
221: user = (User) query.uniqueResult();
222: return user;
223: }
224:
225: private static final Log log = LogFactory
226: .getLog(IdentitySession.class);
227: }
|