View Javadoc
1   package org.kuali.ole.ingest.controller;
2   
3   /**
4    * Created by IntelliJ IDEA.
5    * User: balakumaranm
6    * Date: 3/28/12
7    * Time: 3:24 PM
8    * To change this template use File | Settings | File Templates.
9    */
10  
11  import org.apache.log4j.Logger;
12  import org.kuali.ole.OLEConstants;
13  import org.kuali.ole.ingest.ProfileBuilder;
14  import org.kuali.ole.ingest.ProfileXMLSchemaValidator;
15  import org.kuali.ole.ingest.form.ProfileBuilderForm;
16  import org.kuali.rice.krad.web.controller.UifControllerBase;
17  import org.kuali.rice.krad.web.form.UifFormBase;
18  import org.springframework.stereotype.Controller;
19  import org.springframework.validation.BindingResult;
20  import org.springframework.web.bind.annotation.ModelAttribute;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.multipart.MultipartFile;
23  import org.springframework.web.servlet.ModelAndView;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  
29  /**
30   * ProfileBuilderController is the controller class for Profile Builder Controller
31   */
32  @Controller
33  @RequestMapping(value = "/profilebuildercontroller")
34  public class ProfileBuilderController
35          extends UifControllerBase {
36      /**
37       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
38       */
39      private static final Logger LOG = Logger.getLogger(ProfileBuilderController.class);
40  
41      /**
42       * This method will return instance of ProfileBuilderForm
43       * @param request
44       * @return  ProfileBuilderForm
45       */
46  
47      @Override
48      protected ProfileBuilderForm createInitialForm(HttpServletRequest request) {
49          return new ProfileBuilderForm();
50      }
51  
52      /**
53       *  This method will take the initial request when click on ProfileBuilder controller.
54       * @param form
55       * @param result
56       * @param request
57       * @param response
58       * @return  ModelAndView
59       */
60      @Override
61      @RequestMapping(params = "methodToCall=start")
62      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
63                                HttpServletRequest request, HttpServletResponse response) {
64          ProfileBuilderForm profileBuilderForm = (ProfileBuilderForm) form;
65          return super.start(profileBuilderForm, result, request, response);
66      }
67  
68      /**
69       *  This method persist the data from ProfileBuilderXml file into database after uploading the ProfileBuilderXml.
70       * @param form
71       * @param result
72       * @param request
73       * @param response
74       * @return  ModelAndView
75       * @throws Exception
76       */
77      @RequestMapping(params = "methodToCall=upload")
78      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
79                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
80          ProfileBuilderForm profileBuilderForm = (ProfileBuilderForm) form;
81          ProfileBuilder profileBuilder = new ProfileBuilder();
82          ProfileXMLSchemaValidator profileXMLSchemaValidator=new ProfileXMLSchemaValidator();
83          MultipartFile multipartFile = profileBuilderForm.getProfileFile();
84          if (validateFile(multipartFile.getOriginalFilename())) {
85              String fileContent = new String(multipartFile.getBytes());
86              try {
87                  boolean validXML=profileXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream());
88                  if(!validXML){
89                      profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_INVALID_SCHEMA);
90                      return super.start(profileBuilderForm, result, request, response);
91                  }
92                  profileBuilder.persistKRMSProfileFromFileContent(fileContent);
93                  profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_SUCCESS);
94              } catch (Exception profileBuilderException) {
95                  profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_FAILURE);
96                  LOG.error("Failed to upload Profile builder.", profileBuilderException);
97              }
98          } else {
99              profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_SELECT_FILE);
100         }
101         return super.start(profileBuilderForm, result, request, response);
102     }
103 
104     /**
105      *  This method validate the xml file type.
106      * @param inputFile
107      * @return  boolean
108      */
109      public boolean validateFile(String inputFile) {
110         return (inputFile.contains(".xml") ? true:false);
111     }
112 
113 
114 }
115