View Javadoc
1   /*
2    * Copyright 2005 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.io.BufferedOutputStream;
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.kuali.ole.gl.GeneralLedgerConstants;
33  import org.kuali.ole.gl.batch.service.impl.OriginEntryFileIterator;
34  import org.kuali.ole.gl.businessobject.LedgerEntryForReporting;
35  import org.kuali.ole.gl.businessobject.LedgerEntryHolder;
36  import org.kuali.ole.gl.businessobject.OriginEntryFull;
37  import org.kuali.ole.gl.businessobject.PosterOutputSummaryEntry;
38  import org.kuali.ole.gl.service.OriginEntryGroupService;
39  import org.kuali.ole.gl.service.OriginEntryService;
40  import org.kuali.ole.sys.OLEConstants;
41  import org.kuali.ole.sys.Message;
42  import org.kuali.rice.core.api.datetime.DateTimeService;
43  import org.kuali.rice.core.api.util.type.KualiDecimal;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  /**
47   * The base implementation of OriginEntryService
48   */
49  @Transactional
50  public class OriginEntryServiceImpl implements OriginEntryService {
51      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryServiceImpl.class);
52  
53      private static final String ENTRY_GROUP_ID = "entryGroupId";
54      private static final String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
55      private static final String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
56  
57      private OriginEntryGroupService originEntryGroupService;
58  
59      private DateTimeService dateTimeService;
60      private String batchFileDirectoryName;
61  
62      /**
63       * Sets the originEntryGroupService attribute
64       * @param originEntryGroupService the implementation of OriginEntryGroupService to set
65       */
66      public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
67          this.originEntryGroupService = originEntryGroupService;
68      }
69  
70      /**
71       * Constructs a new instance of OriginEntryServiceImpl
72       */
73      public OriginEntryServiceImpl() {
74          super();
75      }
76      
77      public void createEntry(OriginEntryFull originEntry, PrintStream ps) {
78          LOG.debug("createEntry() with PrintStream started");   
79          
80          try {
81              ps.printf("%s\n", originEntry.getLine());
82          } catch (Exception e) {
83              throw new RuntimeException(e.toString());
84          }
85          
86      }
87  
88      /**
89       * Given a collection of group ids, summarize the entries in each group.
90       * @param groupIdList a Collection of the ids of origin entry groups to summarize
91       * @return a LedgerEntryHolder with all of the summarized information
92       * @see org.kuali.ole.gl.service.OriginEntryService#getSummaryByGroupId(Collection)
93       */
94      
95      //TODO:- This method used for report. I will delete this method after all reports are done.
96      public LedgerEntryHolder getSummaryByGroupId(Collection groupIdList) {
97          LOG.debug("getSummaryByGroupId() started");
98  
99          LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
100 
101         if (groupIdList.size() == 0) {
102             return ledgerEntryHolder;
103         }
104 
105         return ledgerEntryHolder;
106     }
107 
108     /**
109      * Creates or updates a ledger entry with the array of information from the given entry summary object
110      * 
111      * @param entrySummary a collection of java.lang.Objects, which is what OJB report queries return
112      * @return a LedgerEntry holding the given report summarization data
113      */
114     public static LedgerEntryForReporting buildLedgerEntry(Object[] entrySummary) {
115         // extract the data from an array and use them to populate a ledger entry
116         Object oFiscalYear = entrySummary[0];
117         Object oPeriodCode = entrySummary[1];
118         Object oBalanceType = entrySummary[2];
119         Object oOriginCode = entrySummary[3];
120         Object oDebitCreditCode = entrySummary[4];
121         Object oAmount = entrySummary[5];
122         Object oCount = entrySummary[6];
123 
124         Integer fiscalYear = oFiscalYear != null ? new Integer(oFiscalYear.toString()) : null;
125         String periodCode = oPeriodCode != null ? oPeriodCode.toString() : GeneralLedgerConstants.getSpaceUniversityFiscalPeriodCode();
126         String balanceType = oBalanceType != null ? oBalanceType.toString() : GeneralLedgerConstants.getSpaceBalanceTypeCode();
127         String originCode = oOriginCode != null ? oOriginCode.toString() : GeneralLedgerConstants.getSpaceFinancialSystemOriginationCode();
128         String debitCreditCode = oDebitCreditCode != null ? oDebitCreditCode.toString() : GeneralLedgerConstants.getSpaceDebitCreditCode();
129         KualiDecimal amount = oAmount != null ? new KualiDecimal(oAmount.toString()) : KualiDecimal.ZERO;
130         int count = oCount != null ? Integer.parseInt(oCount.toString()) : 0;
131 
132         // construct a ledger entry with the information fetched from the given array
133         LedgerEntryForReporting ledgerEntry = new LedgerEntryForReporting(fiscalYear, periodCode, balanceType, originCode);
134         if (OLEConstants.GL_CREDIT_CODE.equals(debitCreditCode)) {
135             ledgerEntry.setCreditAmount(amount);
136             ledgerEntry.setCreditCount(count);
137         }
138         else if (OLEConstants.GL_DEBIT_CODE.equals(debitCreditCode)) {
139             ledgerEntry.setDebitAmount(amount);
140             ledgerEntry.setDebitCount(count);
141         }
142         else {
143             ledgerEntry.setNoDCAmount(amount);
144             ledgerEntry.setNoDCCount(count);
145         }
146         ledgerEntry.setRecordCount(count);
147 
148         return ledgerEntry;
149     }
150 
151     /**
152      * This method writes origin entries into a file format. This particular implementation will use the OriginEntryFull.getLine
153      * method to generate the text for this file.
154      * 
155      * @param entries An iterator of OriginEntries
156      * @param bw an opened, ready-for-output bufferedOutputStream.
157      * @see org.kuali.ole.gl.service.OriginEntryService#flatFile(java.util.Iterator, java.io.BufferedOutputStream)
158      */
159     public void flatFile(Iterator<OriginEntryFull> entries, BufferedOutputStream bw) {
160         try {
161             while (entries.hasNext()) {
162                 OriginEntryFull e = entries.next();
163                 bw.write((e.getLine() + "\n").getBytes());
164             }
165         }
166         catch (IOException e) {
167             LOG.error("flatFile() Error writing to file", e);
168             throw new RuntimeException("Error writing to file: " + e.getMessage());
169         }
170     }
171     
172     public  Map getEntriesByGroupIdWithPath(String fileNameWithPath, List<OriginEntryFull> originEntryList) {
173         
174         FileReader INPUT_GLE_FILE = null;
175         BufferedReader INPUT_GLE_FILE_br;
176         try {
177             INPUT_GLE_FILE = new FileReader(fileNameWithPath);
178         } catch (FileNotFoundException e) {
179             throw new RuntimeException(e);
180         }
181         INPUT_GLE_FILE_br = new BufferedReader(INPUT_GLE_FILE);
182         
183         boolean loadError = false;
184         //returnErrorList is list of List<Message>
185         Map returnMessageMap = getEntriesByBufferedReader(INPUT_GLE_FILE_br, originEntryList);
186 
187         try{
188             INPUT_GLE_FILE_br.close();
189             INPUT_GLE_FILE.close();
190         } catch (IOException e) {
191             throw new RuntimeException(e);
192         }
193         
194         return returnMessageMap;
195     }
196     
197     public Map getEntriesByBufferedReader(BufferedReader inputBufferedReader, List<OriginEntryFull> originEntryList) {
198         String line;
199         int lineNumber = 0;
200         Map returnMessageMap = new HashMap();
201         try {
202             List<Message> tmperrors = new ArrayList();    
203             while ((line = inputBufferedReader.readLine()) != null) {
204                 lineNumber++;
205                 OriginEntryFull originEntry = new OriginEntryFull();
206                 tmperrors = originEntry.setFromTextFileForBatch(line, lineNumber);
207                 originEntry.setEntryId(lineNumber);
208                 if (tmperrors.size() > 0){
209                     returnMessageMap.put(new Integer(lineNumber), tmperrors);
210                 } else {
211                     originEntryList.add(originEntry);
212                 }
213             }
214         } catch (IOException e) {
215             throw new RuntimeException(e);
216         }
217             
218         return returnMessageMap;
219 
220         
221     }
222 
223     /**
224      * get the summarized information of poster input entries that belong to the entry groups with the given group id list
225      * 
226      * @param groupIdList the origin entry groups
227      * @return a map of summarized information of poster input entries within the specified groups
228      * @see org.kuali.ole.gl.service.OriginEntryService#getPosterOutputSummaryByGroupId(java.util.Collection)
229      */
230     
231     //TODO:- This method used for report. This method can be deleted after all reports are done.
232     public Map<String, PosterOutputSummaryEntry> getPosterOutputSummaryByGroupId(Collection groupIdList) {
233         LOG.debug("getPosterOutputSummaryByGroupId() started");
234 
235         Map<String, PosterOutputSummaryEntry> output = new HashMap<String, PosterOutputSummaryEntry>();
236 
237         if (groupIdList.size() == 0) {
238             return output;
239         }
240 
241         return output;
242     }
243 
244     public Integer getGroupCount(String fileNameWithPath){
245         File file = new File(fileNameWithPath);
246         Iterator<OriginEntryFull> fileIterator = new OriginEntryFileIterator(file);
247         int count = 0;
248         
249         while(fileIterator.hasNext()){
250             count++;
251             fileIterator.next();
252         }
253         return count;
254     }
255 
256     public void setDateTimeService(DateTimeService dateTimeService) {
257         this.dateTimeService = dateTimeService;
258     }
259 
260     public void setBatchFileDirectoryName(String batchFileDirectoryName) {
261         this.batchFileDirectoryName = batchFileDirectoryName;
262     }
263 }