- initialize netezza
[Tadpole.git] / com.hangum.tadpole.rdb.core / src / com / hangum / tadpole / rdb / core / dialog / dbconnect / composite / AbstractLoginComposite.java
blobd515b9648f35af889437f9924d08000a424b1049
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.dialog.dbconnect.composite;
13 import java.io.File;
14 import java.sql.SQLException;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.apache.commons.io.FileUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.swt.widgets.Composite;
27 import com.hangum.tadpole.commons.exception.TadpoleSQLManagerException;
28 import com.hangum.tadpole.commons.exception.dialog.ExceptionDetailsErrorDialog;
29 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
30 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine.DATA_STATUS;
31 import com.hangum.tadpole.commons.libs.core.message.CommonMessages;
32 import com.hangum.tadpole.commons.libs.core.utils.ValidChecker;
33 import com.hangum.tadpole.engine.define.DBDefine;
34 import com.hangum.tadpole.engine.define.DBGroupDefine;
35 import com.hangum.tadpole.engine.manager.TadpoleSQLExtManager;
36 import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
37 import com.hangum.tadpole.engine.query.dao.ManagerListDTO;
38 import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
39 import com.hangum.tadpole.engine.query.sql.TadpoleSystem_UserDBQuery;
40 import com.hangum.tadpole.mongodb.core.connection.MongoConnectionManager;
41 import com.hangum.tadpole.rdb.core.Activator;
42 import com.hangum.tadpole.rdb.core.Messages;
43 import com.hangum.tadpole.rdb.core.dialog.dbconnect.sub.PreConnectionInfoGroup;
44 import com.hangum.tadpole.rdb.core.dialog.dbconnect.sub.others.OthersConnectionGroup;
45 import com.hangum.tadpole.rdb.core.dialog.dbconnect.sub.others.dao.OthersConnectionInfoDAO;
46 import com.hangum.tadpole.rdb.core.dialog.msg.TDBErroDialog;
47 import com.hangum.tadpole.rdb.core.dialog.msg.TDBYesNoErroDialog;
48 import com.hangum.tadpole.session.manager.SessionManager;
49 import com.ibatis.sqlmap.client.SqlMapClient;
51 /**
52 * 로그인시에 사용하게될 디비의 abstract composite
54 * @author hangum
57 public abstract class AbstractLoginComposite extends Composite {
58 /**
61 private static final long serialVersionUID = -3434604591881525231L;
62 private static final Logger logger = Logger.getLogger(AbstractLoginComposite.class);
64 /** 입력 항목을 읽기 전용으로 만들것인지 여무 */
65 protected boolean isReadOnly = false;
67 /** 현재 창의 저장, 수정 상태인지 상태. */
68 protected DATA_STATUS dataActionStatus = DATA_STATUS.NEW;
70 protected String displayName = ""; //$NON-NLS-1$
72 protected PreConnectionInfoGroup preDBInfo;
73 protected OthersConnectionGroup othersConnectionInfo;
75 protected String strOtherGroupName = CommonMessages.get().Others;
76 protected String selGroupName = ""; //$NON-NLS-1$
78 protected List<String> listGroupName = new ArrayList<String>();
80 // start table filters define
81 protected boolean isTableFilter = false;
82 protected String strTableFilterInclude = ""; //$NON-NLS-1$
83 protected String strTableFilterExclude = ""; //$NON-NLS-1$
84 // end table filters define
86 /** 기존에 접속한 user db */
87 protected UserDBDAO oldUserDB = null;
89 protected UserDBDAO userDB;
90 protected DBDefine selectDB;
92 /**
93 * Create the composite.
94 * @param displayName
95 * @param parent
96 * @param style
98 public AbstractLoginComposite(String displayName, DBDefine dbDefine, Composite parent, int style, List<String> listGroupName, String selGroupName, UserDBDAO oldUserDB, boolean isUIReadOnly) {
99 super(parent, style);
101 this.displayName = displayName;
102 this.selectDB = dbDefine;
103 this.listGroupName = listGroupName;
104 this.selGroupName = selGroupName;
105 this.oldUserDB = oldUserDB;
106 this.isReadOnly = isUIReadOnly;
108 crateComposite();
112 * 현재 창의 저장, 수정 상태인지 상태.
113 * @return
115 public DATA_STATUS getDataActionStatus() {
116 return dataActionStatus;
120 * dialog 상태 신규데이터 인지 수정상태인지.
122 * @param dalog_status
124 public void setDataActionStatus(DATA_STATUS dalog_status) {
125 this.dataActionStatus = dalog_status;
129 * ping test
131 * @param host
132 * @param port
134 protected void pingTest(String host, String port) {
135 host = StringUtils.trimToEmpty(host);
136 port = StringUtils.trimToEmpty(port);
138 if("".equals(host) || "".equals(port)) { //$NON-NLS-1$ //$NON-NLS-2$
139 MessageDialog.openWarning(null, CommonMessages.get().Warning, String.format(Messages.get().DBLoginDialog_11, Messages.get().Host, Messages.get().Port));
140 return;
143 try {
144 if(ValidChecker.isPing(host, port)) {
145 MessageDialog.openInformation(null, CommonMessages.get().Confirm, Messages.get().DBLoginDialog_13);
146 } else {
147 MessageDialog.openWarning(null, CommonMessages.get().Warning, Messages.get().DBLoginDialog_15);
149 } catch(NumberFormatException nfe) {
150 MessageDialog.openWarning(null, CommonMessages.get().Warning, Messages.get().MySQLLoginComposite_4);
155 * 화면을 읽기 전용으로 만들것인지 여부
156 * @return
158 public boolean isReadOnly() {
159 return isReadOnly;
164 * @param parent
165 * @return
167 protected abstract void crateComposite();
170 * 초기데이터 로드 및 처리
172 protected abstract void init();
175 * DB 정보 저장.
177 * @return
178 * @throws Exception
180 public boolean saveDBData() {
181 if(!testConnection(false)) return false;
183 // 기존 데이터 업데이트
184 if(getDataActionStatus() == DATA_STATUS.MODIFY) {
185 if(!MessageDialog.openConfirm(null, CommonMessages.get().Confirm, Messages.get().SQLiteLoginComposite_13)) return false; //$NON-NLS-1$
187 try {
188 TadpoleSystem_UserDBQuery.updateUserDB(userDB, oldUserDB, SessionManager.getUserSeq());
189 } catch (Exception e) {
190 logger.error("DB moidfy data", e);
191 Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); //$NON-NLS-1$
192 ExceptionDetailsErrorDialog.openError(getShell(),CommonMessages.get().Error, Messages.get().SQLiteLoginComposite_5, errStatus); //$NON-NLS-1$
194 return false;
197 // 신규 데이터 저장.
198 } else {
199 try {
200 TadpoleSystem_UserDBQuery.newUserDB(userDB, SessionManager.getUserSeq());
201 } catch (Exception e) {
202 logger.error("Add database", e);
203 Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); //$NON-NLS-1$
204 ExceptionDetailsErrorDialog.openError(getShell(),CommonMessages.get().Error, Messages.get().MySQLLoginComposite_2, errStatus); //$NON-NLS-1$
206 return false;
210 return true;
214 * input validation
216 * @return
218 public abstract boolean isValidateInput(boolean isTest);
221 * test connection
223 * @param isTest
224 * @return
225 * @throws Exception
227 public boolean testConnection(boolean isTest) {
228 if(!makeUserDBDao(isTest)) return false;
229 if(!isValidateDatabase(userDB, isTest)) return false;
231 return true;
235 * 1. input validation
236 * 2. make UserDBDAO
238 public abstract boolean makeUserDBDao(boolean isTest);
241 * DB 가 정상 연결되었을때 객체
243 * @return
245 public UserDBDAO getDBDTO() {
246 return userDB;
250 * database의 중복 입력, 실제 연결할 수 있는지 검사합니다.
252 * @param userDB
253 * @param isTest
254 * @return
256 protected boolean isValidateDatabase(final UserDBDAO userDB, boolean isTest) {
257 if(!checkDatabase(userDB, isTest)) return false;
258 if(!isTest) if(!isAlreadExistDB(userDB)) return false;
260 return true;
264 * db가 이미 저장되어 있는지 검사합니다.
266 * @param userDBDao
267 * @param searchTable 디비의 테이블 검증을위한 쿼리
268 * @return
270 private boolean isAlreadExistDB(UserDBDAO userDBDao) {
272 try {
273 // 기존 데이터 업데이트
274 if(getDataActionStatus() == DATA_STATUS.MODIFY) {
275 // 그룹 이름과 디스플레이스 이름이 같은지 검사한다.
276 if(TadpoleSystem_UserDBQuery.isOldDBValidate(SessionManager.getUserSeq(), userDBDao, oldUserDB)) {
277 if(!MessageDialog.openConfirm(null, CommonMessages.get().Warning, Messages.get().AbstractLoginComposite_4)) {
278 return false;
282 } else {
283 // 그룹 이름과 디스플레이스 이름이 같은지 검사한다.
284 if(isNewDBValidate(SessionManager.getUserSeq(), userDBDao)) {
285 if(!MessageDialog.openConfirm(null, CommonMessages.get().Warning, Messages.get().AbstractLoginComposite_4)) {
286 return false;
291 } catch(Exception e) {
292 logger.error("DB Connecting... ", e); //$NON-NLS-1$
293 MessageDialog.openError(null,CommonMessages.get().Error, Messages.get().DBLoginDialog_27 + "\n" + e.getMessage()); //$NON-NLS-1$
295 return false;
298 return true;
302 * 이미 등록 되어 있는 디비 중에 ip, port, user가 같은 것이 있는지 검사합니다.
304 * @param user_seq
305 * @param userDBDao
306 * @return
307 * @throws TadpoleSQLManagerException, SQLException
309 private static boolean isNewDBValidate(int user_seq, UserDBDAO userDBDao) throws TadpoleSQLManagerException, SQLException {
310 for (ManagerListDTO managerListDTO : SessionManager.getManagerDBList()) {
311 for (UserDBDAO tmpUserDB : managerListDTO.getManagerList()) {
312 if(DBGroupDefine.DYNAMODB_GROUP == userDBDao.getDBGroup()) {
313 if(StringUtils.equals(userDBDao.getUsers(), tmpUserDB.getUsers()) && StringUtils.equals(userDBDao.getDb(), tmpUserDB.getDb())) {
314 return true;
316 } else {
317 if(StringUtils.equals(userDBDao.getHost(), tmpUserDB.getHost()) && StringUtils.equals(userDBDao.getPort(), tmpUserDB.getPort()) && StringUtils.equals(userDBDao.getUsers(), tmpUserDB.getUsers())) {
318 return true;
324 return false;
328 * db가 정상적으로 접속가능한지 검사합니다.
330 * @param userDB
331 * @param isTest
332 * @return
334 private boolean checkDatabase(final UserDBDAO userDB, boolean isTest) {
335 try {
336 if(userDB.getDBDefine() == DBDefine.MONGODB_DEFAULT) {
337 MongoConnectionManager.getInstance(userDB);
339 // } else if(userDB.getDBDefine() == DBDefine.TAJO_DEFAULT) {
340 // new TajoConnectionManager().connectionCheck(userDB);
341 } else if(userDB.getDBDefine() == DBDefine.HIVE2_DEFAULT || userDB.getDBDefine() == DBDefine.DYNAMODB_DEFAULT) {
342 TadpoleSQLExtManager.getInstance().connectionCheck(userDB);
343 } else if(userDB.getDBDefine() == DBDefine.SQLite_DEFAULT) {
344 String strFileLoc = StringUtils.difference(StringUtils.remove(userDB.getDBDefine().getDB_URL_INFO(), "%s"), userDB.getUrl());
345 File fileDB = new File(strFileLoc);
346 if(fileDB.exists()) {
347 List<String> strArr = FileUtils.readLines(fileDB);
349 if(!StringUtils.contains(strArr.get(0), "SQLite format")) {
350 throw new SQLException("Doesn't SQLite files.");
353 } else {
354 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
355 sqlClient.queryForList("connectionCheck", userDB.getDb()); //$NON-NLS-1$
358 return true;
359 } catch (Exception e) {
360 String errMsg = e.getMessage();
362 // If UserDBDao is not invalid, remove UserDBDao at internal cache
363 logger.error("DB Connecting... [url]"+ userDB.getUrl(), e); //$NON-NLS-1$
364 TadpoleSQLManager.removeInstance(userDB);
366 // driver 가 없을때 메시지 추가.
367 try {
368 Throwable cause = e.getCause().getCause();
369 if(cause instanceof ClassNotFoundException) {
370 errMsg = String.format(Messages.get().TadpoleTableComposite_driverMsg, userDB.getDbms_type(), e.getMessage());
372 } catch(Exception ee) {
373 // igonre exception
376 // mssql 데이터베이스가 연결되지 않으면 등록되면 안됩니다. 하여서 제외합니다.
377 // https://github.com/hangum/TadpoleForDBTools/issues/512
378 if(!isTest) {// && loginInfo.getDBDefine() != DBDefine.MSSQL_DEFAULT) {
379 TDBYesNoErroDialog dialog = new TDBYesNoErroDialog(getShell(),
380 Messages.get().DBLoginDialog_9,
381 String.format(Messages.get().AbstractLoginComposite_3, errMsg));
382 if(dialog.open() == IDialogConstants.OK_ID) return true;
384 } else {
385 TDBErroDialog dialog = new TDBErroDialog(getShell(), Messages.get().DBLoginDialog_43, errMsg);
386 dialog.open();
389 return false;
394 * @return the isTableFilter
396 public boolean isTableFilter() {
397 return isTableFilter;
401 * @param isTableFilter the isTableFilter to set
403 public void setTableFilter(boolean isTableFilter) {
404 this.isTableFilter = isTableFilter;
408 * @return the strTableFilterInclude
410 public String getStrTableFilterInclude() {
411 return strTableFilterInclude;
415 * @param strTableFilterInclude the strTableFilterInclude to set
417 public void setStrTableFilterInclude(String strTableFilterInclude) {
418 this.strTableFilterInclude = strTableFilterInclude;
422 * @return the strTableFilterExclude
424 public String getStrTableFilterExclude() {
425 return strTableFilterExclude;
429 * @param strTableFilterExclude the strTableFilterExclude to set
431 public void setStrTableFilterExclude(String strTableFilterExclude) {
432 this.strTableFilterExclude = strTableFilterExclude;
436 * display name
437 * @return
439 public String getDisplayName() {
440 return displayName;
443 public DBDefine getSelectDB() {
444 return selectDB;
447 public List<String> getListGroupName() {
448 return listGroupName;
451 public String getSelGroupName() {
452 return selGroupName;
455 public OthersConnectionGroup getOthersConnectionInfo() {
456 return othersConnectionInfo;
460 * set ext value
462 protected void setExtValue() {
463 if(oldUserDB != null) {
464 userDB.setExt1(oldUserDB.getExt1());
465 userDB.setExt2(oldUserDB.getExt2());
466 userDB.setExt3(oldUserDB.getExt3());
467 userDB.setExt4(oldUserDB.getExt4());
468 userDB.setExt5(oldUserDB.getExt5());
469 userDB.setExt6(oldUserDB.getExt6());
470 userDB.setExt7(oldUserDB.getExt7());
471 userDB.setExt8(oldUserDB.getExt8());
472 userDB.setExt9(oldUserDB.getExt9());
473 userDB.setExt10(oldUserDB.getExt10());
478 * Set others connection info
480 protected void setOtherConnectionInfo() {
481 OthersConnectionInfoDAO otherConnectionDAO = othersConnectionInfo.getOthersConnectionInfo();
482 userDB.setIs_readOnlyConnect(otherConnectionDAO.isReadOnlyConnection()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
483 userDB.setIs_autocommit(otherConnectionDAO.isAutoCommit()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
484 userDB.setIs_showtables(otherConnectionDAO.isShowTables()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
486 // userDB.setIs_table_filter(otherConnectionDAO.isTableFilter()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
487 // userDB.setTable_filter_include(otherConnectionDAO.getStrTableFilterInclude());
488 // userDB.setTable_filter_exclude(otherConnectionDAO.getStrTableFilterExclude());
490 userDB.setIs_profile(otherConnectionDAO.isProfiling()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
491 userDB.setQuestion_dml(otherConnectionDAO.isDMLStatement()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
493 userDB.setIs_external_browser(otherConnectionDAO.isExterBrowser()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
494 userDB.setListExternalBrowserdao(otherConnectionDAO.getListExterBroswer());
496 // userDB.setIs_visible(otherConnectionDAO.isVisible()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
497 userDB.setIs_summary_report(otherConnectionDAO.isSummaryReport()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());
498 userDB.setIs_monitoring(otherConnectionDAO.isMonitoring()?PublicTadpoleDefine.YES_NO.YES.name():PublicTadpoleDefine.YES_NO.NO.name());