001package org.kuali.ole.deliver.defaultload;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.deliver.bo.OlePatronDocument;
005import org.kuali.ole.deliver.bo.OlePatronIngestSummaryRecord;
006import org.kuali.ole.ingest.FileUtil;
007import org.kuali.ole.service.OlePatronConverterService;
008import org.kuali.rice.coreservice.api.parameter.Parameter;
009import org.kuali.rice.coreservice.api.parameter.ParameterType;
010import org.kuali.rice.coreservice.framework.parameter.ParameterService;
011
012import java.io.File;
013import java.net.URL;
014import java.util.Collections;
015import java.util.List;
016
017/**
018 * Created with IntelliJ IDEA.
019 * User: peris
020 * Date: 10/31/12
021 * Time: 11:06 AM
022 * To change this template use File | Settings | File Templates.
023 */
024public class LoadDefaultPatronsBean {
025    private String fileName;
026    private OlePatronConverterService olePatronIngestService;
027    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LoadDefaultPatronsBean.class);
028    protected static final String LOAD_DEFAULT_PATRONS_IND = "LOAD_DEFAULT_PATRONS_IND";
029    protected ParameterService parameterService;
030
031    public List<OlePatronDocument> loadDefaultPatrons(boolean forceLoad) throws Exception {
032        if (forceLoad || parameterService.getParameterValueAsBoolean(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_PATRONS_IND, Boolean.TRUE)) {
033            LOG.debug("Starting Load of Default Patrons");
034
035            URL resource = getClass().getResource(fileName);
036            File file = new File(resource.toURI());
037            String fileContent = new FileUtil().readFile(file);
038            List<OlePatronDocument> olePatronDocuments =
039                    olePatronIngestService.persistPatronFromFileContent(fileContent,
040                            true,
041                            file.getName(),
042                            new OlePatronIngestSummaryRecord(), null, "");
043            LOG.debug("Completed Load of Default Patrons");
044            Parameter existingParameter = parameterService.getParameter(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_PATRONS_IND);
045            if (existingParameter != null) {
046                Parameter.Builder updatedParameter = Parameter.Builder.create(existingParameter);
047                updatedParameter.setValue("N");
048                parameterService.updateParameter(updatedParameter.build());
049            } else {
050                Parameter.Builder newParameter = Parameter.Builder.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_PATRONS_IND, ParameterType.Builder.create("CONFG"));
051                newParameter.setDescription("Set to 'Y' to have the application ingest the default patrons upon next startup.");
052                newParameter.setValue("N");
053                parameterService.createParameter(newParameter.build());
054            }
055            return olePatronDocuments;
056        }
057        return Collections.emptyList();
058    }
059
060    public String getFileName() {
061        return fileName;
062    }
063
064    public void setFileName(String fileName) {
065        this.fileName = fileName;
066    }
067
068    public OlePatronConverterService getOlePatronIngestService() {
069        return olePatronIngestService;
070    }
071
072    public void setOlePatronIngestService(OlePatronConverterService olePatronIngestService) {
073        this.olePatronIngestService = olePatronIngestService;
074    }
075
076    public void setParameterService(ParameterService parameterService) {
077        this.parameterService = parameterService;
078    }
079}