View Javadoc
1   /**
2    * Copyright 2005-2014 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 org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.encryption.EncryptionService;
21  import org.kuali.rice.kew.api.WorkflowDocument;
22  import org.kuali.rice.krad.UserSession;
23  import org.kuali.rice.krad.UserSessionUtils;
24  import org.kuali.rice.krad.bo.SessionDocument;
25  import org.kuali.rice.krad.dao.SessionDocumentDao;
26  import org.kuali.rice.krad.datadictionary.DocumentEntry;
27  import org.kuali.rice.krad.service.DataDictionaryService;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.service.LegacyDataAdapter;
30  import org.kuali.rice.krad.service.SessionDocumentService;
31  import org.kuali.rice.krad.web.form.DocumentFormBase;
32  import org.springframework.beans.factory.annotation.Required;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  import java.io.ByteArrayInputStream;
36  import java.io.ByteArrayOutputStream;
37  import java.io.ObjectInputStream;
38  import java.io.ObjectOutputStream;
39  import java.sql.Timestamp;
40  import java.util.HashMap;
41  
42  /**
43   * Implementation of <code>SessionDocumentService</code> that persists the document form
44   * contents to the underlying database
45   *
46   * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @Transactional
51  @Deprecated
52  public class SessionDocumentServiceImpl implements SessionDocumentService {
53      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SessionDocumentServiceImpl.class);
54  
55      protected static final String IP_ADDRESS = "ipAddress";
56      protected static final String PRINCIPAL_ID = "principalId";
57      protected static final String DOCUMENT_NUMBER = "documentNumber";
58      protected static final String SESSION_ID = "sessionId";
59  
60      private EncryptionService encryptionService;
61  
62      private LegacyDataAdapter legacyDataAdapter;
63      private DataDictionaryService dataDictionaryService;
64      private SessionDocumentDao sessionDocumentDao;
65  
66      /**
67       * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
68       *
69       */
70      @Override
71      @Deprecated
72      public DocumentFormBase getDocumentForm(String documentNumber, String docFormKey, UserSession userSession,
73              String ipAddress) {
74          DocumentFormBase documentForm = null;
75  
76          LOG.debug("getDocumentForm DocumentFormBase from db");
77          try {
78              // re-create the DocumentFormBase object
79              documentForm = (DocumentFormBase) retrieveDocumentForm(userSession, docFormKey, documentNumber, ipAddress);
80  
81              //re-store workFlowDocument into session
82              WorkflowDocument workflowDocument =
83                      documentForm.getDocument().getDocumentHeader().getWorkflowDocument();
84              UserSessionUtils.addWorkflowDocument(userSession, workflowDocument);
85          } catch (Exception e) {
86              LOG.error("getDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() + "/" +
87                      documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
88          }
89  
90          return documentForm;
91      }
92  
93      @Deprecated
94      protected Object retrieveDocumentForm(UserSession userSession, String sessionId, String documentNumber,
95              String ipAddress) throws Exception {
96          HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
97          primaryKeys.put(SESSION_ID, sessionId);
98          if (documentNumber != null) {
99              primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
100         }
101         primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
102         primaryKeys.put(IP_ADDRESS, ipAddress);
103 
104         SessionDocument sessionDoc = legacyDataAdapter.findByPrimaryKey(SessionDocument.class, primaryKeys);
105         if (sessionDoc != null) {
106             byte[] formAsBytes = sessionDoc.getSerializedDocumentForm();
107             if (sessionDoc.isEncrypted()) {
108                 formAsBytes = getEncryptionService().decryptBytes(formAsBytes);
109             }
110             ByteArrayInputStream baip = new ByteArrayInputStream(formAsBytes);
111             ObjectInputStream ois = new ObjectInputStream(baip);
112 
113             return ois.readObject();
114         }
115 
116         return null;
117     }
118 
119     /**
120      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
121      *
122      */
123     @Override
124     @Deprecated
125     public WorkflowDocument getDocumentFromSession(UserSession userSession, String docId) {
126         return UserSessionUtils.getWorkflowDocument(userSession, docId);
127     }
128 
129     /**
130      * @see org.kuali.rice.krad.service.SessionDocumentService#addDocumentToUserSession(org.kuali.rice.krad.UserSession,
131      *      org.kuali.rice.kew.api.WorkflowDocument)
132      *
133      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)     *
134      */
135     @Override
136     @Deprecated
137     public void addDocumentToUserSession(UserSession userSession, WorkflowDocument document) {
138         UserSessionUtils.addWorkflowDocument(userSession, document);
139     }
140 
141     /**
142      * @see org.kuali.rice.krad.service.SessionDocumentService#purgeDocumentForm(String, String,
143      *      org.kuali.rice.krad.UserSession, String)
144      *
145      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
146      */
147     @Override
148     @Deprecated
149     public void purgeDocumentForm(String documentNumber, String docFormKey, UserSession userSession, String ipAddress) {
150         synchronized (userSession) {
151             LOG.debug("purge document form from session");
152             userSession.removeObject(docFormKey);
153             try {
154                 LOG.debug("purge document form from database");
155                 HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
156                 primaryKeys.put(SESSION_ID, userSession.getKualiSessionId());
157                 primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
158                 primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
159                 primaryKeys.put(IP_ADDRESS, ipAddress);
160                 legacyDataAdapter.deleteMatching(SessionDocument.class, primaryKeys);
161             } catch (Exception e) {
162                 LOG.error("purgeDocumentForm failed for SessId/DocNum/PrinId/IP:" + userSession.getKualiSessionId() +
163                         "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress, e);
164             }
165         }
166     }
167 
168     /**
169      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
170      *
171      */
172     @Override
173     @Deprecated
174     public void setDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress) {
175         synchronized (userSession) {
176             //formKey was set in KualiDocumentActionBase execute method
177             String formKey = form.getFormKey();
178             String key = userSession.getKualiSessionId() + "-" + formKey;
179 
180             String documentNumber = form.getDocument().getDocumentNumber();
181             if (StringUtils.isNotBlank(formKey)) {
182                 //FIXME: Currently using formKey for sessionId
183                 persistDocumentForm(form, userSession, ipAddress, formKey, documentNumber);
184             } else {
185                 LOG.warn("documentNumber is null on form's document: " + form);
186             }
187         }
188     }
189 
190     @Deprecated
191     protected void persistDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress,
192             String sessionId, String documentNumber) {
193         try {
194             LOG.debug("set Document Form into database");
195 
196             Timestamp currentTime = new Timestamp(System.currentTimeMillis());
197             ByteArrayOutputStream baos = new ByteArrayOutputStream();
198             ObjectOutputStream oos = new ObjectOutputStream(baos);
199             oos.writeObject(form);
200 
201             // serialize the DocumentFormBase object into a byte array
202             byte[] formAsBytes = baos.toByteArray();
203             boolean encryptContent = false;
204             DocumentEntry documentEntry =
205                     getDataDictionaryService().getDataDictionary().getDocumentEntry(form.getDocTypeName());
206             if (documentEntry != null) {
207                 encryptContent = documentEntry.isEncryptDocumentDataInPersistentSessionStorage();
208             }
209 
210             if (encryptContent) {
211                 formAsBytes = getEncryptionService().encryptBytes(formAsBytes);
212             }
213 
214             // check if a record is already there in the database
215             // this may only happen under jMeter testing, but there is no way to be sure
216             HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
217             primaryKeys.put(SESSION_ID, sessionId);
218             primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
219             primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
220             primaryKeys.put(IP_ADDRESS, ipAddress);
221 
222             SessionDocument sessionDocument =
223                     legacyDataAdapter.findByPrimaryKey(SessionDocument.class, primaryKeys);
224             if (sessionDocument == null) {
225                 sessionDocument = new SessionDocument();
226                 sessionDocument.setSessionId(sessionId);
227                 sessionDocument.setDocumentNumber(documentNumber);
228                 sessionDocument.setPrincipalId(userSession.getPrincipalId());
229                 sessionDocument.setIpAddress(ipAddress);
230             }
231             sessionDocument.setSerializedDocumentForm(formAsBytes);
232             sessionDocument.setEncrypted(encryptContent);
233             sessionDocument.setLastUpdatedDate(currentTime);
234 
235             legacyDataAdapter.save(sessionDocument);
236         } catch (Exception e) {
237             final String className = form != null ? form.getClass().getName() : "null";
238             LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" + userSession.getKualiSessionId() +
239                     "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress + "/" + className, e);
240         }
241     }
242 
243     /**
244      * @see org.kuali.rice.krad.service.SessionDocumentService#purgeAllSessionDocuments(java.sql.Timestamp)
245      *
246      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
247      */
248     @Override
249     @Deprecated
250     public void purgeAllSessionDocuments(Timestamp expirationDate) {
251         sessionDocumentDao.purgeAllSessionDocuments(expirationDate);
252     }
253 
254     @Deprecated
255     protected SessionDocumentDao getSessionDocumentDao() {
256         return this.sessionDocumentDao;
257     }
258 
259     /**
260      * @deprecated (Deprecated and removed from use in KRAD  (KULRICE-9149)
261      *
262      */
263     @Deprecated
264     public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) {
265         this.sessionDocumentDao = sessionDocumentDao;
266     }
267     
268     @Required
269     public void setLegacyDataAdapter(LegacyDataAdapter legacyDataAdapter) {
270         this.legacyDataAdapter = legacyDataAdapter;
271     }
272 
273     @Deprecated
274     protected EncryptionService getEncryptionService() {
275         if (encryptionService == null) {
276             encryptionService = CoreApiServiceLocator.getEncryptionService();
277         }
278         return encryptionService;
279     }
280 
281 
282     @Deprecated
283     protected DataDictionaryService getDataDictionaryService() {
284         if (dataDictionaryService == null) {
285             dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
286         }
287         return dataDictionaryService;
288     }
289 }