KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > dao > AccountDAOImpl


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.dao;
8
9 import java.sql.SQLException JavaDoc;
10 import java.util.List JavaDoc;
11 import javax.ejb.Local JavaDoc;
12 import javax.ejb.Stateless JavaDoc;
13 import javax.persistence.PersistenceContext;
14 import javax.persistence.Query;
15 import javax.persistence.NamedNativeQuery;
16 import javax.persistence.NamedNativeQueries;
17 import javax.persistence.EntityManager;
18 import javax.persistence.QueryHint;
19
20 import org.jfox.petstore.entity.Account;
21 import org.jfox.entity.EntityManagerExt;
22 import org.jfox.entity.dao.DAOSupport;
23
24 /**
25  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
26  */

27 @NamedNativeQueries(
28         {
29         @NamedNativeQuery(
30                 name = AccountDAOImpl.GET_ACCOUNT_BY_USERNAME,
31                 query = "select " +
32                         "signon.username as userid," +
33                         "account.email as email, " +
34                         "account.firstname as firstname," +
35                         "account.lastname as lastname," +
36                         "account.status as status," +
37                         "account.addr1 as addr1," +
38                         "account.addr2 as addr2," +
39                         "account.city as city," +
40                         "account.state as state," +
41                         "account.zip as zip," +
42                         "account.country as country," +
43                         "account.phone as phone," +
44                         "signon.password," +
45                         "profile.langpref as langpref," +
46                         "profile.favcategory as favcategory," +
47                         "profile.mylistopt as mylistopt," +
48                         "profile.banneropt as banneropt," +
49                         "bannerdata.bannername as bannername" +
50                         " from account, profile, signon, bannerdata" +
51                         " where account.userid = $username" +
52                         " and signon.username = account.userid" +
53                         " and profile.userid = account.userid" +
54                         " and profile.favcategory = bannerdata.favcategory",
55                 resultClass = Account.class,
56                 hints = {
57                         @QueryHint(name = "cache.partition", value = "account")
58                         }
59         ),
60
61         @NamedNativeQuery(
62                 name = AccountDAOImpl.GET_ACCOUNT_BY_USERNAME_AND_PASSWORD,
63                 query = "select " +
64                         "signon.username as userid," +
65                         "account.email," +
66                         "account.firstname," +
67                         "account.lastname," +
68                         "account.status," +
69                         "account.addr1," +
70                         "account.addr2," +
71                         "account.city," +
72                         "account.state," +
73                         "account.zip," +
74                         "account.country," +
75                         "account.phone," +
76                         "signon.password," +
77                         "profile.langpref," +
78                         "profile.favcategory," +
79                         "profile.mylistopt," +
80                         "profile.banneropt," +
81                         "bannerdata.bannername" +
82                         " from account, profile, signon, bannerdata" +
83                         " where account.userid = $username" +
84                         " and signon.password = $password" +
85                         " and signon.username = account.userid" +
86                         " and profile.userid = account.userid" +
87                         " and profile.favcategory = bannerdata.favcategory",
88                 resultClass = Account.class,
89                 hints = {
90                         @QueryHint(name = "cache.partition", value = "account")
91                         }
92
93         ),
94
95         @NamedNativeQuery(
96                 name = AccountDAOImpl.GET_USERNAME_LIST,
97                 query = "select username as userid from signon",
98                 resultClass = String JavaDoc.class,
99                 hints = {
100                         @QueryHint(name = "cache.partition", value = "account")
101                         }
102
103         ),
104
105         @NamedNativeQuery(
106                 name = AccountDAOImpl.UPDATE_ACCOUNT,
107                 query = "update account set " +
108                         "email = $account.getEmail(), " +
109                         "firstname = $account.getFirstName(), " +
110                         "lastname = $account.getLastName(), " +
111                         "status = $account.getStatus(), " +
112                         "addr1 = $account.getAddress1(), " +
113                         "addr2 = $account.getAddress2(), " +
114                         "city = $account.getCity(), " +
115                         "state = $account.getState(), " +
116                         "zip = $account.getZip(), " +
117                         "country = $account.getCountry(), " +
118                         "phone = $account.getPhone() " +
119                         "where " +
120                         "userid = $account.getUsername()",
121                 hints = {
122                         @QueryHint(name = "cache.partition", value = "account")
123                         }
124
125         ),
126
127         @NamedNativeQuery(
128                 name = AccountDAOImpl.INSERT_ACCOUNT,
129                 query = "insert into account (" +
130                         "email, " +
131                         "firstname, " +
132                         "lastname, " +
133                         "status, " +
134                         "addr1, " +
135                         "addr2, " +
136                         "city, " +
137                         "state, " +
138                         "zip, " +
139                         "country, " +
140                         "phone, " +
141                         "userid" +
142                         ") values (" +
143                         "$account.getEmail(), " +
144                         "$account.getFirstName(), " +
145                         "$account.getLastName(), " +
146                         "$account.getStatus(), " +
147                         "$account.getAddress1(), " +
148                         "$account.getAddress2(), " +
149                         "$account.getCity(), " +
150                         "$account.getState(), " +
151                         "$account.getZip(), " +
152                         "$account.getCountry(), " +
153                         "$account.getPhone(), " +
154                         "$account.getUsername())",
155                 hints = {
156                         @QueryHint(name = "cache.partition", value = "account")
157                         }
158
159         ),
160
161         @NamedNativeQuery(
162                 name = AccountDAOImpl.UPDATE_PROFILE,
163                 query = "update profile set langpref = $account.getLanguagePreference(), favcategory = $account.getFavouriteCategoryId(), mylistopt = $account.getListOption(), banneropt = $account.getBannerOption() where userid = $account.getUsername()"
164         ),
165
166         @NamedNativeQuery(
167                 name = AccountDAOImpl.INSERT_PROFILE,
168                 query = "insert into profile (" +
169                         "langpref, " +
170                         "favcategory, " +
171                         "mylistopt, " +
172                         "banneropt, " +
173                         "userid" +
174                         ") values (" +
175                         "$account.getLanguagePreference(), " +
176                         "$account.getFavouriteCategoryId(), " +
177                         "$account.getListOption(), " +
178                         "$account.getBannerOption(), " +
179                         "$account.getUsername())"
180         ),
181
182         @NamedNativeQuery(
183                 name = AccountDAOImpl.UPDATE_SIGNON,
184                 query = "update signon set password = $password where username = $username"
185         ),
186
187         @NamedNativeQuery(
188                 name = AccountDAOImpl.INSERT_SIGNON,
189                 query = "insert into signon (password,username) values ($account.getPassword(),$account.getUsername())"
190         )
191
192                 }
193 )
194 @Stateless JavaDoc
195 @Local JavaDoc
196 @SuppressWarnings JavaDoc("unchecked")
197 public class AccountDAOImpl extends DAOSupport implements AccountDAO {
198     public static final String JavaDoc GET_ACCOUNT_BY_USERNAME = "getAccountByUsername";
199     public static final String JavaDoc GET_ACCOUNT_BY_USERNAME_AND_PASSWORD = "getAccountByUsernameAndPassword";
200     public static final String JavaDoc GET_USERNAME_LIST = "getUsernameList";
201     public static final String JavaDoc UPDATE_ACCOUNT = "updateAccount";
202     public static final String JavaDoc INSERT_ACCOUNT = "insertAccount";
203     public static final String JavaDoc UPDATE_PROFILE = "updateProfile";
204     public static final String JavaDoc INSERT_PROFILE = "insertProfile";
205     public static final String JavaDoc UPDATE_SIGNON = "updateSignon";
206     public static final String JavaDoc INSERT_SIGNON = "insertSignon";
207
208     /**
209      * 用æ?¥è¿›è¡Œæ•°æ?®åº“æ“?作
210      */

211     @PersistenceContext(unitName = "JPetstoreMysqlDS")
212     private EntityManagerExt em = null;
213
214     /**
215      * 返回 EntityManager,默认注入的是 default
216      */

217     protected EntityManager getEntityManager() {
218         return em;
219     }
220
221     public Account getAccount(String JavaDoc username) throws SQLException JavaDoc {
222         Query query = createNamedNativeQuery(GET_ACCOUNT_BY_USERNAME).setParameter("username", username);
223         return (Account)query.getSingleResult();
224     }
225
226     public Account getAccount(String JavaDoc username, String JavaDoc password)
227             throws SQLException JavaDoc {
228         Query query = createNamedNativeQuery(GET_ACCOUNT_BY_USERNAME_AND_PASSWORD)
229                             .setParameter("username",username)
230                             .setParameter("password",password);
231         return (Account)query.getSingleResult();
232     }
233
234     public List JavaDoc<String JavaDoc> getUsernameList() throws SQLException JavaDoc {
235         Query query = createNamedNativeQuery(GET_USERNAME_LIST);
236         return (List JavaDoc<String JavaDoc>)query.getResultList();
237     }
238
239     public void insertAccount(Account account) throws SQLException JavaDoc {
240 // getEntityManager().getTransaction().begin();
241
createNamedNativeQuery(INSERT_ACCOUNT).setParameter("account", account).executeUpdate();
242         createNamedNativeQuery(INSERT_SIGNON).setParameter("account", account).executeUpdate();
243         createNamedNativeQuery(INSERT_PROFILE).setParameter("account", account).executeUpdate();
244 // em.getTransaction().commit();
245
}
246
247     public void updateAccount(Account account) throws SQLException JavaDoc {
248         createNamedNativeQuery(UPDATE_ACCOUNT).setParameter("account", account).executeUpdate();
249         createNamedNativeQuery(UPDATE_PROFILE).setParameter("account", account).executeUpdate();
250         createNamedNativeQuery(UPDATE_SIGNON).setParameter("username", account.getUsername()).setParameter("password",account.getPassword()).executeUpdate();
251     }
252
253 }
254
Popular Tags