001package org.kuali.ole.describe.defaultload;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.ingest.FileUtil;
005import org.kuali.ole.service.OleLocationConverterService;
006import org.kuali.rice.coreservice.api.parameter.Parameter;
007import org.kuali.rice.coreservice.api.parameter.ParameterType;
008import org.kuali.rice.coreservice.framework.parameter.ParameterService;
009
010import java.io.File;
011import java.net.URL;
012import java.util.ArrayList;
013import java.util.List;
014
015/**
016 * Created with IntelliJ IDEA.
017 * User: peris
018 * Date: 10/31/12
019 * Time: 8:18 AM
020 * To change this template use File | Settings | File Templates.
021 */
022public class LoadDefaultLocationsBean {
023    private String fileName;
024    private List<String> locationFiles = new ArrayList<String>();
025    private OleLocationConverterService oleLocationIngestService;
026    protected ParameterService parameterService;
027
028    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LoadDefaultLocationsBean.class);
029    protected static final String LOAD_DEFAULT_LOCATIONS_IND = "LOAD_DEFAULT_LOCATIONS_IND";
030
031    public void loadDefaultLocations(boolean forceLoad) throws Exception {
032        if (forceLoad || parameterService.getParameterValueAsBoolean(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LOCATIONS_IND, Boolean.TRUE)) {
033            LOG.info("Starting Load of Default Locations");
034
035            for (int i = 0; i < locationFiles.size(); i++) {
036                fileName = this.locationFiles.get(i);
037                URL resource = getClass().getResource(fileName);
038                File file = new File(resource.toURI());
039                String fileContent = new FileUtil().readFile(file);
040                oleLocationIngestService.persistLocationFromFileContent(fileContent, file.getName());
041                LOG.info("Completed Load of Default Locations");
042
043                Parameter existingParameter = parameterService.getParameter(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LOCATIONS_IND);
044                if (existingParameter != null) {
045                    Parameter.Builder updatedParameter = Parameter.Builder.create(existingParameter);
046                    updatedParameter.setValue("N");
047                    parameterService.updateParameter(updatedParameter.build());
048                } else {
049                    Parameter.Builder newParameter = Parameter.Builder.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LOCATIONS_IND, ParameterType.Builder.create("CONFG"));
050                    newParameter.setDescription("Set to 'Y' to have the application ingest the default circulation policies upon next startup.");
051                    newParameter.setValue("N");
052                    parameterService.createParameter(newParameter.build());
053                }
054            }
055        }
056    }
057
058    public String getFileName() {
059        return fileName;
060    }
061
062    public void setFileName(String fileName) {
063        this.fileName = fileName;
064    }
065
066    public OleLocationConverterService getOleLocationIngestService() {
067        return oleLocationIngestService;
068    }
069
070    public void setOleLocationIngestService(OleLocationConverterService oleLocationIngestService) {
071        this.oleLocationIngestService = oleLocationIngestService;
072    }
073
074    public List<String> getLocationFiles() {
075        return locationFiles;
076    }
077
078    public void setLocationFiles(List<String> locationFiles) {
079        this.locationFiles = locationFiles;
080    }
081
082    public void setParameterService(ParameterService parameterService) {
083        this.parameterService = parameterService;
084    }
085}