KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > CategoryTag


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.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import net.sf.hibernate.HibernateException;
26 import net.sf.hibernate.Session;
27 import dlog4j.CategoryManager;
28 import dlog4j.Globals;
29 import dlog4j.LogManager;
30 import dlog4j.SiteManager;
31 import dlog4j.formbean.CategoryForm;
32 import dlog4j.formbean.SiteForm;
33 import dlog4j.formbean.UserForm;
34
35 /**
36  * @author Liudong
37  * 读取所有日志分类的标签库
38  * 如果请求中包含cat_id的参数则读取其对应的日记分类对象并输出
39  */

40 public class CategoryTag extends DlogBaseTag {
41
42     String JavaDoc catid = null;
43     /**
44      * 如果指定了security值则需要对用户的权限进行控制,判断那些目录对用户是可见的
45      * 否则只对那些类型为TYPE_OWNER进行控制
46      */

47     boolean security = false;
48     
49     boolean sortByNewest = false;
50     
51     boolean withLogCount = false;
52     
53     boolean addLog = false;
54     
55     /* (non-Javadoc)
56      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
57      */

58     public int doStartTag() throws JspException JavaDoc {
59         if(pageContext.getAttribute(id)==null){
60             Session session = null;
61             List JavaDoc categories = null;
62             try{
63                 session = getSession();
64                 SiteForm site = SiteManager.getCurrentSite(pageContext.getRequest());
65                 UserForm user = UserForm.getLoginUser((HttpServletRequest JavaDoc)pageContext.getRequest());
66                 if(addLog)
67                     categories = CategoryManager.listCategoriesForModify(session,site,user);
68                 else
69                     categories = CategoryManager.listCategories(session,site,user);
70                 Iterator JavaDoc cs = categories.iterator();
71                 while(cs.hasNext()){
72                     CategoryForm cat = (CategoryForm)cs.next();
73                     if(withLogCount)
74                         cat.setLogCount(LogManager.getLogCount(session,site,user,cat.getId(),-1,-1,-1,-1));
75                 }
76                 int categoryId = -1;
77                 try{
78                     categoryId = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_CATEGORYID));
79                 }catch(Exception JavaDoc e){}
80                 for(int i=0;catid!=null&&i<categories.size();i++){
81                     CategoryForm cat = (CategoryForm)categories.get(i);
82                     if(cat.getId()==categoryId){
83                         pageContext.setAttribute(catid,cat);
84                         break;
85                     }
86                 }
87             }catch(SQLException JavaDoc e){
88                 throw new JspException JavaDoc(e);
89             }catch(HibernateException e){
90                 throw new JspException JavaDoc(e);
91             }finally{
92                 try{
93                     closeSession(session);
94                 }catch(Exception JavaDoc e){}
95             }
96             pageContext.setAttribute(id,categories);
97         }
98         return SKIP_BODY;
99     }
100
101     public int doEndTag() throws JspException JavaDoc {
102         release();
103         return EVAL_PAGE;
104     }
105     public void release() {
106         catid = null;
107         security = false;
108         
109         withLogCount = false;
110     }
111     /**
112      * @return
113      */

114     public String JavaDoc getCatid() {
115         return catid;
116     }
117
118     /**
119      * @param string
120      */

121     public void setCatid(String JavaDoc string) {
122         catid = string;
123     }
124
125     /**
126      * @return
127      */

128     public boolean isSecurity() {
129         return security;
130     }
131
132     /**
133      * @param b
134      */

135     public void setSecurity(boolean b) {
136         security = b;
137     }
138
139     public boolean isWithLogCount() {
140         return withLogCount;
141     }
142     public void setWithLogCount(boolean withLogCount) {
143         this.withLogCount = withLogCount;
144     }
145     public boolean isSortByNewest() {
146         return sortByNewest;
147     }
148     public void setSortByNewest(boolean sortByNewest) {
149         this.sortByNewest = sortByNewest;
150     }
151     public boolean isAddLog() {
152         return addLog;
153     }
154     public void setAddLog(boolean addLog) {
155         this.addLog = addLog;
156     }
157 }
158
Popular Tags