1 package org.kuali.ole.sys.batch;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.File;
6 import java.io.InputStreamReader;
7
8 import org.apache.log4j.Logger;
9 import org.kuali.ole.gl.service.impl.StringHelper;
10 import org.kuali.ole.sys.context.SpringContext;
11 import org.kuali.ole.sys.exception.ParseException;
12 import org.kuali.rice.core.api.datetime.DateTimeService;
13
14
15
16
17 public class FlatFileParserBase extends BatchInputFileTypeBase {
18 protected static Logger LOG = Logger.getLogger(FlatFileParserBase.class);
19 protected FlatFileSpecification flatFileSpecification;
20 protected String fileNamePrefix;
21 protected DateTimeService dateTimeService;
22 protected String fileTypeIdentifier;
23 protected String titleKey;
24 protected FlatFileDataHandler processor;
25
26
27
28
29 @Override
30 public String getFileName(String principalName, Object parsedFileContents, String fileUserIdentifier) {
31 String fileName = processor.getFileName(principalName, parsedFileContents, fileUserIdentifier);
32 if(StringHelper.isNullOrEmpty(fileName)) {
33 fileName = getFileNamePrefix();
34 fileName += principalName;
35 if (org.apache.commons.lang.StringUtils.isNotBlank(fileUserIdentifier)) {
36 fileName += "_" + fileUserIdentifier;
37 }
38 fileName += "_" + dateTimeService.toDateTimeStringForFilename(dateTimeService.getCurrentDate());
39
40
41 fileName = org.apache.commons.lang.StringUtils.remove(fileName, " ");
42 }
43 return fileName;
44 }
45
46
47
48
49
50
51 public void setFileTypeIdentifer(String fileTypeIdentifier) {
52 this.fileTypeIdentifier = fileTypeIdentifier;
53 }
54
55
56
57
58
59
60 public void setFileTypeIdentifier(String fileTypeIdentifier) {
61 this.fileTypeIdentifier = fileTypeIdentifier;
62 }
63
64
65
66
67
68 public void setTitleKey(String titleKey) {
69 this.titleKey = titleKey;
70 }
71
72
73
74
75 public FlatFileDataHandler getProcessor() {
76 return processor;
77 }
78
79
80
81
82
83 public void setProcessor(FlatFileDataHandler processor) {
84 this.processor = processor;
85 }
86
87
88
89
90
91
92
93 @Override
94 public Object parse(byte[] fileByteContent) throws ParseException {
95 BufferedReader bufferedFileReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(fileByteContent)));
96 String lineToParse;
97 Object returnObject = null;
98 FlatFileParseTracker tracker = SpringContext.getBean(FlatFileParseTracker.class);
99 tracker.initialize(flatFileSpecification);
100 int lineNumber = 1;
101 try {
102 while ((lineToParse = bufferedFileReader.readLine()) != null) {
103 Object parseIntoObject = tracker.getObjectToParseInto(lineToParse);
104
105 if (parseIntoObject != null) {
106 FlatFileObjectSpecification parseSpecification = flatFileSpecification.getObjectSpecification(parseIntoObject.getClass());
107
108 if (parseSpecification.getParseProperties() != null && !parseSpecification.getParseProperties().isEmpty()) {
109 flatFileSpecification.parseLineIntoObject(parseSpecification, lineToParse, parseIntoObject, lineNumber);
110 tracker.completeLineParse();
111 }
112 }
113 lineNumber++;
114 }
115 returnObject = tracker.getParsedObjects();
116
117
118 }
119 catch (Exception e) {
120 LOG.error(e.getMessage() + " happend in parsing file content ", e);
121 throw new ParseException(e.getMessage());
122 }
123
124 return returnObject;
125 }
126
127
128
129
130 @Override
131 public void process(String fileName, Object parsedFileContents) {
132 if (getProcessor() != null)
133 getProcessor().process(fileName, parsedFileContents);
134 }
135
136
137
138
139 @Override
140 public boolean validate(Object parsedFileContents) {
141 if (getProcessor() == null)
142 return true;
143 return getProcessor().validate(parsedFileContents);
144 }
145
146
147
148
149
150 public void setFlatFileSpecification(AbstractFlatFileSpecificationBase flatFileClassIdentifier) {
151 this.flatFileSpecification = flatFileClassIdentifier;
152 }
153
154
155
156
157
158 public void setFileNamePrefix(String fileNamePrefix) {
159 this.fileNamePrefix = fileNamePrefix;
160 }
161
162
163
164
165
166 public void setDateTimeService(DateTimeService dateTimeService) {
167 this.dateTimeService = dateTimeService;
168 }
169
170
171
172
173
174
175 @Override
176 public String getAuthorPrincipalName(File file) {
177 return org.apache.commons.lang.StringUtils.substringBetween(file.getName(), getFileNamePrefix(), "_");
178 }
179
180
181
182
183 @Override
184 public String getFileTypeIdentifer() {
185 return fileTypeIdentifier;
186 }
187
188
189
190
191 public String getFileNamePrefix() {
192 return fileNamePrefix;
193 }
194
195
196
197
198 @Override
199 public String getTitleKey() {
200 return titleKey;
201 }
202 }