View Javadoc
1   package org.kuali.ole;
2   
3   import org.kuali.ole.deliver.bo.OlePatronDocument;
4   import org.kuali.ole.deliver.bo.OlePatronIngestSummaryRecord;
5   import org.kuali.ole.ingest.FileUtil;
6   import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
7   import org.kuali.ole.ingest.pojo.OlePatron;
8   import org.kuali.ole.service.OlePatronConverterService;
9   import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
10  import org.kuali.rice.core.api.impex.xml.XmlIngesterService;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.File;
14  import java.io.InputStream;
15  import java.util.*;
16  
17  /**
18   * Created by pvsubrah on 12/9/13.
19   */
20  public class PatronsIngesterService implements XmlIngesterService {
21      private OlePatronConverterService olePatronConverterService;
22      private FileUtil fileUtil;
23      private OlePatronXMLSchemaValidator olePatronXMLSchemaValidator;
24  
25      @Override
26      public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections) throws Exception {
27          List<XmlDocCollection> failedRecords = new ArrayList<>();
28  
29          for (Iterator<XmlDocCollection> iterator = xmlDocCollections.iterator(); iterator.hasNext(); ) {
30              StringBuffer reportContent = new StringBuffer();
31              XmlDocCollection xmlDocCollection = iterator.next();
32              File file = xmlDocCollection.getFile();
33              String fileContent = getFileUtil().readFile(file);
34  
35              InputStream inputStream = getByteArrayInputStream(fileContent);
36              //boolean validContent = getOlePatronXMLSchemaValidator().validateContentsAgainstSchema(inputStream);
37              Map validateResultMap = getOlePatronXMLSchemaValidator().validateContentsAgainstSchema(inputStream);
38              boolean validContent = (boolean)validateResultMap.get(OLEConstants.OlePatron.PATRON_XML_ISVALID);
39              String errorMessage = (String)validateResultMap.get(OLEConstants.OlePatron.PATRON_POLLERSERVICE_ERROR_MESSAGE);
40              OlePatronIngestSummaryRecord olePatronIngestSummaryRecord = getOlePatronIngestSummaryRecord();
41              if (validContent) {
42                  List<OlePatronDocument> successPatronDocuments = getOlePatronConverterService().persistPatronFromFileContent(fileContent, true, file.getName(), olePatronIngestSummaryRecord, null, "");
43                  for (OlePatronDocument olePatronDocument : successPatronDocuments) {
44                      buildReportContent(olePatronDocument.getOlePatronId(), file.getName(), reportContent, "Success", null);
45                  }
46                  for (OlePatron olePatron : olePatronIngestSummaryRecord.getFailurePatronRecords()) {
47                      buildReportContent(olePatron.getPatronID(), file.getName(), reportContent, "Failure", olePatron.getErrorMessage());
48                  }
49              } else {
50                  failedRecords.add(xmlDocCollection);
51                  buildReportContent(null, file.getName(), reportContent, "Failure", errorMessage);
52              }
53              if (olePatronIngestSummaryRecord.getPatronFailedCount() > 0) {
54                  failedRecords.add(xmlDocCollection);
55              }
56              if (xmlDocCollection.getXmlDocs() != null && xmlDocCollection.getXmlDocs().size() > 0) {
57                  xmlDocCollection.getXmlDocs().get(0).setProcessingMessage(reportContent.toString());
58              }
59          }
60          if (failedRecords.size() == 0) {
61              return new LinkedList<>();
62          } else {
63              return failedRecords;
64          }
65      }
66  
67      protected OlePatronIngestSummaryRecord getOlePatronIngestSummaryRecord() {
68          return new OlePatronIngestSummaryRecord();
69      }
70  
71      protected ByteArrayInputStream getByteArrayInputStream(String fileContent) {
72          return new ByteArrayInputStream(fileContent.getBytes());
73      }
74  
75      private OlePatronXMLSchemaValidator getOlePatronXMLSchemaValidator() {
76          if (null == olePatronXMLSchemaValidator) {
77              olePatronXMLSchemaValidator = new OlePatronXMLSchemaValidator();
78          }
79          return olePatronXMLSchemaValidator;
80      }
81  
82      public void setOlePatronXMLSchemaValidator(OlePatronXMLSchemaValidator olePatronXMLSchemaValidator) {
83          this.olePatronXMLSchemaValidator = olePatronXMLSchemaValidator;
84      }
85  
86      private FileUtil getFileUtil() {
87          if (null == fileUtil) {
88              fileUtil = new FileUtil();
89          }
90          return fileUtil;
91      }
92  
93      public void setFileUtil(FileUtil fileUtil) {
94          this.fileUtil = fileUtil;
95      }
96  
97      @Override
98      public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections, String principalId) throws Exception {
99          return null;
100     }
101 
102     public void setOlePatronConverterService(OlePatronConverterService olePatronConverterService) {
103         this.olePatronConverterService = olePatronConverterService;
104     }
105 
106     public OlePatronConverterService getOlePatronConverterService() {
107         if (null == olePatronConverterService) {
108             olePatronConverterService = new OlePatronConverterService();
109         }
110         return olePatronConverterService;
111     }
112 
113     private void buildReportContent(String patronId, String importFileName, StringBuffer reportContent, String importStatus, String errorMessage) {
114         reportContent.append(importStatus);
115         reportContent.append(OLEConstants.COMMA);
116         reportContent.append(OLEConstants.SPACE);
117         reportContent.append(importFileName);
118         if (patronId!=null){
119             reportContent.append(OLEConstants.COMMA);
120             reportContent.append(OLEConstants.SPACE);
121             reportContent.append(patronId);
122         }
123         if (errorMessage!=null){
124             reportContent.append(OLEConstants.COMMA);
125             reportContent.append(OLEConstants.SPACE);
126             reportContent.append(errorMessage);
127         }
128         reportContent.append("\n");
129     }
130 
131 }