| 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 java.io.ByteArrayInputStream; |
| 19 | |
import java.io.ByteArrayOutputStream; |
| 20 | |
import java.io.ObjectInputStream; |
| 21 | |
import java.io.ObjectOutputStream; |
| 22 | |
import java.sql.Timestamp; |
| 23 | |
import java.util.Collections; |
| 24 | |
import java.util.HashMap; |
| 25 | |
import java.util.Map; |
| 26 | |
|
| 27 | |
import org.apache.commons.lang.StringUtils; |
| 28 | |
import org.kuali.rice.core.service.EncryptionService; |
| 29 | |
import org.kuali.rice.kns.UserSession; |
| 30 | |
import org.kuali.rice.kns.bo.SessionDocument; |
| 31 | |
import org.kuali.rice.kns.dao.SessionDocumentDao; |
| 32 | |
import org.kuali.rice.kns.datadictionary.DocumentEntry; |
| 33 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
| 34 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
| 35 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 36 | |
import org.kuali.rice.kns.service.SessionDocumentService; |
| 37 | |
import org.kuali.rice.kns.util.KualiLRUMap; |
| 38 | |
import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase; |
| 39 | |
import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument; |
| 40 | |
import org.springframework.beans.factory.InitializingBean; |
| 41 | |
import org.springframework.transaction.annotation.Transactional; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
@Transactional |
| 51 | 0 | public class SessionDocumentServiceImpl implements SessionDocumentService, InitializingBean { |
| 52 | |
protected static final String IP_ADDRESS = "ipAddress"; |
| 53 | |
protected static final String PRINCIPAL_ID = "principalId"; |
| 54 | |
protected static final String DOCUMENT_NUMBER = "documentNumber"; |
| 55 | |
protected static final String SESSION_ID = "sessionId"; |
| 56 | |
|
| 57 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class); |
| 58 | |
|
| 59 | |
private BusinessObjectService businessObjectService; |
| 60 | |
private DataDictionaryService dataDictionaryService; |
| 61 | |
private SessionDocumentDao sessionDocumentDao; |
| 62 | |
private Map<String,CachedObject> cachedObjects; |
| 63 | |
private EncryptionService encryptionService; |
| 64 | |
private int maxCacheSize; |
| 65 | |
|
| 66 | 0 | public static class CachedObject { |
| 67 | |
private UserSession userSession; |
| 68 | |
private String formKey; |
| 69 | 0 | CachedObject(UserSession userSession, String formKey) { |
| 70 | 0 | this.userSession = userSession; |
| 71 | 0 | this.formKey = formKey; |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
@Override |
| 75 | |
public String toString() { |
| 76 | 0 | return "CachedObject: principalId="+userSession.getPrincipalId()+" / objectWithFormKey="+userSession.retrieveObject(formKey); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public UserSession getUserSession() { |
| 80 | 0 | return this.userSession; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public String getFormKey() { |
| 84 | 0 | return this.formKey; |
| 85 | |
} |
| 86 | |
} |
| 87 | |
|
| 88 | |
@SuppressWarnings("unchecked") |
| 89 | |
public void afterPropertiesSet() throws Exception { |
| 90 | 0 | cachedObjects = Collections.synchronizedMap( new KualiLRUMap(maxCacheSize) ); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
public KualiDocumentFormBase getDocumentForm( String documentNumber, String docFormKey, UserSession userSession, String ipAddress){ |
| 97 | 0 | KualiDocumentFormBase documentForm = null; |
| 98 | |
|
| 99 | 0 | LOG.debug("getDocumentForm KualiDocumentFormBase from db"); |
| 100 | |
try{ |
| 101 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 102 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 103 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 104 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 105 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 106 | |
|
| 107 | 0 | SessionDocument sessionDoc = (SessionDocument)getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 108 | 0 | if(sessionDoc != null){ |
| 109 | 0 | byte[] formAsBytes = sessionDoc.getSerializedDocumentForm(); |
| 110 | 0 | if ( sessionDoc.isEncrypted() ) { |
| 111 | 0 | formAsBytes = getEncryptionService().decryptBytes(formAsBytes); |
| 112 | |
} |
| 113 | 0 | ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes); |
| 114 | 0 | ObjectInputStream ois = new ObjectInputStream(baip); |
| 115 | |
|
| 116 | |
|
| 117 | 0 | documentForm = (KualiDocumentFormBase)ois.readObject(); |
| 118 | |
|
| 119 | |
|
| 120 | 0 | KualiWorkflowDocument workflowDocument = documentForm.getDocument().getDocumentHeader().getWorkflowDocument(); |
| 121 | 0 | userSession.setWorkflowDocument(workflowDocument); |
| 122 | |
} |
| 123 | 0 | } catch(Exception e) { |
| 124 | 0 | LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 125 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 126 | 0 | } |
| 127 | |
|
| 128 | 0 | return documentForm; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public void purgeDocumentForm(String documentNumber, String docFormKey, |
| 136 | |
UserSession userSession, String ipAddress) { |
| 137 | 0 | synchronized (userSession) { |
| 138 | |
|
| 139 | 0 | LOG.debug("purge document form from session"); |
| 140 | 0 | userSession.removeObject(docFormKey); |
| 141 | |
try { |
| 142 | 0 | LOG.debug("purge document form from database"); |
| 143 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 144 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 145 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 146 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 147 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 148 | 0 | getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys); |
| 149 | 0 | } catch (Exception e) { |
| 150 | 0 | LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 151 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 152 | 0 | } |
| 153 | 0 | } |
| 154 | 0 | } |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public void setDocumentForm(KualiDocumentFormBase form, UserSession userSession, String ipAddress){ |
| 161 | 0 | synchronized ( userSession ) { |
| 162 | |
|
| 163 | 0 | String formKey = form.getFormKey(); |
| 164 | 0 | String key = userSession.getKualiSessionId() + "-" + formKey; |
| 165 | 0 | cachedObjects.put(key, new CachedObject( userSession, formKey )); |
| 166 | |
|
| 167 | 0 | String documentNumber = form.getDocument().getDocumentNumber(); |
| 168 | |
|
| 169 | 0 | if( StringUtils.isNotBlank(documentNumber)) { |
| 170 | |
try { |
| 171 | 0 | LOG.debug("set Document Form into database"); |
| 172 | 0 | Timestamp currentTime = new Timestamp(System.currentTimeMillis()); |
| 173 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 174 | 0 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 175 | 0 | oos.writeObject(form); |
| 176 | |
|
| 177 | 0 | byte[] formAsBytes = baos.toByteArray(); |
| 178 | 0 | boolean encryptContent = false; |
| 179 | 0 | if ( form.getDocTypeName() != null ) { |
| 180 | 0 | DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry( form.getDocTypeName() ); |
| 181 | 0 | if ( documentEntry != null ) { |
| 182 | 0 | encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage(); |
| 183 | |
} |
| 184 | |
} |
| 185 | 0 | if ( encryptContent ) { |
| 186 | 0 | formAsBytes = getEncryptionService().encryptBytes(formAsBytes); |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | 0 | HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 192 | 0 | primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 193 | 0 | primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 194 | 0 | primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 195 | 0 | primaryKeys.put(IP_ADDRESS, ipAddress); |
| 196 | |
|
| 197 | 0 | SessionDocument sessionDocument = (SessionDocument)getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 198 | 0 | if ( sessionDocument == null ) { |
| 199 | 0 | sessionDocument = new SessionDocument(); |
| 200 | 0 | sessionDocument.setSessionId(userSession.getKualiSessionId()); |
| 201 | 0 | sessionDocument.setDocumentNumber(documentNumber); |
| 202 | 0 | sessionDocument.setPrincipalId(userSession.getPrincipalId()); |
| 203 | 0 | sessionDocument.setIpAddress(ipAddress); |
| 204 | |
} |
| 205 | 0 | sessionDocument.setSerializedDocumentForm(formAsBytes); |
| 206 | 0 | sessionDocument.setEncrypted(encryptContent); |
| 207 | 0 | sessionDocument.setLastUpdatedDate(currentTime); |
| 208 | |
|
| 209 | 0 | businessObjectService.save(sessionDocument); |
| 210 | |
|
| 211 | 0 | } catch(Exception e) { |
| 212 | 0 | final String className = form != null ? form.getClass().getName() : "null"; |
| 213 | 0 | LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" |
| 214 | |
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress+"/"+className , e); |
| 215 | 0 | } |
| 216 | |
} else { |
| 217 | 0 | LOG.warn("documentNumber is null on form's document: " + form); |
| 218 | |
} |
| 219 | 0 | } |
| 220 | 0 | } |
| 221 | |
|
| 222 | |
public void purgeAllSessionDocuments(Timestamp expirationDate){ |
| 223 | 0 | sessionDocumentDao.purgeAllSessionDocuments(expirationDate); |
| 224 | 0 | } |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
protected SessionDocumentDao getSessionDocumentDao() { |
| 232 | 0 | return this.sessionDocumentDao; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) { |
| 239 | 0 | this.sessionDocumentDao = sessionDocumentDao; |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
protected BusinessObjectService getBusinessObjectService() { |
| 246 | 0 | return this.businessObjectService; |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 253 | 0 | this.businessObjectService = businessObjectService; |
| 254 | 0 | } |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
public int getMaxCacheSize() { |
| 260 | 0 | return maxCacheSize; |
| 261 | |
} |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
public void setMaxCacheSize(int maxCacheSize) { |
| 267 | 0 | this.maxCacheSize = maxCacheSize; |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
protected EncryptionService getEncryptionService() { |
| 271 | 0 | if ( encryptionService == null ) { |
| 272 | 0 | encryptionService = KNSServiceLocator.getEncryptionService(); |
| 273 | |
} |
| 274 | 0 | return encryptionService; |
| 275 | |
} |
| 276 | |
|
| 277 | |
protected DataDictionaryService getDataDictionaryService() { |
| 278 | 0 | if ( dataDictionaryService == null ) { |
| 279 | 0 | dataDictionaryService = KNSServiceLocator.getDataDictionaryService(); |
| 280 | |
} |
| 281 | 0 | return dataDictionaryService; |
| 282 | |
} |
| 283 | |
|
| 284 | |
} |