Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.nkapps.billing.services; import java.io.File; import java.io.FileInputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import com.nkapps.billing.dao.PaymentDao; import com.nkapps.billing.models.ClaimListPojo; import com.nkapps.billing.models.KeyPaymentListPojo; import com.nkapps.billing.models.Payment; import com.nkapps.billing.models.PaymentListPojo; import com.nkapps.billing.models.SimpleComboPojo; import com.nkapps.billing.webservices.beans.AccountInfoBean; /** * * @author nuraddin */ @Service("paymentService") public class PaymentServiceImpl implements PaymentService { @Autowired private PaymentDao paymentDao; @Autowired private Environment environment; @Override public AccountInfoBean getAccountInfo(String tin) throws Exception { AccountInfoBean aib = new AccountInfoBean(); BigDecimal overpaymentSum = paymentDao.getOverpaymentSumByTin(tin); if (overpaymentSum == null) { throw new Exception("Overpayment sum returned null value"); } if (overpaymentSum.compareTo(BigDecimal.ZERO) == -1) { throw new Exception("Overpayment sum returned negative value"); } BigDecimal keyCost = getKeyCost(); aib.setTin(tin); aib.setOverpaymentSum(overpaymentSum); aib.setKeyCost(keyCost); return aib; } @Override public BigDecimal getTransaction(String tin, Long serialNumber) throws Exception { AccountInfoBean aib = getAccountInfo(tin); if (aib.getOverpaymentSum().compareTo(aib.getKeyCost()) == -1) { throw new Exception("Account overpayment not enough for key"); } return paymentDao.getTransactionId(tin, serialNumber, aib.getKeyCost()); } @Override public void finishTransaction(BigDecimal keyTransactionId) throws Exception { paymentDao.saveKeyPaymentAndTransaction(keyTransactionId, getKeyCost()); } private BigDecimal getKeyCost() throws Exception { BigDecimal keyCost = new BigDecimal(environment.getProperty("key_cost")); if (keyCost == null) { throw new Exception("Key cost is undefined"); } return keyCost; } @Override public List<PaymentListPojo> getPaymentList(Map parameters) throws Exception { return paymentDao.getPaymentList(parameters); } @Override public List<KeyPaymentListPojo> getKeyPaymentList(Map parameters) throws Exception { return paymentDao.getKeyPaymentList(parameters); } @Override public List<SimpleComboPojo> getSources() throws Exception { return paymentDao.getSources(); } @Override public void parseAndSavePaymentManual(File file, Long issuerSerialNumber, String issuerIp) throws Exception { FileInputStream fis = new FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(fis); HSSFWorkbook workbook = new HSSFWorkbook(fs); HSSFSheet sheet = workbook.getSheetAt(0); String bankStatementId = null; List<Payment> paymentList = new LinkedList<Payment>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); int rowCurrent = 0, cellCurrent = 0; Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { rowCurrent++; cellCurrent = 0; Row row = rowIterator.next(); if (rowCurrent == 1) { // bank statement id bankStatementId = row.getCell(1).getStringCellValue(); } else if (rowCurrent > 3) { Payment payment = new Payment(); payment.setTin(row.getCell(1).getStringCellValue()); payment.setPaymentNum(row.getCell(4).getStringCellValue()); payment.setPaymentDate(dateFormat.parse(row.getCell(5).getStringCellValue())); payment.setPaymentSum(new BigDecimal(row.getCell(6).getNumericCellValue())); payment.setTinDebtor(row.getCell(7).getStringCellValue()); payment.setSourceCode((short) 1); payment.setState((short) 1); payment.setClaim((short) 0); paymentList.add(payment); } } fis.close(); paymentDao.savePaymentManual(bankStatementId, paymentList, issuerSerialNumber, issuerIp); } @Override public void insertPaymentManual(String tin, String paymentNum, Date paymentDate, BigDecimal paymentSum, String tinDebtor, Long issuerSerialNumber, String issuerIp) throws Exception { paymentDao.insertPaymentManual(tin, paymentNum, paymentDate, paymentSum, tinDebtor, issuerSerialNumber, issuerIp); } @Override public void updatePaymentManual(BigDecimal paymentId, String tin, String paymentNum, Date paymentDate, BigDecimal paymentSum, String tinDebtor, Long issuerSerialNumber, String issuerIp) throws Exception { paymentDao.updatePaymentManual(paymentId, tin, paymentNum, paymentDate, paymentSum, tinDebtor, issuerSerialNumber, issuerIp); } @Override public void removePaymentManual(BigDecimal paymentId) throws Exception { paymentDao.removePaymentManual(paymentId); } @Override public List<ClaimListPojo> getClaimList(Map parameters) throws Exception { return paymentDao.getClaimList(parameters); } }