com.accounting.mbeans.SmsController.java Source code

Java tutorial

Introduction

Here is the source code for com.accounting.mbeans.SmsController.java

Source

/*
 * 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.accounting.mbeans;

import com.accounting.smsbundle.SmsInfoSessionBean;
import com.accounting.commonutils.HelperUtil;
import com.accounting.entities.office.Office;
import com.accounting.smsbundle.SmsInfo;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.PhaseId;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author faisal
 */
@ManagedBean(name = "smsController")
@SessionScoped
public class SmsController {

    @EJB
    SmsInfoSessionBean smsInfoSessionBean;
    // 1. sms to selected member 
    //2. mobile number upload and send sms.
    //3. logs of sms
    //4. balance check of sms account.
    private UploadedFile file;
    List<SmsInfo> exceluploadSmsList;
    String smsMessage;
    private static final Double PER_SMS_CHARGE = 1.1;
    boolean disableSendSmsBtn;
    Double futsalBalance;
    int orgid;
    Date fromDate, toDate;

    public SmsController() {
    }

    @PostConstruct
    public void init() {
        futsalBalance = HelperUtil.getLoggedInOffice().getSmsBalance();
        orgid = HelperUtil.getLoggedInOfficeID();
    }

    public String initPage() {
        smsMessage = "";
        exceluploadSmsList = new ArrayList<SmsInfo>();
        disableSendSmsBtn = true;
        return "smsExcel.xhtml?faces-redirect=true";
    }

    public void handleFileUpload(FileUploadEvent event) {
        //        FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        //        FacesContext.getCurrentInstance().addMessage(null, message);
        //        System.out.println("entering into excel upload ");
        if (!PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId())) {
            event.setPhaseId(PhaseId.INVOKE_APPLICATION);
            event.queue();
        } else {
            //do stuff here, #{ngoPhotoBean.description} is set
            System.out.println("smsMessage " + smsMessage);
            if (smsMessage.isEmpty() || smsMessage.length() < 5) {
                System.out.println("Please write sms message first.");
                HelperUtil.showErrorMessage("Please write sms message first.");
            } else {
                try {

                    UploadedFile uploadedFile = (UploadedFile) event.getFile();
                    String filename = uploadedFile.getFileName();
                    //            System.out.println("file name is " + filename);
                    InputStream input = uploadedFile.getInputstream();

                    if (filename.endsWith("xlsx")) {

                        XSSFWorkbook wb = new XSSFWorkbook(input);
                        XSSFSheet sheet = wb.getSheetAt(0);
                        Iterator rows = sheet.rowIterator();
                        setupSmsInfoData(rows);
                    } else if (filename.endsWith("xls")) {

                        HSSFWorkbook wb = new HSSFWorkbook(input);
                        HSSFSheet sheet = wb.getSheetAt(0);
                        Iterator rows = sheet.rowIterator();
                        setupSmsInfoData(rows);
                    }

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            checkBalance();
        }

    }

    public void setupSmsInfoData(Iterator rows) {
        //        System.out.println("setupSmsInfoData called.");
        exceluploadSmsList = new ArrayList<SmsInfo>();
        // col1 = mobile number, col2 = send on date optionalCOLumn
        int rowcount = 0; //skipping first row
        while (rows.hasNext()) {

            XSSFRow row = (XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            if (rowcount > 0) {
                int cellCount = 0;
                SmsInfo smsinfo = new SmsInfo();
                smsinfo.setSmsText(smsMessage);
                smsinfo.setOrgId(orgid);
                while (cells.hasNext()) {
                    try {

                        XSSFCell cell = (XSSFCell) cells.next();
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        System.out.println("mobile " + cell.getStringCellValue());
                        if (cellCount == 0) {

                            smsinfo.setMobileNumber(String.valueOf(cell.getStringCellValue()));
                        }
                        if (cellCount == 1) {
                            try {
                                smsinfo.setSentDate(cell.getDateCellValue());
                            } catch (Exception e) {

                            }
                        }
                        cellCount++;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                exceluploadSmsList.add(smsinfo);
            }
            rowcount++;
        }
    }

    private void checkBalance() {
        if (exceluploadSmsList != null && !exceluploadSmsList.isEmpty()) {
            Double futsalBalance = HelperUtil.getLoggedInOffice().getSmsBalance();
            if (futsalBalance == null) {
                System.out.println("balance is null");
                HelperUtil.showErrorMessage("Insufficient Balance. Please recharge.");
                disableSendSmsBtn = true;
            } else {
                double totalsmscost = exceluploadSmsList.size() * PER_SMS_CHARGE;
                System.out.println("totalsmscost " + totalsmscost);
                if (futsalBalance < totalsmscost) {
                    System.out.println("Insufficient Balance. Please recharege your account.");
                    HelperUtil.showErrorMessage(
                            "Your balance " + futsalBalance + " is insufficient. Please recharge.");
                    disableSendSmsBtn = true;
                } else {
                    disableSendSmsBtn = false;
                }
            }
        }
    }

    public void saveForSendingSms() {
        /// save the list in database
        System.out.println("saveForSendingSms called");
        if (exceluploadSmsList != null && !exceluploadSmsList.isEmpty()) {
            smsInfoSessionBean.addSmsQueue(exceluploadSmsList);
            HelperUtil.showSuccessMessage("Successfully queued.");
            disableSendSmsBtn = true;

            // change futsal balance here..
            Office org = HelperUtil.getLoggedInOffice();
            double futsalbalance = org.getSmsBalance();
            double totalsmscost = exceluploadSmsList.size() * PER_SMS_CHARGE;
            double newbalnce = futsalbalance - totalsmscost;
            org.setSmsBalance(newbalnce);
            smsInfoSessionBean.mergeObject(org);
        }

    }

    public Double getFutsalBalance() {
        return futsalBalance;
    }

    public void setFutsalBalance(Double futsalBalance) {
        this.futsalBalance = futsalBalance;
    }

    public boolean isDisableSendSmsBtn() {
        return disableSendSmsBtn;
    }

    public void setDisableSendSmsBtn(boolean disableSendSmsBtn) {
        this.disableSendSmsBtn = disableSendSmsBtn;
    }

    public SmsInfoSessionBean getSmsInfoSessionBean() {
        return smsInfoSessionBean;
    }

    public int getOrgid() {
        return orgid;
    }

    public void setOrgid(int orgid) {
        this.orgid = orgid;
    }

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }

    public void setSmsInfoSessionBean(SmsInfoSessionBean smsInfoSessionBean) {
        this.smsInfoSessionBean = smsInfoSessionBean;
    }

    public List<SmsInfo> getExceluploadSmsList() {
        return exceluploadSmsList;
    }

    public void setExceluploadSmsList(List<SmsInfo> exceluploadSmsList) {
        this.exceluploadSmsList = exceluploadSmsList;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public String getSmsMessage() {
        return smsMessage;
    }

    public void setSmsMessage(String smsMessage) {
        this.smsMessage = smsMessage;
    }

}