Java tutorial
/******************************************************************************* * Copyright (c) 2012 Cho Hyun Jong. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Cho Hyun Jong - initial API and implementation ******************************************************************************/ package com.hangum.tadpole.erd.core.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.eclipse.draw2d.geometry.Rectangle; import com.hangum.tadpole.commons.sql.TadpoleSQLManager; import com.hangum.tadpole.commons.sql.define.DBDefine; import com.hangum.tadpole.dao.mysql.TableColumnDAO; import com.hangum.tadpole.dao.mysql.TableDAO; import com.hangum.tadpole.dao.system.UserDBDAO; import com.hangum.tadpole.erd.core.relation.RelationUtil; import com.hangum.tadpole.model.Column; import com.hangum.tadpole.model.DB; import com.hangum.tadpole.model.Table; import com.hangum.tadpole.model.TadpoleFactory; import com.hangum.tadpole.mongodb.core.utils.MongoDBTableColumn; import com.ibatis.sqlmap.client.SqlMapClient; import com.mongodb.DBAddress; import com.mongodb.DBCollection; import com.mongodb.Mongo; /** * db? ?? ?. * * @author hangum * */ public enum TadpoleModelUtils { INSTANCE; private static final Logger logger = Logger.getLogger(TadpoleModelUtils.class); private UserDBDAO userDB; /** ? ?? */ public static final int WIDTH_COUNT = 5; /** ?? ?? */ public static final int START_TABLE_WIDTH_POINT = 50; public static final int START_TABLE_HIGHT_POINT = 50; public static final int END_TABLE_WIDTH_POINT = -1; public static final int END_TABLE_HIGHT_POINT = -1; /** ? ?? */ public static final int GAP_HIGHT = 300; public static final int GAP_WIDTH = 300; private TadpoleFactory factory = TadpoleFactory.eINSTANCE; /** * logindb? ? . * * @param userDB * @return */ public DB getDBAllTable(UserDBDAO userDB) throws Exception { this.userDB = userDB; DB db = factory.createDB(); db.setDbType(userDB.getTypes()); db.setId(userDB.getUsers()); db.setUrl(userDB.getUrl()); // try { // ?? List<TableDAO> tables = getTables(); // ? ? Map<String, Table> mapDBTables = new HashMap<String, Table>(); // int count = 0; Rectangle prevRectangle = null; int nextTableX = START_TABLE_WIDTH_POINT; int nextTableY = START_TABLE_HIGHT_POINT; for (TableDAO table : tables) { Table tableModel = factory.createTable(); tableModel.setDb(db); tableModel.setName(table.getName()); mapDBTables.put(tableModel.getName(), tableModel); // if (prevRectangle == null) { prevRectangle = new Rectangle(START_TABLE_WIDTH_POINT, START_TABLE_HIGHT_POINT, END_TABLE_WIDTH_POINT, END_TABLE_HIGHT_POINT); } else { // ?? ?. prevRectangle = new Rectangle(nextTableX, nextTableY, END_TABLE_WIDTH_POINT, END_TABLE_HIGHT_POINT); } // logger.debug("###########################################################################################################################"); // logger.debug("###########################################################################################################################"); // logger.debug("###########################################################################################################################"); // logger.debug(prevRectangle.toString()); // logger.debug("###########################################################################################################################"); // logger.debug("###########################################################################################################################"); tableModel.setConstraints(prevRectangle); // column add List<TableColumnDAO> columnList = getColumns(userDB.getDb(), table.getName()); for (TableColumnDAO columnDAO : columnList) { Column column = factory.createColumn(); column.setDefault(columnDAO.getDefault()); column.setExtra(columnDAO.getExtra()); column.setField(columnDAO.getField()); column.setNull(columnDAO.getNull()); column.setKey(columnDAO.getKey()); column.setType(columnDAO.getType()); column.setTable(tableModel); tableModel.getColumns().add(column); } // count++; // ? ? ? ?. if (count == (WIDTH_COUNT + 1)) { count = 0; nextTableX = START_TABLE_WIDTH_POINT; nextTableY = prevRectangle.getBottomLeft().y + GAP_HIGHT; } else { nextTableX = prevRectangle.getTopRight().x + GAP_WIDTH; } } // end table list // . RelationUtil.calRelation(userDB, mapDBTables, db); // } catch(Exception e) { // logger.error("all table initialize error", e); //$NON-NLS-1$ // // Status errStatus = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e); //$NON-NLS-1$ // ExceptionDetailsErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", Messages.TadpoleModelUtils_2, errStatus); //$NON-NLS-1$ // } return db; } /** * table . */ public List<TableDAO> getTables() throws Exception { List<TableDAO> showTables = new ArrayList<TableDAO>(); if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) { SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB); return sqlClient.queryForList("tableList", userDB.getDb()); //$NON-NLS-1$ } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) { Mongo mongo = new Mongo(new DBAddress(userDB.getUrl())); com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb()); for (String col : mongoDB.getCollectionNames()) { TableDAO dao = new TableDAO(); dao.setName(col); showTables.add(dao); } return showTables; } return showTables; } /** * table? . * * @param strTBName * @return * @throws Exception */ public List<TableColumnDAO> getColumns(String db, String strTBName) throws Exception { if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) { SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB); Map<String, String> param = new HashMap<String, String>(); param.put("db", db); param.put("table", strTBName); return sqlClient.queryForList("tableColumnList", param); } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) { Mongo mongo = new Mongo(new DBAddress(userDB.getUrl())); com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb()); DBCollection coll = mongoDB.getCollection(strTBName); return MongoDBTableColumn.tableColumnInfo(coll.getIndexInfo(), coll.findOne()); } return null; } }