Saturday, April 20, 2013

Grails 2.x Excel Import via File Upload

Step 1: Install the excel-import-1.0.0 plugin to your the application you wish to add functionality to.
Step 2: Grails 2.X may throw an error installing the excel-import. simply open application.properties and append "plugins.joda-time=1.3.1" to the file. You may "Resolve Dependencies" or clean depending on your IDE. This seems to be an unresolved bug in the plugin codestream with Grails 2

Now for the code:

index.gsp
(add the following to your view)
<g:form action="upload" method="post" enctype="multipart/form-data">
<label for="file">File:</label>
<input type="file" name="file" id="file" />
<input class="save" type="submit" value="Upload" />


FilesController.groovy:

package edu.harvard.edu
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.grails.plugins.excelimport.*
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
class FilesController {
def excelImportService
def index() {
}
def upload = {

Map CONFIG_BOOK_COLUMN_MAP = [
sheet:'Sheet1',
startRow: 1,
columnMap:  [
      //Col, Map-Key
'A':'val1',
'B':'val2',
'C':'val3',
]
]


MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("file");

Workbook workbook = WorkbookFactory.create(file.inputStream)
 //Iterate through bookList and create/persists your domain instances
def bookList = excelImportService.columns(workbook, CONFIG_BOOK_COLUMN_MAP)
println bookList
}
}

Wednesday, September 19, 2012

Grails + Weblogic 10.3 JDBC Datasources

When deploying Grails applications in a production environment, utilizing the Weblogic application server JDBC dataSouces through Java EE JNDI is highly recommended. By using the datasource you offload the responsibility of connection pooling and tuning to the application server. In addition, personally I have found it makes maintenance safer and easier for developers since your datasource is coupled to it's environment rather than to the application. This was part of a requirement I recently had and the details seemed to be overlooked in the Grails books I had at my disposal. Here is an example of DataSource.groovy in a Weblogic 10.3 / Oracle 11g environment.


DataSource.groovy:



environments {
test {
             dataSource {
pooled = true
dialect = "org.hibernate.dialect.Oracle10gDialect"
driverClassName = "oracle.jdbc.driver.OracleDriver"
jndiName = "jdbc/myDataSource"
             }
     }
}