View Javadoc
1   package org.kuali.ole.deliver.drools.controller;
2   
3   import org.apache.commons.io.FilenameUtils;
4   import org.directwebremoting.convert.ContextConverter;
5   import org.kuali.ole.deliver.drools.DroolsConstants;
6   import org.kuali.ole.deliver.drools.form.DroolUploadForm;
7   import org.kuali.rice.core.api.config.property.ConfigContext;
8   import org.kuali.rice.krad.util.GlobalVariables;
9   import org.kuali.rice.krad.web.controller.UifControllerBase;
10  import org.kuali.rice.krad.web.form.UifFormBase;
11  import org.springframework.stereotype.Controller;
12  import org.springframework.validation.BindingResult;
13  import org.springframework.web.bind.annotation.ModelAttribute;
14  import org.springframework.web.bind.annotation.RequestMapping;
15  import org.springframework.web.multipart.MultipartFile;
16  import org.springframework.web.multipart.commons.CommonsMultipartFile;
17  import org.springframework.web.servlet.ModelAndView;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  import java.io.File;
22  
23  /**
24   * Created by sheiksalahudeenm on 6/30/15.
25   */
26  @Controller
27  @RequestMapping(value = "/droolUploadController")
28  public class DroolUploadController extends UifControllerBase {
29  
30      @Override
31      protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
32          return new DroolUploadForm();
33      }
34  
35      @Override
36      @RequestMapping(params = "methodToCall=start")
37      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
38                                HttpServletRequest request, HttpServletResponse response) {
39          DroolUploadForm droolUploadForm = (DroolUploadForm) form;
40          return super.start(droolUploadForm, result, request, response);
41      }
42  
43      @RequestMapping(params = "methodToCall=upload")
44      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
45                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
46          DroolUploadForm droolUploadForm = (DroolUploadForm) form;
47          MultipartFile multipartFile = droolUploadForm.getDroolFile();
48          if(multipartFile != null){
49              if (validateFile(multipartFile.getOriginalFilename())) {
50                  String droolBaseDirectoryPath = getDroolBaseDirectory();
51                  File droolDirectory = new File(droolBaseDirectoryPath + File.separator + droolUploadForm.getSelectedRuleDirectory());
52                  if(!(droolDirectory.exists() && droolDirectory.isDirectory())){
53                      droolDirectory.mkdirs();
54                  }
55                  multipartFile.transferTo(new File(droolBaseDirectoryPath + File.separator
56                          + droolUploadForm.getSelectedRuleDirectory() +
57                          File.separator + multipartFile.getOriginalFilename()));
58                  GlobalVariables.getMessageMap().putInfo(DroolsConstants.DROOL_UPLOAD_SUCCESS, DroolsConstants.DROOL_UPLOAD_SUCCESS);
59              } else {
60                  GlobalVariables.getMessageMap().putError(DroolsConstants.DROOL_UPLOAD_INVALID_FILE, DroolsConstants.DROOL_UPLOAD_INVALID_FILE);
61              }
62          }else {
63              GlobalVariables.getMessageMap().putError(DroolsConstants.DROOL_UPLOAD_SELECT_FILE, DroolsConstants.DROOL_UPLOAD_SELECT_FILE);
64          }
65          return super.start(droolUploadForm, result, request, response);
66      }
67  
68      public boolean validateFile(String inputFile) {
69          return (FilenameUtils.getExtension(inputFile).equals("drl") ? true:false);
70      }
71  
72      private String getDroolBaseDirectory(){
73          return ConfigContext.getCurrentContextConfig().getProperty("rules.directory");
74      }
75  }