1 19 20 package org.efs.openreports.providers.persistence; 21 22 import java.util.List ; 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 name) throws ProviderException 44 { 45 try 46 { 47 Session session = HibernateProvider.openSession(); 48 49 try 50 { 51 List 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 id) throws ProviderException 78 { 79 return (ReportUser) HibernateProvider.load(ReportUser.class, id); 80 } 81 82 public List getUsers() throws ProviderException 83 { 84 String 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 session.delete(user); 111 112 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 |