KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > RepliesTag


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.text.DateFormat JavaDoc;
19 import java.text.SimpleDateFormat 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.Session;
26
27 import org.apache.commons.lang.StringUtils;
28
29 import dlog4j.Globals;
30 import dlog4j.ParamManager;
31 import dlog4j.ReplyManager;
32 import dlog4j.SiteManager;
33 import dlog4j.formbean.SiteForm;
34 import dlog4j.formbean.UserForm;
35
36 /**用于评论的显示
37  * @author ZT
38  *
39  */

40 public class RepliesTag extends DlogBaseTag {
41     
42     public final static int PAGE_PER_SCREEN = 10;
43     
44     public final static DateFormat JavaDoc FMT_DATE = new SimpleDateFormat JavaDoc("yyyyMMdd");
45
46     int catid = -1; //评论所属日记分类编号
47
String JavaDoc search = null; //待查询的内容
48
int year = -1;
49     int month = -1;
50     int date = -1; //要查看某日的评论
51
String JavaDoc sort = null; //排序方式
52
int userid = -1;
53     int size = -1;
54     
55     /* (non-Javadoc)
56      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
57      */

58     public int doStartTag() throws JspException JavaDoc {
59         //获取当前的SiteForm
60
Session ssn = null;
61         try{
62             ssn = getSession();
63             SiteForm site = SiteManager.getCurrentSite(pageContext.getRequest());
64             if(catid==-1)
65                 try{
66                     catid = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_CATEGORYID));
67                 }catch(Exception JavaDoc e){}
68             if(year==-1)
69                 try{
70                     year = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_YEAR));
71                 }catch(Exception JavaDoc e){}
72             if(month==-1)
73                 try{
74                     month = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_MONTH));
75                 }catch(Exception JavaDoc e){}
76             if(date==-1)
77                 try{
78                     date = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_DATE));
79                 }catch(Exception JavaDoc e){}
80             if(userid==-1)
81                 try{
82                     userid = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_USER));
83                 }catch(Exception JavaDoc e){}
84             String JavaDoc search = pageContext.getRequest().getParameter("query");
85             
86             UserForm loginUser = UserForm.getLoginUser((HttpServletRequest JavaDoc)pageContext.getRequest());
87             List JavaDoc searchReplies = null;
88             int Replycount = 0;
89             if(StringUtils.isEmpty(search))
90                 Replycount = ReplyManager.getRepliesCount(ssn,site,loginUser,catid,userid,year,month,date);
91             else {
92                 searchReplies = ReplyManager.searchAllReplies(ssn,site,loginUser,catid,search,sort);
93                 Replycount = searchReplies.size();
94                 //Replycount = ReplyManager.getSearchReplyCount(ssn,site,loginUser,catid,userid,search);
95
}
96             //int logcount = LogManager.getLogCount(ssn,site,loginUser,catid,userid,year,month,date);
97
pageContext.setAttribute(RepliesTei.REPLY_COUNT,new Integer JavaDoc(Replycount)) ;
98             //pageContext.setAttribute(LogsTei.LOG_COUNT, new Integer(logcount));
99
int perPage = size;
100             if(perPage<1)
101                 perPage = ParamManager.getIntParam(ssn,site,"REPLIES_PER_PAGE",10);
102             pageContext.setAttribute(RepliesTei.PER_PAGE,new Integer JavaDoc(perPage)) ;
103             //pageContext.setAttribute(LogsTei.PER_PAGE, new Integer(perPage));
104
int pageCount = (Replycount / perPage) + (((Replycount%perPage)>0)?1:0);
105             pageContext.setAttribute(RepliesTei.PAGE_COUNT,new Integer JavaDoc(pageCount)) ;
106             //pageContext.setAttribute(LogsTei.PAGE_COUNT, new Integer(pageCount));
107
int screenCount = (pageCount / PAGE_PER_SCREEN) + (((pageCount%PAGE_PER_SCREEN)>0)?1:0);
108             pageContext.setAttribute(RepliesTei.SCREEN_COUNT,new Integer JavaDoc(screenCount));
109             //pageContext.setAttribute(LogsTei.SCREEN_COUNT, new Integer(screenCount));
110
int curPage = 1;
111             try{
112                 curPage = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_PAGE));
113             }catch(Exception JavaDoc e){}
114             pageContext.setAttribute(RepliesTei.CUR_PAGE, new Integer JavaDoc(curPage));
115             //pageContext.setAttribute(LogsTei.CUR_PAGE, new Integer(curPage));
116
if(sort==null)
117                 sort = pageContext.getRequest().getParameter("sort");
118             int from = (curPage-1)*perPage;
119             if(from<0)
120                 from = 0;
121             if(StringUtils.isEmpty(search)) {
122                 List JavaDoc replies = ReplyManager.listReplies(ssn,site,loginUser,catid,userid,from,perPage,sort,year,month,date);
123                 pageContext.setAttribute(id,replies);
124                 //组合时间条件字符串
125
String JavaDoc timestr = null;
126                 if(year!=-1&&month!=-1&&date!=-1)
127                     timestr = year+"年"+month+"月"+date+"日";
128                 else
129                 if(year!=-1&&month!=-1)
130                     timestr = year+"年"+month+"月";
131                 else
132                 if(year!=-1)
133                     timestr = year+"年";
134                 if(timestr!=null)
135                     pageContext.setAttribute(RepliesTei.TIME_STR,timestr);
136             }
137             else {
138                 int to = from+perPage;
139                 if(to>searchReplies.size())
140                     to = searchReplies.size();
141
142                 if(from>searchReplies.size())
143                     from = searchReplies.size();
144                 //List replies = ReplyManager.searchReplies(ssn,site,loginUser,catid,userid,search,sort,from,perPage);
145
pageContext.setAttribute(id, searchReplies.subList(from,to));
146             }
147         }catch(Exception JavaDoc e){
148             throw new JspException JavaDoc(e);
149         }finally{
150             try{
151                 closeSession(ssn);
152             }catch(Exception JavaDoc e){}
153         }
154         //读取指定分类指定日期的总日记数
155
//读取日记信息
156
userid = -1;
157         size = -1;
158         return EVAL_BODY_INCLUDE;
159     }
160
161     /* (non-Javadoc)
162      * @see javax.servlet.jsp.tagext.Tag#release()
163      */

