集合的运算、打印(打印表名、编码、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 陈小稳 [email protected]
 *
 */
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;
	}

}

猜你喜欢

转载自ccxw1983.iteye.com/blog/2344552