// Path: src/org/ohdsi/meshXmlToDatabase/MeshParserMain.java
// public class MeshParserMain {
// 
// 	private Map<String, String>	treeNumberToUi;
// 
// 	public static void main(String[] args) {
// 		IniFile iniFile = new IniFile(args[0]);
// 		MeshParserMain meshParser = new MeshParserMain();
// 		meshParser.parseMesh(iniFile.get("MESH_XML_FOLDER"), iniFile.get("SERVER"), iniFile.get("SCHEMA"), iniFile.get("DOMAIN"), iniFile.get("USER"),
// 				iniFile.get("PASSWORD"), iniFile.get("DATA_SOURCE_TYPE"));
// 
// 	}
// 
// 	private void parseMesh(String folder, String server, String schema, String domain, String user, String password, String dateSourceType) {
// 		String meshFile = null;
// 		String meshSupplementFile = null;
// 		for (File file : new File(folder).listFiles()) {
// 			String fileName = file.getName();
// 			if (fileName.startsWith("desc") && fileName.endsWith(".gz")) {
// 				if (meshFile != null)
// 					throw new RuntimeException("Error: Multiple main MeSH files found. Please have only 1 in the folder");
// 				else
// 					meshFile = file.getAbsolutePath();
// 			}
// 			if (fileName.startsWith("supp") && fileName.endsWith(".gz")) {
// 				if (meshSupplementFile != null)
// 					throw new RuntimeException("Error: Multiple supplementary MeSH files found. Please have only 1 in the folder");
// 				else
// 					meshSupplementFile = file.getAbsolutePath();
// 			}
// 		}
// 		if (meshFile == null)
// 			throw new RuntimeException("Error: No main MeSH file found.");
// 		if (meshSupplementFile == null)
// 			throw new RuntimeException("Error: No supplementary MeSH file found.");
// 		ConnectionWrapper connectionWrapper = new ConnectionWrapper(server, domain, user, password, new DbType(dateSourceType));
// 		connectionWrapper.use(schema);
// 		
// 		treeNumberToUi = new HashMap<String, String>();
// 		InsertableDbTable outTerms = new InsertableDbTable(connectionWrapper, "mesh_term");
// 		InsertableDbTable outRelationship = new InsertableDbTable(connectionWrapper, "mesh_relationship");
// 		
// 		MainMeshParser.parse(meshFile, outTerms, outRelationship, treeNumberToUi);
// 		SupplementaryMeshParser.parse(meshSupplementFile, outTerms, outRelationship);
// 		outTerms.close();
// 		outRelationship.close();
// 
// 		InsertableDbTable outAncestor = new InsertableDbTable(connectionWrapper, "mesh_ancestor");
// 		generateAncestorTable(outAncestor);
// 		outAncestor.close();
// 	}
// 
// 	private void generateAncestorTable(InsertableDbTable outAncestor) {
// 		Map<String, PairInfo> pairToInfo = new HashMap<String, PairInfo>();
// 		for (String treeNumber1 : treeNumberToUi.keySet()) {
// 			String parts[] = treeNumber1.split("\\.");
// 			String ui1 = treeNumberToUi.get(treeNumber1);
// 			for (int i = 0; i < parts.length; i++) {
// 				int distance = parts.length - i - 1;
// 				String treeNumber2 = StringUtilities.join(Arrays.copyOfRange(parts, 0, i + 1), ".");
// 				String ui2 = treeNumberToUi.get(treeNumber2);
// 				PairInfo pairInfo = pairToInfo.get(ui1 + "_" + ui2);
// 				if (pairInfo == null) {
// 					pairInfo = new PairInfo(distance, distance);
// 					pairToInfo.put(ui1 + "_" + ui2, pairInfo);
// 				} else {
// 					if (pairInfo.maxDistance < distance)
// 						pairInfo.maxDistance = distance;
// 					if (pairInfo.minDistance > distance)
// 						pairInfo.minDistance = distance;
// 				}
// 			}
// 		}
// 
// 		for (String pair : pairToInfo.keySet()) {
// 			PairInfo pairInfo = pairToInfo.get(pair);
// 			Row row = new Row();
// 			row.add("ancestor_ui", pair.split("_")[1]);
// 			row.add("descendant_ui", pair.split("_")[0]);
// 			row.add("max_distance", pairInfo.maxDistance);
// 			row.add("min_distance", pairInfo.minDistance);
// 			outAncestor.write(row);
// 		}
// 	}
// 
// 	private class PairInfo {
// 		public int	minDistance;
// 		public int	maxDistance;
// 
// 		public PairInfo(int minDistance, int maxDistance) {
// 			this.minDistance = minDistance;
// 			this.maxDistance = maxDistance;
// 		}
// 	}
// }


