`
ccxw1983
  • 浏览: 26560 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

集合的运算、打印(打印表名、编码、pdm中所属包方便人工判断)、保存

 
阅读更多
工具的开发背景:
crm系统以ework为基础改造而来,以前简单起见和ework混合了代码和表的,现在需要分析,就需要重新分析,哪些是共用的,哪些是私有的。
通过日志中打印的表名,可以分析出哪些表是crm独有的。
通过pdm文件表的创建记录确定哪些表是新表,新的表少,再除去crm独有的,所以方便人工检查哪些是crm专有的。

另外分析了us系统那边用到的表(那边用到的就肯定不能公用),
cas、权限、用户、组织单位、数据字典的肯定是公用的。
另外考虑以前开发中折中,偷懒,有些方法混合了ework、crm一方使用无需用到的表,目前一时没法剥离,需要确定备忘。

总体思路:
日志(或初始化sql脚本)或pdm中分析出来的表名列表结果分别存到集合包里面,通过里面的CollectionTool.java做集合的运算、打印(打印表名、编码、pdm中所属包方便人工判断)、保存。

工具编写匆忙,可能有少量bug和注释不统一的地方,不懂的请研究代码,恕不做技术支持。

这是集合的操作工具。
package chenxiaowen.tool.collections;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeSet;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.DocumentException;

import chenxiaowen.tool.pdm.PdmParser;
import chenxiaowen.tool.pdm.pojo.PhysicalDiagram;
import chenxiaowen.tool.pdm.pojo.Table;

/**
 * 管理各个集合的表名列表,进行集合的加、减、交集,打印、保存、和pdm文件对应查找
 * @author 陈小稳 33881270@qq.com
 *
 */
public class CollectionTool {
	public static HashMap<String, TreeSet<String>> fileTables = new HashMap<String, TreeSet<String>>();
	public static String encoding = "UTF-8";
	public TreeSet<String> tables = new TreeSet<String>();

	static {
		// 集合里面每个txt文件记录一个表名集合,这里以表名首字母为key存储了每个集合到 fileTables
		File baseLogResultDir = new File(
				"D:\\workspace\\sqltablename\\src\\chenxiaowen\\tool\\collections");
		File[] fs = baseLogResultDir.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".txt");
			}
		});
		for (int i = 0; i < fs.length; i++) {
			File f = fs[i];
			String baseName = FilenameUtils.getBaseName(f.getAbsolutePath());
			String fileFirstName = baseName.charAt(0) + "";
			try {
				String txt = FileUtils.readFileToString(f, encoding);
				String[] names = txt.split(",");
				TreeSet<String> namesSet = new TreeSet<String>();
				for (int j = 0; j < names.length; j++) {
					String name = StringUtils.trimToNull(names[j]);
					if (StringUtils.isNotEmpty(name)) {
						namesSet.add(name);
					}
				}
				if (namesSet.isEmpty()) {
					System.err.println("文件的表集合为空 " + baseName);
				} else {
					System.out.println("解析了表集合 " + baseName);
				}
				fileTables.put(fileFirstName, namesSet);
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
		}
	}

	/**
	 * @param args
	 * @throws DocumentException
	 */
	public static void main(String[] args) throws Exception {
		CollectionTool tool = new CollectionTool();
		// System.out.println("-----------------------");
		// tool.init("1").add("2").print();
		// System.out.println("-----------------------");
		// tool.init("1").add("c").remove("2").print2();

		tool.init("1").add("2").writeCollectionFile("3.txt");
	}

	/**
	 * 打印表名
	 */
	public void print() {
		TreeSet<String> a = this.getSet();
		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			System.out.println(name + ",");
		}
	}

	/**
	 * 打印表名到文件
	 * 
	 * @throws IOException
	 */
	public void writeCollectionFile(String filename) throws IOException {
		TreeSet<String> a = this.getSet();
		writeCollectionFile(filename, a);
	}

	/**
	 * 打印表名到文件
	 * 
	 * @param filename
	 *            输出到集合目录的文件名称
	 * @param a
	 *            表名集合
	 * @throws IOException
	 */
	public static void writeCollectionFile(String filename, TreeSet<String> a)
			throws IOException {
		File file = new File(getCollectionDir(), filename);
		StringBuffer b = new StringBuffer();
		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			b.append(name);
			b.append(",\r\n");
		}
		System.out.println(b.toString());
		FileUtils.writeStringToFile(file, b.toString(), encoding);
	}

	/**
	 * 集合目录
	 * 
	 * @return
	 */
	public static File getCollectionDir() {
		String x = CollectionTool.class.getResource("").getPath().replace(
				"bin", "src");
		// System.out.println(new File(x).getAbsolutePath());
		return new File(x);
	}

	/**
	 * 打印包名-表名
	 * 
	 * @throws DocumentException
	 */
	public void print2() throws DocumentException {
		String[] pdmFiles = new String[] { "D:\\Desktop\\common.pdm",
				"D:\\Desktop\\crm.pdm" };
		PdmParser parseTool = PdmParser
				.parseTablesAndPhysicalDiagrams(pdmFiles);
		// <表code,表>
		HashMap<String, Table> codeTableMap = parseTool.getCodeTableMap();
		// <表id,表所属PhysicalDiagram>
		HashMap<String, PhysicalDiagram> idPhysicalDiagramMap = parseTool
				.getIdPhysicalDiagramMap();

		TreeSet<String> toprinttables = this.getSet();
		System.out.println("表编码\t\t表名\t\t包名");
		for (Iterator iterator = toprinttables.iterator(); iterator.hasNext();) {
			String code = (String) iterator.next();
			Table table = codeTableMap.get(code);
			if (table == null) {
				System.err.println(code + "\t pdm不存在该表");
			} else {
				String name = table.getName();
				PhysicalDiagram symbol = idPhysicalDiagramMap
						.get(table.getId());
				if (symbol == null) {
					System.err.println(code + "\t" + name
							+ "\tpdm中该表只存在于tables不在模块中");
				} else {
					String packname = symbol.getName();
					System.out.println(code + "\t" + name + "\t" + packname);
				}
			}
		}
	}

	/**
	 * 初始
	 * 
	 * @param collectionName
	 * @return
	 */
	public CollectionTool init(String collectionName) {
		TreeSet<String> c = fileTables.get(collectionName);
		this.setSet(c);
		return this;
	}

	/**
	 * 加
	 * 
	 * @param collectionName
	 * @return
	 */
	public CollectionTool add(String collectionName) {
		TreeSet<String> a = this.getSet();
		TreeSet<String> b = fileTables.get(collectionName);
		if (b == null) {
			throw new RuntimeException("集合 " + collectionName + " 不存在");
		}
		TreeSet<String> c = bingji(a, b);
		this.setSet(c);
		return this;
	}

	public CollectionTool jia(String collectionName) {
		return jia(collectionName);
	}

	public CollectionTool jian(String collectionName) {
		return jian(collectionName);
	}

	/**
	 * 减
	 * 
	 * @param collectionName
	 * @return
	 */
	public CollectionTool remove(String collectionName) {
		TreeSet<String> a = this.getSet();
		TreeSet<String> b = fileTables.get(collectionName);
		if (b == null) {
			throw new RuntimeException("集合 " + collectionName + " 不存在");
		}
		TreeSet<String> c = jian(a, b);
		this.setSet(c);
		return this;
	}

	/**
	 * 交集
	 * 
	 * @param collectionName
	 * @return
	 */
	public CollectionTool cross(String collectionName) {
		TreeSet<String> a = this.getSet();
		TreeSet<String> b = fileTables.get(collectionName);
		if (b == null) {
			throw new RuntimeException("集合 " + collectionName + " 不存在");
		}
		TreeSet<String> c = jiaoji(a, b);
		this.setSet(c);
		return this;
	}

	/**
	 * 并集 a∪b 一个包含所有a的元素以及b的元素的集合
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static TreeSet<String> bingji(TreeSet<String> a, TreeSet<String> b) {
		TreeSet<String> c = new TreeSet<String>();
		c.addAll(a);
		c.addAll(b);
		return c;
	}

	/**
	 * 获得a中独有的元素的集合
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static TreeSet<String> jian(TreeSet<String> a, TreeSet<String> b) {
		TreeSet<String> c = new TreeSet<String>();
		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			if (!b.contains(name)) {
				c.add(name);
			}
		}
		return c;
	}

	/**
	 * a∩b:获得a也有b也有的元素的集合
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static TreeSet<String> jiaoji(TreeSet<String> a, TreeSet<String> b) {
		TreeSet<String> c = new TreeSet<String>();
		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			if (b.contains(name)) {
				c.add(name);
			}
		}
		return c;
	}

	public TreeSet<String> getSet() {
		return tables;
	}

	public void setSet(TreeSet<String> tables) {
		this.tables = tables;
	}

}
分享到:
评论

相关推荐

    大学离散数学实验集合运算.zip

    【实验目的】掌握用计算机求集合的交、并、差和...(5)集合的补运算:将数组E中的元素逐一与数组A中的元素进行比较,把不相同的元素保存到数组C中,数组C便是集合A关于集合E的补集。 求补集是一种种特殊的集合差运算。

    天云双色集合运算

    天云双色集合运算 V2014 Vol7.06 x86 Realease

    数据结构-集合运算.docx

    数据结构-集合运算 数据结构-集合运算全文共16页,当前为第1页。数据结构-集合运算全文共16页,当前为第1页。 数据结构-集合运算全文共16页,当前为第1页。 数据结构-集合运算全文共16页,当前为第1页。 合肥工业...

    C语言集合运算器课设报告

    课程设计的内容及要求: (一)主要内容 1.任务描述 编制一个能够演示执行集合操作的程序: ① 能够实现集合的并、交、差运算; ② 实现集合的元素判定和子集判定运算; ③ 求集合的补集。

    数据结构(C语言版)综合实践集合运算

    7. 集合运算 (单循环链表) 1.问题描述: 设有两个带头结点的单循环链表存储的集合A、B,其元素类型为字符或者整形,且以非递减方式存储,其头结点分别为ha、hb。要求下面各问题中的结果集合同样以非递减方式存储,...

    链表实现集合运算 链表实现集合交并差运算

    链表实现集合运算 链表实现集合交并差运算

    数据库——的集合运算

    集合运算:是用来把两个或多个查询的结果集做 并、交、差的集合运算,包含集合运算的查询称 为复合查询。

    (C++)简单集合运算

    本程序是运用C++数组可现简单的 集合运算 如并集,交集,差集,和笛卡尔积~~

    离散数学集合运算C++或C语言实验报告.pdf

    离散数学集合运算C++或C语言实验报告 离散数学的实验

    集合运算及关系运算

    在下不才初学java,用java写了一个求两个集合的集合运算,若程序中有漏洞请各位大虾指出谢谢啦!

    java单链表集合的包含运算

    单链表的插入,删除,排序!集合的并、交和差运算 编制一个能演示执行集合的并、交和差...vc mfc编的 集合运算 交并补差 包含...采用有序单链表表示集合

    集合运算器代码

    关于对集合的交差并补运算,子集与元素的判定

    集合运算.zip

    c语言课程设计--集合运算

    C++语言交并差集合运算

    C++语言交并差集合运算

    C语言版集合运算器

    平时上机的代码,用单链表实现集合运算器,适合于数据结构初学者

    课程设计--集合运算

    主要解决集合的交、并、补集等运算 要求如下: 全集:大写字母“A”~“Z” 要求实现以下功能: 1、 集合的输入:自动去掉重复和非法的字符。 2、 集合的显示:输出集合的全部元素。 3、 输出一个给定集合的补集。 4...

    数据结构C语言版链表的集合运算

    数据结构C语言版链表的集合运算,包含交集、并集、差集。

    c语言实现集合运算

    集合运算,实现输入、交、并、属于的操作。可以运行,有点小错。

    c语言集合的运算

    c语言集合的运算,对学习了离散数学要完成老师布置的作业-用c语言对集合的运算进行编程

Global site tag (gtag.js) - Google Analytics