01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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.formbean;
17:
18: import java.io.Serializable;
19: import java.sql.SQLException;
20: import java.text.DateFormat;
21: import java.text.SimpleDateFormat;
22: import java.util.Date;
23:
24: import javax.servlet.ServletContext;
25:
26: import web.struts.ActionFormExtend;
27:
28: import net.sf.hibernate.HibernateException;
29: import net.sf.hibernate.Session;
30: import net.sf.hibernate.SessionFactory;
31:
32: /**
33: * Dlog系统的扩展ActioForm
34: * Dlog系统中的所有的formBean都必须扩展该类而不是ActionForm
35: * 2004-2-29 21:23 修正了时间显示12小时制为24小时制
36: * @author Liudong
37: */
38: public abstract class DlogActionForm extends ActionFormExtend implements
39: Serializable {
40:
41: public final static DateFormat df1 = new SimpleDateFormat(
42: "yyyy-M-d H:mm:ss");
43: public final static DateFormat df2 = new SimpleDateFormat(
44: "yyyy-M-d");
45: public final static DateFormat df3 = new SimpleDateFormat("H:mm:ss");
46:
47: protected boolean isToday(Date date) {
48: return df2.format(new Date()).equals(df2.format(date));
49: }
50:
51: /**
52: * 获取Hibernate持久层的操作实例
53: * @return
54: */
55: protected Session getSession() throws SQLException {
56: ServletContext context = servlet.getServletContext();
57: SessionFactory sessions = (SessionFactory) context
58: .getAttribute(dlog4j.Globals.HIBERNATE_SESSIONS_KEY);
59: return sessions.openSession(getConnection());
60: }
61:
62: /**
63: * 提交Hibernate操作
64: * @param session
65: * @throws SQLException
66: * @throws HibernateException
67: */
68: protected void commitSession(Session session, boolean close)
69: throws SQLException, HibernateException {
70: session.flush();
71: session.connection().commit();
72: if (close)
73: session.close();
74: }
75:
76: /**
77: * 关闭session
78: * @param session
79: * @throws SQLException
80: * @throws HibernateException
81: */
82: protected void closeSession(Session session) throws SQLException,
83: HibernateException {
84: session.connection().close();
85: session.close();
86: }
87:
88: public static void main(String[] args) {
89: System.out.println(df1.format(new Date()));
90: System.out.println(df2.format(new Date()));
91: System.out.println(df3.format(new Date()));
92: }
93:
94: }
|