View Javadoc

1   package net.trajano.twiff.internal.token.dao;
2   
3   import java.io.Serializable;
4   import java.net.URL;
5   import java.util.Iterator;
6   import java.util.List;
7   import net.trajano.twiff.internal.token.entity.SessionToken;
8   import org.hibernate.Criteria;
9   import org.hibernate.Session;
10  import org.hibernate.criterion.Expression;
11  
12  /***
13   * DAO to manipate session token data.
14   * 
15   * @author Archimedes Trajano
16   */
17  public final class SessionTokenDao {
18      /***
19       * Field name for the session ID.
20       */
21      private static final String FIELD_SESSIONID = "sessionId"; //$NON-NLS-1$
22  
23      /***
24       * Field name for the token ID.
25       */
26      private static final String FIELD_TOKENID = "tokenId"; //$NON-NLS-1$
27  
28      /***
29       * Hibernate session object.
30       */
31      private final Session session;
32  
33      /***
34       * Constructs the DAO.
35       * 
36       * @param session
37       *                   the hibernate session.
38       */
39      public SessionTokenDao(final Session session) {
40          this.session = session;
41      }
42  
43      /***
44       * Adds a token into the table.
45       * 
46       * @param sessionId
47       *                   the session ID
48       * @param tokenId
49       *                   the token ID
50       */
51      public void addToken(final String sessionId, final String tokenId) {
52          SessionToken newToken = new SessionToken();
53          newToken.setSessionId(sessionId);
54          newToken.setTokenId(tokenId);
55          session.save(newToken);
56      }
57  
58      /***
59       * Gets the redirect URL based on the session and token ID.
60       * 
61       * @param sessionId
62       *                   the session ID
63       * @param tokenId
64       *                   the token ID
65       * @return the page bean data
66       */
67      public Serializable getPageBean(final String sessionId, final String tokenId) {
68          final SessionToken sessionToken = getSessionToken(sessionId, tokenId);
69          return sessionToken.getPageBean();
70      }
71  
72      /***
73       * Gets the redirect URL based on the session and token ID.
74       * 
75       * @param sessionId
76       *                   the session ID
77       * @param tokenId
78       *                   the token ID
79       * @return the redirect URL
80       */
81      public URL getRedirectUrl(final String sessionId, final String tokenId) {
82          final SessionToken sessionToken = getSessionToken(sessionId, tokenId);
83          return sessionToken.getRedirectUrl();
84      }
85  
86      /***
87       * Gets the session token given the lookup key.
88       * 
89       * @param sessionId
90       *                   session id
91       * @param tokenId
92       *                   token id
93       * @return the session token.
94       */
95      private SessionToken getSessionToken(final String sessionId, final String tokenId) {
96          Criteria criteria = session.createCriteria(SessionToken.class);
97          criteria.add(Expression.eq(FIELD_SESSIONID, sessionId));
98          criteria.add(Expression.eq(FIELD_TOKENID, tokenId));
99          return (SessionToken) criteria.uniqueResult();
100     }
101 
102     /***
103      * This checks if a token is available in the database. It does not matter
104      * if it is valid or not.
105      * 
106      * @param sessionId
107      *                   session Id
108      * @param tokenId
109      *                   token Id
110      * @return true if the token is in the database
111      */
112     public boolean isTokenAvailable(final String sessionId, final String tokenId) {
113         Criteria criteria = session.createCriteria(SessionToken.class);
114         criteria.add(Expression.eq(FIELD_SESSIONID, sessionId));
115         criteria.add(Expression.eq(FIELD_TOKENID, tokenId));
116         return criteria.list().size() > 0;
117     }
118 
119     /***
120      * Checks if the session token is valid.
121      * 
122      * @param sessionId
123      *                   session id
124      * @param tokenId
125      *                   token id
126      * @return true if the session token is valid.
127      */
128     public boolean isTokenValid(final String sessionId, final String tokenId) {
129         return getSessionToken(sessionId, tokenId).isValid();
130     }
131 
132     /***
133      * Removes all the token and session data for the session id.
134      * 
135      * @param sessionId
136      *                   session id
137      */
138     public void removeSession(final String sessionId) {
139         Criteria criteria = session.createCriteria(SessionToken.class);
140         criteria.add(Expression.eq(FIELD_SESSIONID, sessionId));
141         List sessionTokens = criteria.list();
142         for (Iterator i = sessionTokens.iterator(); i.hasNext();) {
143             session.delete(i.next());
144         }
145     }
146 
147     /***
148      * Adds the token info.
149      * 
150      * @param sessionId
151      *                   the session ID
152      * @param tokenId
153      *                   the token ID
154      * @param redirectURL
155      *                   the redirect URL
156      * @param pageBean
157      *                   the page bean data
158      */
159     public void updateTokenInfo(final String sessionId, final String tokenId, final URL redirectURL, final Serializable pageBean) {
160         SessionToken sessionToken = getSessionToken(sessionId, tokenId);
161         sessionToken.setRedirectUrl(redirectURL);
162         sessionToken.setPageBean(pageBean);
163         session.save(sessionToken);
164     }
165 
166     /***
167      * Gets the request count of the object.
168      * 
169      * @param sessionId
170      *                   the session ID
171      * @param tokenId
172      *                   the token ID
173      * @return the request count.
174      */
175     public int getRequestCount(final String sessionId, final String tokenId) {
176         return getSessionToken(sessionId, tokenId).getRequestCount();
177     }
178 }