1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.web.struts;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.InputStream;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.commons.io.IOUtils;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.upload.FormFile;
31 import org.kuali.ole.sys.OLEConstants;
32 import org.kuali.ole.sys.OLEKeyConstants;
33 import org.kuali.ole.sys.batch.BatchInputFileType;
34 import org.kuali.ole.sys.batch.BatchSpringContext;
35 import org.kuali.ole.sys.batch.service.BatchInputFileService;
36 import org.kuali.ole.sys.businessobject.BatchUpload;
37 import org.kuali.ole.sys.context.SpringContext;
38 import org.kuali.ole.sys.exception.FileStorageException;
39 import org.kuali.ole.sys.exception.ParseException;
40 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
41 import org.kuali.rice.kim.api.KimConstants;
42 import org.kuali.rice.kim.api.services.IdentityManagementService;
43 import org.kuali.rice.kns.util.KNSGlobalVariables;
44 import org.kuali.rice.kns.web.struts.action.KualiAction;
45 import org.kuali.rice.krad.exception.AuthorizationException;
46 import org.kuali.rice.krad.service.ModuleService;
47 import org.kuali.rice.krad.util.GlobalVariables;
48 import org.kuali.rice.krad.util.KRADConstants;
49
50
51
52
53 public class KualiBatchInputFileAction extends KualiAction {
54 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiBatchInputFileAction.class);
55 private static IdentityManagementService identityManagementService;
56 private IdentityManagementService getIdentityManagementService() {
57 if (identityManagementService == null) {
58 identityManagementService = SpringContext.getBean(IdentityManagementService.class);
59 }
60 return identityManagementService;
61 }
62
63
64
65
66
67 @Override
68 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
69 ActionForward forward = super.execute(mapping, form, request, response);
70 setupForm((KualiBatchInputFileForm) form);
71 return forward;
72 }
73
74 @Override
75 protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
76 BatchUpload batchUpload = ((KualiBatchInputFileForm) form).getBatchUpload();
77 BatchInputFileType batchInputFileType = retrieveBatchInputFileTypeImpl(batchUpload.getBatchInputTypeName());
78 Map<String,String> permissionDetails = new HashMap<String,String>();
79 permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, getNamespaceCode(batchInputFileType.getClass()));
80 permissionDetails.put(KimConstants.AttributeConstants.BEAN_NAME, batchUpload.getBatchInputTypeName());
81 if (!getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, OLEConstants.PermissionTemplate.UPLOAD_BATCH_INPUT_FILES.name, permissionDetails, null)) {
82 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), methodToCall, batchUpload.getBatchInputTypeName());
83 }
84 }
85
86 protected static String getNamespaceCode(Class<? extends Object> clazz) {
87 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
88 if (moduleService == null) {
89 return KimConstants.KIM_TYPE_DEFAULT_NAMESPACE;
90 }
91 return moduleService.getModuleConfiguration().getNamespaceCode();
92 }
93
94
95
96
97 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
98 return mapping.findForward(OLEConstants.MAPPING_BASIC);
99 }
100
101
102
103
104
105
106 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
107 BatchUpload batchUpload = ((KualiBatchInputFileForm) form).getBatchUpload();
108 if (LOG.isDebugEnabled()) {
109 LOG.debug("------------BatchInputTypeName---------->"+batchUpload.getBatchInputTypeName());
110 }
111 BatchInputFileType batchType = retrieveBatchInputFileTypeImpl(batchUpload.getBatchInputTypeName());
112
113
114 BatchInputFileService batchInputFileService = SpringContext.getBean(BatchInputFileService.class);
115 FormFile uploadedFile = ((KualiBatchInputFileForm) form).getUploadFile();
116
117 StringBuffer fileName = new StringBuffer(uploadedFile.getFileName());
118
119 int fileExtensionCount = fileName.indexOf(".");
120 String extension = fileName.substring(fileExtensionCount+1);
121
122
123
124 if (uploadedFile == null || uploadedFile.getInputStream() == null || uploadedFile.getInputStream().available() == 0) {
125 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_SELECTED_SAVE, new String[] {});
126 return mapping.findForward(OLEConstants.MAPPING_BASIC);
127 }
128
129 if (!batchInputFileService.isFileUserIdentifierProperlyFormatted(batchUpload.getFileUserIdentifer())) {
130 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_FILE_USER_IDENTIFIER_BAD_FORMAT, new String[] {});
131 return mapping.findForward(OLEConstants.MAPPING_BASIC);
132 }
133
134 InputStream fileContents = ((KualiBatchInputFileForm) form).getUploadFile().getInputStream();
135 byte[] fileByteContent = IOUtils.toByteArray(fileContents);
136
137 Object parsedObject = null;
138 try {
139 parsedObject = batchInputFileService.parse(batchType, fileByteContent);
140 }
141 catch (ParseException e) {
142 LOG.error("errors parsing xml " + e.getMessage(), e);
143 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_PARSING_XML, new String[] { e.getMessage() });
144 }
145
146 if (parsedObject != null && GlobalVariables.getMessageMap().hasNoErrors()) {
147 boolean validateSuccessful = batchInputFileService.validate(batchType, parsedObject);
148
149 if (validateSuccessful && GlobalVariables.getMessageMap().hasNoErrors()) {
150 try {
151 InputStream saveStream = new ByteArrayInputStream(fileByteContent);
152 String savedFileName ="";
153 if(batchUpload.getBatchInputTypeName().equalsIgnoreCase("marcInputFileType")){
154 if(batchUpload == null || batchUpload.getBatchDestinationFilePath() == null){
155 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_PATH_SELECTED_SAVE, new String[] {});
156 return mapping.findForward(OLEConstants.MAPPING_BASIC);
157 }
158 savedFileName = batchInputFileService.save(GlobalVariables.getUserSession().getPerson(), batchType, batchUpload.getFileUserIdentifer(), saveStream, parsedObject,batchUpload.getBatchDestinationFilePath(),extension);
159 }else{
160 savedFileName = batchInputFileService.save(GlobalVariables.getUserSession().getPerson(), batchType, batchUpload.getFileUserIdentifer(), saveStream, parsedObject);
161 }
162 KNSGlobalVariables.getMessageList().add(OLEKeyConstants.MESSAGE_BATCH_UPLOAD_SAVE_SUCCESSFUL);
163 }
164 catch (FileStorageException e1) {
165 LOG.error("errors saving xml " + e1.getMessage(), e1);
166 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_SAVE, new String[] { e1.getMessage() });
167 }
168 }
169 }
170
171 return mapping.findForward(OLEConstants.MAPPING_BASIC);
172 }
173
174
175
176
177 private BatchInputFileType retrieveBatchInputFileTypeImpl(String batchInputTypeName) {
178 BatchInputFileType batchInputType = BatchSpringContext.getBatchInputFileType(batchInputTypeName);
179 if (batchInputType == null) {
180 LOG.error("Batch input type implementation not found for id " + batchInputTypeName);
181 throw new RuntimeException(("Batch input type implementation not found for id " + batchInputTypeName));
182 }
183
184 return batchInputType;
185 }
186
187
188
189
190
191 private void setupForm(KualiBatchInputFileForm form) {
192 BatchInputFileType batchInputFileType = retrieveBatchInputFileTypeImpl(form.getBatchUpload().getBatchInputTypeName());
193
194 if (batchInputFileType == null) {
195 LOG.error("Batch input type implementation not found for id " + form.getBatchUpload().getBatchInputTypeName());
196 throw new RuntimeException(("Batch input type implementation not found for id " + form.getBatchUpload().getBatchInputTypeName()));
197 }
198 ParameterService parmeterService = SpringContext.getBean(ParameterService.class);
199 String url = parmeterService.getSubParameterValueAsString(BatchUpload.class,OLEConstants.BATCH_UPLOAD_HELP_SYS_PARAM_NAME, batchInputFileType.getFileTypeIdentifer());
200 form.setUrl(url);
201
202 form.setTitleKey(batchInputFileType.getTitleKey());
203 }
204
205 }
206