View Javadoc
1   package org.kuali.ole.deliver.defaultload;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.ingest.FileUtil;
5   import org.kuali.ole.ingest.krms.builder.OleKrmsBuilder;
6   import org.kuali.rice.coreservice.api.parameter.Parameter;
7   import org.kuali.rice.coreservice.api.parameter.ParameterType;
8   import org.kuali.rice.coreservice.framework.parameter.ParameterService;
9   
10  import java.io.File;
11  import java.net.URL;
12  import java.util.Collections;
13  import java.util.List;
14  
15  /**
16   * Created with IntelliJ IDEA.
17   * User: juliyamonicas
18   * Date: 08/21/13
19   * Time: 3:39 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class LoadDefaultEResourceBean {
23      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LoadDefaultEResourceBean.class);
24      protected static final String LOAD_DEFAULT_EREC_IND = "LOAD_DEFAULT_EREC_IND";
25      protected String fileName;
26      protected OleKrmsBuilder eresourceBuilder;
27      protected ParameterService parameterService;
28  
29      public List<String> loadDefaultEResource(boolean forceLoad) throws Exception {
30          if (forceLoad || parameterService.getParameterValueAsBoolean(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_EREC_IND, Boolean.TRUE)) {
31              LOG.debug("Starting Load of Default EResource");
32  
33              URL resource = getClass().getResource(fileName);
34              File file = new File(resource.toURI());
35              String fileContent = new FileUtil().readFile(file);
36              List<String> policies = eresourceBuilder.persistKrmsFromFileContent(fileContent);
37  
38              LOG.debug("Completed Load of Default EResource");
39  
40              Parameter existingParameter = parameterService.getParameter(OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_EREC_IND);
41              if (existingParameter != null) {
42                  Parameter.Builder updatedParameter = Parameter.Builder.create(existingParameter);
43                  updatedParameter.setValue("N");
44                  parameterService.updateParameter(updatedParameter.build());
45              } else {
46                  Parameter.Builder newParameter = Parameter.Builder.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, LOAD_DEFAULT_EREC_IND, ParameterType.Builder.create("CONFG"));
47                  newParameter.setDescription("Set to 'Y' to have the application ingest the default EResource upon next startup.");
48                  newParameter.setValue("N");
49                  parameterService.createParameter(newParameter.build());
50              }
51  
52              return policies;
53          }
54          return Collections.emptyList();
55      }
56  
57      public void setFileName(String fileName) {
58          this.fileName = fileName;
59      }
60  
61      public void setEresourceBuilder(OleKrmsBuilder eresourceBuilder) {
62          this.eresourceBuilder = eresourceBuilder;
63      }
64  
65      public void setParameterService(ParameterService parameterService) {
66          this.parameterService = parameterService;
67      }
68  }