001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.gl.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.codec.binary.Base64;
023    import org.kuali.kfs.gl.businessobject.OriginEntryFull;
024    import org.kuali.kfs.gl.service.GlCorrectionProcessOriginEntryService;
025    import org.kuali.kfs.sys.KFSConstants;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.rice.kns.bo.LookupResults;
028    import org.kuali.rice.kns.service.BusinessObjectService;
029    import org.kuali.rice.kns.service.DateTimeService;
030    import org.kuali.rice.kns.util.GlobalVariables;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    import org.springframework.transaction.annotation.Transactional;
033    
034    /**
035     * This implementation of GlCorrectionProcessOriginEntryService uses the database to temporarily store lists of origin entries.
036     * While this implementation does not clear out persisted origin entries, the batch job defined using the
037     * org.kuali.kfs.sys.batch.PurgeOldLookupResultsStep class may cause the purging of origin entries persisted with this implementation.
038     * 
039     * @see GlCorrectionProcessOriginEntryService
040     */
041    @Transactional
042    public class GlCorrectionProcessOriginEntryServiceImpl implements GlCorrectionProcessOriginEntryService {
043    
044        private BusinessObjectService businessObjectService;
045    
046        /**
047         * Persists the origin entries under a given sequence number. If entries are persisted again under the same sequence number,
048         * then they will be overridden.
049         * 
050         * @param glcpSearchResuiltsSequenceNumber a sequence number
051         * @param allEntries a list of origin entries
052         * @throws Exception thrown if anything goes wrong
053         * @see org.kuali.kfs.gl.service.GlCorrectionProcessOriginEntryService#persistAllEntries(java.lang.String, java.util.List)
054         */
055        public void persistAllEntries(String glcpSearchResuiltsSequenceNumber, List<OriginEntryFull> allEntries) throws Exception {
056            String serializedOriginEntries = new String(Base64.encodeBase64(ObjectUtils.toByteArray(allEntries)));
057    
058            LookupResults lookupResults = retrieveGlcpAllOriginEntries(glcpSearchResuiltsSequenceNumber);
059            if (lookupResults == null) {
060                lookupResults = new LookupResults();
061                lookupResults.setLookupResultsSequenceNumber(glcpSearchResuiltsSequenceNumber);
062                lookupResults.setLookupPersonId(GlobalVariables.getUserSession().getPerson().getPrincipalId());
063            }
064            lookupResults.setLookupDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
065            lookupResults.setSerializedLookupResults(serializedOriginEntries);
066            businessObjectService.save(lookupResults);
067        }
068    
069        /**
070         * Retrieves the origin entries stored under the given sequence number
071         * 
072         * @param glcpSearchResuiltsSequenceNumber a sequence number
073         * @return a list of origin entries, or null if no results are currently not in the system.
074         * @throws Exception thrown if something goes wrong - vague documentation for a vague exception
075         * @see org.kuali.kfs.gl.service.GlCorrectionProcessOriginEntryService#retrieveAllEntries(java.lang.String)
076         */
077        public List<OriginEntryFull> retrieveAllEntries(String glcpSearchResuiltsSequenceNumber) throws Exception {
078            LookupResults lookupResults = retrieveGlcpAllOriginEntries(glcpSearchResuiltsSequenceNumber);
079            if (lookupResults == null) {
080                return null;
081            }
082            List<OriginEntryFull> allOEs = (List<OriginEntryFull>) ObjectUtils.fromByteArray(Base64.decodeBase64(lookupResults.getSerializedLookupResults().getBytes()));
083            return allOEs;
084        }
085    
086        protected LookupResults retrieveGlcpAllOriginEntries(String glcpSearchResuiltsSequenceNumber) {
087            Map<String, String> criteria = new HashMap<String, String>();
088            criteria.put(KFSConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER, glcpSearchResuiltsSequenceNumber);
089            LookupResults gaoe = (LookupResults) getBusinessObjectService().findByPrimaryKey(LookupResults.class, criteria);
090            return gaoe;
091        }
092    
093        /**
094         * Gets the businessObjectService attribute.
095         * 
096         * @return Returns the businessObjectService.
097         */
098        protected BusinessObjectService getBusinessObjectService() {
099            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