1 package org.argosfields.service;
2
3 import java.util.List;
4
5 import net.sf.hibernate.HibernateException;
6 import net.sf.hibernate.ObjectNotFoundException;
7 import net.sf.hibernate.Session;
8
9 import org.apache.commons.lang.NullArgumentException;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argosfields.persistence.Account;
13 import org.springframework.orm.hibernate.SessionFactoryUtils;
14 import org.springframework.orm.hibernate.support.HibernateDaoSupport;
15
16 /***
17 * AccountManager.java
18 *
19 * @author Xavier Cho
20 * @version $Revision: 1.1 $ $Date: 2004/04/15 05:43:38 $
21 */
22 public class AccountManager
23 extends HibernateDaoSupport
24 implements IAccountManager {
25 private static Log log = LogFactory.getLog(AccountManager.class);
26
27 /***
28 * @see org.argosfields.persistence.IAccountManager#createAccount(org.argosfields.persistence.Account)
29 */
30 public void createAccount(final Account account) {
31 if (account == null) {
32 throw new NullArgumentException("account");
33 }
34
35 if (log.isDebugEnabled()) {
36 log.debug("Creating new account : ");
37 log.debug(" - account : " + account);
38 }
39
40 Session session =
41 SessionFactoryUtils.getSession(getSessionFactory(), false);
42
43 try {
44 session.save(account);
45 } catch (HibernateException e) {
46 SessionFactoryUtils.convertHibernateAccessException(e);
47 }
48
49 if (log.isInfoEnabled()) {
50 log.info("New account has been created successfully : ");
51 log.info(" - user name : " + account.getUserName());
52 }
53 }
54
55 /***
56 * @see org.argosfields.persistence.IAccountManager#updateAccount(org.argosfields.persistence.Account)
57 */
58 public void updateAccount(final Account account) {
59 if (account == null) {
60 throw new NullArgumentException("account");
61 }
62
63 if (log.isDebugEnabled()) {
64 log.debug("Updating account : ");
65 log.debug(" - account : " + account);
66 }
67
68 Session session =
69 SessionFactoryUtils.getSession(getSessionFactory(), false);
70
71 try {
72 Account old = getAccount(account.getUserName());
73 old.setAdmin(account.isAdmin());
74 old.setEmail(account.getEmail());
75 old.setPassword(account.getPassword());
76 old.setRealName(account.getRealName());
77
78 session.update(old);
79 } catch (HibernateException e) {
80 SessionFactoryUtils.convertHibernateAccessException(e);
81 }
82
83 if (log.isInfoEnabled()) {
84 log.info("Account has been updated successfully : ");
85 log.info(" - user name : " + account.getUserName());
86 }
87 }
88
89 /***
90 * @see org.argosfields.persistence.IAccountManager#deleteAccount(java.lang.String)
91 */
92 public void deleteAccount(final String userName) {
93 if (userName == null) {
94 throw new NullArgumentException("userName");
95 }
96
97 if (log.isDebugEnabled()) {
98 log.debug("Deleting account : ");
99 }
100
101 Session session =
102 SessionFactoryUtils.getSession(getSessionFactory(), false);
103
104 try {
105 Account account = getAccount(userName);
106
107 if (log.isDebugEnabled()) {
108 log.debug(" - account : " + account);
109 }
110
111 session.delete(account);
112 } catch (HibernateException e) {
113 SessionFactoryUtils.convertHibernateAccessException(e);
114 }
115
116 if (log.isInfoEnabled()) {
117 log.info("Account has been deleted successfully : ");
118 log.info(" - user name : " + userName);
119 }
120 }
121
122 /***
123 * @see org.argosfields.persistence.IAccountManager#getAccount(java.lang.String)
124 */
125 public Account getAccount(final String userName) {
126 if (userName == null) {
127 throw new NullArgumentException("userName");
128 }
129
130 Account account = null;
131
132 if (log.isDebugEnabled()) {
133 log.debug("Retrieving account information : ");
134 log.debug(" - user name : " + userName);
135 }
136
137 Session session =
138 SessionFactoryUtils.getSession(getSessionFactory(), false);
139
140 try {
141 account = (Account) session.load(Account.class, userName);
142 } catch (ObjectNotFoundException e) {
143 return null;
144 } catch (HibernateException e) {
145 SessionFactoryUtils.convertHibernateAccessException(e);
146 }
147
148 if (log.isDebugEnabled()) {
149 log.debug("Retrieved account information : ");
150 log.debug(" - account : " + account);
151 }
152
153 return account;
154 }
155
156 /***
157 * @see org.argosfields.persistence.IAccountManager#getAccounts()
158 */
159 public List getAccounts() {
160 if (log.isDebugEnabled()) {
161 log.debug("Retrieving all account information...");
162 }
163
164 List accounts = null;
165
166 Session session =
167 SessionFactoryUtils.getSession(getSessionFactory(), false);
168
169 try {
170 accounts = session.find("from Account");
171 } catch (HibernateException e) {
172 SessionFactoryUtils.convertHibernateAccessException(e);
173 }
174
175 if (log.isInfoEnabled()) {
176 log.debug("Found " + accounts.size() + " accounts.");
177 }
178
179 return accounts;
180 }
181 }
This page was automatically generated by Maven