KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > formbean > UserForm


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.formbean;
17
18 import java.sql.SQLException JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 import net.sf.hibernate.HibernateException;
28 import net.sf.hibernate.Session;
29
30 import dlog4j.SiteManager;
31 import dlog4j.UserManager;
32 import dlog4j.security.DlogRole;
33 import dlog4j.util.StringUtils;
34
35 /**
36  * UserForm.java created by EasyStruts - XsltGen.
37  * http://easystruts.sf.net
38  * created on 02-02-2004
39  *
40  * XDoclet definition:
41  * @struts:form name="userForm"
42  */

43 public class UserForm extends DlogActionForm implements Cloneable JavaDoc{
44
45     public final static String JavaDoc KEY = "dlog4j.loginUser";
46     public final static SimpleDateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
47     public final static int MODE_LOGIN = 0;
48     public final static int MODE_REG = 1;
49     public final static int MODE_EDIT = 2;
50     public final static int MODE_CREATE = 4; //创建新站点的管理员
51
public final static int MODE_DELETE = 8;
52     
53     // --------------------------------------------------------- Instance Variables
54

55     /** age property */
56     private int id;
57
58     /** email property */
59     private String JavaDoc email;
60
61     /** name property */
62     private String JavaDoc loginName;
63     
64     private String JavaDoc password;
65     private String JavaDoc displayName;
66     private String JavaDoc homePage;
67     private String JavaDoc resume;
68     private String JavaDoc portrait;
69     private int loginCount;
70     private Date JavaDoc regTime;
71     private Date JavaDoc lastTime;
72     private int userRole = DlogRole.ROLE_GUEST;
73     private int mode = MODE_LOGIN;
74     
75     private List JavaDoc logs;
76     private int logCount;
77     private List JavaDoc replies;
78     private int replyCount;
79     private int bookMarkCount; //此字段不对应数据库,而是由getLoginUser标签设置该字段的值
80

81     SiteForm site;
82     
83     DlogRole role;
84     
85     /**
86      * 如果用户是密友则该属性为拥有特权(查看与添加)的分类
87      * 如果用户是好友则该属性是用户可添加日记的分类
88      * 多个分类使用逗号格开,例如 1,4,5
89      */

90     private String JavaDoc cats;
91     
92     public static UserForm getLoginUser(HttpServletRequest JavaDoc request){
93         SiteForm sf = SiteManager.getCurrentSite(request);
94         if(sf!=null)
95             return (UserForm) request.getSession().getAttribute(sf.getName()+'_'+UserForm.KEY);
96         return null;
97     }
98     
99     public void saveLoginUser(HttpServletRequest JavaDoc request){
100         SiteForm sf = SiteManager.getCurrentSite(request);
101         if(sf!=null)
102             request.getSession().setAttribute(sf.getName()+'_'+UserForm.KEY, this);
103     }
104     
105     public static void removeFromSession(HttpServletRequest JavaDoc request){
106         SiteForm sf = SiteManager.getCurrentSite(request);
107         if(sf!=null)
108             request.getSession().removeAttribute(sf.getName()+'_'+UserForm.KEY);
109     }
110     
111     public boolean isLogin(){
112         return regTime!=null;
113     }
114     
115     public boolean isAdmin(){
116         return userRole == DlogRole.ROLE_MANAGER;
117     }
118     
119     public boolean isFriend(){
120         return userRole == DlogRole.ROLE_FRIEND;
121     }
122
123     // --------------------------------------------------------- Methods
124

125     public String JavaDoc toString(){
126         StringBuffer JavaDoc user = new StringBuffer JavaDoc(100);
127         user.append("Name:");
128         user.append(loginName);
129         user.append(",displayName:");
130         user.append(displayName);
131         user.append(",email:");
132         user.append(email);
133         user.append(",password:");
134         user.append(password);
135         return user.toString();
136     }
137     /**
138      * 检查登录帐号是否已存在
139      * @param name
140      * @return
141      */

142     protected UserForm checkLoginName(SiteForm site, String JavaDoc name){
143         Session ssn = null;
144         UserForm user = null;
145         try{
146             ssn = getSession();
147             user = UserManager.getUser(ssn,site,name);
148         }catch(HibernateException e){
149         }catch(SQLException JavaDoc e){
150         }finally{
151             try{
152                 closeSession(ssn);
153             }catch(Exception JavaDoc e){}
154         }
155         return user;
156     }
157
158     /**
159      * @return
160      */

161     public String JavaDoc getDisplayName() {
162         return displayName;
163     }
164
165     /**
166      * @return
167      */

168     public String JavaDoc getEmail() {
169         return email;
170     }
171
172     /**
173      * @return
174      */

175     public String JavaDoc getHomePage() {
176         return homePage;
177     }
178
179     /**
180      * @return
181      */

182     public int getId() {
183         return id;
184     }
185
186     /**
187      * @return
188      */

189     public Date JavaDoc getLastTime() {
190         return lastTime;
191     }
192
193     /**
194      * @return
195      */

196     public int getLoginCount() {
197         return loginCount;
198     }
199
200     /**
201      * @return
202      */

203     public String JavaDoc getPassword() {
204         return password;
205     }
206     
207     public String JavaDoc getCryptPassword() {
208         return StringUtils.encrypt(password);
209     }
210     
211     public void setCryptPassword(String JavaDoc pwd) {
212         password = StringUtils.decrypt(pwd);
213     }
214
215     /**
216      * @return
217      */

218     public Date JavaDoc getRegTime() {
219         return regTime;
220     }
221
222     /**
223      * @return
224      */

225     public String JavaDoc getResume() {
226         return resume;
227     }
228
229     /**
230      * @return
231      */

232     public int getUserRole() {
233         return userRole;
234     }
235
236     public String JavaDoc getRoleDesc(){
237         String JavaDoc desc = null;
238         switch(userRole){
239         case DlogRole.ROLE_MANAGER:
240             desc = "管理员";
241             break;
242         case DlogRole.ROLE_GUEST:
243             desc = "过客";
244             break;
245         case DlogRole.ROLE_COMMON:
246             desc = "普通用户";
247             break;
248         case DlogRole.ROLE_FRIEND:
249             desc = "我的好友";
250             break;
251         case DlogRole.ROLE_BUDDY:
252             desc = "密友";
253             break;
254         default:
255             desc = "未知";
256         }
257         return desc;
258     }
259
260     /**
261      * @param string
262      */

263     public void setDisplayName(String JavaDoc string) {
264         displayName = string;
265     }
266
267     /**
268      * @param string
269      */

270     public void setEmail(String JavaDoc string) {
271         email = string;
272     }
273
274     /**
275      * @param string
276      */

277     public void setHomePage(String JavaDoc string) {
278         homePage = string;
279     }
280
281     /**
282      * @param i
283      */

284     public void setId(int i) {
285         id = i;
286     }
287
288     /**
289      * @param date
290      */

291     public void setLastTime(Date JavaDoc date) {
292         lastTime = date;
293     }
294
295     /**
296      * @param i
297      */

298     public void setLoginCount(int i) {
299         loginCount = i;
300     }
301
302     /**
303      * @param string
304      */

305     public void setPassword(String JavaDoc string) {
306         password = string;
307     }
308
309     /**
310      * @param date
311      */

312     public void setRegTime(Date JavaDoc date) {
313         regTime = date;
314     }
315     
316     public String JavaDoc getRegDate(){
317         if(regTime==null)
318             return null;
319         return DATE_FORMAT.format(regTime);
320     }
321     
322     public String JavaDoc getLastDate(){
323         if(lastTime==null)
324             return null;
325         return DATE_FORMAT.format(lastTime);
326     }
327
328     /**
329      * @param string
330      */

331     public void setResume(String JavaDoc string) {
332         resume = string;
333     }
334
335     /**
336      * @param i
337      */

338     public void setUserRole(int i) {
339         userRole = i;
340     }
341
342     /**
343      * @return
344      */

345     public List JavaDoc getLogs() {
346         return logs;
347     }
348     
349     public int getLogCount(){
350         return logCount;
351     }
352
353     /**
354      * @return
355      */

356     public List JavaDoc getReplies() {
357         return replies;
358     }
359
360     /**
361      * @param list
362      */

363     public void setLogs(List JavaDoc list) {
364         logs = list;
365     }
366
367     /**
368      * @param list
369      */

370     public void setReplies(List JavaDoc list) {
371         replies = list;
372     }
373
374     /**
375      * @return
376      */

377     public String JavaDoc getLoginName() {
378         return loginName;
379     }
380
381     /**
382      * @param string
383      */

384     public void setLoginName(String JavaDoc string) {
385         loginName = string;
386     }
387
388     /**
389      * @return
390      */

391     public int getMode() {
392         return mode;
393     }
394
395     /**
396      * @param i
397      */

398     public void setMode(int i) {
399         mode = i;
400     }
401
402     /**
403      * @return
404      */

405     public int getReplyCount() {
406         return replyCount;
407     }
408
409     /**
410      * @param i
411      */

412     public void setLogCount(int i) {
413         logCount = i;
414     }
415
416     /**
417      * @param i
418      */

419     public void setReplyCount(int i) {
420         replyCount = i;
421     }
422
423     /**
424      * @return
425      */

426     public SiteForm getSite() {
427         return site;
428     }
429
430     /**
431      * @param form
432      */

433     public void setSite(SiteForm form) {
434         site = form;
435     }
436     
437     public String JavaDoc getPortrait() {
438         return portrait;
439     }
440     public void setPortrait(String JavaDoc portrait) {
441         this.portrait = portrait;
442     }
443     public DlogRole getRole() {
444         return role;
445     }
446     public void setRole(DlogRole role) {
447         this.role = role;
448     }
449     public String JavaDoc getCats() {
450         return cats;
451     }
452     public void setCats(String JavaDoc cats) {
453         this.cats = cats;
454     }
455
456     public int getBookMarkCount() {
457         return bookMarkCount;
458     }
459     public void setBookMarkCount(int bookMarkCount) {
460         this.bookMarkCount = bookMarkCount;
461     }
462     /**
463      * 该方法由CategoryManager调用
464      * @return
465      */

466     public int[] getOwnerCatids(){
467         List JavaDoc ids = new ArrayList JavaDoc();
468         if(cats!=null){
469             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cats,",");
470             while(st.hasMoreElements()){
471                 String JavaDoc sId = st.nextToken();
472                 try{
473                     Integer JavaDoc IId = new Integer JavaDoc(Integer.parseInt(sId));
474                     if(!ids.contains(IId))
475                         ids.add(IId);
476                 }catch(Exception JavaDoc e){}
477             }
478         }
479         int[] nIds = new int[ids.size()];
480         for(int i=0;i<ids.size();i++)
481             nIds[i] = ((Integer JavaDoc)ids.get(i)).intValue();
482         return nIds;
483     }
484 }
485
Popular Tags