001 package org.kuali.ole.ingest.controller; 002 003 /** 004 * Created by IntelliJ IDEA. 005 * User: balakumaranm 006 * Date: 3/28/12 007 * Time: 3:24 PM 008 * To change this template use File | Settings | File Templates. 009 */ 010 011 import org.apache.log4j.Logger; 012 import org.kuali.ole.OLEConstants; 013 import org.kuali.ole.ingest.ProfileBuilder; 014 import org.kuali.ole.ingest.ProfileXMLSchemaValidator; 015 import org.kuali.ole.ingest.form.ProfileBuilderForm; 016 import org.kuali.rice.krad.web.controller.UifControllerBase; 017 import org.kuali.rice.krad.web.form.UifFormBase; 018 import org.springframework.stereotype.Controller; 019 import org.springframework.validation.BindingResult; 020 import org.springframework.web.bind.annotation.ModelAttribute; 021 import org.springframework.web.bind.annotation.RequestMapping; 022 import org.springframework.web.multipart.MultipartFile; 023 import org.springframework.web.servlet.ModelAndView; 024 025 import javax.servlet.http.HttpServletRequest; 026 import javax.servlet.http.HttpServletResponse; 027 028 029 /** 030 * ProfileBuilderController is the controller class for Profile Builder Controller 031 */ 032 @Controller 033 @RequestMapping(value = "/profilebuildercontroller") 034 public class ProfileBuilderController 035 extends UifControllerBase { 036 /** 037 * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest) 038 */ 039 private static final Logger LOG = Logger.getLogger(ProfileBuilderController.class); 040 041 /** 042 * This method will return instance of ProfileBuilderForm 043 * @param request 044 * @return ProfileBuilderForm 045 */ 046 047 @Override 048 protected ProfileBuilderForm createInitialForm(HttpServletRequest request) { 049 return new ProfileBuilderForm(); 050 } 051 052 /** 053 * This method will take the initial request when click on ProfileBuilder controller. 054 * @param form 055 * @param result 056 * @param request 057 * @param response 058 * @return ModelAndView 059 */ 060 @Override 061 @RequestMapping(params = "methodToCall=start") 062 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 063 HttpServletRequest request, HttpServletResponse response) { 064 ProfileBuilderForm profileBuilderForm = (ProfileBuilderForm) form; 065 return super.start(profileBuilderForm, result, request, response); 066 } 067 068 /** 069 * This method persist the data from ProfileBuilderXml file into database after uploading the ProfileBuilderXml. 070 * @param form 071 * @param result 072 * @param request 073 * @param response 074 * @return ModelAndView 075 * @throws Exception 076 */ 077 @RequestMapping(params = "methodToCall=upload") 078 public ModelAndView upload(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 079 HttpServletRequest request, HttpServletResponse response) throws Exception { 080 ProfileBuilderForm profileBuilderForm = (ProfileBuilderForm) form; 081 ProfileBuilder profileBuilder = new ProfileBuilder(); 082 ProfileXMLSchemaValidator profileXMLSchemaValidator=new ProfileXMLSchemaValidator(); 083 MultipartFile multipartFile = profileBuilderForm.getProfileFile(); 084 if (validateFile(multipartFile.getOriginalFilename())) { 085 String fileContent = new String(multipartFile.getBytes()); 086 try { 087 boolean validXML=profileXMLSchemaValidator.validateContentsAgainstSchema(multipartFile.getInputStream()); 088 if(!validXML){ 089 profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_INVALID_SCHEMA); 090 return super.start(profileBuilderForm, result, request, response); 091 } 092 profileBuilder.persistKRMSProfileFromFileContent(fileContent); 093 profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_SUCCESS); 094 } catch (Exception profileBuilderException) { 095 profileBuilderForm.setMessage(OLEConstants.PROFILE_BUILDER_FAILURE); 096 LOG.error("Failed to upload Profile builder.", profileBuilderException); 097 } 098 } else { 099 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