001: /*
002: * Copyright (C) 2002 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.providers.impl;
021:
022: import java.util.List;
023:
024: import org.apache.log4j.Logger;
025: import org.efs.openreports.objects.ReportGroup;
026: import org.efs.openreports.objects.ReportUser;
027: import org.efs.openreports.providers.HibernateProvider;
028: import org.efs.openreports.providers.ProviderException;
029: import org.efs.openreports.providers.UserProvider;
030: import org.hibernate.HibernateException;
031: import org.hibernate.Session;
032: import org.hibernate.Transaction;
033:
034: public class UserProviderImpl implements UserProvider {
035: protected static Logger log = Logger
036: .getLogger(UserProviderImpl.class.getName());
037:
038: private HibernateProvider hibernateProvider;
039:
040: public UserProviderImpl(HibernateProvider hibernateProvider)
041: throws ProviderException {
042: this .hibernateProvider = hibernateProvider;
043:
044: log.info("UserProviderImpl created");
045: }
046:
047: @SuppressWarnings("unchecked")
048: public ReportUser getUser(String name) throws ProviderException {
049: try {
050: Session session = hibernateProvider.openSession();
051:
052: try {
053: List<ReportUser> list = session.createQuery(
054: "from org.efs.openreports.objects.ReportUser as user "
055: + "where user.name = ?").setString(0,
056: name).list();
057:
058: if (list.size() == 0)
059: return null;
060:
061: ReportUser user = list.get(0);
062:
063: return user;
064: } catch (HibernateException he) {
065: throw he;
066: } finally {
067: session.close();
068: }
069: } catch (HibernateException he) {
070: throw new ProviderException(he);
071: }
072: }
073:
074: public ReportUser getUser(Integer id) throws ProviderException {
075: return (ReportUser) hibernateProvider
076: .load(ReportUser.class, id);
077: }
078:
079: @SuppressWarnings("unchecked")
080: public List<ReportUser> getUsers() throws ProviderException {
081: String fromClause = "from org.efs.openreports.objects.ReportUser reportUser order by reportUser.name ";
082:
083: return (List<ReportUser>) hibernateProvider.query(fromClause);
084: }
085:
086: public ReportUser insertUser(ReportUser user)
087: throws ProviderException {
088: return (ReportUser) hibernateProvider.save(user);
089: }
090:
091: public void updateUser(ReportUser user) throws ProviderException {
092: hibernateProvider.update(user);
093: }
094:
095: public void deleteUser(ReportUser user) throws ProviderException {
096: Session session = hibernateProvider.openSession();
097: Transaction tx = null;
098:
099: try {
100: tx = session.beginTransaction();
101:
102: //delete user
103: session.delete(user);
104:
105: //delete report log entries for user
106: session
107: .createQuery(
108: "DELETE org.efs.openreports.objects.ReportLog reportLog where reportLog.user.id = ? ")
109: .setInteger(0, user.getId().intValue())
110: .executeUpdate();
111:
112: tx.commit();
113: } catch (HibernateException he) {
114: hibernateProvider.rollbackTransaction(tx);
115:
116: throw new ProviderException(he.getMessage());
117: } finally {
118: hibernateProvider.closeSession(session);
119: }
120: }
121:
122: @SuppressWarnings("unchecked")
123: public List<ReportUser> getUsersForGroup(ReportGroup reportGroup)
124: throws ProviderException {
125: try {
126: Session session = hibernateProvider.openSession();
127:
128: try {
129: List<ReportUser> list = session
130: .createQuery(
131: "from org.efs.openreports.objects.ReportUser as reportUser "
132: + "where ? in elements(reportUser.groups)")
133: .setEntity(0, reportGroup).list();
134:
135: if (list.size() == 0)
136: return null;
137:
138: return list;
139: } catch (HibernateException he) {
140: throw he;
141: } finally {
142: session.close();
143: }
144: } catch (HibernateException he) {
145: throw new ProviderException(he);
146: }
147: }
148: }
|