164     public void release() {
165         catid = -1;
166         search = null;
167         date = -1;
168         year = -1;
169         month = -1;
170         sort = null;
171         userid = -1;
172         size = -1;
173     }
174
175     /* (non-Javadoc)
176      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
177      */

178     public int doEndTag() throws JspException JavaDoc {
179         release();
180         return EVAL_PAGE;
181     }
182     /**
183      * @return
184      */

185     public int getCatid() {
186         return catid;
187     }
188
189     /**
190      * @return
191      */

192     public String JavaDoc getSearch() {
193         return search;
194     }
195
196     /**
197      * @param i
198      */

199     public void setCatid(int i) {
200         catid = i;
201     }
202
203     /**
204      * @param i
205      */

206     public void setCatid(String JavaDoc string) {
207         try{
208             catid = Integer.parseInt(string);
209         }catch(Exception JavaDoc e){}
210     }
211
212     /**
213      * @param string
214      */

215     public void setSearch(String JavaDoc string) {
216         search = string;
217     }
218
219     /**
220      * @return
221      */

222     public int getDate() {
223         return date;
224     }
225
226     /**
227      * @param string
228      */

229     public void setDate(int string) {
230         date = string;
231     }
232
233     /**
234      * @return
235      */

236     public String JavaDoc getSort() {
237         return sort;
238     }
239
240     /**
241      * @param string
242      */

243     public void setSort(String JavaDoc string) {
244         sort = string;
245     }
246
247     /**
248      * @return
249      */

250     public int getMonth() {
251         return month;
252     }
253
254     /**
255      * @return
256      */

257     public int getYear() {
258         return year;
259     }
260
261     /**
262      * @param i
263      */

264     public void setMonth(int i) {
265         month = i;
266     }
267
268     /**
269      * @param i
270      */

271     public void setYear(int i) {
272         year = i;
273     }
274
275     public int getUserid() {
276         return userid;
277     }
278     public void setUserid(int userid) {
279         this.userid = userid;
280     }
281     public int getSize() {
282         return size;
283     }
284     public void setSize(int size) {
285         this.size = size;
286     }
287 }
288
Popular Tags