KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > BookmarkTag


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.tags;
17
18 import java.sql.SQLException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import dlog4j.formbean.BookMarkBean;
25 import dlog4j.formbean.SiteForm;
26 import dlog4j.formbean.UserForm;
27
28
29 import net.sf.hibernate.HibernateException;
30 import net.sf.hibernate.Query;
31 import net.sf.hibernate.Session;
32
33 /**
34  * 用于读取书签信息的标签库
35  * @author Liudong
36  */

37 public class BookmarkTag extends DlogBaseTag {
38     
39     String JavaDoc countId = null;
40     boolean list = true;
41
42     public int doStartTag() throws JspException JavaDoc {
43         
44         Session ssn = null;
45         UserForm loginUser = getLoginUser();
46         SiteForm site = getCurrentSite();
47         List JavaDoc books = null;
48         int bcount = 0;
49         if(loginUser!=null&&loginUser.isLogin())
50         try {
51             ssn = getSession();
52             if(list) {
53                 String JavaDoc hql = "FROM "+BookMarkBean.class.getName()+" AS b WHERE b.site.id=? AND b.user.id=?";
54                 Query q = ssn.createQuery(hql);
55                 q.setInteger(0,site.getId());
56                 q.setInteger(1,loginUser.getId());
57                 books = q.list();
58                 bcount = books.size();
59             }
60             else {
61                 String JavaDoc hql = "SELECT COUNT(b.id) FROM "+BookMarkBean.class.getName()+" AS b WHERE b.site.id=? AND b.user.id=?";
62                 Query q = ssn.createQuery(hql);
63                 q.setInteger(0,site.getId());
64                 q.setInteger(1,loginUser.getId());
65                 bcount = ((Integer JavaDoc)q.list().get(0)).intValue();
66             }
67         }catch(HibernateException e) {
68             e.printStackTrace();
69         }catch(SQLException JavaDoc e) {
70             e.printStackTrace();
71         }finally {
72             close(ssn);
73         }
74         if(list)
75             pageContext.setAttribute(id,(books==null)?new ArrayList JavaDoc():books);
76         pageContext.setAttribute(countId,new Integer JavaDoc(bcount));
77         
78         return SKIP_BODY;
79     }
80     public String JavaDoc getCountId() {
81         return countId;
82     }
83     public void setCountId(String JavaDoc countId) {
84         this.countId = countId;
85     }
86     public boolean isList() {
87         return list;
88     }
89     public void setList(boolean list) {
90         this.list = list;
91     }
92 }
93
Popular Tags