Coverage Report - org.kuali.rice.krad.service.impl.SessionDocumentServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SessionDocumentServiceImpl
0%
0/115
0%
0/24
1.81
SessionDocumentServiceImpl$CachedObject
0%
0/7
N/A
1.81
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.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.CoreApiServiceLocator;
 29  
 import org.kuali.rice.core.api.encryption.EncryptionService;
 30  
 import org.kuali.rice.kew.api.WorkflowDocument;
 31  
 import org.kuali.rice.kew.exception.WorkflowException;
 32  
 import org.kuali.rice.kew.util.KEWConstants;
 33  
 import org.kuali.rice.krad.UserSession;
 34  
 import org.kuali.rice.krad.bo.SessionDocument;
 35  
 import org.kuali.rice.krad.dao.SessionDocumentDao;
 36  
 import org.kuali.rice.krad.datadictionary.DocumentEntry;
 37  
 import org.kuali.rice.krad.service.BusinessObjectService;
 38  
 import org.kuali.rice.krad.service.DataDictionaryService;
 39  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 40  
 import org.kuali.rice.krad.service.SessionDocumentService;
 41  
 import org.kuali.rice.krad.util.KualiLRUMap;
 42  
 import org.kuali.rice.krad.web.form.DocumentFormBase;
 43  
 import org.springframework.beans.factory.InitializingBean;
 44  
 import org.springframework.transaction.annotation.Transactional;
 45  
 
 46  
 /**
 47  
  * Implementation of <code>SessionDocumentService</code> that persists the document form
 48  
  * contents to the underlying database
 49  
  *
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  
 @Transactional
 53  0
 public class SessionDocumentServiceImpl implements SessionDocumentService, InitializingBean {
 54  0
     private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class);
 55  
 
 56  
     protected static final String IP_ADDRESS = "ipAddress";
 57  
     protected static final String PRINCIPAL_ID = "principalId";
 58  
     protected static final String DOCUMENT_NUMBER = "documentNumber";
 59  
     protected static final String SESSION_ID = "sessionId";
 60  
 
 61  
     private Map<String, CachedObject> cachedObjects;
 62  
     private EncryptionService encryptionService;
 63  
     private int maxCacheSize;
 64  
 
 65  
     private BusinessObjectService businessObjectService;
 66  
     private DataDictionaryService dataDictionaryService;
 67  
     private SessionDocumentDao sessionDocumentDao;
 68  
 
 69  0
     public static class CachedObject {
 70  
         private UserSession userSession;
 71  
         private String formKey;
 72  
 
 73  0
         CachedObject(UserSession userSession, String formKey) {
 74  0
             this.userSession = userSession;
 75  0
             this.formKey = formKey;
 76  0
         }
 77  
 
 78  
         @Override
 79  
         public String toString() {
 80  0
             return "CachedObject: principalId=" + userSession.getPrincipalId() + " / objectWithFormKey=" +
 81  
                     userSession.retrieveObject(formKey);
 82  
         }
 83  
 
 84  
         public UserSession getUserSession() {
 85  0
             return this.userSession;
 86  
         }
 87  
 
 88  
         public String getFormKey() {
 89  0
             return this.formKey;
 90  
         }
 91  
     }
 92  
 
 93  
     @Override
 94  
     @SuppressWarnings("unchecked")
 95  
     public void afterPropertiesSet() throws Exception {
 96  0
         cachedObjects = Collections.synchronizedMap(new KualiLRUMap(maxCacheSize));
 97  0
     }
 98  
 
 99  
     /**
 100  
      * @see org.kuali.rice.krad.service.SessionDocumentService#getDocumentForm(org.kuali.rice.krad.web.form.DocumentFormBase,
 101  
      *      org.kuali.rice.krad.UserSession, java.lang.String)
 102  
      */
 103  
     @Override
 104  
     public DocumentFormBase getDocumentForm(String documentNumber, String docFormKey, UserSession userSession,
 105  
             String ipAddress) {
 106  0
         DocumentFormBase documentForm = null;
 107  
 
 108  0
         LOG.debug("getDocumentForm DocumentFormBase from db");
 109  
         try {
 110  
             // re-create the DocumentFormBase object
 111  0
             documentForm = (DocumentFormBase) retrieveDocumentForm(userSession, docFormKey, documentNumber, ipAddress);
 112  
 
 113  
             //re-store workFlowDocument into session
 114  0
             WorkflowDocument workflowDocument =
 115  
                     documentForm.getDocument().getDocumentHeader().getWorkflowDocument();
 116  0
             addDocumentToUserSession(userSession, workflowDocument);
 117  0
         } catch (Exception e) {
 118  0
             LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() + "/" +
 119  
                     documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
 120  0
         }
 121  
 
 122  0
         return documentForm;
 123  
     }
 124  
 
 125  
     protected Object retrieveDocumentForm(UserSession userSession, String sessionId, String documentNumber,
 126  
             String ipAddress) throws Exception {
 127  0
         HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
 128  0
         primaryKeys.put(SESSION_ID, sessionId);
 129  0
         if (documentNumber != null) {
 130  0
             primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
 131  
         }
 132  0
         primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
 133  0
         primaryKeys.put(IP_ADDRESS, ipAddress);
 134  
 
 135  0
         SessionDocument sessionDoc = getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys);
 136  0
         if (sessionDoc != null) {
 137  0
             byte[] formAsBytes = sessionDoc.getSerializedDocumentForm();
 138  0
             if (sessionDoc.isEncrypted()) {
 139  0
                 formAsBytes = getEncryptionService().decryptBytes(formAsBytes);
 140  
             }
 141  0
             ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes);
 142  0
             ObjectInputStream ois = new ObjectInputStream(baip);
 143  
 
 144  0
             return ois.readObject();
 145  
         }
 146  
 
 147  0
         return null;
 148  
     }
 149  
 
 150  
     @Override
 151  
     public WorkflowDocument getDocumentFromSession(UserSession userSession, String docId) {
 152  0
         @SuppressWarnings("unchecked") Map<String, WorkflowDocument> workflowDocMap =
 153  
                 (Map<String, WorkflowDocument>) userSession
 154  
                         .retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME);
 155  
 
 156  0
         if (workflowDocMap == null) {
 157  0
             workflowDocMap = new HashMap<String, WorkflowDocument>();
 158  0
             userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap);
 159  0
             return null;
 160  
         }
 161  0
         return workflowDocMap.get(docId);
 162  
     }
 163  
 
 164  
     /**
 165  
      * @see org.kuali.rice.krad.service.SessionDocumentService#addDocumentToUserSession(org.kuali.rice.krad.UserSession,
 166  
      *      org.kuali.rice.krad.workflow.service.KualiWorkflowDocument)
 167  
      */
 168  
     @Override
 169  
     public void addDocumentToUserSession(UserSession userSession, WorkflowDocument document) {
 170  0
         @SuppressWarnings("unchecked") Map<String, WorkflowDocument> workflowDocMap =
 171  
                 (Map<String, WorkflowDocument>) userSession
 172  
                         .retrieveObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME);
 173  0
         if (workflowDocMap == null) {
 174  0
             workflowDocMap = new HashMap<String, WorkflowDocument>();
 175  
         }
 176  0
         workflowDocMap.put(document.getDocumentId(), document);
 177  0
         userSession.addObject(KEWConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap);
 178  0
     }
 179  
 
 180  
     /**
 181  
      * @see org.kuali.rice.krad.service.SessionDocumentService#purgeDocumentForm(String
 182  
      *      documentNumber, String docFormKey, UserSession userSession)
 183  
      */
 184  
     @Override
 185  
     public void purgeDocumentForm(String documentNumber, String docFormKey, UserSession userSession, String ipAddress) {
 186  0
         synchronized (userSession) {
 187  
 
 188  0
             LOG.debug("purge document form from session");
 189  0
             userSession.removeObject(docFormKey);
 190  
             try {
 191  0
                 LOG.debug("purge document form from database");
 192  0
                 HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
 193  0
                 primaryKeys.put(SESSION_ID, userSession.getKualiSessionId());
 194  0
                 primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
 195  0
                 primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
 196  0
                 primaryKeys.put(IP_ADDRESS, ipAddress);
 197  0
                 getBusinessObjectService().deleteMatching(SessionDocument.class, primaryKeys);
 198  0
             } catch (Exception e) {
 199  0
                 LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() +
 200  
                         "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
 201  0
             }
 202  0
         }
 203  0
     }
 204  
 
 205  
     /**
 206  
      * @see org.kuali.rice.krad.service.SessionDocumentService#setDocumentForm(org.kuali.rice.krad.web.form.DocumentFormBase,
 207  
      *      org.kuali.rice.krad.UserSession, java.lang.String)
 208  
      */
 209  
     @Override
 210  
     public void setDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress) {
 211  0
         synchronized (userSession) {
 212  
             //formKey was set in KualiDocumentActionBase execute method
 213  0
             String formKey = form.getFormKey();
 214  0
             String key = userSession.getKualiSessionId() + "-" + formKey;
 215  
 
 216  0
             String documentNumber = form.getDocument().getDocumentNumber();
 217  0
             if (StringUtils.isNotBlank(formKey)) {
 218  
                 //FIXME: Currently using formKey for sessionId
 219  0
                 persistDocumentForm(form, userSession, ipAddress, formKey, documentNumber);
 220  
             } else {
 221  0
                 LOG.warn("documentNumber is null on form's document: " + form);
 222  
             }
 223  0
         }
 224  0
     }
 225  
 
 226  
     protected void persistDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress,
 227  
             String sessionId, String documentNumber) {
 228  
         try {
 229  0
             LOG.debug("set Document Form into database");
 230  
 
 231  0
             Timestamp currentTime = new Timestamp(System.currentTimeMillis());
 232  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 233  0
             ObjectOutputStream oos = new ObjectOutputStream(baos);
 234  0
             oos.writeObject(form);
 235  
 
 236  
             // serialize the DocumentFormBase object into a byte array
 237  0
             byte[] formAsBytes = baos.toByteArray();
 238  0
             boolean encryptContent = false;
 239  0
             DocumentEntry documentEntry =
 240  
                     getDataDictionaryService().getDataDictionary().getDocumentEntry(form.getDocTypeName());
 241  0
             if (documentEntry != null) {
 242  0
                 encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage();
 243  
             }
 244  
 
 245  0
             if (encryptContent) {
 246  0
                 formAsBytes = getEncryptionService().encryptBytes(formAsBytes);
 247  
             }
 248  
 
 249  
             // check if a record is already there in the database
 250  
             // this may only happen under jMeter testing, but there is no way to be sure
 251  0
             HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
 252  0
             primaryKeys.put(SESSION_ID, sessionId);
 253  0
             primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
 254  0
             primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
 255  0
             primaryKeys.put(IP_ADDRESS, ipAddress);
 256  
 
 257  0
             SessionDocument sessionDocument =
 258  
                     getBusinessObjectService().findByPrimaryKey(SessionDocument.class, primaryKeys);
 259  0
             if (sessionDocument == null) {
 260  0
                 sessionDocument = new SessionDocument();
 261  0
                 sessionDocument.setSessionId(sessionId);
 262  0
                 sessionDocument.setDocumentNumber(documentNumber);
 263  0
                 sessionDocument.setPrincipalId(userSession.getPrincipalId());
 264  0
                 sessionDocument.setIpAddress(ipAddress);
 265  
             }
 266  0
             sessionDocument.setSerializedDocumentForm(formAsBytes);
 267  0
             sessionDocument.setEncrypted(encryptContent);
 268  0
             sessionDocument.setLastUpdatedDate(currentTime);
 269  
 
 270  0
             businessObjectService.save(sessionDocument);
 271  0
         } catch (Exception e) {
 272  0
             final String className = form != null ? form.getClass().getName() : "null";
 273  0
             LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" + userSession.getKualiSessionId() +
 274  
                     "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress + "/" + className, e);
 275  0
         }
 276  0
     }
 277  
 
 278  
     /**
 279  
      * @see org.kuali.rice.krad.service.SessionDocumentService#purgeAllSessionDocuments(java.sql.Timestamp)
 280  
      */
 281  
     @Override
 282  
     public void purgeAllSessionDocuments(Timestamp expirationDate) {
 283  0
         sessionDocumentDao.purgeAllSessionDocuments(expirationDate);
 284  0
     }
 285  
 
 286  
     protected SessionDocumentDao getSessionDocumentDao() {
 287  0
         return this.sessionDocumentDao;
 288  
     }
 289  
 
 290  
     public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) {
 291  0
         this.sessionDocumentDao = sessionDocumentDao;
 292  0
     }
 293  
 
 294  
     protected BusinessObjectService getBusinessObjectService() {
 295  0
         return this.businessObjectService;
 296  
     }
 297  
 
 298  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 299  0
         this.businessObjectService = businessObjectService;
 300  0
     }
 301  
 
 302  
     public int getMaxCacheSize() {
 303  0
         return maxCacheSize;
 304  
     }
 305  
 
 306  
     public void setMaxCacheSize(int maxCacheSize) {
 307  0
         this.maxCacheSize = maxCacheSize;
 308  0
     }
 309  
 
 310  
     protected EncryptionService getEncryptionService() {
 311  0
         if (encryptionService == null) {
 312  0
             encryptionService = CoreApiServiceLocator.getEncryptionService();
 313  
         }
 314  0
         return encryptionService;
 315  
     }
 316  
 
 317  
     protected DataDictionaryService getDataDictionaryService() {
 318  0
         if (dataDictionaryService == null) {
 319  0
             dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
 320  
         }
 321  0
         return dataDictionaryService;
 322  
     }
 323  
 }