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.InputStream;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.upload.FormFile;
30 import org.kuali.ole.sys.OLEConstants;
31 import org.kuali.ole.sys.OLEKeyConstants;
32 import org.kuali.ole.sys.batch.BatchInputFileSetType;
33 import org.kuali.ole.sys.batch.BatchSpringContext;
34 import org.kuali.ole.sys.batch.service.BatchInputFileSetService;
35 import org.kuali.ole.sys.businessobject.BatchUpload;
36 import org.kuali.ole.sys.context.SpringContext;
37 import org.kuali.ole.sys.exception.FileStorageException;
38 import org.kuali.rice.kim.api.KimConstants;
39 import org.kuali.rice.kim.api.services.IdentityManagementService;
40 import org.kuali.rice.kns.util.KNSGlobalVariables;
41 import org.kuali.rice.kns.web.struts.action.KualiAction;
42 import org.kuali.rice.krad.exception.AuthorizationException;
43 import org.kuali.rice.krad.exception.ValidationException;
44 import org.kuali.rice.krad.service.ModuleService;
45 import org.kuali.rice.krad.util.GlobalVariables;
46 import org.kuali.rice.krad.util.KRADConstants;
47
48
49
50
51 public class KualiBatchInputFileSetAction extends KualiAction {
52 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiBatchInputFileSetAction.class);
53 private static IdentityManagementService identityManagementService;
54 private IdentityManagementService getIdentityManagementService() {
55 if (identityManagementService == null) {
56 identityManagementService = SpringContext.getBean(IdentityManagementService.class);
57 }
58 return identityManagementService;
59 }
60
61
62
63
64
65 @Override
66 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
67 ActionForward forward = super.execute(mapping, form, request, response);
68 setupForm((KualiBatchInputFileSetForm) form);
69 return forward;
70 }
71
72
73
74
75 @Override
76 protected void checkAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
77 BatchUpload batchUpload = ((KualiBatchInputFileSetForm) form).getBatchUpload();
78 BatchInputFileSetType batchInputFileType = retrieveBatchInputFileSetTypeImpl(batchUpload.getBatchInputTypeName());
79 Map<String,String> permissionDetails = new HashMap<String,String>();
80 permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, getNamespaceCode(batchInputFileType.getClass()));
81 permissionDetails.put(KimConstants.AttributeConstants.BEAN_NAME, batchUpload.getBatchInputTypeName());
82 if (!getIdentityManagementService().isAuthorizedByTemplateName(GlobalVariables.getUserSession().getPrincipalId(), KRADConstants.KNS_NAMESPACE, OLEConstants.PermissionTemplate.UPLOAD_BATCH_INPUT_FILES.name, permissionDetails, null)) {
83 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalName(), methodToCall, batchUpload.getBatchInputTypeName());
84 }
85 }
86
87 protected static String getNamespaceCode(Class<? extends Object> clazz) {
88 ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(clazz);
89 if (moduleService == null) {
90 return KimConstants.KIM_TYPE_DEFAULT_NAMESPACE;
91 }
92 return moduleService.getModuleConfiguration().getNamespaceCode();
93 }
94
95
96
97
98 public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
99 BatchUpload batchUpload = ((KualiBatchInputFileSetForm) form).getBatchUpload();
100 BatchInputFileSetType batchType = retrieveBatchInputFileSetTypeImpl(batchUpload.getBatchInputTypeName());
101 return mapping.findForward(OLEConstants.MAPPING_BASIC);
102 }
103
104
105
106
107
108
109 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
110 BatchUpload batchUpload = ((KualiBatchInputFileSetForm) form).getBatchUpload();
111 BatchInputFileSetType batchType = retrieveBatchInputFileSetTypeImpl(batchUpload.getBatchInputTypeName());
112
113 boolean requiredValuesForFilesMissing = false;
114 if (StringUtils.isBlank(batchUpload.getFileUserIdentifer())) {
115 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_SET_IDENTIFIER_SELECTED, new String[] {});
116 requiredValuesForFilesMissing = true;
117 }
118
119 BatchInputFileSetService batchInputFileSetService = SpringContext.getBean(BatchInputFileSetService.class);
120 if (!batchInputFileSetService.isFileUserIdentifierProperlyFormatted(batchUpload.getFileUserIdentifer())) {
121 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_FILE_SET_IDENTIFIER_BAD_FORMAT);
122 requiredValuesForFilesMissing = true;
123 }
124
125 Map<String, FormFile> uploadedFiles = ((KualiBatchInputFileSetForm) form).getUploadedFiles();
126 Map<String, InputStream> typeToStreamMap = new HashMap<String, InputStream>();
127
128 for (String fileType : uploadedFiles.keySet()) {
129 FormFile uploadedFile = uploadedFiles.get(fileType);
130 if (uploadedFile == null || uploadedFile.getInputStream() == null || uploadedFile.getInputStream().available() == 0) {
131 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_NO_FILE_SELECTED_SAVE_FOR_FILE_TYPE, new String[] { batchType.getFileTypeDescription().get(fileType) });
132 requiredValuesForFilesMissing = true;
133 }
134 else {
135 typeToStreamMap.put(fileType, uploadedFile.getInputStream());
136 }
137 }
138
139 if (requiredValuesForFilesMissing) {
140 return mapping.findForward(OLEConstants.MAPPING_BASIC);
141 }
142
143 try {
144 Map<String, String> typeToSavedFileNames = batchInputFileSetService.save(GlobalVariables.getUserSession().getPerson(), batchType, batchUpload.getFileUserIdentifer(), typeToStreamMap);
145 }
146 catch (FileStorageException e) {
147 LOG.error("Error occured while trying to save file set (probably tried to save a file that already exists).", e);
148 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_FILE_SAVE_ERROR, new String[] { e.getMessage() });
149 return mapping.findForward(OLEConstants.MAPPING_BASIC);
150 }
151 catch (ValidationException e) {
152 LOG.error("Error occured while trying to validate file set.", e);
153 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_BATCH_UPLOAD_FILE_VALIDATION_ERROR);
154 return mapping.findForward(OLEConstants.MAPPING_BASIC);
155 }
156 KNSGlobalVariables.getMessageList().add(OLEKeyConstants.MESSAGE_BATCH_UPLOAD_SAVE_SUCCESSFUL);
157
158 return mapping.findForward(OLEConstants.MAPPING_BASIC);
159 }
160
161
162
163
164 public BatchInputFileSetType retrieveBatchInputFileSetTypeImpl(String batchInputTypeName) {
165 BatchInputFileSetType batchInputType = BatchSpringContext.getBatchInputFileSetType(batchInputTypeName);
166 if (batchInputType == null) {
167 LOG.error("Batch input type implementation not found for id " + batchInputTypeName);
168 throw new RuntimeException(("Batch input type implementation not found for id " + batchInputTypeName));
169 }
170
171 return batchInputType;
172 }
173
174
175
176
177
178
179 public void setupForm(KualiBatchInputFileSetForm form) {
180 BatchInputFileSetType batchInputFileSetType = retrieveBatchInputFileSetTypeImpl(form.getBatchUpload().getBatchInputTypeName());
181
182 if (batchInputFileSetType == null) {
183 LOG.error("Batch input type implementation not found for id " + form.getBatchUpload().getBatchInputTypeName());
184 throw new RuntimeException("Batch input type implementation not found for id " + form.getBatchUpload().getBatchInputTypeName());
185 }
186
187 if (!SpringContext.getBean(BatchInputFileSetService.class).isBatchInputTypeActive(batchInputFileSetType)) {
188 throw new RuntimeException("Batch input file set type is not active.");
189 }
190 form.setBatchInputFileSetType(batchInputFileSetType);
191
192
193 form.setTitleKey(batchInputFileSetType.getTitleKey());
194 }
195
196 }
197