View Javadoc
1   /*
2    * Copyright 2007 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.ole.gl.service.impl;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.commons.codec.binary.Base64;
23  import org.kuali.ole.gl.businessobject.OriginEntryFull;
24  import org.kuali.ole.gl.service.GlCorrectionProcessOriginEntryService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.core.api.datetime.DateTimeService;
28  import org.kuali.rice.kns.lookup.LookupResults;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  /**
35   * This implementation of GlCorrectionProcessOriginEntryService uses the database to temporarily store lists of origin entries.
36   * While this implementation does not clear out persisted origin entries, the batch job defined using the
37   * org.kuali.ole.sys.batch.PurgeOldLookupResultsStep class may cause the purging of origin entries persisted with this implementation.
38   * 
39   * @see GlCorrectionProcessOriginEntryService
40   */
41  @Transactional
42  public class GlCorrectionProcessOriginEntryServiceImpl implements GlCorrectionProcessOriginEntryService {
43  
44      private BusinessObjectService businessObjectService;
45  
46      /**
47       * Persists the origin entries under a given sequence number. If entries are persisted again under the same sequence number,
48       * then they will be overridden.
49       * 
50       * @param glcpSearchResuiltsSequenceNumber a sequence number
51       * @param allEntries a list of origin entries
52       * @throws Exception thrown if anything goes wrong
53       * @see org.kuali.ole.gl.service.GlCorrectionProcessOriginEntryService#persistAllEntries(java.lang.String, java.util.List)
54       */
55      public void persistAllEntries(String glcpSearchResuiltsSequenceNumber, List<OriginEntryFull> allEntries) throws Exception {
56          String serializedOriginEntries = new String(Base64.encodeBase64(ObjectUtils.toByteArray(allEntries)));
57  
58          LookupResults lookupResults = retrieveGlcpAllOriginEntries(glcpSearchResuiltsSequenceNumber);
59          if (lookupResults == null) {
60              lookupResults = new LookupResults();
61              lookupResults.setLookupResultsSequenceNumber(glcpSearchResuiltsSequenceNumber);
62              lookupResults.setLookupPersonId(GlobalVariables.getUserSession().getPerson().getPrincipalId());
63          }
64          lookupResults.setLookupDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
65          lookupResults.setSerializedLookupResults(serializedOriginEntries);
66          businessObjectService.save(lookupResults);
67      }
68  
69      /**
70       * Retrieves the origin entries stored under the given sequence number
71       * 
72       * @param glcpSearchResuiltsSequenceNumber a sequence number
73       * @return a list of origin entries, or null if no results are currently not in the system.
74       * @throws Exception thrown if something goes wrong - vague documentation for a vague exception
75       * @see org.kuali.ole.gl.service.GlCorrectionProcessOriginEntryService#retrieveAllEntries(java.lang.String)
76       */
77      public List<OriginEntryFull> retrieveAllEntries(String glcpSearchResuiltsSequenceNumber) throws Exception {
78          LookupResults lookupResults = retrieveGlcpAllOriginEntries(glcpSearchResuiltsSequenceNumber);
79          if (lookupResults == null) {
80              return null;
81          }
82          List<OriginEntryFull> allOEs = (List<OriginEntryFull>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes()));
83          return allOEs;
84      }
85  
86      protected LookupResults retrieveGlcpAllOriginEntries(String glcpSearchResuiltsSequenceNumber) {
87          Map<String, String> criteria = new HashMap<String, String>();
88          criteria.put(OLEConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, glcpSearchResuiltsSequenceNumber);
89          LookupResults gaoe = (LookupResults) getBusinessObjectService().findByPrimaryKey(LookupResults.class, criteria);
90          return gaoe;
91      }
92  
93      /**
94       * Gets the businessObjectService attribute.
95       * 
96       * @return Returns the businessObjectService.
97       */
98      protected BusinessObjectService getBusinessObjectService() {
99          return businessObjectService;
100     }
101 
102     /**
103      * Sets the businessObjectService attribute value.
104      * 
105      * @param businessObjectService The businessObjectService to set.
106      */
107     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
108         this.businessObjectService = businessObjectService;
109     }
110 }
111