001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.labor.util;
017:
018: import static org.kuali.module.gl.bo.OriginEntrySource.LABOR_BACKUP;
019: import static org.kuali.module.gl.bo.OriginEntrySource.LABOR_SCRUBBER_VALID;
020:
021: import java.sql.Date;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025: import java.util.Properties;
026:
027: import org.apache.commons.lang.ArrayUtils;
028: import org.apache.commons.lang.StringUtils;
029: import org.kuali.core.service.BusinessObjectService;
030: import org.kuali.kfs.KFSConstants;
031: import org.kuali.module.gl.bo.OriginEntryGroup;
032: import org.kuali.module.gl.web.TestDataGenerator;
033: import org.kuali.module.labor.bo.LaborLedgerPendingEntry;
034: import org.kuali.module.labor.bo.LaborOriginEntry;
035: import org.kuali.module.labor.service.LaborOriginEntryService;
036: import org.kuali.module.labor.service.impl.LaborScrubberProcess;
037: import org.kuali.module.labor.util.testobject.PendingLedgerEntryForTesting;
038:
039: public class TestDataLoader {
040: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
041: .getLogger(LaborScrubberProcess.class);
042:
043: private Properties properties;
044: private String fieldNames;
045: private String fieldLength;
046: private String deliminator;
047:
048: private List<String> keyFieldList;
049: private List<String> fieldLengthList;
050:
051: private BusinessObjectService businessObjectService;
052: private LaborOriginEntryService laborOriginEntryService;
053:
054: public TestDataLoader() {
055: String messageFileName = "test/src/org/kuali/module/labor/testdata/message.properties";
056: String propertiesFileName = "test/src/org/kuali/module/labor/testdata/laborTransaction.properties";
057:
058: properties = (new TestDataGenerator(propertiesFileName,
059: messageFileName)).getProperties();
060: fieldNames = properties.getProperty("fieldNames");
061: fieldLength = properties.getProperty("fieldLength");
062: deliminator = properties.getProperty("deliminator");
063:
064: LaborSpringContext.initializeApplicationContext();
065: keyFieldList = Arrays.asList(StringUtils.split(fieldNames,
066: deliminator));
067: fieldLengthList = Arrays.asList(StringUtils.split(fieldLength,
068: deliminator));
069: businessObjectService = LaborSpringContext
070: .getBean(BusinessObjectService.class);
071:
072: laborOriginEntryService = LaborSpringContext
073: .getBean(LaborOriginEntryService.class);
074: }
075:
076: public int loadTransactionIntoPendingEntryTable() {
077: int numberOfInputData = Integer.valueOf(properties
078: .getProperty("numOfData"));
079: int[] fieldLength = this .getFieldLength(fieldLengthList);
080: return this .loadInputData("data", numberOfInputData,
081: keyFieldList, fieldLength);
082: }
083:
084: public int loadTransactionIntoOriginEntryTable(
085: OriginEntryGroup group) {
086: int numberOfInputData = Integer.valueOf(properties
087: .getProperty("numOfData"));
088: businessObjectService.save(group);
089:
090: int[] fieldLength = this .getFieldLength(fieldLengthList);
091: List<LaborOriginEntry> originEntries = this .loadInputData(
092: LaborOriginEntry.class, "data", numberOfInputData,
093: keyFieldList, fieldLength);
094: for (LaborOriginEntry entry : originEntries) {
095: entry.setEntryGroupId(group.getId());
096: }
097:
098: businessObjectService.save(originEntries);
099: return originEntries.size();
100: }
101:
102: private int loadInputData(String propertyKeyPrefix,
103: int numberOfInputData, List<String> keyFieldList,
104: int[] fieldLength) {
105: int count = 0;
106: for (int i = 1; i <= numberOfInputData; i++) {
107: String propertyKey = propertyKeyPrefix + i;
108: PendingLedgerEntryForTesting inputData = new PendingLedgerEntryForTesting();
109: ObjectUtil.populateBusinessObject(inputData, properties,
110: propertyKey, fieldLength, keyFieldList);
111:
112: if (businessObjectService.countMatching(
113: LaborLedgerPendingEntry.class, inputData
114: .getPrimaryKeyMap()) <= 0) {
115: inputData
116: .setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
117: businessObjectService.save(inputData);
118: count++;
119: }
120: }
121: return count;
122: }
123:
124: private List loadInputData(Class clazz, String propertyKeyPrefix,
125: int numberOfInputData, List<String> keyFieldList,
126: int[] fieldLength) {
127: List inputDataList = new ArrayList();
128: for (int i = 1; i <= numberOfInputData; i++) {
129: String propertyKey = propertyKeyPrefix + i;
130: try {
131: Object inputData = clazz.newInstance();
132: ObjectUtil.populateBusinessObject(inputData,
133: properties, propertyKey, fieldLength,
134: keyFieldList);
135:
136: inputDataList.add(inputData);
137: } catch (Exception e) {
138: e.printStackTrace();
139: }
140: }
141: return inputDataList;
142: }
143:
144: private int[] getFieldLength(List<String> fieldLengthList) {
145: int[] fieldLengthArray = new int[fieldLengthList.size()];
146: for (int i = 0; i < fieldLengthArray.length; i++) {
147: fieldLengthArray[i] = Integer.valueOf(fieldLengthList
148: .get(i).trim());
149: }
150: return fieldLengthArray;
151: }
152:
153: public static void main(String[] args) {
154: TestDataLoader testDataLoader = new TestDataLoader();
155: Date groupCreationDate = new Date(0);
156:
157: if (ArrayUtils.isEmpty(args) || args.length < 2) {
158: System.out
159: .println("The program requires at least two arguments.");
160: return;
161: }
162:
163: if (!StringUtils.isAlphanumeric(args[0])) {
164: System.out
165: .println("The first argument should be a number.");
166: return;
167: }
168:
169: for (int numOfRound = Integer.parseInt(args[0]); numOfRound > 0; numOfRound--) {
170: if (ArrayUtils.contains(args, "poster")) {
171: OriginEntryGroup group = new OriginEntryGroup();
172: group.setSourceCode(LABOR_SCRUBBER_VALID);
173: group.setValid(true);
174: group.setScrub(false);
175: group.setProcess(true);
176: group.setDate(groupCreationDate);
177: int numOfData = testDataLoader
178: .loadTransactionIntoOriginEntryTable(group);
179: System.out
180: .println("Number of Origin Entries for Poster = "
181: + numOfData);
182: }
183:
184: if (ArrayUtils.contains(args, "scrubber")) {
185: OriginEntryGroup group = new OriginEntryGroup();
186: group.setSourceCode(LABOR_BACKUP);
187: group.setValid(true);
188: group.setScrub(true);
189: group.setProcess(true);
190: group.setDate(groupCreationDate);
191: int numOfData = testDataLoader
192: .loadTransactionIntoOriginEntryTable(group);
193: System.out
194: .println("Number of Origin Entries for Scrubber = "
195: + numOfData);
196: }
197:
198: if (ArrayUtils.contains(args, "pending")) {
199: int numOfData = testDataLoader
200: .loadTransactionIntoPendingEntryTable();
201: System.out.println("Number of Pending Entries = "
202: + numOfData);
203: }
204: }
205: System.exit(0);
206: }
207: }
|