Last active
January 30, 2017 03:36
-
-
Save Kavignon/633e55913199c9f349ab0fd397bedede to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.sql.Connection; | |
import java.sql.*; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.HashMap; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.DocumentBuilder; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.NodeList; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.Element; | |
import java.io.File; | |
import database.*; | |
import java.lang.*; | |
import java.util.concurrent.ThreadLocalRandom; | |
public class DataValidator { | |
public final int client_Count = 21550; | |
public final int film_Count = 631; | |
public final int personne_Count = 4549; | |
public Document xmlDocument; | |
public String[] xmlFilePaths; | |
public HashMap<String,HashMap<String, HashSet<String>>> validLoadedData; | |
public ArrayList<String[]> randomValidData; | |
public Connection dbConnection; | |
public String[] filmElements; | |
public String[] clientElements; | |
public String[] personElements; | |
public void initialize(Connection coTo){ | |
validLoadedData = new HashMap<>(); | |
filmElements = new String[] { "titre", "pays", "langue", "duree", "resume", "genre", "realisateur", | |
"scenariste" }; | |
clientElements = new String[] { "nom-famille", "prenom", "courriel", "tel", "aaniversaire", "adresse", "ville", | |
"province", "code-postal", "mot-de-passe", "forfait" }; | |
personElements = new String[] { "nom", "photo", "bio" }; | |
dbConnection = coTo; | |
} | |
public DataValidator(String movieXMLFilepath, | |
String personXMLFilepath, | |
String clientXMLFilepath, | |
Connection coTo) | |
{ | |
xmlFilePaths = new String[] { movieXMLFilepath, personXMLFilepath, clientXMLFilepath }; | |
initialize(coTo); | |
} | |
public void reopenConnectionWhenClosed() throws SQLException{ | |
if(dbConnection.isClosed()) | |
dbConnection = DriverManager.getConnection("jdbc:oracle:thin:@big-data-2.logti.etsmtl.ca:1521:GTI660", "equipe8", "bxlnx4iA"); | |
} | |
public String[] getElementArray(int table){ | |
switch(table){ | |
case 0 : return filmElements; | |
case 1 : return clientElements; | |
case 2 : return personElements; | |
default : return null; | |
} | |
} | |
public String getTableName(int table) { | |
switch (table) { | |
case 0: return "film"; | |
case 1: return "client"; | |
case 2: return "personne"; | |
default: return ""; | |
} | |
} | |
public int computeTableCount(String tableName) throws SQLException{ | |
Statement st = dbConnection.createStatement(); | |
ResultSet res = st.executeQuery("SELECT COUNT(*) FROM "+tableName); | |
int count = 0; | |
while (res.next()){ | |
count = res.getInt(1); | |
} | |
return count; | |
} | |
public boolean areTableCountValid(String[] tables) throws SQLException { | |
int tableIndex = 0; | |
boolean tableAreValid = true; | |
for(String table : tables) | |
{ | |
int count = computeTableCount(table); | |
switch(tableIndex) | |
{ | |
case 0 : | |
if(count != film_Count) { | |
System.out.printf("The film data isn't valid. The count isn't right. Count is " + | |
"%d instead of %d", count, film_Count); | |
tableAreValid = false; | |
} | |
break; | |
case 1 : | |
if (count != client_Count) { | |
System.out.printf("The client data isn't valid. The count isn't right. Count is " + | |
"%d instead of %d", count, client_Count); | |
tableAreValid = false; | |
} | |
break; | |
case 2 : | |
if(count != personne_Count) { | |
System.out.printf("The personne data isn't valid. The count isn't right. Count is " + | |
"%d instead of %d", count, personne_Count); | |
tableAreValid = false; | |
} | |
break; | |
} | |
tableIndex++; | |
} | |
return tableAreValid; | |
} | |
public Document parseXmlDocument(String filepath) throws IOException { | |
try { | |
File inputFile = new File(filepath); | |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
Document doc = dBuilder.parse(inputFile); | |
doc.getDocumentElement().normalize(); | |
System.out.printf("The document was parsed for %s", filepath); | |
return doc; | |
} | |
catch(Exception e){ | |
System.out.println(e.toString()); | |
} | |
System.out.println("Something went wrong"); | |
return null; | |
} | |
private void loadDocumentStructure(int table) throws IOException{ | |
String[] elementArray = getElementArray(table); | |
Document doc = parseXmlDocument(xmlFilePaths[table]); | |
String tableName = getTableName(table); | |
NodeList nList = doc.getElementsByTagName(tableName); | |
HashMap<String, HashSet<String>> nodeInformation = new HashMap<String, HashSet<String>>(); | |
for(int tempNodeIndex = 0; tempNodeIndex < nList.getLength(); tempNodeIndex++){ | |
Node tNode = nList.item(tempNodeIndex); | |
if(tNode.getNodeType() == Node.ELEMENT_NODE) { | |
Element nodeElement = (Element) tNode; | |
for (int index = 0; index < elementArray.length; index++) { | |
String clientKey = elementArray[index]; | |
String elementInformation = nodeElement.getElementsByTagName(clientKey).item(0).getTextContent(); | |
nodeInformation.get(clientKey).add(elementInformation); | |
nodeInformation.put(clientKey, nodeInformation.get(clientKey)); | |
} | |
} | |
} | |
validLoadedData.put(tableName, nodeInformation); | |
} | |
private void loadRandomData() { | |
for(int i = 0; i < 50; i++){ | |
// nextInt is normally exclusive of the top value, | |
// so add 1 to make it inclusive | |
int randomNum = ThreadLocalRandom.current().nextInt(0, 2 + 1); | |
} | |
} | |
private boolean areInsertedXmlNodesValid() throws SQLException { | |
for(int i=0; i<validLoadedData.size(); i++){ | |
String table = getTableName(i); | |
HashMap<String, HashSet<String>> map = validLoadedData.get(table); | |
String[] array = getElementArray(i); | |
reopenConnectionWhenClosed(); | |
for(int rowIndex=0; rowIndex < array.length; rowIndex++){ | |
String row = array[rowIndex]; | |
int rowCount = computeTableCount(table); | |
int validRowCount = map.get(row).size(); | |
if(rowCount != validRowCount){ | |
System.out.printf("The count isn't valid. It should have been %d instead of %d for row %s in table %s", | |
validRowCount, rowCount, row, table); | |
return false; | |
} | |
} | |
System.out.printf("The %s table node children are valid",table); | |
dbConnection.close(); | |
} | |
return true; | |
} | |
public boolean isDatabaseValid(){ | |
try { | |
if (!areTableCountValid(new String[]{"film", "client", "personne"})) | |
return false; | |
System.out.println("The lengths for the client, personne and film table are valid"); | |
for(int i =0; i < 3; i++) | |
loadDocumentStructure(i); | |
if(!areInsertedXmlNodesValid()) | |
return false; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment