KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > persistence > UserPersistenceProvider


1 /*
2  * Copyright (C) 2002 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later 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
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.providers.persistence;
21
22 import java.util.List JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.efs.openreports.objects.ReportUser;
26 import org.efs.openreports.providers.HibernateProvider;
27 import org.efs.openreports.providers.ProviderException;
28
29 import org.hibernate.*;
30
31 public class UserPersistenceProvider
32 {
33     protected static Logger log =
34         Logger.getLogger(UserPersistenceProvider.class.getName());
35
36     public UserPersistenceProvider() throws ProviderException
37     {
38         super();
39
40         log.info("UserPersistenceProvider Created.");
41     }
42
43     public ReportUser getUser(String JavaDoc name) throws ProviderException
44     {
45         try
46         {
47             Session session = HibernateProvider.openSession();
48             
49             try
50             {
51                 List JavaDoc list = session.createQuery(
52                         "from org.efs.openreports.objects.ReportUser as user "
53                                 + "where user.name = ?").setString(0, name).list();
54
55                 if (list.size() == 0)
56                     return null;
57
58                 ReportUser user = (ReportUser) list.get(0);
59
60                 return user;
61             }
62             catch (HibernateException he)
63             {
64                 throw he;
65             }
66             finally
67             {
68                 session.close();
69             }
70         }
71         catch (HibernateException he)
72         {
73             throw new ProviderException(he);
74         }
75     }
76
77     public ReportUser getUser(Integer JavaDoc id) throws ProviderException
78     {
79         return (ReportUser) HibernateProvider.load(ReportUser.class, id);
80     }
81
82     public List JavaDoc getUsers() throws ProviderException
83     {
84         String JavaDoc fromClause =
85             "from org.efs.openreports.objects.ReportUser reportUser order by reportUser.name ";
86
87         return HibernateProvider.query(fromClause);
88     }
89
90     public ReportUser insertUser(ReportUser user) throws ProviderException
91     {
92         return (ReportUser) HibernateProvider.save(user);
93     }
94
95     public void updateUser(ReportUser user) throws ProviderException
96     {
97         HibernateProvider.update(user);
98     }
99
100     public void deleteUser(ReportUser user) throws ProviderException
101     {
102         Session session = HibernateProvider.openSession();
103         Transaction tx = null;
104
105         try
106         {
107             tx = session.beginTransaction();
108             
109             //delete user
110
session.delete(user);
111             
112             //delete report log entries for user
113
session
114                     .createQuery(
115                             "DELETE org.efs.openreports.objects.ReportLog reportLog where reportLog.user.id = ? ")
116                     .setInteger(0, user.getId().intValue()).executeUpdate();
117             
118             tx.commit();
119         }
120         catch (HibernateException he)
121         {
122             HibernateProvider.rollbackTransaction(tx);
123
124             throw new ProviderException(he.getMessage());
125         }
126         finally
127         {
128             HibernateProvider.closeSession(session);
129         }
130     }
131 }
Popular Tags