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.OLEKeyConstants;
14  import org.kuali.ole.ingest.KrmsBuilder;
15  import org.kuali.ole.ingest.KrmsXMLSchemaValidator;
16  import org.kuali.ole.ingest.form.KrmsBuilderForm;
17  import org.kuali.ole.ingest.krms.builder.OleKrmsBuilder;
18  import org.kuali.rice.krad.util.GlobalVariables;
19  import org.kuali.rice.krad.web.controller.UifControllerBase;
20  import org.kuali.rice.krad.web.form.UifFormBase;
21  import org.springframework.stereotype.Controller;
22  import org.springframework.validation.BindingResult;
23  import org.springframework.web.bind.annotation.ModelAttribute;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.multipart.MultipartFile;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  
32  /**
33   * KrmsBuilderController is the controller class for Krms Builder
34   */
35  @Controller
36  @RequestMapping(value = "/krmsbuildercontroller")
37  public class KrmsBuilderController
38          extends UifControllerBase {
39      /**
40       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
41       */
42      private static final Logger LOG = Logger.getLogger(KrmsBuilderController.class);
43  
44      /**
45       *  This method returns the instance of KrmsBuilderForm.
46       * @param request
47       * @return krmsBuilderForm
48       */
49      @Override
50      protected KrmsBuilderForm createInitialForm(HttpServletRequest request) {
51          return new KrmsBuilderForm();
52      }
53  
54      /**
55       * This method takes the initial request when click on KrmsBuilder Screen.
56       * @param form
57       * @param result
58       * @param request
59       * @param response
60       * @return  ModelAndView.
61       */
62      @Override
63      @RequestMapping(params = "methodToCall=start")
64      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
65                                HttpServletRequest request, HttpServletResponse response) {
66          KrmsBuilderForm krmsBuilderForm = (KrmsBuilderForm) form;
67          return super.start(krmsBuilderForm, result, request, response);
68      }
69  
70      /**
71       * This method persist the data from krmsBuilderXml file into database after uploading the krmsBuilderXml.
72       * @param form
73       * @param result
74       * @param request
75       * @param response
76       * @return  ModelAndView
77       * @throws Exception
78       */
79      @RequestMapping(params = "methodToCall=upload")
80      public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
81                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
82          KrmsBuilderForm krmsBuilderForm = (KrmsBuilderForm) form;
83          KrmsBuilder krmsBuilder = new KrmsBuilder();
84          OleKrmsBuilder loanKrmsBuilder = new OleKrmsBuilder();
85          KrmsXMLSchemaValidator krmsXMLSchemaValidator = new KrmsXMLSchemaValidator();
86          MultipartFile multipartFile = krmsBuilderForm.getKrmsFile();
87          if (validateFile(multipartFile.getOriginalFilename())) {
88              String fileContent = new String(multipartFile.getBytes());
89              try {
90                  boolean validXML= true;
91                  if(!validXML){
92                      GlobalVariables.getMessageMap().putError(OLEKeyConstants.KRMS_BUILDER_INVALID_SCHEMA, OLEKeyConstants.KRMS_BUILDER_INVALID_SCHEMA);
93                      return super.start(krmsBuilderForm, result, request, response);
94                  }
95                  boolean license  = krmsXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream());
96                  if(license)
97                      krmsBuilder.persistKrmsFromFileContent(fileContent);
98                  else
99                      loanKrmsBuilder.persistKrmsFromFileContent(fileContent);
100 
101                 GlobalVariables.getMessageMap().putInfo(OLEKeyConstants.KRMS_BUILDER_SUCCESS,OLEKeyConstants.KRMS_BUILDER_SUCCESS);
102             } catch (Exception krmsBuilderException) {
103                 GlobalVariables.getMessageMap().putError(OLEKeyConstants.KRMS_BUILDER_FAILURE, OLEKeyConstants.KRMS_BUILDER_FAILURE);
104                 LOG.error("Failed to upload Krms builder.", krmsBuilderException);
105             }
106         } else {
107             GlobalVariables.getMessageMap().putError(OLEKeyConstants.KRMS_BUILDER_SELECT_FILE, OLEKeyConstants.KRMS_BUILDER_SELECT_FILE);
108         }
109         return super.start(krmsBuilderForm, result, request, response);
110     }
111 
112     /**
113      * This method validate the xml file type.
114      * @param inputFile
115      * @return  boolean
116      */
117      public boolean validateFile(String inputFile) {
118         return (inputFile.contains(".xml") ? true:false);
119     }
120 
121 
122 }
123