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.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
34
35 @Controller
36 @RequestMapping(value = "/krmsbuildercontroller")
37 public class KrmsBuilderController
38 extends UifControllerBase {
39
40
41
42 private static final Logger LOG = Logger.getLogger(KrmsBuilderController.class);
43
44
45
46
47
48
49 @Override
50 protected KrmsBuilderForm createInitialForm(HttpServletRequest request) {
51 return new KrmsBuilderForm();
52 }
53
54
55
56
57
58
59
60
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
72
73
74
75
76
77
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
114
115
116
117 public boolean validateFile(String inputFile) {
118 return (inputFile.contains(".xml") ? true:false);
119 }
120
121
122 }
123