KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > webflow > ExcelFormAction


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.webflow;
18
19 import info.jtrac.domain.ExcelFile;
20 import org.springframework.validation.Errors;
21 import org.springframework.web.multipart.MultipartFile;
22 import org.springframework.web.multipart.MultipartHttpServletRequest;
23 import org.springframework.webflow.execution.Event;
24 import org.springframework.webflow.execution.RequestContext;
25 import org.springframework.webflow.execution.ScopeType;
26 import org.springframework.webflow.context.servlet.ServletExternalContext;
27
28 /**
29  * Multiaction that participates in the Excel Import flow
30  */

31 public class ExcelFormAction extends AbstractFormAction {
32     
33     public ExcelFormAction() {
34         setFormObjectClass(ExcelFile.class);
35         setFormObjectName("excelFile");
36         setFormObjectScope(ScopeType.FLOW);
37     }
38     
39     public Event uploadHandler(RequestContext context) throws Exception JavaDoc {
40         ServletExternalContext servletContext = (ServletExternalContext) context.getLastEvent().getSource();
41         MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletContext.getRequest();
42         MultipartFile multipartFile = request.getFile("file");
43         if(multipartFile.isEmpty()) {
44             return error();
45         }
46         ExcelFile excelFile = null;
47         try {
48             excelFile = new ExcelFile(multipartFile.getInputStream());
49         } catch (Exception JavaDoc e) {
50             Errors errors = getFormErrors(context);
51             errors.reject("excel_upload.error.invalidFile");
52             return error();
53         }
54         context.getFlowScope().put("excelFile", excelFile);
55         return success();
56     }
57     
58     public Event submitHandler(RequestContext context) throws Exception JavaDoc {
59         ExcelFile excelFile = (ExcelFile) getFormObject(context);
60         switch (excelFile.getAction()) {
61             case 1: excelFile.deleteSelectedRowsAndColumns(); break;
62             case 2: excelFile.convertSelectedColumnsToDate(); break;
63             case 3: excelFile.concatenateSelectedColumns(); break;
64             case 4: excelFile.extractSummaryFromSelectedColumn(); break;
65         }
66         return success();
67     }
68     
69 }
70
Popular Tags