KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > hibernate > IdentitySession


1 package org.jbpm.identity.hibernate;
2
3 import java.util.*;
4
5 import org.apache.commons.logging.*;
6 import org.hibernate.*;
7 import org.jbpm.db.JbpmSession;
8 import org.jbpm.identity.*;
9 import org.jbpm.identity.assignment.*;
10 import org.jbpm.identity.security.*;
11
12 public class IdentitySession implements IdentityService, ExpressionSession {
13   
14   Session session = null;
15   private Transaction transaction;
16
17   public IdentitySession(Session session) {
18     this.session = session;
19   }
20
21   public IdentitySession() {
22     JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
23     if ( (currentJbpmSession==null)
24          || (currentJbpmSession.getSession()==null)
25          || (! currentJbpmSession.getSession().isOpen())
26        ) {
27       throw new RuntimeException JavaDoc("no active JbpmSession to create an identity session");
28     }
29     session = currentJbpmSession.getSession();
30   }
31
32   // IdentityService methods //////////////////////////////////////////////////
33

34   public Object JavaDoc verify(String JavaDoc userName, String JavaDoc pwd) {
35     Query query = session.createQuery(
36         "select user.id " +
37         "from org.jbpm.identity.User as user " +
38         "where user.name = :userName " +
39         " and password.text = :password");
40     query.setString("userName", userName);
41     query.setString("password", pwd);
42     List userIds = query.list();
43     if ( (userIds==null)
44          || (userIds.size()==0) ) {
45       throw new RuntimeException JavaDoc("couldn't find user '"+userName+"'");
46     }
47     return userIds.get(0);
48   }
49
50   public User getUserById(Object JavaDoc userId) {
51     return (User) session.load(User.class, (Long JavaDoc) userId);
52   }
53   
54   // transaction convenience methods //////////////////////////////////////////
55

56   public Session getSession() {
57     return session;
58   }
59
60   public Transaction getTransaction() {
61     return transaction;
62   }
63
64   public void beginTransaction() {
65     try {
66       transaction = session.beginTransaction();
67     } catch (HibernateException e) {
68       log.error( e );
69       throw new RuntimeException JavaDoc( "couldn't begin a transaction", e );
70     }
71   }
72
73   public void commitTransaction() {
74     if ( transaction == null ) {
75       throw new RuntimeException JavaDoc("can't commit : no transaction started" );
76     }
77     try {
78       session.flush();
79       transaction.commit();
80     } catch (HibernateException e) {
81       log.error( e );
82       throw new RuntimeException JavaDoc( "couldn't commit transaction", e );
83     }
84     transaction = null;
85   }
86
87   public void rollbackTransaction() {
88     if ( transaction == null ) {
89       throw new RuntimeException JavaDoc("can't rollback : no transaction started" );
90     }
91     try {
92       transaction.rollback();
93     } catch (HibernateException e) {
94       log.error( e );
95       throw new RuntimeException JavaDoc( "couldn't rollback transaction", e );
96     }
97     transaction = null;
98   }
99   
100   public void commitTransactionAndClose() {
101     commitTransaction();
102     close();
103   }
104   
105   public void rollbackTransactionAndClose() {
106     rollbackTransaction();
107     close();
108   }
109
110   public void close() {
111     try {
112       session.close();
113     } catch (HibernateException e) {
114       log.error( e );
115       throw new RuntimeException JavaDoc( "couldn't close the hibernate connection", e );
116     }
117   }
118   
119   // identity methods /////////////////////////////////////////////////////////
120

121   public void saveUser(User user) {
122     session.save(user);
123   }
124   public void saveGroup(Group group) {
125     session.save(group);
126   }
127   public void saveEntity(Entity entity) {
128     session.save(entity);
129   }
130
131   public User loadUser(long userId) {
132     return (User) session.load(User.class, new Long JavaDoc(userId));
133   }
134
135   public Group loadGroup(long groupId) {
136     return (Group) session.load(Group.class, new Long JavaDoc(groupId));
137   }
138
139   public User getUserByName(String JavaDoc userName) {
140     User user = null;
141     Query query = session.createQuery(
142       "select u " +
143       "from org.jbpm.identity.User as u " +
144       "where u.name = :userName"
145     );
146     query.setString("userName", userName);
147     List users = query.list();
148     if ( (users!=null)
149          && (users.size()>0) ) {
150       user = (User) users.get(0);
151     }
152     return user;
153   }
154
155   public Group getGroupByName(String JavaDoc groupName) {
156     Group group = null;
157     Query query = session.createQuery(
158       "select g " +
159       "from org.jbpm.identity.Group as g " +
160       "where g.name = :groupName"
161     );
162     query.setString("groupName", groupName);
163     List groups = query.list();
164     if ( (groups!=null)
165          && (groups.size()>0) ) {
166       group = (Group) groups.get(0);
167     }
168     return group;
169   }
170   
171   public List getUsers() {
172     Query query = session.createQuery(
173       "select u " +
174       "from org.jbpm.identity.User as u"
175     );
176     return query.list();
177   }
178
179   private static final Log log = LogFactory.getLog(IdentitySession.class);
180 }
181
Popular Tags