KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > ParamManager


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;
17
18 import java.sql.SQLException JavaDoc;
19 import java.text.DateFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.List JavaDoc;
24
25 import dlog4j.formbean.ParamForm;
26 import dlog4j.formbean.SiteForm;
27
28 import net.sf.hibernate.HibernateException;
29 import net.sf.hibernate.Session;
30 import net.sf.hibernate.expression.Expression;
31
32 /**
33  * @author Liudong
34  * 系统参数管理接口
35  */

36 public class ParamManager {
37
38     public final static String JavaDoc LOGS_PER_PAGE = "LOGS_PER_PAGE";
39     
40     /**
41      * 获取指定站点设定的每页显示的日记数
42      * @param ssn
43      * @param site
44      * @return
45      * @throws Exception
46      */

47     public static int getLogPerPage(Session ssn, SiteForm site) throws Exception JavaDoc{
48         return getIntParam(ssn,site,LOGS_PER_PAGE,5);
49     }
50     /**
51      * 列出某个站点的所有参数信息
52      * @param ssn
53      * @param site
54      * @return
55      * @throws HibernateException
56      */

57     public static List JavaDoc listParams(Session ssn, SiteForm site)
58         throws HibernateException {
59         int siteid = (site != null) ? site.getId() : -1;
60         return ssn
61             .createCriteria(ParamForm.class)
62             .add(Expression.eq("site.id", new Integer JavaDoc(siteid)))
63             .list();
64     }
65     /**
66      * 获取整形参数
67      * @param ssn
68      * @param site
69      * @param param
70      * @param defValue
71      * @return
72      * @throws Exception
73      */

74     public static ParamForm getParam(Session ssn, SiteForm site, String JavaDoc param)
75         throws SQLException JavaDoc, HibernateException {
76         int siteid = (site == null) ? 0 : site.getId();
77         ParamForm p = null;
78         List JavaDoc params =
79             ssn
80                 .createCriteria(ParamForm.class)
81                 .add(Expression.eq("name", param))
82                 .add(Expression.eq("site.id", new Integer JavaDoc(siteid)))
83                 .list();
84         if (params.size() > 0)
85             p = (ParamForm) params.get(0);
86         return p;
87     }
88     /**
89      * 获取整形参数值
90      * @param ssn
91      * @param site
92      * @param param
93      * @param defValue
94      * @return
95      * @throws Exception
96      */

97     public static int getIntParam(
98         Session ssn,
99         SiteForm site,
100         String JavaDoc param,
101         int defValue)
102         throws SQLException JavaDoc, HibernateException {
103         ParamForm p = getParam(ssn, site, param);
104         if (p == null
105             || p.getValue() == null
106             || p.getValue().trim().length() == 0)
107             return defValue;
108         return Integer.parseInt(p.getValue());
109     }
110     /**
111      * 获取字符串参数值
112      * @param ssn
113      * @param site
114      * @param param
115      * @param defValue
116      * @return
117      * @throws Exception
118      */

119     public static String JavaDoc getStringParam(
120         Session ssn,
121         SiteForm site,
122         String JavaDoc param,
123         String JavaDoc defValue)
124         throws SQLException JavaDoc, HibernateException {
125         ParamForm p = getParam(ssn, site, param);
126         if (p == null || p.getValue() == null)
127             return defValue;
128         return p.getValue();
129     }
130     /**
131      * 获取日期型参数值
132      * @param ssn
133      * @param site
134      * @param param
135      * @param defValue
136      * @return
137      * @throws Exception
138      */

139     public static Date JavaDoc getDateParam(
140         Session ssn,
141         SiteForm site,
142         String JavaDoc param,
143         Date JavaDoc defValue)
144         throws SQLException JavaDoc, HibernateException, ParseException JavaDoc {
145         ParamForm p = getParam(ssn, site, param);
146         if (p == null || p.getValue() == null)
147             return defValue;
148         return FMT_DATE.parse(p.getValue());
149     }
150     /**
151      * 获取时间型参数值
152      * @param ssn
153      * @param site
154      * @param param
155      * @param defValue
156      * @return
157      * @throws Exception
158      */

159     public static Date JavaDoc getTimeParam(
160         Session ssn,
161         SiteForm site,
162         String JavaDoc param,
163         Date JavaDoc defValue)
164         throws SQLException JavaDoc, HibernateException, ParseException JavaDoc {
165         ParamForm p = getParam(ssn, site, param);
166         if (p == null || p.getValue() == null)
167             return defValue;
168         return FMT_TIME.parse(p.getValue());
169     }
170     /**
171      * 获取日期时间类型参数值
172      * @param ssn
173      * @param site
174      * @param param
175      * @param defValue
176      * @return
177      * @throws Exception
178      */

179     public static Date JavaDoc getDateTimeParam(
180         Session ssn,
181         SiteForm site,
182         String JavaDoc param,
183         Date JavaDoc defValue)
184         throws SQLException JavaDoc, HibernateException, ParseException JavaDoc {
185         ParamForm p = getParam(ssn, site, param);
186         if (p == null || p.getValue() == null)
187             return defValue;
188         return FMT_DATETIME.parse(p.getValue());
189     }
190
191     public final static DateFormat JavaDoc FMT_TIME = new SimpleDateFormat JavaDoc("hh:mm:ss");
192     public final static DateFormat JavaDoc FMT_DATE =
193         new SimpleDateFormat JavaDoc("yyyy-MM-dd");
194     public final static DateFormat JavaDoc FMT_DATETIME =
195         new SimpleDateFormat JavaDoc("yyyy-MM-dd hh:mm:ss");
196
197 }
198
Popular Tags