Package com.cin.mdb

Source Code of com.cin.mdb.PurchaseProcessor

package com.cin.mdb;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.ejb.*;
import javax.jms.*;
import javax.naming.NamingException;

import com.cin.dto.InsurencePolicyDTO;
import com.cin.dto.InvitationDTO;
import com.cin.dto.UserDTO;
import com.cin.ejb.transactionejb.TransactionRemote;

import com.cin.jndi.lookup.EJBLookup;

/**
* Message-Driven Bean implementation class for: PurchaseProcessor
* A message is generated and sent to this bean after a customer
* purchased a policy. This bean is responsible for post processing
* of a purchase.
* Currently, the bean compensates agent(s) involved with this customer.
*/
@MessageDriven(
    activationConfig = { @ActivationConfigProperty(
        propertyName = "destinationType", propertyValue = "javax.jms.Queue"
    ) },
    mappedName = "jms/PurchaseQueue")
public class PurchaseProcessor implements MessageListener {
  public static final String POLICY_PROPERTY_NAME = "policy";
  private Logger log = Logger.getLogger("com.cin.mdb");
  private TransactionRemote aRemote;

    /**
     * Default constructor.
     */
    public PurchaseProcessor() throws RemoteException, NamingException{
    aRemote = (TransactionRemote)EJBLookup.getInstance().getTransactionEJB();
    }
 
  /**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
      log.entering("PurchaseProcessor", "onMessage");
     
      InsurencePolicyDTO policy;
        try {
          ObjectMessage objectMessage = (ObjectMessage) message;
      policy = (InsurencePolicyDTO) objectMessage.getObject();
      compensateAgentForInvite(policy);
    } catch (JMSException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    }
   
    private void compensateAgentForInvite(InsurencePolicyDTO policy){
      List<InvitationDTO> userInvites = aRemote.getInvitationsByCustomer(policy.getUserSSN());
   
    userInvites = filterNoAgentInvite(userInvites);
    InvitationDTO invite = getNewestInvite(userInvites);
   
    if( invite == null){
      // nobody to compensate
      log.finer("Nobody to compensate for given purchase.");
      return;
    }
   
    UserDTO agent = invite.getAgent();
    int compensation = Math.round(policy.getQuote() * 0.1f);
   
    aRemote.addPayroll(agent, compensation);
    }
   
    private List<InvitationDTO> filterNoAgentInvite(List<InvitationDTO> all){
      List<InvitationDTO> selected = new ArrayList<InvitationDTO>();
      for(InvitationDTO inv : all){
        if( inv.getAgent() != null){
          selected.add(inv);
        }
      }
      return selected;
    }
   
    private InvitationDTO getNewestInvite(List<InvitationDTO> all){
    if( all.size() == 0 ){
      return null;
    }
    InvitationDTO newest = all.get(0);
    for(InvitationDTO inv : all){
      if( inv.getDateCreated().after(newest.getDateCreated())){
        newest = inv;
      }
    }
   
    return newest;
    }
}
TOP

Related Classes of com.cin.mdb.PurchaseProcessor

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.