001package org.kuali.ole;
002
003import org.kuali.ole.deliver.bo.OlePatronDocument;
004import org.kuali.ole.deliver.bo.OlePatronIngestSummaryRecord;
005import org.kuali.ole.ingest.FileUtil;
006import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
007import org.kuali.ole.ingest.pojo.OlePatron;
008import org.kuali.ole.service.OlePatronConverterService;
009import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
010import org.kuali.rice.core.api.impex.xml.XmlIngesterService;
011
012import java.io.ByteArrayInputStream;
013import java.io.File;
014import java.io.InputStream;
015import java.util.*;
016
017/**
018 * Created by pvsubrah on 12/9/13.
019 */
020public class PatronsIngesterService implements XmlIngesterService {
021    private OlePatronConverterService olePatronConverterService;
022    private FileUtil fileUtil;
023    private OlePatronXMLSchemaValidator olePatronXMLSchemaValidator;
024
025    @Override
026    public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections) throws Exception {
027        List<XmlDocCollection> failedRecords = new ArrayList<>();
028
029        for (Iterator<XmlDocCollection> iterator = xmlDocCollections.iterator(); iterator.hasNext(); ) {
030            StringBuffer reportContent = new StringBuffer();
031            XmlDocCollection xmlDocCollection = iterator.next();
032            File file = xmlDocCollection.getFile();
033            String fileContent = getFileUtil().readFile(file);
034
035            InputStream inputStream = getByteArrayInputStream(fileContent);
036            boolean validContent = getOlePatronXMLSchemaValidator().validateContentsAgainstSchema(inputStream);
037            OlePatronIngestSummaryRecord olePatronIngestSummaryRecord = getOlePatronIngestSummaryRecord();
038            if (validContent) {
039                List<OlePatronDocument> successPatronDocuments = getOlePatronConverterService().persistPatronFromFileContent(fileContent, true, file.getName(), olePatronIngestSummaryRecord, null, "");
040                for (OlePatronDocument olePatronDocument : successPatronDocuments) {
041                    buildReportContent(olePatronDocument.getOlePatronId(), file.getName(), reportContent, "Success", null);
042                }
043                for (OlePatron olePatron : olePatronIngestSummaryRecord.getFailurePatronRecords()) {
044                    buildReportContent(olePatron.getPatronID(), file.getName(), reportContent, "Failure", olePatron.getErrorMessage());
045                }
046            } else {
047                failedRecords.add(xmlDocCollection);
048                buildReportContent(null, file.getName(), reportContent, "Failure", "Invalid Content");
049            }
050            if (olePatronIngestSummaryRecord.getPatronFailedCount() > 0) {
051                failedRecords.add(xmlDocCollection);
052            }
053            if (xmlDocCollection.getXmlDocs() != null && xmlDocCollection.getXmlDocs().size() > 0) {
054                xmlDocCollection.getXmlDocs().get(0).setProcessingMessage(reportContent.toString());
055            }
056        }
057        if (failedRecords.size() == 0) {
058            return new LinkedList<>();
059        } else {
060            return failedRecords;
061        }
062    }
063
064    protected OlePatronIngestSummaryRecord getOlePatronIngestSummaryRecord() {
065        return new OlePatronIngestSummaryRecord();
066    }
067
068    protected ByteArrayInputStream getByteArrayInputStream(String fileContent) {
069        return new ByteArrayInputStream(fileContent.getBytes());
070    }
071
072    private OlePatronXMLSchemaValidator getOlePatronXMLSchemaValidator() {
073        if (null == olePatronXMLSchemaValidator) {
074            olePatronXMLSchemaValidator = new OlePatronXMLSchemaValidator();
075        }
076        return olePatronXMLSchemaValidator;
077    }
078
079    public void setOlePatronXMLSchemaValidator(OlePatronXMLSchemaValidator olePatronXMLSchemaValidator) {
080        this.olePatronXMLSchemaValidator = olePatronXMLSchemaValidator;
081    }
082
083    private FileUtil getFileUtil() {
084        if (null == fileUtil) {
085            fileUtil = new FileUtil();
086        }
087        return fileUtil;
088    }
089
090    public void setFileUtil(FileUtil fileUtil) {
091        this.fileUtil = fileUtil;
092    }
093
094    @Override
095    public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections, String principalId) throws Exception {
096        return null;
097    }
098
099    public void setOlePatronConverterService(OlePatronConverterService olePatronConverterService) {
100        this.olePatronConverterService = olePatronConverterService;
101    }
102
103    public OlePatronConverterService getOlePatronConverterService() {
104        if (null == olePatronConverterService) {
105            olePatronConverterService = new OlePatronConverterService();
106        }
107        return olePatronConverterService;
108    }
109
110    private void buildReportContent(String patronId, String importFileName, StringBuffer reportContent, String importStatus, String errorMessage) {
111        reportContent.append(importStatus);
112        reportContent.append(OLEConstants.COMMA);
113        reportContent.append(OLEConstants.SPACE);
114        reportContent.append(importFileName);
115        if (patronId!=null){
116            reportContent.append(OLEConstants.COMMA);
117            reportContent.append(OLEConstants.SPACE);
118            reportContent.append(patronId);
119        }
120        if (errorMessage!=null){
121            reportContent.append(OLEConstants.COMMA);
122            reportContent.append(OLEConstants.SPACE);
123            reportContent.append(errorMessage);
124        }
125        reportContent.append("\n");
126    }
127
128}