com.hangum.tadpole.mongodb.erd.core.relation.RelationUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.mongodb.erd.core.relation.RelationUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2013 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.mongodb.erd.core.relation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;

import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
import com.hangum.tadpole.engine.query.dao.mysql.ReferencedTableDAO;
import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
import com.hangum.tadpole.mongodb.erd.core.utils.MongodbUtils;
import com.hangum.tadpole.mongodb.model.Column;
import com.hangum.tadpole.mongodb.model.DB;
import com.hangum.tadpole.mongodb.model.MongodbFactory;
import com.hangum.tadpole.mongodb.model.Relation;
import com.hangum.tadpole.mongodb.model.RelationKind;
import com.hangum.tadpole.mongodb.model.Table;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * ERD  Table relation  
 * 
 * <pre>      
  
   constraint_name      :    ?? ?
   table_name            :     ? ?
   column_name         :     ? 
       
   referenced_table_name   :    ? ?
   referenced_column_name   :     ? 
 * </pre>
 * 
 * @author hangum
 *
 */
public class RelationUtil {
    private static final Logger logger = Logger.getLogger(RelationUtil.class);

    /**
     * <pre>
     *  ?  .
     * 
     * object type? org.bson.types.ObjectId ? field?? _id   _id ?? ?  ??.
     * </pre>
     * 
     * @param userDB
     * @param mapDBTables
     * @param db
     * @param refTableNames
     * @throws Exception
     */
    public static void calRelation(UserDBDAO userDB, Map<String, Table> mapDBTables, DB db, String refTableNames)
            throws Exception {
        List<ReferencedTableDAO> listRealRefTableDAO = new ArrayList<ReferencedTableDAO>();

        Set<String> keySet = mapDBTables.keySet();
        for (String keyTable : keySet) {
            Table table = mapDBTables.get(keyTable);
            EList<Column> listColumn = table.getColumns();
            for (Column column : listColumn) {
                String strField = column.getField();

                if (MongodbUtils.isReferenceKey(column.getType(), strField)) {
                    String strRefName = StringUtils.remove(strField, "_id");

                    ReferencedTableDAO refTableDao = new ReferencedTableDAO();
                    refTableDao.setTable_name(table.getName());
                    refTableDao.setColumn_name(strRefName);
                    refTableDao.setReferenced_table_name(strRefName);
                    refTableDao.setReferenced_column_name(table.getName());
                    refTableDao.setConstraint_name(strRefName);

                    listRealRefTableDAO.add(refTableDao);
                }
            }
        }

        calRelation(userDB, mapDBTables, db, listRealRefTableDAO);
    }

    /**
     *  ?? .
     * 
     * @param userDB
     * @param mapDBTables
     * @param db
     * @throws Exception
     */
    public static void calRelation(UserDBDAO userDB, Map<String, Table> mapDBTables, DB db) throws Exception {
        List<ReferencedTableDAO> listRealRefTableDAO = new ArrayList<ReferencedTableDAO>();

        Set<String> keySet = mapDBTables.keySet();
        for (String keyTable : keySet) {
            Table table = mapDBTables.get(keyTable);
            EList<Column> listColumn = table.getColumns();
            for (Column column : listColumn) {
                String strField = column.getField();

                if (MongodbUtils.isReferenceKey(column.getType(), strField)) {
                    ReferencedTableDAO refTableDao = new ReferencedTableDAO();
                    refTableDao.setTable_name(table.getName());
                    refTableDao.setColumn_name(strField);
                    refTableDao.setReferenced_table_name(StringUtils.remove(strField, "_id"));
                    refTableDao.setReferenced_column_name(strField);
                    refTableDao.setConstraint_name(strField);

                    listRealRefTableDAO.add(refTableDao);
                }
            }
        }

        calRelation(userDB, mapDBTables, db, listRealRefTableDAO);
    }

    /**
     * ?  .
     * 
     * @param userDB
     * @param mapDBTables
     * @param db
     * @throws Exception
     */
    public static void calRelation(UserDBDAO userDB, Map<String, Table> mapDBTables, DB db,
            List<ReferencedTableDAO> referenceTableList) throws Exception {

        MongodbFactory tadpoleFactory = MongodbFactory.eINSTANCE;

        // ?    .
        for (ReferencedTableDAO refTabDAO : referenceTableList) {
            Table soTabMod = mapDBTables.get(refTabDAO.getTable_name());
            Table tarTabMod = mapDBTables.get(refTabDAO.getReferenced_table_name());

            // ?? ?? ,  ??  . 
            if (soTabMod != null && tarTabMod != null) {

                // ? ? relation? .
                boolean isAlrealyAppend = false;
                for (Relation relation : soTabMod.getOutgoingLinks()) {

                    if (relation.getConstraint_name() != null && refTabDAO.getConstraint_name() != null) {
                        if (relation.getConstraint_name().equals(refTabDAO.getConstraint_name())) {
                            isAlrealyAppend = true;
                            break;
                        }
                    }
                }
                for (Relation relation : soTabMod.getIncomingLinks()) {

                    if (relation.getConstraint_name() != null && refTabDAO.getConstraint_name() != null) {
                        if (relation.getConstraint_name().equals(refTabDAO.getConstraint_name())) {
                            isAlrealyAppend = true;
                            break;
                        }
                    }
                }

                // TODO  ?? ??  ?  ? ? ? ?  . 
                // java.lang.RuntimeException: Cycle detected in graph
                if (refTabDAO.getTable_name().equals(refTabDAO.getReferenced_table_name()))
                    continue;

                // ?  ? ?
                if (isAlrealyAppend)
                    continue;

                //  ?  ?.
                Relation relation = tadpoleFactory.createRelation();
                /*   ?  ?  
                 *   ? ? 
                 *  
                 * org.eclipse.emf.ecore.resource.Resource$IOWrappedException: 
                 * The object 'com.hangum.tadpole.model.impl.RelationImpl@5e44efa0 (source_kind: ONLY_ONE, target_kind: ONE_MANY, column_name: country_id, referenced_column_name: country_id, bendpoint: [], constraint_name: null)' 
                 * is not contained in a resource.
                 */
                relation.setDb(db);

                relation.setConstraint_name(refTabDAO.getConstraint_name());

                relation.setColumn_name(refTabDAO.getColumn_name());
                relation.setReferenced_column_name(refTabDAO.getReferenced_column_name());

                relation.setSource_kind(RelationKind.ONLY_ONE);
                relation.setTarget_kind(RelationKind.ONLY_ONE);

                // 
                soTabMod.getIncomingLinks().add(relation);
                tarTabMod.getOutgoingLinks().add(relation);

                relation.setSource(soTabMod);
                relation.setTarget(tarTabMod);

            } // if(souceModel != null && targetModel != null

        } // for
    }

    /**
     * ??  ?  .
     * 
     * @return
     * @throws Exception
     */
    public static List<ReferencedTableDAO> getReferenceTable(UserDBDAO userDB) throws Exception {
        SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
        return sqlClient.queryForList("referencedTableListALL");
    }

}