Reputation: 47
I have a table in my database which needs to updates with value for some rows and columns from csv file( this file outside of the grails application). The csv file contains large set of data with map to specific address and city. Some of the address in my application have wrong cities. So I want to get a city from database(grails application db), compare it with the city in csv file, map address to it, and add that address to the application database.
what is the best approach?
Upvotes: 0
Views: 428
Reputation: 3932
For Grails 3 use https://bintray.com/sachinverma/plugins/org.grails.plugins:csv to parse CSV, add the following to build.gradle. The plugin is also available for Grails 2.
repositories {
https://bintray.com/sachinverma/plugins/org.grails.plugins:csv
}
dependencies {
compile "org.grails.plugins:csv:1+"
}
Then in your service use like:
def is
try {
is = params.csvfile.getInputStream()
def csvMapReader = new CSVMapReader( new InputStreamReader( is ) )
csvMapReader.fieldKeys = ["city","address1", "address2"]
csvMapReader.eachWithIndex { map, idx ->
def dbEntry = DomainObject.findByAddress1AndAddress2( map.address1, map.address2 )
if ( map.city != dbEntry.city ) {
// assuming we're just updating the city on current entry?
dbEntry.city = map.city
dbEntry.save()
}
// do whatever logic
}
finally {
is?.close
}
This is of course a simplified version as I don't know you're csv or schema layout.
Upvotes: 1