- 코드 리팩토링 (csv 익스포트 관련 코드 정리)
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / sql / util / export / CSVExpoter.java
blob497b1eb3012e546f02c32cf0f1c9417ca21fc3ea
1 /*******************************************************************************
2 * Copyright (c) 2015 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.engine.sql.util.export;
13 import java.io.File;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
18 import org.apache.commons.io.FileUtils;
19 import org.apache.log4j.Logger;
21 import com.hangum.tadpole.commons.util.CSVUtils;
22 import com.hangum.tadpole.engine.sql.util.SQLUtil;
23 import com.hangum.tadpole.engine.sql.util.resultset.QueryExecuteResultDTO;
25 /**
26 * CSV 유틸
28 * @author hangum
31 public class CSVExpoter extends AbstractTDBExporter {
32 private static final Logger logger = Logger.getLogger(CSVExpoter.class);
34 public static String makeContent(boolean isAddHead, QueryExecuteResultDTO queryExecuteResultDTO, char seprator, String strDefaultNullValue) throws Exception {
35 return makeContent(isAddHead, queryExecuteResultDTO, seprator, -1, strDefaultNullValue);
39 /**
40 * make file header
42 * @param strFullPath
43 * @param isAddHead
44 * @param listCsvData
45 * @param seprator
46 * @param encoding
47 * @throws Exception
49 public static void makeListFile(String strFullPath, boolean isAddHead, List<String[]> listCsvData, char seprator, String encoding) throws Exception {
50 // make header
51 FileUtils.writeByteArrayToFile(new File(strFullPath), CSVUtils.makeData(listCsvData, seprator), true);
55 /**
56 * make file header
58 * @param strFullPath
59 * @param isAddHead
60 * @param listCsvData
61 * @param seprator
62 * @param encoding
63 * @throws Exception
65 public static byte[] makeListFileStream( List<String[]> listCsvData, char seprator, String encoding) throws Exception {
66 return CSVUtils.makeData(listCsvData, seprator);
69 /**
70 * make file header
72 * @param strFullPath
73 * @param isAddHead
74 * @param rsDAO
75 * @param seprator
76 * @param encoding
77 * @throws Exception
79 public static void makeHeaderFile(String strFullPath, boolean isAddHead, QueryExecuteResultDTO rsDAO, char seprator, String encoding) throws Exception {
80 FileUtils.writeByteArrayToFile(new File(strFullPath), makeHeader(isAddHead, rsDAO, seprator), true);
83 /**
84 * make csv header
86 * @param isAddHead
87 * @param rsDAO
88 * @param seprator
89 * @return
90 * @throws Exception
92 public static byte[] makeHeader(boolean isAddHead, QueryExecuteResultDTO rsDAO, char seprator) throws Exception {
93 List<String[]> listCsvData = new ArrayList<String[]>();
95 if(isAddHead) {
96 // column .
97 Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
98 List<String> listLabel = new ArrayList<String>();
100 for(int i=0; i<mapLabelName.size(); i++) {
101 String strLabelName = mapLabelName.get(i);
102 if(!SQLUtil.isTDBSpecialColumn(strLabelName)) {
103 listLabel.add(strLabelName);
106 listCsvData.add(listLabel.toArray(new String[listLabel.size()]));
109 return CSVUtils.makeData(listCsvData, seprator);
113 * make content
115 * @param tableName
116 * @param rsDAO
117 * @param intLimitCnt
118 * @return
120 public static String makeContent(boolean isAddHead, QueryExecuteResultDTO rsDAO, char seprator, int intLimitCnt, String strDefaultNullValue) throws Exception {
122 // make header
123 StringBuffer sbReturn = new StringBuffer();
124 sbReturn.append(makeHeader(isAddHead, rsDAO, seprator));
126 // data
127 List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();
128 List<String[]> listCsvData = new ArrayList<String[]>();
129 List<String> listLabel = new ArrayList<String>();
131 // column name
132 Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
134 for(int i=0; i<dataList.size(); i++) {
135 Map<Integer, Object> mapColumns = dataList.get(i);
137 listLabel.clear();
138 for(int j=0; j<mapColumns.size(); j++) {
139 // tdb 내부적으로 사용하는 컬럼을 보이지 않도록 합니다.
140 if(!SQLUtil.isTDBSpecialColumn(mapLabelName.get(j))) {
141 listLabel.add(mapColumns.get(j) == null?strDefaultNullValue:""+mapColumns.get(j));
144 listCsvData.add(listLabel.toArray(new String[listLabel.size()]));
146 sbReturn.append(CSVUtils.makeData(listCsvData, seprator));
147 listCsvData.clear();
148 if (intLimitCnt == i) break;
151 return sbReturn.toString();
155 * csv 파일을 생성하여 파일 위치를 넘겨줍니다.
157 * @param tableName
158 * @param rsDAO
159 * @param seprator
160 * @param encoding
161 * @return 파일 위치
162 * @return strDefaultNullValue
164 * @throws Exception
166 public static void makeContentFile(String strFullPath, boolean isAddHead, QueryExecuteResultDTO rsDAO, char seprator, String encoding, String strDefaultNullValue) throws Exception {
167 // data
168 List<Map<Integer, Object>> dataList = rsDAO.getDataList().getData();
169 List<String[]> listCsvData = new ArrayList<String[]>();
170 List<String> listValues = new ArrayList<String>();
172 // column name
173 Map<Integer, String> mapLabelName = rsDAO.getColumnLabelName();
175 for(int i=0; i<dataList.size(); i++) {
176 Map<Integer, Object> mapColumns = dataList.get(i);
178 listValues.clear();
179 for(int j=0; j<mapColumns.size(); j++) {
181 // tdb 내부적으로 사용하는 컬럼을 보이지 않도록 합니다.
182 if(!SQLUtil.isTDBSpecialColumn(mapLabelName.get(j))) {
183 listValues.add(mapColumns.get(j) == null?strDefaultNullValue:""+mapColumns.get(j));
186 listCsvData.add(listValues.toArray(new String[listValues.size()]));
189 FileUtils.writeByteArrayToFile(new File(strFullPath), CSVUtils.makeData(listCsvData, seprator), true);