Set operation, printing (print table name, code, and the package in pdm to facilitate manual judgment), save

The development background of the tool: The
crm system is transformed based on ework. In the past, code and tables were mixed with ework for simplicity. Now it needs to be analyzed, and it needs to be re-analyzed. Which are shared and which are private.
Through the table names printed in the log, you can analyze which tables are unique to crm.
Determine which tables are new tables and few new tables through the creation records of pdm file tables, and then remove the unique ones of crm, so it is convenient to manually check which ones are exclusive to crm.

In addition, the tables used in the us system are analyzed (the ones used there must not be public), and the
cas, permissions, users, organizational units, and data dictionaries must be public.
In addition, considering the compromise and laziness in the previous development, some methods are mixed with ework and crm parties to use tables that do not need to be used.

General idea:
The log (or initialization sql script) or the table name list results analyzed in pdm are stored in the collection package respectively, and the collection operation and printing (print table name, code, and package in pdm) are done through CollectionTool.java inside. To facilitate manual judgment), save.

The tool is written in a hurry, and there may be a small number of bugs and inconsistent comments. If you don't understand, please study the code, and we will not provide technical support.

This is the manipulation tool for collections.
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;

/**
 * Manage the table name list of each set, add, subtract, intersect, print, save, and search for pdm files corresponding to sets
 * @author Chen [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 {
		// Each txt file in the collection records a table name collection, where each collection is stored in fileTables with the first letter of the table name as the key
		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("The table set of the file is empty" + baseName);
				} else {
					System.out.println("parsed table set" + 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");
	}

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

	/**
	 * print table name to file
	 *
	 * @throws IOException
	 */
	public void writeCollectionFile(String filename) throws IOException {
		TreeSet<String> a = this.getSet();
		writeCollectionFile(filename, a);
	}

	/**
	 * print table name to file
	 *
	 * @param filename
	 * The name of the file output to the collection directory
	 * @param a
	 * collection of table names
	 * @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);
	}

	/**
	 * Collection directory
	 *
	 * @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);
	}

	/**
	 * print package name-table name
	 *
	 * @throws DocumentException
	 */
	public void print2() throws DocumentException {
		String[] pdmFiles = new String[] { "D:\\Desktop\\common.pdm",
				"D:\\Desktop\\crm.pdm" };
		PdmParser parseTool = PdmParser
				.parseTablesAndPhysicalDiagrams(pdmFiles);
		// <table code, table>
		HashMap<String, Table> codeTableMap = parseTool.getCodeTableMap();
		// <table id, PhysicalDiagram to which the table belongs>
		HashMap<String, PhysicalDiagram> idPhysicalDiagramMap = parseTool
				.getIdPhysicalDiagramMap();

		TreeSet<String> toprinttables = this.getSet();
		System.out.println("Table code\t\tTable name\t\tPackage name");
		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 does not exist this table");
			} else {
				String name = table.getName();
				PhysicalDiagram symbol = idPhysicalDiagramMap
						.get(table.getId());
				if (symbol == null) {
					System.err.println(code + "\t" + name
							+ "\tpdm this table only exists in tables and not in modules");
				} else {
					String packname = symbol.getName();
					System.out.println(code + "\t" + name + "\t" + packname);
				}
			}
		}
	}

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

	/**
	 * add
	 *
	 * @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);
	}

	/**
	 * reduce
	 *
	 * @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;
	}

	/**
	 * Intersection
	 *
	 * @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;
	}

	/**
	 * Union a∪b A set containing all elements of a and elements of 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;
	}

	/**
	 * Get the set of unique elements in 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: get the set of elements that a also has 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;
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326414281&siteId=291194637