View Javadoc
1   /******************************************************************************
2    * DatabaseImpl - This object implements the database API for BSAccount
3    * 
4    * BSAccount - BuckoSoft Web Account Manager 
5    * Copyright(c) 2007 - Dick Balaska and BuckoSoft, Corp.
6    * 
7    */
8   
9   package com.buckosoft.BSAccount.db;
10  
11  import java.util.Timer;
12  import java.util.TimerTask;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.hibernate.HibernateException;
17  import org.hibernate.Session;
18  
19  import com.buckosoft.BSAccount.domain.AppJumper;
20  import com.buckosoft.BSAccount.domain.BSAccount;
21  
22  /** Database for the BSAccount library. We have one function, login a user.
23   * @author dick
24   *
25   */
26  public class DatabaseImpl implements Database {
27  	protected final Log log = LogFactory.getLog(getClass());
28  	private Timer		dbTimer = new Timer("BSAccountDBCheck", true);
29  	private TimerTask	dbTimerTask = new DBTimerTask();
30  	private	boolean		refreshNeeded = true;
31  	private	int			timerDelay = 60*5;
32  
33  	private HibernateUtil	hibernateUtil;
34  
35  	public DatabaseImpl() {
36  		hibernateUtil = new HibernateUtil();
37  		hibernateUtil.initialize(null);
38  		log.info("Initialize database timer to " + timerDelay);
39  		dbTimer.schedule(dbTimerTask, timerDelay*1000, timerDelay*1000);
40  	}
41  
42  	class DBTimerTask extends TimerTask {
43  		@Override
44  		public void run() {
45  			if (refreshNeeded) {
46  				log.debug("Refresh database");
47  				getAccountCount();
48  			}
49  			refreshNeeded = true;
50  		}
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see com.buckosoft.BSAccount.db.Database#getAccount(java.lang.String, java.lang.String)
55  	 */
56  	@Override
57  	public BSAccount getAccount(String username, String password) {
58  		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
59  		session.getTransaction().begin();
60  		BSAccount a = null;
61  		try {
62  			a = (BSAccount)session.createQuery("from BSAccount a where a.username = :username and password = PASSWORD(:password)")
63  									.setParameter("username", username)
64  									.setParameter("password", password)
65  									.uniqueResult();
66  			if (a != null) {
67  				a.setLastAccess(null);
68  				session.update(a);
69  			}
70  			refreshNeeded = false;
71  		} catch (HibernateException e) {
72  			e.printStackTrace();
73  		} finally {
74  			session.getTransaction().commit();
75  		}
76  		return(a);
77  	}
78  
79  	/* (non-Javadoc)
80  	 * @see com.buckosoft.BSAccount.db.Database#getAccount(int)
81  	 */
82  	@Override
83  	public BSAccount getAccount(int userId) {
84  		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
85  		session.getTransaction().begin();
86  		BSAccount a = null;
87  		try {
88  			a = (BSAccount)session.createQuery("from BSAccount a where a.userId = :userId")
89  									.setParameter("userId", userId)
90  									.uniqueResult();
91  			if (a != null) {
92  				a.setLastAccess(null);
93  				session.update(a);
94  			}
95  			refreshNeeded = false;
96  		} catch (HibernateException e) {
97  			e.printStackTrace();
98  		} finally {
99  			session.getTransaction().commit();
100 		}
101 		return(a);
102 	}
103 
104 	
105 	/* (non-Javadoc)
106 	 * @see com.buckosoft.BSAccount.db.Database#getAccountCount()
107 	 */
108 	@Override
109 	public int getAccountCount() {
110 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
111 		session.getTransaction().begin();
112 		try {
113 			Long count = (Long)session.createQuery("select count(*) from BSAccount").uniqueResult();
114 			refreshNeeded = false;
115 			int c = count.intValue();
116 			return(c);
117 		} catch (Exception e) {
118 			e.printStackTrace();
119 		} finally {
120 			session.getTransaction().commit();
121 		}
122 		return(0);
123 	}
124 
125 	/* (non-Javadoc)
126 	 * @see com.buckosoft.BSAccount.db.Database#getAppJumperUser(long)
127 	 */
128 	@Override
129 	public int getAppJumperUser(long appJumper) {
130 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
131 		session.getTransaction().begin();
132 		AppJumper a = null;	
133 			try {
134 				a = (AppJumper)session.createQuery("from AppJumper a where a.jumpToken = :toke")
135 						.setParameter("toke", (Long)appJumper)
136 						.uniqueResult();
137 //				a = (AppJumper)session.createQuery("from AppJumper aj where aj.userId = 1")
138 						//.setParameter("toke", (Long)appJumper)
139 //						.uniqueResult();
140 				if (a != null)
141 					return(a.getUserId());
142 			} catch (HibernateException e) {
143 				e.printStackTrace();
144 			} finally {
145 				session.getTransaction().commit();
146 			}
147 		return 0;
148 	}
149 
150 	/* (non-Javadoc)
151 	 * @see com.buckosoft.BSAccount.db.Database#setAppJumper(int, long)
152 	 */
153 	@Override
154 	public void setAppJumper(int userId, long appJumper) {
155 		AppJumper a = new AppJumper(userId, appJumper);
156 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
157 		session.getTransaction().begin();
158 		try {
159 			session.saveOrUpdate(a);
160 			refreshNeeded = false;
161 		} catch (Exception e) {
162 			session.getTransaction().rollback();
163 			e.printStackTrace();
164 			return;
165 		} finally {
166 			session.getTransaction().commit();
167 		}
168 	}
169 	/* (non-Javadoc)
170 	 * @see com.buckosoft.BSAccount.db.Database#deleteAppJumper(int, long)
171 	 */
172 	@Override
173 	public void deleteAppJumper(int userId, long appJumper) {
174 		Session session = hibernateUtil.getSessionFactory().getCurrentSession();
175 		session.getTransaction().begin();
176 		try {
177 			session.createQuery("delete from AppJumper a where a.jumpToken = :token and a.userId = :id")
178 				.setParameter("id", userId)
179 				.setParameter("token", appJumper)
180 				.executeUpdate();
181 
182 		} catch (Exception e) {
183 			session.getTransaction().rollback();
184 			e.printStackTrace();
185 			return;
186 		} finally {
187 			session.getTransaction().commit();
188 		}
189 	}
190 
191 }