Package net.octal.supinbank.service

Source Code of net.octal.supinbank.service.TransfertService

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.octal.supinbank.service;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import net.octal.supinbank.dao.AccountDao;
import net.octal.supinbank.dao.OperationDao;
import net.octal.supinbank.entity.Account;

import com.google.common.base.Preconditions;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import net.octal.banking.BasicBankAccountNumber;
import net.octal.supinbank.entity.Operation;

import net.octal.supinbank.scala.TransfertSerializer;
import net.octal.supinbank.constant.Resources;

/**
*
* @author octal
*/

@Stateless
public class TransfertService {
   
    @EJB
    private AccountService accountService;
   
    @EJB
    private OperationDao operationDao;

    @Resource(mappedName=Resources.JMS_CONNECTIONFACTORY)
    private QueueConnectionFactory qFactory;

    @Resource(mappedName=Resources.JMS_QUEUE)
    private Queue queue;

    public void performInternalTransfert(Account from, Account to, Double amount,
                                         String description) {
        Preconditions.checkNotNull(from);
        Preconditions.checkNotNull(to);
        Preconditions.checkArgument(amount <= from.getTotalAmount());
       
        final Calendar cal = Calendar.getInstance();
       
        Operation operationTo = new Operation();
        operationTo.setAmount(amount);
        operationTo.setDate(cal.getTime());
        operationTo.setWording(description);
        to.addOperation(operationTo);

        Operation operationFrom = new Operation();
        operationFrom.setAmount(-amount);
        operationFrom.setDate(cal.getTime());
        operationFrom.setWording(description);
        from.addOperation(operationFrom);

        operationDao.addOperation(operationFrom);
        operationDao.addOperation(operationTo);

        accountService.withDrawAndCredit(from.getId(), to.getId(), amount);

    }
   
    public void performAnotherCustomerTransfert(Account from, Account to, String bban,
                                         Double amount, String description) {
        Preconditions.checkNotNull(from);
        Preconditions.checkNotNull(to);
        Preconditions.checkArgument(amount <= from.getTotalAmount());
       
        final Calendar cal = Calendar.getInstance();
       
        final StringBuilder sb = new StringBuilder();
        sb.append(description);
        sb.append(String.format(" [INT TRANSFERT - %s]", bban));

        Operation operationFrom = new Operation();
        operationFrom.setAmount(-amount);
        operationFrom.setDate(cal.getTime());
        operationFrom.setWording(sb.toString());
        from.addOperation(operationFrom);

        Operation operationTo = new Operation();
        operationTo.setAmount(amount);
        operationTo.setDate(cal.getTime());
        operationTo.setWording(sb.toString());
        to.addOperation(operationTo);

        accountService.withDrawAndCredit(from.getId(), to.getId(), amount);
        operationDao.addOperation(operationFrom);
        operationDao.addOperation(operationTo);
    }
   
    public boolean performAnotherBankTransfert(Account from, BasicBankAccountNumber bban,
                                            String establishmentCode,
                                            Double amount, String description) {
       
        Preconditions.checkArgument(amount <= from.getTotalAmount());
       
        Connection connection = null;
         try {
            final Calendar cal = Calendar.getInstance();
            connection = qFactory.createConnection();
            final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            final MessageProducer producer = session.createProducer(queue);
           
            /* SCALA Part */
                   
            net.octal.supinbank.scala.xml.Account accFrom = new net.octal.supinbank.scala.xml.Account(
                     String.format("%s %s", from.getUser().getFirstName(), from.getUser().getLastName()),
                     establishmentCode, "00000",
                     BasicBankAccountNumber.normalizedAccountNumber(from.getId().toString()),
                     "00");
            net.octal.supinbank.scala.xml.Account accTo = new net.octal.supinbank.scala.xml.Account(
                     "John Doe",
                     bban.getNormalizedEstablishmentCode(), bban.getNormalizedBranchCode(),
                     bban.getNormalizedAccountNumber(),
                     bban.getNormalizedKey());

            TransfertSerializer ts = new TransfertSerializer(accFrom, accTo, description, amount, new Date());

            TextMessage message = session.createTextMessage();
           
            message.setText(ts.toXml().buildString(true));
            producer.send(message);

            final StringBuilder sb = new StringBuilder();
            sb.append(description);
            sb.append(String.format(" [EXT TRANSFERT - %s]", bban));

            Operation operation = new Operation();
            operation.setAmount(-amount);
            operation.setWording(sb.toString());
            operation.setDate(cal.getTime());
            from.addOperation(operation);

            accountService.withDrawAccount(from.getId(), amount);
            operationDao.addOperation(operation);


        } catch (JMSException ex) {
            return false;
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    return false;
                }
            }
        }

        /* JAXB Part */
        /*
       
        net.octal.supinbank.xml.ObjectFactory of = new net.octal.supinbank.xml.ObjectFactory();
                net.octal.supinbank.xml.Account accFrom = of.createAccount();
        accFrom.setAccountNumber(BasicBankAccountNumber.normalizedAccountNumber(
                from.getId().toString()));
        accFrom.setBranchCode("00000");
        accFrom.setEstablishmentCode(establishmentCode);
        accFrom.setOwnerName(String.format("%s %s", from.getUser().getFirstName(),
                from.getUser().getLastName()));
        accFrom.setKey("00");

        net.octal.supinbank.xml.Account accTo = of.createAccount();
        accTo.setAccountNumber(bban.getNormalizedAccountNumber());
        accTo.setBranchCode("00000");
        accTo.setEstablishmentCode(bban.getNormalizedEstablishmentCode());
        accTo.setKey(bban.getNormalizedKey());

        net.octal.supinbank.xml.BankTransfer bt = of.createBankTransfer();
        bt.setAmount(new BigDecimal(amount));
        bt.setRecipient(accTo);
        bt.setSender(accFrom);
        try {
            DatatypeFactory dataTypeFactory = DatatypeFactory.newInstance();
            final GregorianCalendar gcal = new GregorianCalendar();
            bt.setTransferDate(dataTypeFactory.newXMLGregorianCalendar(gcal));
        } catch (DatatypeConfigurationException ex) {
            return false;
        }
        bt.setWording(description);
       
        JAXBElement<net.octal.supinbank.xml.BankTransfer> jaxb = of.createBankTransfer(bt);
        try {
            /*JAXBContext jc = JAXBContext.newInstance("net.octal.supinbank.xml");
            Marshaller marshaller = jc.createMarshaller();
            final StringWriter sw = new StringWriter();
            marshaller.marshal(jaxb, sw);
             

        } catch (JAXBException ex) {
            return false;
        }
       
        */
        return true;
    }
   
}
TOP

Related Classes of net.octal.supinbank.service.TransfertService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.