KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > LogsTag


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.LogManager;
31 import dlog4j.ParamManager;
32 import dlog4j.SiteManager;
33 import dlog4j.formbean.SiteForm;
34 import dlog4j.formbean.UserForm;
35
36 /**
37  * @author Liudong
38  * 用于获取待显示的所有日记信息
39  */

40 public class LogsTag 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 count = -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             if(search==null)
85                 search = pageContext.getRequest().getParameter("query");
86             
87             List JavaDoc searchLogs = null;
88             UserForm loginUser = UserForm.getLoginUser((HttpServletRequest JavaDoc)pageContext.getRequest());
89             int logcount = 0;
90             if(StringUtils.isEmpty(search))
91                 logcount = LogManager.getLogCount(ssn,site,loginUser,catid,userid,year,month,date);
92             else {
93                 searchLogs = LogManager.searchAllLogs(ssn,site,loginUser,catid,search,sort);
94                 logcount = searchLogs.size();
95             }
96             pageContext.setAttribute(LogsTei.LOG_COUNT, new Integer JavaDoc(logcount));
97             int perPage = (count==-1)?ParamManager.getLogPerPage(ssn,site):count;
98             pageContext.setAttribute(LogsTei.PER_PAGE, new Integer JavaDoc(perPage));
99             int pageCount = (logcount / perPage) + (((logcount%perPage)>0)?1:0);
100             pageContext.setAttribute(LogsTei.PAGE_COUNT, new Integer JavaDoc(pageCount));
101             int screenCount = (pageCount / PAGE_PER_SCREEN) + (((pageCount%PAGE_PER_SCREEN)>0)?1:0);
102             pageContext.setAttribute(LogsTei.SCREEN_COUNT, new Integer JavaDoc(screenCount));
103             int curPage = 1;
104             try{
105                 curPage = Integer.parseInt(pageContext.getRequest().getParameter(Globals.PARAM_PAGE));
106             }catch(Exception JavaDoc e){}
107             pageContext.setAttribute(LogsTei.CUR_PAGE, new Integer JavaDoc(curPage));
108             if(sort==null)
109                 sort = pageContext.getRequest().getParameter("sort");
110             int from = (curPage-1)*perPage;
111             if(from<0)
112                 from = 0;
113             
114             if(StringUtils.isEmpty(search)) {
115                 List JavaDoc logs = LogManager.listLogs(ssn,site,loginUser,catid,userid,from,perPage,sort,year,month,date);
116                 pageContext.setAttribute(id, logs);
117             
118                 //组合时间条件字符串
119
String JavaDoc timestr = null;
120                 if(year!=-1&&month!=-1&&date!=-1)
121                     timestr = year+"年"+month+"月"+date+"日";
122                 else
123                 if(year!=-1&&month!=-1)
124                     timestr = year+"年"+month+"月";
125                 else
126                 if(year!=-1)
127                     timestr = year+"年";
128                 if(timestr!=null)
129                     pageContext.setAttribute(LogsTei.TIME_STR,timestr);
130             }
131             else {
132                 int to = from+perPage;
133                 if(to>searchLogs.size())
134                     to = searchLogs.size();
135                 if(from>searchLogs.size())
136                     from = searchLogs.size();
137                 List JavaDoc logs = searchLogs.subList(from,to);
138                 pageContext.setAttribute(id, logs);
139             }
140         }catch(Exception JavaDoc e){
141             e.printStackTrace();
142             throw new JspException JavaDoc(e);
143         }finally{
144             try{
145                 closeSession(ssn);
146             }catch(Exception JavaDoc e){}
147         }
148         //读取指定分类指定日期的总日记数
149
//读取日记信息
150
userid = -1;
151         return EVAL_BODY_INCLUDE;
152     }
153
154     /* (non-Javadoc)
155      * @see javax.servlet.jsp.tagext.Tag#release()
156      */

157     public void release() {
158         catid = -1;
159         search = null;
160         date = -1;
161         year = -1;
162         month = -1;
163         sort = null;
164         userid = -1;
165         count = -1;
166     }
167
168     /* (non-Javadoc)
169      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
170      */

171     public int doEndTag() throws JspException JavaDoc {
172         release();
173         return EVAL_PAGE;
174     }
175     /**
176      * @return
177      */

178     public int getCatid() {
179         return catid;
180     }
181
182     /**
183      * @return
184      */

185     public String JavaDoc getSearch() {
186         return search;
187     }
188
189     /**
190      * @param i
191      */

192     public void setCatid(int i) {
193         catid = i;
194     }
195
196     /**
197      * @param i
198      */

199     public void setCatid(String JavaDoc string) {
200         try{
201             catid = Integer.parseInt(string);
202         }catch(Exception JavaDoc e){}
203     }
204
205     /**
206      * @param string
207      */

208     public void setSearch(String JavaDoc string) {
209         search = string;
210     }
211
212     /**
213      * @return
214      */

215     public int getDate() {
216         return date;
217     }
218
219     /**
220      * @param string
221      */

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

229     public String JavaDoc getSort() {
230         return sort;
231     }
232
233     /**
234      * @param string
235      */

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

243     public int getMonth() {
244         return month;
245     }
246
247     /**
248      * @return
249      */

250     public int getYear() {
251         return year;
252     }
253
254     /**
255      * @param i
256      */

257     public void setMonth(int i) {
258         month = i;
259     }
260
261     /**
262      * @param i
263      */

264     public void setYear(int i) {
265         year = i;
266     }
267
268     public int getUserid() {
269         return userid;
270     }
271     public void setUserid(int userid) {
272         this.userid = userid;
273     }
274     public int getCount() {
275         return count;
276     }
277     public void setCount(int count) {
278         this.count = count;
279     }
280 }
281
Popular Tags