private void export(Session sess) throws IOException, DataAccessException { DataSource dataSource = registry.getDataSource(); Period period = registry.getPeriod(); List<Document> documents = dataSource.getDocumentDAO(sess).getByPeriodId(period.getId(), 1); final HashMap<Integer, Document> documentMap = new HashMap<Integer, Document>(); for (Document document : documents) { documentMap.put(document.getId(), document); } documents = null; final CSVWriter writer = new CSVWriter(new FileWriter(file)); writer.writeField("Tosite"); writer.writeField("Päivämäärä"); writer.writeField("Nro"); writer.writeField("Tili"); writer.writeField("Debet"); writer.writeField("Kredit"); writer.writeField("Selite"); writer.writeLine(); dataSource .getEntryDAO(sess) .getByPeriodId( period.getId(), EntryDAO.ORDER_BY_DOCUMENT_NUMBER, new DTOCallback<Entry>() { public void process(Entry entry) { Document document = documentMap.get(entry.getDocumentId()); if (document == null) return; Account account = registry.getAccountById(entry.getAccountId()); try { writer.writeField(Integer.toString(document.getNumber())); writer.writeField(dateFormat.format(document.getDate())); writer.writeField(account.getNumber()); writer.writeField(account.getName()); writer.writeField(entry.isDebit() ? numberFormat.format(entry.getAmount()) : ""); writer.writeField(!entry.isDebit() ? numberFormat.format(entry.getAmount()) : ""); writer.writeField(entry.getDescription()); writer.writeLine(); } catch (IOException e) { exception = e; } } }); writer.close(); if (exception != null) throw exception; setProgress(100); }
protected Void doInBackground() throws Exception { DataSource dataSource = registry.getDataSource(); Session sess = null; try { sess = dataSource.openSession(); export(sess); sess.commit(); } catch (Exception e) { e.printStackTrace(); if (sess != null) sess.rollback(); throw e; } finally { sess.close(); } return null; }