1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.service.impl;
17
18 import org.apache.commons.collections.map.LRUMap;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.core.api.CoreApiServiceLocator;
22 import org.kuali.rice.core.api.encryption.EncryptionService;
23 import org.kuali.rice.kew.api.WorkflowDocument;
24 import org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase;
25 import org.kuali.rice.kns.service.SessionDocumentService;
26 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
27 import org.kuali.rice.krad.UserSession;
28 import org.kuali.rice.krad.UserSessionUtils;
29 import org.kuali.rice.krad.bo.SessionDocument;
30 import org.kuali.rice.krad.dao.SessionDocumentDao;
31 import org.kuali.rice.krad.datadictionary.DocumentEntry;
32 import org.kuali.rice.krad.service.BusinessObjectService;
33 import org.kuali.rice.krad.service.DataDictionaryService;
34 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
35 import org.kuali.rice.krad.util.KRADConstants;
36 import org.springframework.beans.factory.InitializingBean;
37 import org.springframework.transaction.annotation.Transactional;
38
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
43 import java.sql.Timestamp;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.Map;
47 import java.util.concurrent.ConcurrentHashMap;
48
49
50
51
52
53
54
55 @Deprecated
56 @Transactional
57 public class SessionDocumentServiceImpl implements SessionDocumentService, InitializingBean {
58 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class);
59
60 protected static final String IP_ADDRESS = "ipAddress";
61 protected static final String PRINCIPAL_ID = "principalId";
62 protected static final String DOCUMENT_NUMBER = "documentNumber";
63 protected static final String SESSION_ID = "sessionId";
64
65 private Map<String, CachedObject> cachedObjects;
66 private EncryptionService encryptionService;
67 private int maxCacheSize;
68
69 private BusinessObjectService businessObjectService;
70 private DataDictionaryService dataDictionaryService;
71 private SessionDocumentDao sessionDocumentDao;
72
73 private static class CachedObject {
74 private UserSession userSession;
75 private String formKey;
76
77 CachedObject(UserSession userSession, String formKey) {
78 this.userSession = userSession;
79 this.formKey = formKey;
80 }
81
82 @Override
83 public String toString() {
84 return "CachedObject: principalId=" + userSession.getPrincipalId() + " / objectWithFormKey=" +
85 userSession.retrieveObject(formKey);
86 }
87
88 public UserSession getUserSession() {
89 return this.userSession;
90 }
91
92 public String getFormKey() {
93 return this.formKey;
94 }
95 }
96
97
98
99
100
101
102 private static class KualiLRUMap extends LRUMap {
103
104
105 private static final long serialVersionUID = 1L;
106
107 private KualiLRUMap() {
108 super();
109 }
110
111 private KualiLRUMap(int maxSize) {
112 super(maxSize);
113 }
114
115 @Override
116 protected void removeEntry(HashEntry entry, int hashIndex, HashEntry previous) {
117
118
119
120
121
122 try {
123 CachedObject cachedObject
124 = (CachedObject)this.entryValue(entry);
125 cachedObject.getUserSession().removeObject(cachedObject.getFormKey());
126 } catch (Exception ex) {
127 Logger.getLogger(getClass()).warn( "Problem purging old entry from the user session when removing from the map: ", ex);
128 }
129
130 super.removeEntry(entry, hashIndex, previous);
131 }
132
133 }
134
135 @Override
136 @SuppressWarnings("unchecked")
137 public void afterPropertiesSet() throws Exception {
138 cachedObjects = Collections.synchronizedMap(new KualiLRUMap(maxCacheSize));
139 }
140
141
142 @Override
143 public KualiDocumentFormBase getDocumentForm(String documentNumber, String docFormKey, UserSession userSession,
144 String ipAddress) {
145 KualiDocumentFormBase documentForm = null;
146
147 LOG.debug("getDocumentForm KualiDocumentFormBase from db");
148 try {
149
150 documentForm = (KualiDocumentFormBase) retrieveDocumentForm(userSession, userSession.getKualiSessionId(),
151 documentNumber, ipAddress);
152
153
154 if (!(StringUtils.equals((String)userSession.retrieveObject(DocumentAuthorizerBase.USER_SESSION_METHOD_TO_CALL_OBJECT_KEY),
155 KRADConstants.TableRenderConstants.SORT_METHOD) ||
156 StringUtils.equals((String)userSession.retrieveObject(DocumentAuthorizerBase.USER_SESSION_METHOD_TO_CALL_OBJECT_KEY),
157 KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY))) {
158 WorkflowDocument workflowDocument =
159 documentForm.getDocument().getDocumentHeader().getWorkflowDocument();
160 UserSessionUtils.addWorkflowDocument(userSession, workflowDocument);
161 }
162 } catch (Exception e) {
163 LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() + "/" +
164 documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
165 }
166
167 return documentForm;
168 }
169
170 protected Object retrieveDocumentForm(UserSession userSession, String sessionId, String documentNumber,
171 String ipAddress) throws Exception {
172 HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
173 primaryKeys.put(SESSION_ID, sessionId);
174 if (documentNumber != null) {
175 primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
176 }
177 primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
178 primaryKeys.put(IP_ADDRESS, ipAddress);
179
180 SessionDocument sessionDoc = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys);
181 if (sessionDoc != null) {
182 byte[] formAsBytes = sessionDoc.getSerializedDocumentForm();
183 if (sessionDoc.isEncrypted()) {
184 formAsBytes = getEncryptionService().decryptBytes(formAsBytes);
185 }
186 ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes);
187 ObjectInputStream ois = new ObjectInputStream(baip);
188
189 return ois.readObject();
190 }
191
192 return null;
193 }
194
195 @Override
196 public WorkflowDocument getDocumentFromSession(UserSession userSession, String docId) {
197 return UserSessionUtils.getWorkflowDocument(userSession, docId);
198 }
199
200
201
202
203
204 @Override
205 public void addDocumentToUserSession(UserSession userSession, WorkflowDocument document) {
206 UserSessionUtils.addWorkflowDocument(userSession, document);
207 }
208
209
210
211
212
213 @Override
214 public void purgeDocumentForm(String documentNumber, String docFormKey, UserSession userSession, String ipAddress) {
215 synchronized (userSession) {
216
217 LOG.debug("purge document form from session");
218 userSession.removeObject(docFormKey);
219 try {
220 LOG.debug("purge document form from database");
221 HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
222 primaryKeys.put(SESSION_ID, userSession.getKualiSessionId());
223 primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
224 primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
225 primaryKeys.put(IP_ADDRESS, ipAddress);
226 getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys);
227 } catch (Exception e) {
228 LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() +
229 "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
230 }
231 }
232 }
233
234 @Override
235 public void setDocumentForm(KualiDocumentFormBase form, UserSession userSession, String ipAddress) {
236 synchronized (userSession) {
237
238 String formKey = form.getFormKey();
239 String key = userSession.getKualiSessionId() + "-" + formKey;
240 cachedObjects.put(key, new CachedObject(userSession, formKey));
241
242 String documentNumber = form.getDocument().getDocumentNumber();
243
244 if (StringUtils.isNotBlank(documentNumber)) {
245 persistDocumentForm(form, userSession, ipAddress, userSession.getKualiSessionId(), documentNumber);
246 } else {
247 LOG.warn("documentNumber is null on form's document: " + form);
248 }
249 }
250 }
251
252 protected void persistDocumentForm(Object form, UserSession userSession, String ipAddress, String sessionId,
253 String documentNumber) {
254 try {
255 LOG.debug("set Document Form into database");
256 Timestamp currentTime = new Timestamp(System.currentTimeMillis());
257 ByteArrayOutputStream baos = new ByteArrayOutputStream();
258 ObjectOutputStream oos = new ObjectOutputStream(baos);
259 oos.writeObject(form);
260
261 byte[] formAsBytes = baos.toByteArray();
262 boolean encryptContent = false;
263
264 if ((form instanceof KualiDocumentFormBase) && ((KualiDocumentFormBase) form).getDocTypeName() != null) {
265 DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary()
266 .getDocumentEntry(((KualiDocumentFormBase) form).getDocTypeName());
267 if (documentEntry != null) {
268 encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage();
269 }
270 }
271 if (encryptContent) {
272 formAsBytes = getEncryptionService().encryptBytes(formAsBytes);
273 }
274
275
276
277 HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
278 primaryKeys.put(SESSION_ID, sessionId);
279 primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
280 primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
281 primaryKeys.put(IP_ADDRESS, ipAddress);
282
283 SessionDocument sessionDocument =
284 getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys);
285 if (sessionDocument == null) {
286 sessionDocument = new SessionDocument();
287 sessionDocument.setSessionId(sessionId);
288 sessionDocument.setDocumentNumber(documentNumber);
289 sessionDocument.setPrincipalId(userSession.getPrincipalId());
290 sessionDocument.setIpAddress(ipAddress);
291 }
292 sessionDocument.setSerializedDocumentForm(formAsBytes);
293 sessionDocument.setEncrypted(encryptContent);
294 sessionDocument.setLastUpdatedDate(currentTime);
295
296 businessObjectService.save(sessionDocument);
297 } catch (Exception e) {
298 final String className = form != null ? form.getClass().getName() : "null";
299 LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" + userSession.getKualiSessionId() +
300 "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress + "/" + className, e);
301 }
302 }
303
304
305
306
307 @Override
308 public void purgeAllSessionDocuments(Timestamp expirationDate) {
309 sessionDocumentDao.purgeAllSessionDocuments(expirationDate);
310 }
311
312 protected SessionDocumentDao getSessionDocumentDao() {
313 return this.sessionDocumentDao;
314 }
315
316 public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) {
317 this.sessionDocumentDao = sessionDocumentDao;
318 }
319
320 protected BusinessObjectService getBusinessObjectService() {
321 return this.businessObjectService;
322 }
323
324 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
325 this.businessObjectService = businessObjectService;
326 }
327
328 public int getMaxCacheSize() {
329 return maxCacheSize;
330 }
331
332 public void setMaxCacheSize(int maxCacheSize) {
333 this.maxCacheSize = maxCacheSize;
334 }
335
336 protected EncryptionService getEncryptionService() {
337 if (encryptionService == null) {
338 encryptionService = CoreApiServiceLocator.getEncryptionService();
339 }
340 return encryptionService;
341 }
342
343 protected DataDictionaryService getDataDictionaryService() {
344 if (dataDictionaryService == null) {
345 dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
346 }
347 return dataDictionaryService;
348 }
349 }