1 package org.kuali.ole.batch.ingest;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.batch.impl.AbstractBatchProcess;
6 import org.kuali.ole.batch.service.BatchProcessDeleteService;
7 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
8 import org.marc4j.*;
9 import org.marc4j.marc.Record;
10 import org.marc4j.marc.impl.DataFieldImpl;
11
12 import java.io.*;
13 import java.util.*;
14
15
16
17
18
19
20
21
22 public class BatchProcessDeleteData extends AbstractBatchProcess {
23
24 private static final Logger LOG = Logger.getLogger(BatchProcessDeleteData.class);
25 private BatchProcessDeleteService batchProcessDeleteService;
26
27 private List docBibIdsList;
28 private List deleteChunkBibIdsList = new ArrayList(0);
29 private int chunkCount = 0;
30 private int totalCount = 0;
31 private int failureCount = 0;
32 private StringBuffer misMatchMarcRecords = new StringBuffer("");
33 private StringBuffer matchMarcRecords = new StringBuffer("");
34 private StringBuffer failureReport = new StringBuffer("");
35 private String fileType = "mrc";
36
37 private BatchProcessDeleteService getBatchProcessDeleteService() {
38 if (batchProcessDeleteService == null)
39 batchProcessDeleteService = GlobalResourceLoader.getService("batchDeleteServiceImpl");
40 return batchProcessDeleteService;
41 }
42
43 @Override
44 protected void prepareForRead() throws Exception {
45
46 String fileContent = getBatchProcessFileContent();
47 job.setNoOfSuccessRecords("0");
48 job.setNoOfFailureRecords("0");
49 job.setNoOfRecordsProcessed("0");
50 if (job.getUploadFileName() != null && job.getUploadFileName().contains(".txt")) {
51 fileType = "txt";
52 }
53 docBibIdsList = getBatchDeleteBibIdsList(fileContent);
54 job.setTotalNoOfRecords(totalCount + "");
55
56 }
57
58 @Override
59 protected void prepareForWrite() throws Exception {
60 if (docBibIdsList != null && docBibIdsList.size() > 0) {
61 if (processDef.getChunkSize() == 1000) {
62 String profileField = getProfileFiled();
63 if (profileField != null) {
64 int sucRecordCount = getBatchProcessDeleteService().performBatchDelete(docBibIdsList, profileField);
65 deleteBatchFile();
66 createBatchFailureFile();
67 job.setNoOfRecordsProcessed(totalCount + "");
68 job.setNoOfSuccessRecords((totalCount - failureCount) + "");
69 job.setNoOfFailureRecords(failureCount + "");
70 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
71 }
72 }
73 } else {
74 job.setNoOfRecordsProcessed(totalCount + "");
75 job.setNoOfSuccessRecords((totalCount - failureCount) + "");
76 job.setNoOfFailureRecords(failureCount + "");
77 deleteBatchFile();
78 createBatchFailureFile();
79 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
80 }
81
82
83 }
84
85 @Override
86 protected void getNextBatch() throws Exception {
87 if (chunkCount < docBibIdsList.size()) {
88 if (chunkCount + processDef.getChunkSize() < docBibIdsList.size()) {
89 deleteChunkBibIdsList = docBibIdsList.subList(chunkCount, chunkCount + processDef.getChunkSize());
90 chunkCount += processDef.getChunkSize();
91 } else {
92 deleteChunkBibIdsList = docBibIdsList.subList(chunkCount, docBibIdsList.size());
93 performBatchDelete();
94 chunkCount += processDef.getChunkSize();
95 deleteBatchFile();
96 createBatchFailureFile();
97 job.setStatus(OLEConstants.OLEBatchProcess.JOB_STATUS_COMPLETED);
98 }
99
100 }
101
102 }
103
104 @Override
105 protected void processBatch() throws Exception {
106
107 if (deleteChunkBibIdsList != null && deleteChunkBibIdsList.size() > 0) {
108
109 performBatchDelete();
110
111 }
112 }
113
114
115
116
117
118 private String getProfileFiled() {
119 if (processDef.getBatchProcessProfileBo() != null && processDef.getBatchProcessProfileBo().getOleBatchProcessProfileBibMatchPointList() != null
120 && processDef.getBatchProcessProfileBo().getOleBatchProcessProfileBibMatchPointList().size() > 0) {
121 String profileField = processDef.getBatchProcessProfileBo().getOleBatchProcessProfileBibMatchPointList().get(0).getOleBibMatchPoint();
122 return profileField;
123 }
124 return null;
125 }
126
127
128
129
130
131
132
133 public List getBatchDeleteBibIdsList(String fileContent) throws Exception {
134
135 String profileField = getProfileFiled();
136 List matchBibIdsList = new ArrayList(0);
137 totalCount = 0;
138 if (profileField != null) {
139 String profileDataFiled = profileField.substring(0, 3);
140 char profileSubField = ' ';
141 if(profileField!=null && profileField.contains("$")) {
142 profileSubField = profileField.substring(profileField.lastIndexOf('$') + 1).toCharArray()[0];
143 } else if (profileField != null && profileField.length() > 3) {
144 profileSubField = profileField.substring(3).toCharArray()[0];
145 }
146 String profileFieldQuery = profileDataFiled;
147 if (profileField != null && profileField.length() > 3) {
148 profileFieldQuery = profileFieldQuery + profileSubField;
149 profileFieldQuery = profileFieldQuery.trim();
150 }
151 try {
152
153 if ("mrc".equals(fileType)) {
154 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileContent.getBytes());
155 ByteArrayOutputStream failureByteArrayOutputStream = new ByteArrayOutputStream();
156 ByteArrayOutputStream successByteArrayOutputStream = new ByteArrayOutputStream();
157 MarcReader reader = new MarcStreamReader(byteArrayInputStream);
158 MarcWriter failureWriter = new MarcStreamWriter(failureByteArrayOutputStream);
159 MarcWriter successWriter = new MarcStreamWriter(successByteArrayOutputStream);
160 while (reader.hasNext()) {
161 Record record = reader.next();
162 totalCount++;
163 boolean recExistFlag = true;
164 for (DataFieldImpl dataFieldImpl : (List<DataFieldImpl>) record.getDataFields()) {
165 String searchMrcFieldData = "";
166 if (profileDataFiled.equals(dataFieldImpl.getTag())) {
167 searchMrcFieldData = dataFieldImpl.getSubfield(profileSubField).getData().replaceAll(":", "");
168 Map batchDeleteMap = getBatchProcessDeleteService().getBibIdsForBatchDelete(searchMrcFieldData, profileFieldQuery);
169 List bibIdList = (List) batchDeleteMap.get(OLEConstants.BIB_SEARCH);
170 if (bibIdList == null || (bibIdList != null && bibIdList.size() > 1)) {
171 failureCount++;
172 failureWriter.write(record);
173 } else {
174 matchBibIdsList.addAll(bibIdList);
175 successWriter.write(record);
176 }
177 String failureInfo = (String) batchDeleteMap.get(OLEConstants.OLEBatchProcess.FAILURE_INFO);
178 if (failureInfo != null) {
179 failureReport.append(searchMrcFieldData + " (" + profileField + ") :: " + failureInfo + "\n");
180 }
181 recExistFlag = false;
182 break;
183 }
184 }
185
186 if (recExistFlag) {
187 failureCount++;
188 failureWriter.write(record);
189 }
190
191 }
192 misMatchMarcRecords.append(failureByteArrayOutputStream.toString());
193 matchMarcRecords.append(successByteArrayOutputStream.toString());
194 failureWriter.close();
195 successWriter.close();
196 }
197
198 else if ("txt".equals(fileType)) {
199 List txtFileDataList = new ArrayList();
200 BufferedReader br = new BufferedReader(new StringReader(fileContent));
201 StringBuilder sb = new StringBuilder();
202 String line = br.readLine();
203 while (line != null) {
204 totalCount++;
205 if (!txtFileDataList.contains(line)) {
206 txtFileDataList.add(line);
207 Map batchDeleteMap = getBatchProcessDeleteService().getBibIdsForBatchDelete(line, profileFieldQuery);
208 List bibIdList = (List) batchDeleteMap.get("bibIdentifier");
209 if (bibIdList == null || (bibIdList != null && bibIdList.size() > 1)) {
210 failureCount++;
211 misMatchMarcRecords.append(line.toString() + "\n");
212 } else {
213 matchBibIdsList.addAll(bibIdList);
214 matchMarcRecords.append(line.toString() + "\n");
215 }
216 String failureInfo = (String) batchDeleteMap.get("failureInfo");
217 if (failureInfo != null) {
218 failureReport.append(line + " (" + profileField + ") :: " + failureInfo + "\n");
219 }
220 } else {
221 failureCount++;
222 misMatchMarcRecords.append(line.toString() + "\n");
223 }
224 line = br.readLine();
225 }
226 }
227 } catch (Exception e) {
228 LOG.error("exception : " + e.getMessage());
229 }
230 }
231 return matchBibIdsList;
232 }
233
234
235
236
237
238 public void performBatchDelete() throws Exception {
239 String profileField = getProfileFiled();
240 if (profileField != null) {
241
242 int sucRecordCount = getBatchProcessDeleteService().performBatchDelete(deleteChunkBibIdsList, profileField);
243
244 job.setNoOfSuccessRecords((Integer.parseInt(job.getNoOfSuccessRecords()) + sucRecordCount) + "");
245 job.setNoOfFailureRecords(totalCount - (Integer.parseInt(job.getNoOfSuccessRecords())) + "");
246 job.setNoOfRecordsProcessed((Integer.parseInt(job.getNoOfRecordsProcessed()) + deleteChunkBibIdsList.size()) + "");
247 }
248 }
249
250
251
252
253
254 public void createBatchFailureFile() throws Exception {
255
256 if (!"".equals(misMatchMarcRecords.toString())) {
257 createBatchFailureFile(misMatchMarcRecords.toString());
258 }
259 if (!"".equals(matchMarcRecords.toString())) {
260 createBatchSuccessFile(matchMarcRecords.toString());
261 }
262 if (!"".equals(failureReport.toString())) {
263 createBatchDeleteFailureReportFile(failureReport.toString());
264 }
265
266 }
267
268
269 }