001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j;
017:
018: import java.sql.SQLException;
019: import java.text.DateFormat;
020: import java.text.ParseException;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023: import java.util.List;
024:
025: import dlog4j.formbean.ParamForm;
026: import dlog4j.formbean.SiteForm;
027:
028: import net.sf.hibernate.HibernateException;
029: import net.sf.hibernate.Session;
030: import net.sf.hibernate.expression.Expression;
031:
032: /**
033: * @author Liudong
034: * 系统参数管理接口
035: */
036: public class ParamManager {
037:
038: public final static String LOGS_PER_PAGE = "LOGS_PER_PAGE";
039:
040: /**
041: * 获取指定站点设定的每页显示的日记数
042: * @param ssn
043: * @param site
044: * @return
045: * @throws Exception
046: */
047: public static int getLogPerPage(Session ssn, SiteForm site)
048: throws Exception {
049: return getIntParam(ssn, site, LOGS_PER_PAGE, 5);
050: }
051:
052: /**
053: * 列出某个站点的所有参数信息
054: * @param ssn
055: * @param site
056: * @return
057: * @throws HibernateException
058: */
059: public static List listParams(Session ssn, SiteForm site)
060: throws HibernateException {
061: int siteid = (site != null) ? site.getId() : -1;
062: return ssn.createCriteria(ParamForm.class).add(
063: Expression.eq("site.id", new Integer(siteid))).list();
064: }
065:
066: /**
067: * 获取整形参数
068: * @param ssn
069: * @param site
070: * @param param
071: * @param defValue
072: * @return
073: * @throws Exception
074: */
075: public static ParamForm getParam(Session ssn, SiteForm site,
076: String param) throws SQLException, HibernateException {
077: int siteid = (site == null) ? 0 : site.getId();
078: ParamForm p = null;
079: List params = ssn.createCriteria(ParamForm.class).add(
080: Expression.eq("name", param)).add(
081: Expression.eq("site.id", new Integer(siteid))).list();
082: if (params.size() > 0)
083: p = (ParamForm) params.get(0);
084: return p;
085: }
086:
087: /**
088: * 获取整形参数值
089: * @param ssn
090: * @param site
091: * @param param
092: * @param defValue
093: * @return
094: * @throws Exception
095: */
096: public static int getIntParam(Session ssn, SiteForm site,
097: String param, int defValue) throws SQLException,
098: HibernateException {
099: ParamForm p = getParam(ssn, site, param);
100: if (p == null || p.getValue() == null
101: || p.getValue().trim().length() == 0)
102: return defValue;
103: return Integer.parseInt(p.getValue());
104: }
105:
106: /**
107: * 获取字符串参数值
108: * @param ssn
109: * @param site
110: * @param param
111: * @param defValue
112: * @return
113: * @throws Exception
114: */
115: public static String getStringParam(Session ssn, SiteForm site,
116: String param, String defValue) throws SQLException,
117: HibernateException {
118: ParamForm p = getParam(ssn, site, param);
119: if (p == null || p.getValue() == null)
120: return defValue;
121: return p.getValue();
122: }
123:
124: /**
125: * 获取日期型参数值
126: * @param ssn
127: * @param site
128: * @param param
129: * @param defValue
130: * @return
131: * @throws Exception
132: */
133: public static Date getDateParam(Session ssn, SiteForm site,
134: String param, Date defValue) throws SQLException,
135: HibernateException, ParseException {
136: ParamForm p = getParam(ssn, site, param);
137: if (p == null || p.getValue() == null)
138: return defValue;
139: return FMT_DATE.parse(p.getValue());
140: }
141:
142: /**
143: * 获取时间型参数值
144: * @param ssn
145: * @param site
146: * @param param
147: * @param defValue
148: * @return
149: * @throws Exception
150: */
151: public static Date getTimeParam(Session ssn, SiteForm site,
152: String param, Date defValue) throws SQLException,
153: HibernateException, ParseException {
154: ParamForm p = getParam(ssn, site, param);
155: if (p == null || p.getValue() == null)
156: return defValue;
157: return FMT_TIME.parse(p.getValue());
158: }
159:
160: /**
161: * 获取日期时间类型参数值
162: * @param ssn
163: * @param site
164: * @param param
165: * @param defValue
166: * @return
167: * @throws Exception
168: */
169: public static Date getDateTimeParam(Session ssn, SiteForm site,
170: String param, Date defValue) throws SQLException,
171: HibernateException, ParseException {
172: ParamForm p = getParam(ssn, site, param);
173: if (p == null || p.getValue() == null)
174: return defValue;
175: return FMT_DATETIME.parse(p.getValue());
176: }
177:
178: public final static DateFormat FMT_TIME = new SimpleDateFormat(
179: "hh:mm:ss");
180: public final static DateFormat FMT_DATE = new SimpleDateFormat(
181: "yyyy-MM-dd");
182: public final static DateFormat FMT_DATETIME = new SimpleDateFormat(
183: "yyyy-MM-dd hh:mm:ss");
184:
185: }
|