View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.database;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.apache.commons.lang.math.RandomUtils;
25  import org.apache.struts.upload.FormFile;
26  import org.seasar.tuigwaa.database.BasicDatabaseService;
27  import org.seasar.tuigwaa.database.util.CSVfileLoader;
28  import org.seasar.tuigwaa.database.util.DatafileLoader;
29  import org.seasar.tuigwaa.database.util.ExcelfileLoader;
30  import org.seasar.tuigwaa.model.common.EntityUtils;
31  import org.seasar.tuigwaa.model.core.TgwEntity;
32  import org.seasar.tuigwaa.plugin.ActionPlugin;
33  import org.seasar.tuigwaa.plugin.PluginService;
34  import org.seasar.tuigwaa.plugin.WebAppli;
35  import org.seasar.tuigwaa.system.SiteConfig;
36  import org.seasar.tuigwaa.system.TgwServiceException;
37  import org.seasar.tuigwaa.util.MimeMappings;
38  import org.seasar.tuigwaa.util.TgwContext;
39  import org.seasar.tuigwaa.util.TgwUtils;
40  
41  import com.isenshi.util.PathUtils;
42  
43  /***
44   * ファイルアップロードプラグインに対応するAction
45   * 
46   * @author nishioka
47   */
48  public class ExcelUpAction extends ActionPlugin {
49  
50  	private PluginService pluginService;
51  
52  	private BasicDatabaseService basicDatabaseService;
53  
54  	public ExcelUpAction(PluginService pluginService,
55  			BasicDatabaseService basicDatabaseService) {
56  		this.pluginService = pluginService;
57  		this.basicDatabaseService = basicDatabaseService;
58  	}
59  
60  	public String execute(ExcelUpDto dto, HttpServletRequest request,
61  			HttpServletResponse response) {
62  		String siteName = TgwContext.getSiteName();
63  		FormFile file = dto.getFile();
64  		String fileName = file.getFileName();
65  		String entityName = dto.getEntityName();
66  		String type = dto.getType();
67  
68  		WebAppli appli = getWebAppli();
69  		TgwEntity entity = appli.getEntity(siteName, entityName);
70  
71  		String message = null;
72  		String contentType = file.getContentType();
73  
74  		try {
75  			DatafileLoader loader = getDatafileLoader(fileName, contentType,
76  					file.getInputStream());
77  			if (loader != null) {
78  				loadFile(entity, loader, type);
79  				message = fileName + " successfully uploaded.";
80  			} else {
81  				message = "This file looks like any other format than CSV or MS Excel.";
82  			}
83  		} catch (IOException ioe) {
84  			// do nothing
85  		} catch (TgwServiceException tse) {
86  			tse.printStackTrace();
87  			// do nothing
88  		}
89  
90  		TgwContext.bindPageName(dto.getPageName());
91  		
92  		pluginService.sendMessageByAction(getClass(), message);
93  		return WebAppli.THIS_PAGE;
94  	}
95  
96  	/***
97  	 * データベース処理を行う
98  	 */
99  	private void loadFile(TgwEntity entity, DatafileLoader loader, String type)
100 			throws IOException, TgwServiceException {
101 
102 		String[] columns = EntityUtils.getColumns(entity);
103 		String[] types = EntityUtils.getColumnTypes(entity);
104 		String domainName = entity.getDomainName();
105 		String schemaName = basicDatabaseService.getSchemaName(domainName);
106 		String tableName = TgwUtils.toTableName(entity);
107 
108 		SiteConfig siteConfig = TgwContext.getSiteConfig();
109 		String dbName = siteConfig.getDbName();
110 
111 		if (ExcelUpDto.UPLOADTYPE_CLEAN.equals(type)) {
112 			basicDatabaseService.insertOnDelete(dbName, schemaName, tableName,
113 					loader, columns, types);
114 		} else {
115 			basicDatabaseService.insert(dbName, schemaName, tableName, loader,
116 					columns, types);
117 		}
118 	}
119 
120 	private DatafileLoader getDatafileLoader(String fileName,
121 			String contentType, InputStream is) {
122 		DatafileLoader loader = null;
123 		String ext = PathUtils.getExtension(fileName);
124 		if (MimeMappings.CONTENTTYPE_XLS.equals(contentType)
125 				|| MimeMappings.EXTENSION_XLS.equals(ext)) {
126 			loader = new ExcelfileLoader(is);
127 		} else if (MimeMappings.CONTENTTYPE_CSV.equals(contentType)
128 				|| MimeMappings.EXTENSION_CSV.equals(ext)) {
129 			loader = new CSVfileLoader(is);
130 		}
131 		return loader;
132 	}
133 }