001package org.kuali.ole.deliver.defaultload;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.ingest.FileUtil;
005import org.kuali.ole.ingest.KrmsBuilder;
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.Collections;
013import java.util.List;
014
015/**
016 * Created with IntelliJ IDEA.
017 * User: srinivasane
018 * Date: 10/31/12
019 * Time: 3:39 PM
020 * To change this template use File | Settings | File Templates.
021 */
022public class LoadDefaultLicensesBean {
023    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LoadDefaultLicensesBean.class);
024    protected static final String LOAD_DEFAULT_LICENSES_IND = "LOAD_DEFAULT_LICENSES_IND";
025    protected String fileName;
026    protected KrmsBuilder licenseBuilder;
027    protected ParameterService parameterService;
028
029    public List<String> loadDefaultLicenses(boolean forceLoad) throws Exception {
030        if (forceLoad || parameterService.getParameterValueAsBoolean(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LICENSES_IND, Boolean.TRUE)) {
031            LOG.debug("Starting Load of Default Licenses");
032
033            URL resource = getClass().getResource(fileName);
034            File file = new File(resource.toURI());
035            String fileContent = new FileUtil().readFile(file);
036            List<String> policies = licenseBuilder.persistKrmsFromFileContent(fileContent);
037
038            LOG.debug("Completed Load of Default Licenses");
039
040            Parameter existingParameter = parameterService.getParameter(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LICENSES_IND);
041            if (existingParameter != null) {
042                Parameter.Builder updatedParameter = Parameter.Builder.create(existingParameter);
043                updatedParameter.setValue("N");
044                parameterService.updateParameter(updatedParameter.build());
045            } else {
046                Parameter.Builder newParameter = Parameter.Builder.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_LICENSES_IND, ParameterType.Builder.create("CONFG"));
047                newParameter.setDescription("Set to 'Y' to have the application ingest the default Licenses upon next startup.");
048                newParameter.setValue("N");
049                parameterService.createParameter(newParameter.build());
050            }
051
052            return policies;
053        }
054        return Collections.emptyList();
055    }
056
057    public void setFileName(String fileName) {
058        this.fileName = fileName;
059    }
060
061    public void setLicenseBuilder(KrmsBuilder licenseBuilder) {
062        this.licenseBuilder = licenseBuilder;
063    }
064
065    public void setParameterService(ParameterService parameterService) {
066        this.parameterService = parameterService;
067    }
068}