private String filename;
private List queryStrings;
public void doStart() {
Session session = null;
SessionFactory sessionFactory = null;
Transaction transaction = null;
try {
sessionFactory = getConfiguration().buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
// TODO: this is not the most efficient loop (opening/closing file)
for (Iterator iter = queryStrings.iterator(); iter.hasNext();) {
String query = (String) iter.next();
List list = session.createQuery(query).list();
if(getFileName()!=null) {
PrintWriter pw = null;
try {
File file = new File( getOutputDirectory(), getFileName() );
getTemplateHelper().ensureExistence( file );
pw = new PrintWriter( new FileWriter( file, true ) );
getArtifactCollector().addFile( file, "query-output" );
for (Iterator iter1 = list.iterator(); iter1.hasNext();) {
Object element = iter1.next();
pw.println(element);
}
}
catch (IOException e) {
throw new ExporterException("Could not write query output",e);
} finally {
if(pw!=null) {
pw.flush();
pw.close();
}
}
}
}
transaction.commit();
} catch(HibernateException he) {
if(transaction!=null) {
transaction.rollback();
}
throw new ExporterException("Error occured while trying to execute query", he);
} finally {
if(session!=null) {
session.close();
}
if(sessionFactory!=null) {
sessionFactory.close();
}