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: /*
017: * Created on Aug 6, 2004
018: *
019: */
020: package org.kuali.module.pdp.batch;
021:
022: import java.io.FileNotFoundException;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.PrintStream;
027: import java.math.BigDecimal;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Properties;
031:
032: import org.apache.log4j.BasicConfigurator;
033: import org.apache.log4j.PropertyConfigurator;
034: import org.kuali.core.bo.user.UniversalUser;
035: import org.kuali.core.exceptions.UserNotFoundException;
036: import org.kuali.core.service.UniversalUserService;
037: import org.kuali.kfs.context.SpringContext;
038: import org.kuali.module.pdp.bo.PdpUser;
039: import org.kuali.module.pdp.exception.PaymentLoadException;
040: import org.kuali.module.pdp.service.LoadPaymentStatus;
041: import org.kuali.module.pdp.service.PaymentFileService;
042: import org.omg.CORBA.UnknownUserException;
043:
044: /**
045: * @author jsissom This will be used in batch to load a payment file. This need the following system properties set for this to
046: * work: jdbc.driver= jdbc database driver (oracle.jdbc.driver.OracleDriver) jdbc.url= jdbc database url
047: * (jdbc:oracle:thin:@es01.uits.indiana.edu:1521:GEN2DEV) jdbc.username= jdbc database username (pdp_proxy) jdbc.password=
048: * jdbc database password (xxxxxx) gds.ldapUrl= EDS url (ldap://eds-stage.iu.edu:636) gds.password= EDS password (xxxxxx)
049: * gds.username= EDS username (IU-UITS-UIS-APP-PDP) mail.username= Mail username mail.password= Mail password
050: */
051: public class LoadPayments {
052: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
053: .getLogger(LoadPayments.class);
054:
055: public LoadPayments() {
056: }
057:
058: private void startLog4j() {
059: if ("debug".equals(System.getProperty("logger"))) {
060: BasicConfigurator.configure();
061: } else {
062: try {
063: // Load log4j.properties into properties object
064: Properties props = new Properties();
065: InputStream is = this .getClass().getResourceAsStream(
066: "/edu/iu/uis/pdp/batch/log4j.properties");
067: props.load(is);
068: is.close();
069:
070: PropertyConfigurator.configure(props);
071: } catch (IOException e) {
072: System.err.println("Unable to start log4j");
073: }
074: }
075: }
076:
077: public static void main(String[] args) {
078: if (args.length != 1) {
079: LOG.error("main() Must have 1 argument");
080: System.exit(8);
081: }
082:
083: LoadPayments lp = new LoadPayments();
084: String dir = null;
085: int returnCode = 8;
086:
087: try {
088: // Start log4j
089: lp.startLog4j();
090:
091: // This code isn't currently being used. It will be updated at some point
092:
093: // TODO abyrne - not sure what we're going to do about this, but i commented out for now, since the file pointed to
094: // doesn't exist
095: // ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/edu/iu/uis/pdp/batch/batchContext.xml");
096: // lp.beanFactory = ctx;
097: // end TODO abyrne
098:
099: // Get the batch output directory
100: // dir = SpringContext.getBean(ParameterService.class).getParameterValue(ParameterConstants.PRE_DISBURSEMENT_ALL.class,
101: // PdpConstants.ApplicationParameterKeys.BATCH_OUTPUT_DIR);
102:
103: // Load the payment
104: LoadPaymentStatus status = lp.loadPaymentFile(args[0]);
105:
106: // Create the output file
107: if (!lp.createOutputFile(
108: lp.getOutputFileName(args[0], dir), "SUCCESS",
109: "Successful Load", status.getDetailCount(), status
110: .getDetailTotal(), status.getWarnings(),
111: args[0])) {
112: System.out.println("Unable to create output file");
113: returnCode = 8;
114: } else {
115: returnCode = 0;
116: }
117: } catch (UnknownUserException e) {
118: LOG.error("main() GDS Account error", e);
119: String msg = e.getMessage() == null ? "" : e.getMessage();
120: lp.createOutputFile(lp.getOutputFileName(args[0], dir),
121: "FAIL", "User Account Invalid " + msg, 0, null,
122: null, args[0]);
123: returnCode = 8;
124: } catch (PaymentLoadException e) {
125: LOG.error("main() Error loading payments", e);
126: String msg = e.getMessage() == null ? "" : e.getMessage();
127: if (!lp.createOutputFile(
128: lp.getOutputFileName(args[0], dir), "FAIL",
129: "Load Failed " + msg, 0, null, e.getErrors(),
130: args[0])) {
131: returnCode = 8;
132: } else {
133: returnCode = 0;
134: }
135: } catch (Throwable e) {
136: LOG.error("loadPaymentFile() Unknown Exception", e);
137: String msg = e.getMessage() == null ? "" : e.getMessage();
138: lp.createOutputFile(lp.getOutputFileName(args[0], dir),
139: "FAIL", "Load Failed " + msg, 0, null, null,
140: args[0]);
141: returnCode = 8;
142: } finally {
143: try {
144: lp.createDoneFile(lp.getDoneFileName(args[0], dir));
145: } catch (Throwable ex) {
146: LOG
147: .error(
148: "main() createDoneFile createDoneFile throws exception",
149: ex);
150: returnCode = 8;
151: }
152: System.exit(returnCode);
153: }
154: }
155:
156: // Sample output file
157: // <pdp_load_status>
158: // <input_file_name>filename</input_file_name>
159: // <code>CODE</code>
160: // <description>MESSAGE</description>
161: // <count>count</count>
162: // <total>total</total>
163: // <messages>
164: // <message>...</message>
165: // <message>...</message>
166: // </messages>
167: // </pdp_load_status>
168:
169: public boolean createOutputFile(String filename, String code,
170: String message, int count, BigDecimal total, List messages,
171: String inputFileName) {
172: FileOutputStream out;
173: PrintStream p;
174:
175: try {
176: out = new FileOutputStream(filename);
177: p = new PrintStream(out);
178:
179: p.println("<pdp_load_status>");
180: p.println(" <input_file_name>" + inputFileName
181: + "</input_file_name>");
182: p.println(" <code>" + code + "</code>");
183: if ("SUCCESS".equals(code)) {
184: p.println(" <count>" + count + "</count>");
185: p.println(" <total>" + total + "</total>");
186: } else {
187: p.println(" <count>0</count>");
188: p.println(" <total>0</total>");
189: }
190: p.println(" <description>" + message + "</description>");
191: if (messages != null) {
192: p.println(" <messages>");
193: for (Iterator iter = messages.iterator(); iter
194: .hasNext();) {
195: String element = (String) iter.next();
196: p.println(" <message>" + element + "</message>");
197: }
198: p.println(" </messages>");
199: }
200: p.println("</pdp_load_status>");
201:
202: p.close();
203: out.close();
204: return true;
205: } catch (FileNotFoundException e) {
206: LOG
207: .error(
208: "createOutputFile() Cannot create output file",
209: e);
210: return false;
211: } catch (IOException e) {
212: LOG.error("createOutputFile() Cannot write to output file",
213: e);
214: return false;
215: }
216: }
217:
218: public boolean createDoneFile(String filename) {
219: FileOutputStream out;
220: PrintStream p;
221:
222: try {
223: out = new FileOutputStream(filename);
224: p = new PrintStream(out);
225:
226: p.println("Programmers rule the world");
227:
228: p.println("This file intentionally left blank");
229:
230: p.println("Why are you looking in this file?");
231:
232: p.close();
233: out.close();
234: return true;
235: } catch (FileNotFoundException e) {
236: LOG.error("createDoneFile() Cannot create done file", e);
237: return false;
238: } catch (IOException e) {
239: LOG.error("createDoneFile() Cannot write to done file", e);
240: return false;
241: }
242: }
243:
244: public LoadPaymentStatus loadPaymentFile(String filename)
245: throws UnknownUserException, PaymentLoadException {
246: // Get the user
247: UniversalUserService us = SpringContext
248: .getBean(UniversalUserService.class);
249: PdpUser pUser = null;
250: try {
251: // TODO Fix hard coded
252: UniversalUser jay = us
253: .getUniversalUserByAuthenticationUserId("jsissom");
254: pUser = new PdpUser(jay);
255: } catch (UserNotFoundException u) {
256: throw new UnknownUserException();
257: }
258:
259: // Load the file
260: PaymentFileService pfs = SpringContext
261: .getBean(PaymentFileService.class);
262: LoadPaymentStatus status = pfs.loadPayments(filename, pUser);
263: System.out.println("Warnings:");
264: for (Iterator iter = status.getWarnings().iterator(); iter
265: .hasNext();) {
266: System.out.println(iter.next());
267: }
268: return status;
269: }
270:
271: // Add the output directory to the base filename of the input file
272:
273: // NOTE: This only works on Unix right now.
274: private String getOutputFileName(String filename, String outputDir) {
275: StringBuffer sb = new StringBuffer(outputDir);
276: sb.append("/");
277:
278: // Trim off any directories in the filename, leaving the base name.
279: int startingPointer = filename.length() - 1;
280: while ((startingPointer > 0)
281: && (filename.charAt(startingPointer) != '/')) {
282: startingPointer--;
283: }
284: sb.append(filename.substring(startingPointer));
285:
286: return sb.toString();
287: }
288:
289: // NOTE: This only works on Unix right now.
290: private String getDoneFileName(String filename, String outputDir) {
291: StringBuffer sb = new StringBuffer(getOutputFileName(filename,
292: outputDir));
293: int x = sb.lastIndexOf(".");
294: sb = new StringBuffer(sb.substring(0, x + 1));
295: sb.append("done");
296:
297: return sb.toString();
298: }
299: }
|