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.service.OlePatronConverterService;
007import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
008import org.kuali.rice.core.api.impex.xml.XmlIngesterService;
009
010import java.io.File;
011import java.util.Collection;
012import java.util.Iterator;
013import java.util.LinkedList;
014import java.util.List;
015
016/**
017 * Created by pvsubrah on 12/9/13.
018 */
019public class PatronsIngesterService implements XmlIngesterService {
020    private OlePatronConverterService olePatronConverterService;
021
022    @Override
023    public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections) throws Exception {
024        boolean failedRecords = false;
025        for (Iterator<XmlDocCollection> iterator = xmlDocCollections.iterator(); iterator.hasNext(); ) {
026            XmlDocCollection xmlDocCollection = iterator.next();
027            File file = xmlDocCollection.getFile();
028            String fileContent = new FileUtil().readFile(file);
029
030            OlePatronIngestSummaryRecord olePatronIngestSummaryRecord = new OlePatronIngestSummaryRecord();
031            List<OlePatronDocument> olePatronDocuments = olePatronConverterService.persistPatronFromFileContent(fileContent,
032                    true,
033                    file.getName(),
034                    olePatronIngestSummaryRecord, null, "");
035            failedRecords = !(olePatronIngestSummaryRecord.getPatronRejectCount() > 0);
036
037        }
038        if (!failedRecords) {
039            return new LinkedList<>();
040        }
041        return xmlDocCollections;
042    }
043
044    @Override
045    public Collection<XmlDocCollection> ingest(List<XmlDocCollection> xmlDocCollections, String principalId) throws Exception {
046        return null;
047    }
048
049    public void setOlePatronConverterService(OlePatronConverterService olePatronConverterService) {
050        this.olePatronConverterService = olePatronConverterService;
051    }
052
053    public OlePatronConverterService getOlePatronConverterService() {
054        return olePatronConverterService;
055    }
056}