Skip to content

Commit

Permalink
Parser creates nulls now
Browse files Browse the repository at this point in the history
  • Loading branch information
KatrinaHoffert committed Mar 10, 2015
1 parent 9df672e commit b6023e9
Show file tree
Hide file tree
Showing 4 changed files with 2,770 additions and 2,757 deletions.
33 changes: 22 additions & 11 deletions app/databaseParser/CSVLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,26 @@ class CSVLoader(writer: Writer) {


// Fix capitalization and escape single quotes
val locationName = WordUtils.capitalizeFully(dataMatrix(0)(1)).replaceAll("'","''")
val locationAddress = WordUtils.capitalizeFully(getIfExists(dataMatrix(0)(3)).replaceAll("'","''"))
val locationPostcode = getPostcode(dataMatrix(0)(5))
val locationCity = getCity(dataMatrix(0)(5)).replaceAll("'","''")
val locationRha = getIfExists(dataMatrix(0)(7))
var locationName = WordUtils.capitalizeFully(dataMatrix(0)(1))
var locationAddress = WordUtils.capitalizeFully(getIfExists(dataMatrix(0)(3)))
var locationPostcode = getPostcode(dataMatrix(0)(5))
var locationCity = getCity(dataMatrix(0)(5))
var locationRha = getIfExists(dataMatrix(0)(7))

// Must remove apostrophes from the places that can have them
if(locationName != null) locationName = locationName.replaceAll("'","''")
if(locationAddress != null) locationAddress = locationAddress.replaceAll("'","''")
if(locationCity != null) locationCity = locationCity.replaceAll("'","''")

// Now we need to add the quotes around the address, city, and postal code iff they aren't null
// The other values can't be null, so are quoted in the SQL below
if(locationAddress != null) locationAddress = "'" + locationAddress + "'"
if(locationCity != null) locationCity = "'" + locationCity + "'"
if(locationPostcode != null) locationPostcode = "'" + locationPostcode + "'"

// Insert location
writer.write("INSERT INTO location(id, name, address, postcode, city, rha)\n" +
" VALUES (%d, \'%s\', \'%s\', \'%s\', \'%s\', \'%s\');\n\n".format(locationId, locationName,
" VALUES (%d, \'%s\', %s, %s, %s, \'%s\');\n\n".format(locationId, locationName,
locationAddress, locationPostcode, locationCity, locationRha))

//this last ID is used because: their reports have duplicated records: a same violation under same inspection
Expand Down Expand Up @@ -167,7 +178,7 @@ class CSVLoader(writer: Writer) {
*/
def getCity(string: String): String = {
if(string == "") {
"Unknown";
null
}
else if(string.matches("^.+, .+$")) {
WordUtils.capitalizeFully(string.substring(0, string.lastIndexOf(',')))
Expand All @@ -176,7 +187,7 @@ class CSVLoader(writer: Writer) {
//some files have this column as ", SASKATCHEWAN"
//(see 'Saskatoon_Other Locations_Leaning Maple Meats - Catering [MCKILLOP].csv')
//need to return Unknown for this case
"Unknown"
null
}
else {
//if not match the above regexs, then it only contains the city name
Expand All @@ -191,13 +202,13 @@ class CSVLoader(writer: Writer) {
*/
def getPostcode(string: String): String = {
if(string == "" || string.length < 7) {
"Unknown"
null
}
else if(string.substring(string.length - 7).matches("^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} \\d{1}[A-Z]{1}\\d{1}$")) {
string.substring(string.length - 7)
}
else {
"Unknown"
null
}
}

Expand All @@ -206,5 +217,5 @@ class CSVLoader(writer: Writer) {
* @param string The full column from the CSV file.
* @return "Unknown" if contains nothing or the string itself otherwise.
*/
def getIfExists(string: String): String = if(string == "") "Unknown" else string
def getIfExists(string: String): String = if(string == "") null else string
}
4 changes: 3 additions & 1 deletion app/models/Location.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ object Location {
"""
)

query().map(_[String]("city")).toList :+ "Unknown city"
val cityList = query().map(_[String]("city")).toList

cityList ++ Seq("Unknown city")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions database/CreateTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ DROP TABLE IF EXISTS coordinate CASCADE;
CREATE TABLE location(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
address TEXT NOT NULL,
postcode TEXT NOT NULL,
city TEXT NOT NULL,
address TEXT,
postcode TEXT,
city TEXT,
rha TEXT NOT NULL,
latitude DECIMAL(10,6),
longitude DECIMAL(10,6)
Expand Down
Loading

0 comments on commit b6023e9

Please sign in to comment.