| 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.api.encryption.EncryptionService; |
| 29 |
|
import org.kuali.rice.core.api.services.CoreApiServiceLocator; |
| 30 |
|
import org.kuali.rice.kew.exception.WorkflowException; |
| 31 |
|
import org.kuali.rice.kew.util.KEWConstants; |
| 32 |
|
import org.kuali.rice.kns.UserSession; |
| 33 |
|
import org.kuali.rice.kns.bo.SessionDocument; |
| 34 |
|
import org.kuali.rice.kns.dao.SessionDocumentDao; |
| 35 |
|
import org.kuali.rice.kns.datadictionary.DocumentEntry; |
| 36 |
|
import org.kuali.rice.kns.service.*; |
| 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 |
|
@author |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@Transactional |
|
|
|
| 0% |
Uncovered Elements: 137 (137) |
Complexity: 31 |
Complexity Density: 0.32 |
|
| 51 |
|
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 |
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
| 66 |
|
public static class CachedObject { |
| 67 |
|
private UserSession userSession; |
| 68 |
|
private String formKey; |
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 69 |
0
|
CachedObject(UserSession userSession, String formKey) {... |
| 70 |
0
|
this.userSession = userSession; |
| 71 |
0
|
this.formKey = formKey; |
| 72 |
|
} |
| 73 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 74 |
0
|
@Override... |
| 75 |
|
public String toString() { |
| 76 |
0
|
return "CachedObject: principalId="+userSession.getPrincipalId()+" / objectWithFormKey="+userSession.retrieveObject(formKey); |
| 77 |
|
} |
| 78 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 79 |
0
|
public UserSession getUserSession() {... |
| 80 |
0
|
return this.userSession; |
| 81 |
|
} |
| 82 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 83 |
0
|
public String getFormKey() {... |
| 84 |
0
|
return this.formKey; |
| 85 |
|
} |
| 86 |
|
} |
| 87 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 88 |
0
|
@Override... |
| 89 |
|
@SuppressWarnings("unchecked") |
| 90 |
|
public void afterPropertiesSet() throws Exception { |
| 91 |
0
|
cachedObjects = Collections.synchronizedMap( new KualiLRUMap(maxCacheSize) ); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
@see |
| 96 |
|
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.2 |
|
| 97 |
0
|
@Override... |
| 98 |
|
public KualiDocumentFormBase getDocumentForm( String documentNumber, String docFormKey, UserSession userSession, String ipAddress){ |
| 99 |
0
|
KualiDocumentFormBase documentForm = null; |
| 100 |
|
|
| 101 |
0
|
LOG.debug("getDocumentForm KualiDocumentFormBase from db"); |
| 102 |
0
|
try{ |
| 103 |
0
|
HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 104 |
0
|
primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 105 |
0
|
primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 106 |
0
|
primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 107 |
0
|
primaryKeys.put(IP_ADDRESS, ipAddress); |
| 108 |
|
|
| 109 |
0
|
SessionDocument sessionDoc = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 110 |
0
|
if(sessionDoc != null){ |
| 111 |
0
|
byte[] formAsBytes = sessionDoc.getSerializedDocumentForm(); |
| 112 |
0
|
if ( sessionDoc.isEncrypted() ) { |
| 113 |
0
|
formAsBytes = getEncryptionService().decryptBytes(formAsBytes); |
| 114 |
|
} |
| 115 |
0
|
ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes); |
| 116 |
0
|
ObjectInputStream ois = new ObjectInputStream(baip); |
| 117 |
|
|
| 118 |
|
|
| 119 |
0
|
documentForm = (KualiDocumentFormBase)ois.readObject(); |
| 120 |
|
|
| 121 |
|
|
| 122 |
0
|
KualiWorkflowDocument workflowDocument = documentForm.getDocument().getDocumentHeader().getWorkflowDocument(); |
| 123 |
0
|
addDocumentToUserSession(userSession, workflowDocument); |
| 124 |
|
} |
| 125 |
|
} catch(Exception e) { |
| 126 |
0
|
LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 127 |
|
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
0
|
return documentForm; |
| 131 |
|
} |
| 132 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 133 |
0
|
@Override... |
| 134 |
|
public KualiWorkflowDocument getDocumentFromSession(UserSession userSession, String docId) { |
| 135 |
0
|
@SuppressWarnings("unchecked") |
| 136 |
|
Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 137 |
|
|
| 138 |
0
|
if (workflowDocMap == null) { |
| 139 |
0
|
workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 140 |
0
|
userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 141 |
0
|
return null; |
| 142 |
|
} |
| 143 |
0
|
return workflowDocMap.get(docId); |
| 144 |
|
} |
| 145 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 146 |
0
|
@Override... |
| 147 |
|
public void addDocumentToUserSession(UserSession userSession, KualiWorkflowDocument document) { |
| 148 |
0
|
try { |
| 149 |
0
|
@SuppressWarnings("unchecked") |
| 150 |
|
Map<String, KualiWorkflowDocument> workflowDocMap = (Map<String, KualiWorkflowDocument>) userSession.retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); |
| 151 |
0
|
if (workflowDocMap == null) { |
| 152 |
0
|
workflowDocMap = new HashMap<String,KualiWorkflowDocument>(); |
| 153 |
|
} |
| 154 |
0
|
workflowDocMap.put(document.getRouteHeaderId().toString(), document); |
| 155 |
0
|
userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap); |
| 156 |
|
} catch (WorkflowException e) { |
| 157 |
0
|
throw new IllegalStateException("could not save the document in the session", e); |
| 158 |
|
} |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
@see |
| 163 |
|
|
| 164 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
|
| 165 |
0
|
@Override... |
| 166 |
|
public void purgeDocumentForm(String documentNumber, String docFormKey, |
| 167 |
|
UserSession userSession, String ipAddress) { |
| 168 |
0
|
synchronized (userSession) { |
| 169 |
|
|
| 170 |
0
|
LOG.debug("purge document form from session"); |
| 171 |
0
|
userSession.removeObject(docFormKey); |
| 172 |
0
|
try { |
| 173 |
0
|
LOG.debug("purge document form from database"); |
| 174 |
0
|
HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 175 |
0
|
primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 176 |
0
|
primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 177 |
0
|
primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 178 |
0
|
primaryKeys.put(IP_ADDRESS, ipAddress); |
| 179 |
0
|
getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys); |
| 180 |
|
} catch (Exception e) { |
| 181 |
0
|
LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" |
| 182 |
|
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress, e); |
| 183 |
|
} |
| 184 |
|
} |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
@see |
| 189 |
|
|
| 190 |
|
|
|
|
|
| 0% |
Uncovered Elements: 51 (51) |
Complexity: 8 |
Complexity Density: 0.21 |
|
| 191 |
0
|
@Override... |
| 192 |
|
public void setDocumentForm(KualiDocumentFormBase form, UserSession userSession, String ipAddress){ |
| 193 |
0
|
synchronized ( userSession ) { |
| 194 |
|
|
| 195 |
0
|
String formKey = form.getFormKey(); |
| 196 |
0
|
String key = userSession.getKualiSessionId() + "-" + formKey; |
| 197 |
0
|
cachedObjects.put(key, new CachedObject( userSession, formKey )); |
| 198 |
|
|
| 199 |
0
|
String documentNumber = form.getDocument().getDocumentNumber(); |
| 200 |
|
|
| 201 |
0
|
if( StringUtils.isNotBlank(documentNumber)) { |
| 202 |
0
|
try { |
| 203 |
0
|
LOG.debug("set Document Form into database"); |
| 204 |
0
|
Timestamp currentTime = new Timestamp(System.currentTimeMillis()); |
| 205 |
0
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 206 |
0
|
ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 207 |
0
|
oos.writeObject(form); |
| 208 |
|
|
| 209 |
0
|
byte[] formAsBytes = baos.toByteArray(); |
| 210 |
0
|
boolean encryptContent = false; |
| 211 |
0
|
if ( form.getDocTypeName() != null ) { |
| 212 |
0
|
DocumentEntry documentEntry = getDataDictionaryService().getDataDictionary().getDocumentEntry( form.getDocTypeName() ); |
| 213 |
0
|
if ( documentEntry != null ) { |
| 214 |
0
|
encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage(); |
| 215 |
|
} |
| 216 |
|
} |
| 217 |
0
|
if ( encryptContent ) { |
| 218 |
0
|
formAsBytes = getEncryptionService().encryptBytes(formAsBytes); |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
0
|
HashMap<String, String> primaryKeys = new HashMap<String, String>(4); |
| 224 |
0
|
primaryKeys.put(SESSION_ID, userSession.getKualiSessionId()); |
| 225 |
0
|
primaryKeys.put(DOCUMENT_NUMBER, documentNumber); |
| 226 |
0
|
primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId()); |
| 227 |
0
|
primaryKeys.put(IP_ADDRESS, ipAddress); |
| 228 |
|
|
| 229 |
0
|
SessionDocument sessionDocument = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys); |
| 230 |
0
|
if ( sessionDocument == null ) { |
| 231 |
0
|
sessionDocument = new SessionDocument(); |
| 232 |
0
|
sessionDocument.setSessionId(userSession.getKualiSessionId()); |
| 233 |
0
|
sessionDocument.setDocumentNumber(documentNumber); |
| 234 |
0
|
sessionDocument.setPrincipalId(userSession.getPrincipalId()); |
| 235 |
0
|
sessionDocument.setIpAddress(ipAddress); |
| 236 |
|
} |
| 237 |
0
|
sessionDocument.setSerializedDocumentForm(formAsBytes); |
| 238 |
0
|
sessionDocument.setEncrypted(encryptContent); |
| 239 |
0
|
sessionDocument.setLastUpdatedDate(currentTime); |
| 240 |
|
|
| 241 |
0
|
businessObjectService.save(sessionDocument); |
| 242 |
|
|
| 243 |
|
} catch(Exception e) { |
| 244 |
0
|
final String className = form != null ? form.getClass().getName() : "null"; |
| 245 |
0
|
LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" |
| 246 |
|
+ userSession.getKualiSessionId()+"/"+documentNumber+"/"+userSession.getPrincipalId()+"/"+ipAddress+"/"+className , e); |
| 247 |
|
} |
| 248 |
|
} else { |
| 249 |
0
|
LOG.warn("documentNumber is null on form's document: " + form); |
| 250 |
|
} |
| 251 |
|
} |
| 252 |
|
} |
| 253 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 254 |
0
|
@Override... |
| 255 |
|
public void purgeAllSessionDocuments(Timestamp expirationDate){ |
| 256 |
0
|
sessionDocumentDao.purgeAllSessionDocuments(expirationDate); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
@return |
| 263 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 264 |
0
|
protected SessionDocumentDao getSessionDocumentDao() {... |
| 265 |
0
|
return this.sessionDocumentDao; |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
@param |
| 270 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 271 |
0
|
public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) {... |
| 272 |
0
|
this.sessionDocumentDao = sessionDocumentDao; |
| 273 |
|
} |
| 274 |
|
|
| 275 |
|
|
| 276 |
|
@return |
| 277 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 278 |
0
|
protected BusinessObjectService getBusinessObjectService() {... |
| 279 |
0
|
return this.businessObjectService; |
| 280 |
|
} |
| 281 |
|
|
| 282 |
|
|
| 283 |
|
@param |
| 284 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 285 |
0
|
public void setBusinessObjectService(BusinessObjectService businessObjectService) {... |
| 286 |
0
|
this.businessObjectService = businessObjectService; |
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 290 |
|
@return |
| 291 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 292 |
0
|
public int getMaxCacheSize() {... |
| 293 |
0
|
return maxCacheSize; |
| 294 |
|
} |
| 295 |
|
|
| 296 |
|
|
| 297 |
|
@param |
| 298 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 299 |
0
|
public void setMaxCacheSize(int maxCacheSize) {... |
| 300 |
0
|
this.maxCacheSize = maxCacheSize; |
| 301 |
|
} |
| 302 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 303 |
0
|
protected EncryptionService getEncryptionService() {... |
| 304 |
0
|
if ( encryptionService == null ) { |
| 305 |
0
|
encryptionService = CoreApiServiceLocator.getEncryptionService(); |
| 306 |
|
} |
| 307 |
0
|
return encryptionService; |
| 308 |
|
} |
| 309 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 310 |
0
|
protected DataDictionaryService getDataDictionaryService() {... |
| 311 |
0
|
if ( dataDictionaryService == null ) { |
| 312 |
0
|
dataDictionaryService = KNSServiceLocatorWeb.getDataDictionaryService(); |
| 313 |
|
} |
| 314 |
0
|
return dataDictionaryService; |
| 315 |
|
} |
| 316 |
|
|
| 317 |
|
} |