1
2
3
4
5
6 package com.hack23.cia.service.impl.common;
7
8 import java.util.Date;
9
10 import net.sf.ehcache.Cache;
11 import net.sf.ehcache.CacheManager;
12 import net.sf.ehcache.Element;
13
14 import org.hibernate.Hibernate;
15
16 import com.hack23.cia.model.application.impl.common.AbstractActionEvent;
17 import com.hack23.cia.model.application.impl.common.ActionEvent;
18 import com.hack23.cia.model.application.impl.common.Agency;
19 import com.hack23.cia.model.application.impl.common.Language;
20 import com.hack23.cia.model.application.impl.common.Portal;
21 import com.hack23.cia.model.application.impl.common.User;
22 import com.hack23.cia.model.application.impl.common.UserSession;
23 import com.hack23.cia.model.application.impl.common.User.UserRole;
24 import com.hack23.cia.service.dao.AgencyDAO;
25 import com.hack23.cia.service.dao.UserDAO;
26 import com.hack23.cia.service.dao.UserSessionDAO;
27
28 /***
29 * The Class UserSessionServiceImpl.
30 */
31 public class UserSessionServiceImpl implements UserSessionService {
32
33 /*** The agency dao. */
34 private final AgencyDAO agencyDAO;
35
36 /*** The agency id. */
37 private final long agencyId = 1;
38
39 /*** The user dao. */
40 private final UserDAO userDAO;
41
42 /*** The user session dao. */
43 private final UserSessionDAO userSessionDAO;
44
45 /*** The cache manager. */
46 private final CacheManager cacheManager;
47
48 /*** The cache. */
49 private Cache cache;
50
51 /***
52 * Instantiates a new user session service impl.
53 *
54 * @param agencyDAO the agency dao
55 * @param userDAO the user dao
56 * @param userSessionDAO the user session dao
57 * @param cacheManager the cache manager
58 */
59 public UserSessionServiceImpl(final AgencyDAO agencyDAO,
60 final UserDAO userDAO, final UserSessionDAO userSessionDAO,final CacheManager cacheManager) {
61 super();
62 this.agencyDAO = agencyDAO;
63 this.userDAO = userDAO;
64 this.userSessionDAO = userSessionDAO;
65 this.cacheManager = cacheManager;
66 }
67
68
69
70
71
72
73
74
75
76 @Override
77 public final UserSession addActionEvent(final ActionEvent actionEvent,
78 final UserSession userSession) {
79 userSession.getEvents().add((AbstractActionEvent) actionEvent);
80 return this.userSessionDAO.save(userSession);
81 }
82
83
84
85
86
87
88
89
90 @Override
91 public final UserSession createUserSession(final String sessionId,
92 final String clientHost, final String serverHost,
93 final String acceptLanguageKeys,final String userAgent) {
94 final Agency agency = loadAgency();
95 final Portal portal = agency.findMatchingPortal(serverHost);
96 final Language language = agency.findMatchingLanguage(acceptLanguageKeys);
97
98 final User user = new User();
99 user.setCreatedDate(new Date());
100 user.setUserRole(UserRole.ANYNOMOUS);
101 final UserSession userSession = new UserSession(user,language,portal,sessionId,clientHost,serverHost,new Date(),userAgent);
102 return userSessionDAO.save(userSession);
103 }
104
105 /***
106 * Load agency.
107 *
108 * @return the agency
109 */
110 private Agency loadAgency() {
111
112 if (cache == null) {
113 cache = cacheManager.getCache("agencyCache");
114 }
115
116 final Element element = cache.get(agencyId);
117
118 Agency agency;
119 if (element == null ) {
120 agency = this.agencyDAO.load(agencyId);
121 Hibernate.initialize(agency.getLanguageContent());
122 Hibernate.initialize(agency.getLanguages());
123 Hibernate.initialize(agency.getPortals());
124
125 final Element agencyElement = new Element(agencyId, agency);
126 cache.put(agencyElement);
127 } else {
128 agency = (Agency) element.getValue();
129 }
130
131 return agency;
132 }
133
134
135
136
137
138
139
140
141 @Override
142 public final UserSession loadUserSessionById(final Long userSessionId) {
143 return this.userSessionDAO.load(userSessionId);
144 }
145
146
147
148
149
150
151
152 @Override
153 public final UserSession login(final String name,
154 final String encodedPassword, final UserSession userSession) {
155 final User ifValidUser = userDAO.getIfValidUser(name, encodedPassword);
156 if (ifValidUser != null) {
157 userSession.setUser(ifValidUser);
158 this.userSessionDAO.save(userSession);
159 return userSession;
160 }
161 return null;
162 }
163
164
165
166
167
168
169
170 @Override
171 public final UserSession logout(final UserSession userSession) {
172 return userSession;
173 }
174
175 }