import org.ohdsi.meshXmlToDatabase.MeshParserMain;
/*******************************************************************************
 * Copyright 2014 Observational Health Data Sciences and Informatics
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ohdsi.medlineXmlToDatabase;


/**
 * The main class that parses the command line arguments, and calls either MedlineAnalyserMain or MedlineParserMain
 * @author MSCHUEMI
 *
 */
public class MainClass {

	private static enum Action {
		ANALYSE, PARSE, PARSE_MESH
	};

	private static Action	action;
	private static String	pathToIniFile;

	public static void main(String[] args) {
		if (args.length == 0) {
			System.out.println("See https://github.com/OHDSI/MedlineXmlToDatabase for details on how to use");
			return;
		}
		parseParameters(args);
		if (action == Action.ANALYSE)
			MedlineAnalyserMain.main(new String[] { pathToIniFile });
		else if (action == Action.PARSE)
			MedlineParserMain.main(new String[] { pathToIniFile });
		if (action == Action.PARSE_MESH)
// Path: src/org/ohdsi/meshXmlToDatabase/MeshParserMain.java
// public class MeshParserMain {
// 
// 	private Map<String, String>	treeNumberToUi;
// 
// 	public static void main(String[] args) {
// 		IniFile iniFile = new IniFile(args[0]);
// 		MeshParserMain meshParser = new MeshParserMain();
// 		meshParser.parseMesh(iniFile.get("MESH_XML_FOLDER"), iniFile.get("SERVER"), iniFile.get("SCHEMA"), iniFile.get("DOMAIN"), iniFile.get("USER"),
// 				iniFile.get("PASSWORD"), iniFile.get("DATA_SOURCE_TYPE"));
// 
// 	}
// 
// 	private void parseMesh(String folder, String server, String schema, String domain, String user, String password, String dateSourceType) {
// 		String meshFile = null;
// 		String meshSupplementFile = null;
// 		for (File file : new File(folder).listFiles()) {
// 			String fileName = file.getName();
// 			if (fileName.startsWith("desc") && fileName.endsWith(".gz")) {
// 				if (meshFile != null)
// 					throw new RuntimeException("Error: Multiple main MeSH files found. Please have only 1 in the folder");
// 				else
// 					meshFile = file.getAbsolutePath();
// 			}
// 			if (fileName.startsWith("supp") && fileName.endsWith(".gz")) {
// 				if (meshSupplementFile != null)
// 					throw new RuntimeException("Error: Multiple supplementary MeSH files found. Please have only 1 in the folder");
// 				else
// 					meshSupplementFile = file.getAbsolutePath();
// 			}
// 		}
// 		if (meshFile == null)
// 			throw new RuntimeException("Error: No main MeSH file found.");
// 		if (meshSupplementFile == null)
// 			throw new RuntimeException("Error: No supplementary MeSH file found.");
// 		ConnectionWrapper connectionWrapper = new ConnectionWrapper(server, domain, user, password, new DbType(dateSourceType));
// 		connectionWrapper.use(schema);
// 		
// 		treeNumberToUi = new HashMap<String, String>();
// 		InsertableDbTable outTerms = new InsertableDbTable(connectionWrapper, "mesh_term");
// 		InsertableDbTable outRelationship = new InsertableDbTable(connectionWrapper, "mesh_relationship");
// 		
// 		MainMeshParser.parse(meshFile, outTerms, outRelationship, treeNumberToUi);
// 		SupplementaryMeshParser.parse(meshSupplementFile, outTerms, outRelationship);
// 		outTerms.close();
// 		outRelationship.close();
// 
// 		InsertableDbTable outAncestor = new InsertableDbTable(connectionWrapper, "mesh_ancestor");
// 		generateAncestorTable(outAncestor);
// 		outAncestor.close();
// 	}
// 
// 	private void generateAncestorTable(InsertableDbTable outAncestor) {
// 		Map<String, PairInfo> pairToInfo = new HashMap<String, PairInfo>();
// 		for (String treeNumber1 : treeNumberToUi.keySet()) {
// 			String parts[] = treeNumber1.split("\\.");
// 			String ui1 = treeNumberToUi.get(treeNumber1);
// 			for (int i = 0; i < parts.length; i++) {
// 				int distance = parts.length - i - 1;
// 				String treeNumber2 = StringUtilities.join(Arrays.copyOfRange(parts, 0, i + 1), ".");
// 				String ui2 = treeNumberToUi.get(treeNumber2);
// 				PairInfo pairInfo = pairToInfo.get(ui1 + "_" + ui2);
// 				if (pairInfo == null) {
// 					pairInfo = new PairInfo(distance, distance);
// 					pairToInfo.put(ui1 + "_" + ui2, pairInfo);
// 				} else {
// 					if (pairInfo.maxDistance < distance)
// 						pairInfo.maxDistance = distance;
// 					if (pairInfo.minDistance > distance)
// 						pairInfo.minDistance = distance;
// 				}
// 			}
// 		}
// 
// 		for (String pair : pairToInfo.keySet()) {
// 			PairInfo pairInfo = pairToInfo.get(pair);
// 			Row row = new Row();
// 			row.add("ancestor_ui", pair.split("_")[1]);
// 			row.add("descendant_ui", pair.split("_")[0]);
// 			row.add("max_distance", pairInfo.maxDistance);
// 			row.add("min_distance", pairInfo.minDistance);
// 			outAncestor.write(row);
// 		}
// 	}
// 
// 	private class PairInfo {
// 		public int	minDistance;
// 		public int	maxDistance;
// 
// 		public PairInfo(int minDistance, int maxDistance) {
// 			this.minDistance = minDistance;
// 			this.maxDistance = maxDistance;
// 		}
// 	}
// }


// Path: src/org/ohdsi/medlineXmlToDatabase/MainClass.java
import org.ohdsi.meshXmlToDatabase.MeshParserMain;

/*******************************************************************************
 * Copyright 2014 Observational Health Data Sciences and Informatics
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ohdsi.medlineXmlToDatabase;


/**
 * The main class that parses the command line arguments, and calls either MedlineAnalyserMain or MedlineParserMain
 * @author MSCHUEMI
 *
 */
public class MainClass {

	private static enum Action {
		ANALYSE, PARSE, PARSE_MESH
	};

	private static Action	action;
	private static String	pathToIniFile;

	public static void main(String[] args) {
		if (args.length == 0) {
			System.out.println("See https://github.com/OHDSI/MedlineXmlToDatabase for details on how to use");
			return;
		}
		parseParameters(args);
		if (action == Action.ANALYSE)
			MedlineAnalyserMain.main(new String[] { pathToIniFile });
		else if (action == Action.PARSE)
			MedlineParserMain.main(new String[] { pathToIniFile });
		if (action == Action.PARSE_MESH)
			MeshParserMain.main(new String[] { pathToIniFile });