- 코드 리팩토링 (csv 익스포트 관련 코드 정리)
[Tadpole.git] / com.hangum.tadpole.rdb.core / src / com / hangum / tadpole / rdb / core / editors / dbinfos / composites / TablesComposite.java
blobc777624d66b20c48dadb2bfe799d65f5e277f553
1 /*******************************************************************************
2 * Copyright (c) 2013 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.rdb.core.editors.dbinfos.composites;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ArrayContentProvider;
24 import org.eclipse.jface.viewers.ITableLabelProvider;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.TableViewerColumn;
28 import org.eclipse.jface.viewers.Viewer;
29 import org.eclipse.jface.viewers.ViewerFilter;
30 import org.eclipse.rap.rwt.RWT;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.KeyAdapter;
33 import org.eclipse.swt.events.KeyEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.Text;
45 import com.hangum.tadpole.commons.exception.dialog.ExceptionDetailsErrorDialog;
46 import com.hangum.tadpole.commons.google.analytics.AnalyticCaller;
47 import com.hangum.tadpole.commons.libs.core.message.CommonMessages;
48 import com.hangum.tadpole.commons.util.CSVUtils;
49 import com.hangum.tadpole.commons.util.NumberFormatUtils;
50 import com.hangum.tadpole.commons.util.download.DownloadServiceHandler;
51 import com.hangum.tadpole.commons.util.download.DownloadUtils;
52 import com.hangum.tadpole.engine.define.DBGroupDefine;
53 import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
54 import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
55 import com.hangum.tadpole.rdb.core.Activator;
56 import com.hangum.tadpole.rdb.core.Messages;
57 import com.hangum.tadpole.rdb.core.editors.dbinfos.RDBDBInfosEditor;
58 import com.ibatis.sqlmap.client.SqlMapClient;
60 /**
61 * Tables composite
63 * @author hangum
66 public class TablesComposite extends DBInfosComposite {
67 /**
68 * Logger for this class
70 private static final Logger logger = Logger.getLogger(TablesComposite.class);
72 private UserDBDAO userDB;
73 private TableViewer tvTableInform;
75 private TableInfoFilter tableFilter;
76 private Text textFilter;
78 /** download service handler. */
79 private Composite compositeTail;
80 private DownloadServiceHandler downloadServiceHandler;
81 private List listTableInform = new ArrayList<>();
83 /**
84 * Create the composite.
85 * @param parent
86 * @param style
88 public TablesComposite(Composite parent, int style, UserDBDAO userDB) {
89 super(parent, style);
90 setLayout(new GridLayout(1, false));
92 this.userDB = userDB;
94 Composite compositeHead = new Composite(this, SWT.NONE);
95 compositeHead.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
96 compositeHead.setLayout(new GridLayout(3, false));
98 Label lblNewLabel = new Label(compositeHead, SWT.NONE);
99 lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
100 lblNewLabel.setText(CommonMessages.get().Filter);
102 textFilter = new Text(compositeHead, SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
103 textFilter.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
104 textFilter.addKeyListener(new KeyAdapter() {
105 public void keyReleased(KeyEvent e) {
106 tableFilter.setSearchString(textFilter.getText());
107 tvTableInform.refresh();
111 Button btnRefresh = new Button(compositeHead, SWT.NONE);
112 btnRefresh.addSelectionListener(new SelectionAdapter() {
113 @Override
114 public void widgetSelected(SelectionEvent e) {
115 initUI(true);
118 btnRefresh.setText(CommonMessages.get().Refresh);
120 tvTableInform = new TableViewer(this, /* SWT.VIRTUAL | */ SWT.BORDER | SWT.FULL_SELECTION);
121 Table table = tvTableInform.getTable();
122 table.setHeaderVisible(true);
123 table.setLinesVisible(true);
124 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
126 createColumn();
128 tvTableInform.setContentProvider(new ArrayContentProvider());
129 tvTableInform.setLabelProvider(new TableInformLabelProvider(userDB));
131 tableFilter = new TableInfoFilter();
132 tvTableInform.addFilter(tableFilter);
134 compositeTail = new Composite(this, SWT.NONE);
135 compositeTail.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
136 compositeTail.setLayout(new GridLayout(1, false));
138 Button btnCsvExport = new Button(compositeTail, SWT.NONE);
139 btnCsvExport.addSelectionListener(new SelectionAdapter() {
140 @Override
141 public void widgetSelected(SelectionEvent e) {
142 download();
145 btnCsvExport.setBounds(0, 0, 94, 28);
146 btnCsvExport.setText(Messages.get().TablesComposite_btnCsvExport_text);
147 btnCsvExport.setEnabled("YES".equals(userDB.getIs_resource_download()));
149 // initUI();
150 registerServiceHandler();
154 * table column head를 생성합니다.
156 private void createColumn() {
157 if(DBGroupDefine.MYSQL_GROUP == userDB.getDBGroup()) {
158 String[] name = {"Name", "Engine", "Rows", "Auto Increment", "collation", "Size(MB)", "Created", "Comment"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
159 int[] size = {120, 70, 70, 100, 80, 80, 120, 220};
160 int[] align = {SWT.LEFT, SWT.LEFT, SWT.RIGHT, SWT.RIGHT, SWT.LEFT, SWT.RIGHT, SWT.RIGHT, SWT.LEFT};
162 createColumn(name, size, align);
163 } else if(DBGroupDefine.ORACLE_GROUP == userDB.getDBGroup()) {
164 String[] name = {"Table Name","Tablespace Name","Pct Free","Ini Trans","Logging","Num Rows","Blocks","Avg Row Len","Degree","Sample Size","Last Analyzed","Partitioned","Buffer Pool","Row Movement","Duration","Compression","Dropped","Read Only","Temporary","Max Extents","Iot Type","Initial Extent","Next Extent","Min Extents"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
165 int[] size = {120, 120, 90, 90, 52 , 90, 90, 90, 80 , 90, 120, 52 , 68 , 72 , 100, 72 , 52 , 52 , 40 , 90, 88 , 90, 90, 90};
166 int[] align = {SWT.LEFT ,SWT.LEFT ,SWT.RIGHT ,SWT.RIGHT ,SWT.LEFT ,SWT.RIGHT ,SWT.RIGHT ,SWT.RIGHT ,SWT.RIGHT ,SWT.RIGHT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.LEFT ,SWT.RIGHT ,SWT.LEFT ,SWT.RIGHT ,SWT.RIGHT ,SWT.RIGHT};
169 createColumn(name, size, align);
170 } else if(DBGroupDefine.ALTIBASE_GROUP == userDB.getDBGroup()) {
171 String[] name = {"Table Name", "Owner", "Rows", "Tablespace Name", "Character Set", "Size (MB)", "Created", "Comment"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
172 int[] size = {200, 70, 70, 200, 100, 80, 120, 250};
173 int[] align = {SWT.LEFT, SWT.LEFT, SWT.RIGHT, SWT.LEFT, SWT.LEFT, SWT.RIGHT, SWT.RIGHT, SWT.LEFT};
175 createColumn(name, size, align);
176 } else {
177 String[] name = {"Name", "comment", "Index", "Shared", "Primary Key", "Triggers", "Sub Class", "Rules", "Option"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
178 int[] size = {120, 150, 60, 60, 100, 80, 80, 60, 60};
179 int[] align = {SWT.LEFT, SWT.LEFT, SWT.CENTER, SWT.CENTER, SWT.CENTER, SWT.CENTER, SWT.CENTER, SWT.CENTER, SWT.CENTER};
181 createColumn(name, size, align);
186 * 실제 컬럼의 헤더를 생성합니다.
188 * @param name
189 * @param size
191 private void createColumn(String[] name, int[] size, int[] align) {
192 for (int i=0; i<name.length; i++) {
193 TableViewerColumn tableColumn = new TableViewerColumn(tvTableInform, align[i]);
194 tableColumn.getColumn().setText(name[i]);
195 tableColumn.getColumn().setWidth(size[i]);
200 * initialize ui data
202 public void initUI(boolean isRefresh) {
203 if(isRefresh) {
204 listTableInform.clear();
205 } else {
206 if(listTableInform.size() != 0) return;
209 try {
210 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
211 if (DBGroupDefine.ORACLE_GROUP == userDB.getDBGroup()){
212 HashMap<String, String>paramMap = new HashMap<String, String>();
213 paramMap.put("schema_name", userDB.getSchema()); //$NON-NLS-1$
214 listTableInform = sqlClient.queryForList("tableInformation", paramMap); //$NON-NLS-1$ //$NON-NLS-2$
215 } else if (DBGroupDefine.MYSQL_GROUP == userDB.getDBGroup()){
216 HashMap<String, String>paramMap = new HashMap<String, String>();
217 paramMap.put("schema_name", userDB.getSchema()); //$NON-NLS-1$
218 listTableInform = sqlClient.queryForList("tableInformation", paramMap); //$NON-NLS-1$ //$NON-NLS-2$
219 } else {
220 listTableInform = sqlClient.queryForList("tableInformation", userDB.getDb()); //$NON-NLS-1$ //$NON-NLS-2$
223 tvTableInform.setInput(listTableInform);
224 tvTableInform.refresh();
225 } catch (Exception e) {
226 logger.error("Initialize session list", e); //$NON-NLS-1$
228 Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); //$NON-NLS-1$
229 ExceptionDetailsErrorDialog.openError(null,CommonMessages.get().Error, Messages.get().MainEditor_19, errStatus); //$NON-NLS-1$
232 // google analytic
233 AnalyticCaller.track(RDBDBInfosEditor.ID, "TablesComposite"); //$NON-NLS-1$
236 /** download service handler call */
237 private void unregisterServiceHandler() {
238 RWT.getServiceManager().unregisterServiceHandler(downloadServiceHandler.getId());
239 downloadServiceHandler = null;
243 * download
245 private void download() {
246 if(tvTableInform.getTable().getItemCount() == 0) return;
247 if(!MessageDialog.openConfirm(null, CommonMessages.get().Confirm, Messages.get().TablesComposite_3)) return;
249 try {
250 byte[] strCVSContent = CSVUtils.tableToCSV(tvTableInform.getTable());//CSVUtils.makeData(listCsvData);
251 downloadExtFile(userDB.getDisplay_name() + "_TableInformation.csv", strCVSContent); //$NON-NLS-1$
253 MessageDialog.openInformation(null, CommonMessages.get().Information, Messages.get().TablesComposite_5);
254 } catch (Exception e) {
255 logger.error("Save CSV Data", e); //$NON-NLS-1$
260 * download external file
262 * @param fileName
263 * @param newContents
265 public void downloadExtFile(String fileName, byte[] newContents) {
266 downloadServiceHandler.setName(fileName);
267 downloadServiceHandler.setByteContent(newContents);
269 DownloadUtils.provideDownload(compositeTail, downloadServiceHandler.getId());
272 /** Register a service handler */
273 private void registerServiceHandler() {
274 downloadServiceHandler = new DownloadServiceHandler();
275 RWT.getServiceManager().registerServiceHandler(downloadServiceHandler.getId(), downloadServiceHandler);
278 public void dispose() {
279 unregisterServiceHandler();
280 super.dispose();
286 * mysql, mariadb label provider
287 * @author hangum
290 class TableInformLabelProvider extends LabelProvider implements ITableLabelProvider {
291 private UserDBDAO userDB;
293 public TableInformLabelProvider(UserDBDAO userDB) {
294 this.userDB = userDB;
297 @Override
298 public Image getColumnImage(Object element, int columnIndex) {
299 return null;
302 @Override
303 public String getColumnText(Object element, int columnIndex) {
304 Map resultMap = (HashMap)element;
306 if(DBGroupDefine.MYSQL_GROUP == userDB.getDBGroup()) {
307 switch(columnIndex) {
308 case 0: return ""+resultMap.get("TABLE_NAME"); //$NON-NLS-1$ //$NON-NLS-2$
309 case 1: return ""+resultMap.get("ENGINE"); //$NON-NLS-1$ //$NON-NLS-2$
310 case 2: return NumberFormatUtils.commaFormat(""+resultMap.get("TABLE_ROWS")); //$NON-NLS-1$ //$NON-NLS-2$
311 case 3: return NumberFormatUtils.commaFormat(StringUtils.replace(""+resultMap.get("AUTO_INCREMENT"), "null", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
312 case 4: return ""+resultMap.get("TABLE_COLLATION"); //$NON-NLS-1$ //$NON-NLS-2$
313 case 5: return ""+resultMap.get("SIZEOFMB"); //$NON-NLS-1$ //$NON-NLS-2$
314 case 6: return ""+resultMap.get("CREATE_TIME"); //$NON-NLS-1$ //$NON-NLS-2$
315 case 7: return ""+resultMap.get("TABLE_COMMENT"); //$NON-NLS-1$ //$NON-NLS-2$
317 } else if(DBGroupDefine.ORACLE_GROUP == userDB.getDBGroup()) {
318 switch(columnIndex) {
319 case 0 : return "" + resultMap.get("TABLE_NAME" ); //$NON-NLS-1$ //$NON-NLS-2$
320 case 1 : return "" + resultMap.get("TABLESPACE_NAME" ); //$NON-NLS-1$ //$NON-NLS-2$
321 case 2 : return NumberFormatUtils.commaFormat("" + resultMap.get("PCT_FREE" )); //$NON-NLS-1$ //$NON-NLS-2$
322 case 3 : return NumberFormatUtils.commaFormat("" + resultMap.get("INI_TRANS" )); //$NON-NLS-1$ //$NON-NLS-2$
323 case 4 : return "" + resultMap.get("LOGGING" ); //$NON-NLS-1$ //$NON-NLS-2$
324 case 5 : return NumberFormatUtils.commaFormat("" + resultMap.get("NUM_ROWS" )); //$NON-NLS-1$ //$NON-NLS-2$
325 case 6 : return NumberFormatUtils.commaFormat("" + resultMap.get("BLOCKS" )); //$NON-NLS-1$ //$NON-NLS-2$
326 case 7 : return NumberFormatUtils.commaFormat("" + resultMap.get("AVG_ROW_LEN" )); //$NON-NLS-1$ //$NON-NLS-2$
327 case 8 : return NumberFormatUtils.commaFormat("" + resultMap.get("DEGREE" )); //$NON-NLS-1$ //$NON-NLS-2$
328 case 9 : return NumberFormatUtils.commaFormat("" + resultMap.get("SAMPLE_SIZE" )); //$NON-NLS-1$ //$NON-NLS-2$
329 case 10: return "" + resultMap.get("LAST_ANALYZED" ); //$NON-NLS-1$ //$NON-NLS-2$
330 case 11: return "" + resultMap.get("PARTITIONED" ); //$NON-NLS-1$ //$NON-NLS-2$
331 case 12: return "" + resultMap.get("BUFFER_POOL" ); //$NON-NLS-1$ //$NON-NLS-2$
332 case 13: return "" + resultMap.get("ROW_MOVEMENT" ); //$NON-NLS-1$ //$NON-NLS-2$
333 case 14: return "" + resultMap.get("DURATION" ); //$NON-NLS-1$ //$NON-NLS-2$
334 case 15: return "" + resultMap.get("COMPRESSION" ); //$NON-NLS-1$ //$NON-NLS-2$
335 case 16: return "" + resultMap.get("DROPPED" ); //$NON-NLS-1$ //$NON-NLS-2$
336 case 17: return "" + resultMap.get("READ_ONLY" ); //$NON-NLS-1$ //$NON-NLS-2$
337 case 18: return "" + resultMap.get("TEMPORARY" ); //$NON-NLS-1$ //$NON-NLS-2$
338 case 19: return NumberFormatUtils.commaFormat("" + resultMap.get("MAX_EXTENTS" )); //$NON-NLS-1$ //$NON-NLS-2$
339 case 20: return "" + resultMap.get("IOT_TYPE" ); //$NON-NLS-1$ //$NON-NLS-2$
340 case 21: return NumberFormatUtils.commaFormat("" + resultMap.get("INITIAL_EXTENT" )); //$NON-NLS-1$ //$NON-NLS-2$
341 case 22: return NumberFormatUtils.commaFormat("" + resultMap.get("NEXT_EXTENT" )); //$NON-NLS-1$ //$NON-NLS-2$
342 case 23: return NumberFormatUtils.commaFormat("" + resultMap.get("MIN_EXTENTS" )); //$NON-NLS-1$ //$NON-NLS-2$
344 } else if(DBGroupDefine.ALTIBASE_GROUP == userDB.getDBGroup()) {
345 switch(columnIndex) {
346 case 0: return ""+resultMap.get("TABLE_NAME"); //$NON-NLS-1$ //$NON-NLS-2$
347 case 1: return ""+resultMap.get("OWNER"); //$NON-NLS-1$ //$NON-NLS-2$
348 case 2: return NumberFormatUtils.commaFormat(""+resultMap.get("TABLE_ROWS")); //$NON-NLS-1$ //$NON-NLS-2$
349 case 3: return NumberFormatUtils.commaFormat(StringUtils.replace(""+resultMap.get("TABLESPACE_NAME"), "null", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
350 case 4: return ""+resultMap.get("CHARACTER_SET"); //$NON-NLS-1$ //$NON-NLS-2$
351 case 5: return ""+resultMap.get("SIZEOFMB"); //$NON-NLS-1$ //$NON-NLS-2$
352 case 6: return ""+resultMap.get("CREATED"); //$NON-NLS-1$ //$NON-NLS-2$
353 case 7: return ""+resultMap.get("TABLE_COMMENT"); //$NON-NLS-1$ //$NON-NLS-2$
355 } else {
356 switch(columnIndex) {
357 case 0: return ""+resultMap.get("name"); //$NON-NLS-1$ //$NON-NLS-2$
358 case 1: return StringUtils.replace(""+resultMap.get("comment"), "null", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
359 case 2: return "true".equals(""+resultMap.get("has_index")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
360 case 3: return "true".equals(""+resultMap.get("is_shared")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
361 case 4: return "true".equals(""+resultMap.get("has_pk")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
362 case 5: return "true".equals(""+resultMap.get("has_triggers")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
363 case 6: return "true".equals(""+resultMap.get("has_subclass")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
364 case 7: return "true".equals(""+resultMap.get("has_rules")) ? "has" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
365 case 8: return StringUtils.replace(""+resultMap.get("options"), "null", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
369 return "*** Invalid column index ***"; //$NON-NLS-1$
375 * name filter
377 * @author hangum
380 class TableInfoFilter extends ViewerFilter {
381 String searchString;
383 public void setSearchString(String s) {
384 this.searchString = ".*" + s + ".*"; //$NON-NLS-1$ //$NON-NLS-2$
387 @Override
388 public boolean select(Viewer viewer, Object parentElement, Object element) {
389 if(searchString == null || searchString.length() == 0) {
390 return true;
393 Map resultMap = (HashMap)element;
394 String tbName = ""; //$NON-NLS-1$
395 if(resultMap.get("TABLE_NAME") != null) tbName = ""+resultMap.get("TABLE_NAME"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
396 else tbName = ""+resultMap.get("name"); //$NON-NLS-1$ //$NON-NLS-2$
398 if(tbName.matches(searchString)) return true;
399 return false;