1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
44  
45  
46  
47  
48  
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  
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              
79              documentForm = (DocumentFormBase) retrieveDocumentForm(userSession, docFormKey, documentNumber, ipAddress);
80  
81              
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 
121 
122 
123     @Override
124     @Deprecated
125     public WorkflowDocument getDocumentFromSession(UserSession userSession, String docId) {
126         return UserSessionUtils.getWorkflowDocument(userSession, docId);
127     }
128 
129     
130 
131 
132 
133 
134 
135     @Override
136     @Deprecated
137     public void addDocumentToUserSession(UserSession userSession, WorkflowDocument document) {
138         UserSessionUtils.addWorkflowDocument(userSession, document);
139     }
140 
141     
142 
143 
144 
145 
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 
170 
171 
172     @Override
173     @Deprecated
174     public void setDocumentForm(DocumentFormBase form, UserSession userSession, String ipAddress) {
175         synchronized (userSession) {
176             
177             String formKey = form.getFormKey();
178             String key = userSession.getKualiSessionId() + "-" + formKey;
179 
180             String documentNumber = form.getDocument().getDocumentNumber();
181             if (StringUtils.isNotBlank(formKey)) {
182                 
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             
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             EncryptionService encryptionService = getEncryptionService();
211             if (encryptContent && encryptionService.isEnabled()) {
212                 formAsBytes = encryptionService.encryptBytes(formAsBytes);
213             }
214 
215             
216             
217             HashMap<String, String> primaryKeys = new HashMap<String, String>(4);
218             primaryKeys.put(SESSION_ID, sessionId);
219             primaryKeys.put(DOCUMENT_NUMBER, documentNumber);
220             primaryKeys.put(PRINCIPAL_ID, userSession.getPrincipalId());
221             primaryKeys.put(IP_ADDRESS, ipAddress);
222 
223             SessionDocument sessionDocument =
224                     legacyDataAdapter.findByPrimaryKey(SessionDocument.class, primaryKeys);
225             if (sessionDocument == null) {
226                 sessionDocument = new SessionDocument();
227                 sessionDocument.setSessionId(sessionId);
228                 sessionDocument.setDocumentNumber(documentNumber);
229                 sessionDocument.setPrincipalId(userSession.getPrincipalId());
230                 sessionDocument.setIpAddress(ipAddress);
231             }
232             sessionDocument.setSerializedDocumentForm(formAsBytes);
233             sessionDocument.setEncrypted(encryptContent);
234             sessionDocument.setLastUpdatedDate(currentTime);
235 
236             legacyDataAdapter.save(sessionDocument);
237         } catch (Exception e) {
238             final String className = form != null ? form.getClass().getName() : "null";
239             LOG.error("setDocumentForm failed for SessId/DocNum/PrinId/IP/class:" + userSession.getKualiSessionId() +
240                     "/" + documentNumber + "/" + userSession.getPrincipalId() + "/" + ipAddress + "/" + className, e);
241         }
242     }
243 
244     
245 
246 
247 
248 
249     @Override
250     @Deprecated
251     public void purgeAllSessionDocuments(Timestamp expirationDate) {
252         sessionDocumentDao.purgeAllSessionDocuments(expirationDate);
253     }
254 
255     @Deprecated
256     protected SessionDocumentDao getSessionDocumentDao() {
257         return this.sessionDocumentDao;
258     }
259 
260     
261 
262 
263 
264     @Deprecated
265     public void setSessionDocumentDao(SessionDocumentDao sessionDocumentDao) {
266         this.sessionDocumentDao = sessionDocumentDao;
267     }
268     
269     @Required
270     public void setLegacyDataAdapter(LegacyDataAdapter legacyDataAdapter) {
271         this.legacyDataAdapter = legacyDataAdapter;
272     }
273 
274     @Deprecated
275     protected EncryptionService getEncryptionService() {
276         if (encryptionService == null) {
277             encryptionService = CoreApiServiceLocator.getEncryptionService();
278         }
279         return encryptionService;
280     }
281 
282 
283     @Deprecated
284     protected DataDictionaryService getDataDictionaryService() {
285         if (dataDictionaryService == null) {
286             dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
287         }
288         return dataDictionaryService;
289     }
290 }