1 package org.kuali.ole.ingest.controller;
2
3
4
5
6
7
8
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
31
32 @Controller
33 @RequestMapping(value = "/profilebuildercontroller")
34 public class ProfileBuilderController
35 extends UifControllerBase {
36
37
38
39 private static final Logger LOG = Logger.getLogger(ProfileBuilderController.class);
40
41
42
43
44
45
46
47 @Override
48 protected ProfileBuilderForm createInitialForm(HttpServletRequest request) {
49 return new ProfileBuilderForm();
50 }
51
52
53
54
55
56
57
58
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
70
71
72
73
74
75
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
106
107
108
109 public boolean validateFile(String inputFile) {
110 return (inputFile.contains(".xml") ? true:false);
111 }
112
113
114 }
115