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.io.BufferedOutputStream;
019 import java.io.BufferedReader;
020 import java.io.BufferedWriter;
021 import java.io.File;
022 import java.io.FileNotFoundException;
023 import java.io.FileReader;
024 import java.io.FileWriter;
025 import java.io.IOException;
026 import java.io.PrintStream;
027 import java.sql.Date;
028 import java.util.ArrayList;
029 import java.util.Collection;
030 import java.util.HashMap;
031 import java.util.Iterator;
032 import java.util.List;
033 import java.util.Map;
034
035 import org.kuali.kfs.gl.GeneralLedgerConstants;
036 import org.kuali.kfs.gl.batch.service.impl.OriginEntryFileIterator;
037 import org.kuali.kfs.gl.businessobject.LedgerEntryForReporting;
038 import org.kuali.kfs.gl.businessobject.LedgerEntryHolder;
039 import org.kuali.kfs.gl.businessobject.OriginEntryFull;
040 import org.kuali.kfs.gl.businessobject.OriginEntryGroup;
041 import org.kuali.kfs.gl.businessobject.OriginEntryStatistics;
042 import org.kuali.kfs.gl.businessobject.PosterOutputSummaryEntry;
043 import org.kuali.kfs.gl.businessobject.Transaction;
044 import org.kuali.kfs.gl.dataaccess.OriginEntryDao;
045 import org.kuali.kfs.gl.service.OriginEntryGroupService;
046 import org.kuali.kfs.gl.service.OriginEntryService;
047 import org.kuali.kfs.sys.KFSConstants;
048 import org.kuali.kfs.sys.KFSPropertyConstants;
049 import org.kuali.kfs.sys.Message;
050 import org.kuali.rice.kns.service.DateTimeService;
051 import org.kuali.rice.kns.util.KualiDecimal;
052 import org.springframework.transaction.annotation.Transactional;
053
054 /**
055 * The base implementation of OriginEntryService
056 */
057 @Transactional
058 public class OriginEntryServiceImpl implements OriginEntryService {
059 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryServiceImpl.class);
060
061 private static final String ENTRY_GROUP_ID = "entryGroupId";
062 private static final String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
063 private static final String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
064
065 private OriginEntryGroupService originEntryGroupService;
066
067 private DateTimeService dateTimeService;
068 private String batchFileDirectoryName;
069
070 /**
071 * Sets the originEntryGroupService attribute
072 * @param originEntryGroupService the implementation of OriginEntryGroupService to set
073 */
074 public void setOriginEntryGroupService(OriginEntryGroupService originEntryGroupService) {
075 this.originEntryGroupService = originEntryGroupService;
076 }
077
078 /**
079 * Constructs a new instance of OriginEntryServiceImpl
080 */
081 public OriginEntryServiceImpl() {
082 super();
083 }
084
085 public void createEntry(OriginEntryFull originEntry, PrintStream ps) {
086 LOG.debug("createEntry() with PrintStream started");
087
088 try {
089 ps.printf("%s\n", originEntry.getLine());
090 } catch (Exception e) {
091 throw new RuntimeException(e.toString());
092 }
093
094 }
095
096 /**
097 * Given a collection of group ids, summarize the entries in each group.
098 * @param groupIdList a Collection of the ids of origin entry groups to summarize
099 * @return a LedgerEntryHolder with all of the summarized information
100 * @see org.kuali.kfs.gl.service.OriginEntryService#getSummaryByGroupId(Collection)
101 */
102
103 //TODO:- This method used for report. I will delete this method after all reports are done.
104 public LedgerEntryHolder getSummaryByGroupId(Collection groupIdList) {
105 LOG.debug("getSummaryByGroupId() started");
106
107 LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
108
109 if (groupIdList.size() == 0) {
110 return ledgerEntryHolder;
111 }
112
113 return ledgerEntryHolder;
114 }
115
116 /**
117 * Creates or updates a ledger entry with the array of information from the given entry summary object
118 *
119 * @param entrySummary a collection of java.lang.Objects, which is what OJB report queries return
120 * @return a LedgerEntry holding the given report summarization data
121 */
122 public static LedgerEntryForReporting buildLedgerEntry(Object[] entrySummary) {
123 // extract the data from an array and use them to populate a ledger entry
124 Object oFiscalYear = entrySummary[0];
125 Object oPeriodCode = entrySummary[1];
126 Object oBalanceType = entrySummary[2];
127 Object oOriginCode = entrySummary[3];
128 Object oDebitCreditCode = entrySummary[4];
129 Object oAmount = entrySummary[5];
130 Object oCount = entrySummary[6];
131
132 Integer fiscalYear = oFiscalYear != null ? new Integer(oFiscalYear.toString()) : null;
133 String periodCode = oPeriodCode != null ? oPeriodCode.toString() : GeneralLedgerConstants.getSpaceUniversityFiscalPeriodCode();
134 String balanceType = oBalanceType != null ? oBalanceType.toString() : GeneralLedgerConstants.getSpaceBalanceTypeCode();
135 String originCode = oOriginCode != null ? oOriginCode.toString() : GeneralLedgerConstants.getSpaceFinancialSystemOriginationCode();
136 String debitCreditCode = oDebitCreditCode != null ? oDebitCreditCode.toString() : GeneralLedgerConstants.getSpaceDebitCreditCode();
137 KualiDecimal amount = oAmount != null ? new KualiDecimal(oAmount.toString()) : KualiDecimal.ZERO;
138 int count = oCount != null ? Integer.parseInt(oCount.toString()) : 0;
139
140 // construct a ledger entry with the information fetched from the given array
141 LedgerEntryForReporting ledgerEntry = new LedgerEntryForReporting(fiscalYear, periodCode, balanceType, originCode);
142 if (KFSConstants.GL_CREDIT_CODE.equals(debitCreditCode)) {
143 ledgerEntry.setCreditAmount(amount);
144 ledgerEntry.setCreditCount(count);
145 }
146 else if (KFSConstants.GL_DEBIT_CODE.equals(debitCreditCode)) {
147 ledgerEntry.setDebitAmount(amount);
148 ledgerEntry.setDebitCount(count);
149 }
150 else {
151 ledgerEntry.setNoDCAmount(amount);
152 ledgerEntry.setNoDCCount(count);
153 }
154 ledgerEntry.setRecordCount(count);
155
156 return ledgerEntry;
157 }
158
159 /**
160 * This method writes origin entries into a file format. This particular implementation will use the OriginEntryFull.getLine
161 * method to generate the text for this file.
162 *
163 * @param entries An iterator of OriginEntries
164 * @param bw an opened, ready-for-output bufferedOutputStream.
165 * @see org.kuali.kfs.gl.service.OriginEntryService#flatFile(java.util.Iterator, java.io.BufferedOutputStream)
166 */
167 public void flatFile(Iterator<OriginEntryFull> entries, BufferedOutputStream bw) {
168 try {
169 while (entries.hasNext()) {
170 OriginEntryFull e = entries.next();
171 bw.write((e.getLine() + "\n").getBytes());
172 }
173 }
174 catch (IOException e) {
175 LOG.error("flatFile() Error writing to file", e);
176 throw new RuntimeException("Error writing to file: " + e.getMessage());
177 }
178 }
179
180 public Map getEntriesByGroupIdWithPath(String fileNameWithPath, List<OriginEntryFull> originEntryList) {
181
182 FileReader INPUT_GLE_FILE = null;
183 BufferedReader INPUT_GLE_FILE_br;
184 try {
185 INPUT_GLE_FILE = new FileReader(fileNameWithPath);
186 } catch (FileNotFoundException e) {
187 throw new RuntimeException(e);
188 }
189 INPUT_GLE_FILE_br = new BufferedReader(INPUT_GLE_FILE);
190
191 boolean loadError = false;
192 //returnErrorList is list of List<Message>
193 Map returnErrorMap = getEntriesByBufferedReader(INPUT_GLE_FILE_br, originEntryList);
194
195 try{
196 INPUT_GLE_FILE_br.close();
197 INPUT_GLE_FILE.close();
198 } catch (IOException e) {
199 throw new RuntimeException(e);
200 }
201
202 return returnErrorMap;
203 }
204
205 public Map getEntriesByBufferedReader(BufferedReader inputBufferedReader, List<OriginEntryFull> originEntryList) {
206 String line;
207 int lineNumber = 0;
208 Map returnErrorMap = new HashMap();
209 try {
210 List<Message> tmperrors = new ArrayList();
211 while ((line = inputBufferedReader.readLine()) != null) {
212 lineNumber++;
213 OriginEntryFull originEntry = new OriginEntryFull();
214 tmperrors = originEntry.setFromTextFileForBatch(line, lineNumber);
215 originEntry.setEntryId(lineNumber);
216 if (tmperrors.size() > 0){
217 returnErrorMap.put(new Integer(lineNumber), tmperrors);
218 } else {
219 originEntryList.add(originEntry);
220 }
221 }
222 } catch (IOException e) {
223 throw new RuntimeException(e);
224 }
225
226 return returnErrorMap;
227
228
229 }
230
231 /**
232 * get the summarized information of poster input entries that belong to the entry groups with the given group id list
233 *
234 * @param groupIdList the origin entry groups
235 * @return a map of summarized information of poster input entries within the specified groups
236 * @see org.kuali.kfs.gl.service.OriginEntryService#getPosterOutputSummaryByGroupId(java.util.Collection)
237 */
238
239 //TODO:- This method used for report. This method can be deleted after all reports are done.
240 public Map<String, PosterOutputSummaryEntry> getPosterOutputSummaryByGroupId(Collection groupIdList) {
241 LOG.debug("getPosterOutputSummaryByGroupId() started");
242
243 Map<String, PosterOutputSummaryEntry> output = new HashMap<String, PosterOutputSummaryEntry>();
244
245 if (groupIdList.size() == 0) {
246 return output;
247 }
248
249 return output;
250 }
251
252 public Integer getGroupCount(String fileNameWithPath){
253 File file = new File(fileNameWithPath);
254 Iterator<OriginEntryFull> fileIterator = new OriginEntryFileIterator(file);
255 int count = 0;
256
257 while(fileIterator.hasNext()){
258 count++;
259 fileIterator.next();
260 }
261 return count;
262 }
263
264 public void setDateTimeService(DateTimeService dateTimeService) {
265 this.dateTimeService = dateTimeService;
266 }
267
268 public void setBatchFileDirectoryName(String batchFileDirectoryName) {
269 this.batchFileDirectoryName = batchFileDirectoryName;
270 }
271 }