be.fgov.minfin.webForm.backing.SqueezeCdcBean.java Source code

Java tutorial

Introduction

Here is the source code for be.fgov.minfin.webForm.backing.SqueezeCdcBean.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 be.fgov.minfin.webForm.backing;

import be.fgov.minfin.ccff.fwk.logging.api.Logger;
import be.fgov.minfin.ccff.fwk.logging.api.LoggerFactory;
import be.fgov.minfin.ccff.fwk.mail.api.exceptions.MailException;
import be.fgov.minfin.webForm.entity.Email;
import be.fgov.minfin.webForm.entity.Sujet;
import be.fgov.minfin.webForm.util.ComStructUtil;
import be.fgov.minfin.webForm.util.ISINValidationUtil;
import be.fgov.minfin.webForm.util.MailHelper;
import be.fgov.minfin.webForm.util.Partie;
import be.fgov.minfin.webForm.util.SitranBCEUtil;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import org.apache.commons.validator.routines.checkdigit.CheckDigitException;
import org.apache.commons.validator.routines.checkdigit.IBANCheckDigit;
import org.primefaces.context.RequestContext;

/**
 *
 * @author gbeurnea
 */
@ManagedBean(name = "squeezeCdcBean")
@ViewScoped
public class SqueezeCdcBean extends WebFormBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(SqueezeCdcBean.class);

    private boolean visibleConfirm = false;
    private boolean mailEnvoye = false;
    private String montantTotal = "0";
    private String numCompte;
    private String nomCompte;
    private List<Partie> partiesActions;
    private List<Partie> partiesCoupons;
    private Partie partieSociete = new Partie();

    @ManagedProperty(value = "#{authentificationCdcBean}")
    private AuthentificationCdcBean authentificationCdcBean;

    @PostConstruct
    public void init() {
        selectedDocType = "CDC10";
        this.initialize();
        partiesActions = new ArrayList<Partie>();
        Partie partieActionA = new Partie();
        partieActionA.setTypePartie("action");
        Partie partieActionB = new Partie();
        partieActionB.setTypePartie("action");
        partiesActions.add(partieActionA);
        partiesActions.add(partieActionB);

        partiesCoupons = new ArrayList<Partie>();
        Partie partieCouponA = new Partie();
        partieCouponA.setTypePartie("coupon");
        Partie partieCouponB = new Partie();
        partieCouponB.setTypePartie("coupon");
        partiesCoupons.add(partieCouponA);
        partiesCoupons.add(partieCouponB);

        if (authentificationCdcBean.getNom() != null) {
            nomCompte = authentificationCdcBean.getNom();
        } else {
            nomCompte = authentificationCdcBean.getNomSociete();
        }

    }

    /*
    Vrifie si le code ISIN est correcte
     */
    public void checkISIN(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Application app = FacesContext.getCurrentInstance().getApplication();
        ResourceBundle bundle = app.getResourceBundle(FacesContext.getCurrentInstance(), "CdcMessages");
        ISINValidationUtil isinValidation = new ISINValidationUtil();
        String code = (String) value;
        try {
            int modulus = isinValidation.calculcateModulus(code, true);
            if (modulus != 0) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        bundle.getString("squeeze.titre.code.valid"), ""));
            }
        } catch (CheckDigitException e) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    bundle.getString("squeeze.titre.code.valid"), ""));
        }
    }

    public void chercheSociete() throws IOException {
        String numBCEFinal = partieSociete.getBce().replaceAll("\\.", "").trim();
        SitranBCEUtil sitran = new SitranBCEUtil(numBCEFinal);
        partieSociete.setNomSociete(sitran.getNomSociete());
        partieSociete.setRue(sitran.getRue());
        partieSociete.setNumrue(sitran.getNumrue());
        partieSociete.setCodepostal(sitran.getCodepostal());
        partieSociete.setVille(sitran.getVille());
        partieSociete.setPays(sitran.getPays());
    }

    /*
    Ajout d'une partie supplmentaire  l'arraylist
     */
    public void ajoutColonneAction() {
        Partie partie = new Partie();
        partie.setTypePartie("action");
        partiesActions.add(partie);
    }

    /*
    Ajout d'une partie supplmentaire  l'arraylist
     */
    public void ajoutColonneCoupon() {
        Partie partie = new Partie();
        partie.setTypePartie("coupon");
        partiesCoupons.add(partie);
    }

    /*
    Retourne la lettre alphabtique correspondant au numro entier
     */
    public String getCharForNumber(int i) {
        return i > 0 && i < 27 ? String.valueOf((char) (i + 64)) : null;
    }

    /*
    Vrifie si le comte bancaire est correcte
     */
    public void checkDigit(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Application app = FacesContext.getCurrentInstance().getApplication();
        ResourceBundle bundle = app.getResourceBundle(FacesContext.getCurrentInstance(), "CdcMessages");
        IBANCheckDigit ibancheck = new IBANCheckDigit();
        String numComptAValider = (String) value;
        if (ibancheck.isValid(numComptAValider) == false) {
            throw new ValidatorException(
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, bundle.getString("squeeze.numcompte.error"), ""));
        }
    }

    public void calculMontantTotal() {
        double montantEnDouble = 0;
        for (int i = 0; i < partiesActions.size(); i++) {
            if (partiesActions.get(i).getMontant() != null && partiesActions.get(i).getNb() != null) {
                String montant = partiesActions.get(i).getMontant().replaceAll(",", ".");
                double somme = Double.valueOf(montant) * Double.valueOf(partiesActions.get(i).getNb());
                montantEnDouble = montantEnDouble + somme;
            }
        }
        for (int i = 0; i < partiesCoupons.size(); i++) {
            if (partiesCoupons.get(i).getMontant() != null && partiesCoupons.get(i).getNb() != null) {
                String montant = partiesCoupons.get(i).getMontant().replaceAll(",", ".");
                double somme = Double.valueOf(montant) * Double.valueOf(partiesCoupons.get(i).getNb());
                montantEnDouble = montantEnDouble + somme;
            }
        }
        montantTotal = String.valueOf(montantEnDouble);
    }

    /*
    Rcupre la communication structure
    */
    public String getCommStruct() {
        ComStructUtil comStruct = new ComStructUtil("188022", super.webFormManager.getSequenceCdc());
        return comStruct.getCommStruct();
    }

    /*
    Envoi un e-mail
     */
    public void sendEmail() {
        Application app = FacesContext.getCurrentInstance().getApplication();
        ResourceBundle bundle = app.getResourceBundle(FacesContext.getCurrentInstance(), "CdcMessages");

        String bodyEmail = null;
        Sujet sujet = new Sujet();
        sujet.setIdSujet(259);
        sujet = this.webFormManager.findSujetById(sujet);
        if (sujet != null) {
            String language = this.getLanguage();
            StringBuilder messageEmail = new StringBuilder();
            MailHelper mailhelper = new MailHelper();
            //Ajoute la communication structure au mail
            messageEmail.append("Donnes communiques pour l'ouverture du dossier :\n");
            //Ajoute la partie authentification au message
            String idenf = mailhelper.getCorpsIdenf(this.authentificationCdcBean);
            messageEmail.append(idenf);
            // Formulaire squeezeout !!!!!
            messageEmail.append("\n###################### Type de dossier : Squeeze-out #####################")
                    .append("\nIdentification de la socit  laquelle les titres se rapportent :")
                    .append("\nNationalit : ").append(partieSociete.getNationalite())
                    .append("\nNom de l'entreprise : ").append(partieSociete.getNomSociete()).append("\nRue : ")
                    .append(partieSociete.getRue()).append("\nNumro de rue : ").append(partieSociete.getNumrue())
                    .append("\nCode postal : ").append(partieSociete.getCodepostal()).append("\nVille : ")
                    .append(partieSociete.getVille()).append("\nPays : ").append(partieSociete.getPays() + "\n");
            messageEmail.append("\nRpartition du montant  verser  la Caisse des Dpts et Consignations");
            // Pour chaque Action
            for (int i = 0; i < partiesActions.size(); i++) {
                messageEmail.append("\n-- Type d'action " + (i + 1) + " --").append("\nCode Isin : ")
                        .append(partiesActions.get(i).getCode()).append("\nNombre d'actions : ")
                        .append(partiesActions.get(i).getNb()).append("\nMontant par action : ")
                        .append(partiesActions.get(i).getMontant()).append("\nCoupon attach  l'action : ")
                        .append(partiesActions.get(i).getNumcoupon());
            }
            // Pour chaque coupon
            for (int i = 0; i < partiesCoupons.size(); i++) {
                messageEmail.append("\n-- Coupon " + (i + 1) + " --").append("\nNombre de coupons : ")
                        .append(partiesCoupons.get(i).getNb())
                        .append("\nValeur du coupon nette de prcompte mobilier : ")
                        .append(partiesCoupons.get(i).getMontant());
            }
            messageEmail.append("\n\nMontant total  verser  la Caisse des Dpts et Consignations : ")
                    .append(montantTotal + " EUR").append("\nNumro de compte de versement : ").append(numCompte)
                    .append("\nNom du titulaire du compte : ").append(nomCompte);

            List emailList = sujet.getEmailList();
            if (emailList != null) {
                for (int i = 0; i < emailList.size(); i++) {
                    Email emailTmp = (Email) emailList.get(i);
                    String emailTo = this.webFormUtil.getPropertyValue((Object) emailTmp, "email", language);
                    emailTmp.setEmail(emailTo);
                    bodyEmail = "Nouvelle demande d'ouverture d'un dossier squeeze-out";
                    try {
                        if (authentificationCdcBean.getMail2() != null) {
                            this.webFormUtil.sendMail(emailTmp.getEmail(), bodyEmail, messageEmail.toString(),
                                    this.authentificationCdcBean.getMail2(), super.attachments);
                        } else {
                            this.webFormUtil.sendMail(emailTmp.getEmail(), bodyEmail, messageEmail.toString(),
                                    this.authentificationCdcBean.getMail(), super.attachments);
                        }
                        this.mailEnvoye = true;

                    } catch (MailException e) {
                        this.sousStatus = "email";
                        logger.error("Erreur dans la mthode sendMail");
                        FacesMessage msg = new FacesMessage(bundle.getString("squeeze.mail.error"));
                        FacesContext.getCurrentInstance().addMessage(null, msg);
                    }
                }
                // Si le mail est envoy  la CDC, on envoi au client !
                if (mailEnvoye == true) {
                    StringBuilder messageClient = new StringBuilder();
                    messageClient.append(mailhelper.getCorpsClientIdenf(authentificationCdcBean));
                    messageClient
                            .append("\n###################### " + bundle.getString("squeeze.mail.titre")
                                    + " #####################")
                            .append("\n" + bundle.getString("squeeze.panelun") + " : ")
                            .append("\n" + bundle.getString("squeeze.panelvalid.nationalite") + " ")
                            .append(partieSociete.getNationalite())
                            .append("\n" + bundle.getString("squeeze.nomsoc") + " : ")
                            .append(partieSociete.getNomSociete())
                            .append("\n" + bundle.getString("squeeze.rue") + " : ").append(partieSociete.getRue())
                            .append("\n" + bundle.getString("squeeze.numrue") + " : ")
                            .append(partieSociete.getNumrue())
                            .append("\n" + bundle.getString("squeeze.codepostal") + " : ")
                            .append(partieSociete.getCodepostal())
                            .append("\n" + bundle.getString("squeeze.ville") + " : ")
                            .append(partieSociete.getVille())
                            .append("\n" + bundle.getString("squeeze.pays") + " : ")
                            .append(partieSociete.getPays() + "\n");
                    messageClient.append("\n" + bundle.getString("squeeze.paneldeux") + " : ");
                    // Pour chaque Action
                    for (int i = 0; i < partiesActions.size(); i++) {
                        messageClient
                                .append("\n-- " + bundle.getString("squeeze.typeaction") + " " + (i + 1) + " --")
                                .append("\n" + bundle.getString("squeeze.isin") + " : ")
                                .append(partiesActions.get(i).getCode())
                                .append("\n" + bundle.getString("squeeze.nbaction") + " : ")
                                .append(partiesActions.get(i).getNb())
                                .append("\n" + bundle.getString("squeeze.montant") + " : ")
                                .append(partiesActions.get(i).getMontant())
                                .append("\n" + bundle.getString("squeeze.couponattach") + " : ")
                                .append(partiesActions.get(i).getNumcoupon());
                    }
                    // Pour chaque coupon
                    for (int i = 0; i < partiesCoupons.size(); i++) {
                        messageClient.append("\n-- " + bundle.getString("squeeze.coupon") + " " + (i + 1) + " --")
                                .append("\n" + bundle.getString("squeeze.nbcoupons") + " : ")
                                .append(partiesCoupons.get(i).getNb())
                                .append("\n" + bundle.getString("squeeze.montantcoupon") + " : ")
                                .append(partiesCoupons.get(i).getMontant());
                    }
                    messageClient.append("\n\n" + bundle.getString("squeeze.montanttotal") + " : ")
                            .append(montantTotal + " EUR")
                            .append("\n" + bundle.getString("squeeze.numcompte.label") + " : ").append(numCompte)
                            .append("\n" + bundle.getString("squeeze.nomcompte.label") + " : ").append(nomCompte);
                    bodyEmail = bundle.getString("squeeze.mail.body");
                    try {
                        if (authentificationCdcBean.getMail2() != null) {
                            this.webFormUtil.sendMail(this.authentificationCdcBean.getMail2(), bodyEmail,
                                    messageClient.toString(), "depots.tresorerie@minfin.fed.be");
                        } else {
                            this.webFormUtil.sendMail(this.authentificationCdcBean.getMail(), bodyEmail,
                                    messageClient.toString(), "depots.tresorerie@minfin.fed.be");
                        }
                        FacesMessage msg = new FacesMessage(bundle.getString("squeeze.validfinal"));
                        FacesContext.getCurrentInstance().addMessage(null, msg);
                        RequestContext.getCurrentInstance().execute("window.scrollTo(0,0);");
                        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
                    } catch (MailException e) {
                        this.sousStatus = "email";
                        logger.error("Erreur dans la mthode sendMail");
                        FacesMessage msg = new FacesMessage(bundle.getString("squeeze.mail.error"));
                        FacesContext.getCurrentInstance().addMessage(null, msg);
                    }
                }

            } else {
                logger.error("emailList = null ******************");
                FacesMessage msg = new FacesMessage(bundle.getString("squeeze.mail.error"));
                FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        } else {
            logger.error("emailList = null ******************");
            FacesMessage msg = new FacesMessage(bundle.getString("squeeze.mail.error"));
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    /*
    Affiche la page de confirmation
     */
    public void afficheConfirm() {
        this.visibleConfirm = !this.visibleConfirm;
    }

    public AuthentificationCdcBean getAuthentificationCdcBean() {
        return authentificationCdcBean;
    }

    public void setAuthentificationCdcBean(AuthentificationCdcBean authentificationCdcBean) {
        this.authentificationCdcBean = authentificationCdcBean;
    }

    public boolean isVisibleConfirm() {
        return visibleConfirm;
    }

    public void setVisibleConfirm(boolean visibleConfirm) {
        this.visibleConfirm = visibleConfirm;
    }

    public List<Partie> getPartiesActions() {
        return partiesActions;
    }

    public void setPartiesActions(List<Partie> partiesActions) {
        this.partiesActions = partiesActions;
    }

    public List<Partie> getPartiesCoupons() {
        return partiesCoupons;
    }

    public void setPartiesCoupons(List<Partie> partiesCoupons) {
        this.partiesCoupons = partiesCoupons;
    }

    public Partie getPartieSociete() {
        return partieSociete;
    }

    public void setPartieSociete(Partie partieSociete) {
        this.partieSociete = partieSociete;
    }

    public String getMontantTotal() {
        return montantTotal;
    }

    public void setMontantTotal(String montantTotal) {
        this.montantTotal = montantTotal;
    }

    public String getNumCompte() {
        return numCompte;
    }

    public void setNumCompte(String numCompte) {
        this.numCompte = numCompte;
    }

    public String getNomCompte() {
        return nomCompte;
    }

    public void setNomCompte(String nomCompte) {
        this.nomCompte = nomCompte;
    }

    public boolean isMailEnvoye() {
        return mailEnvoye;
    }

    public void setMailEnvoye(boolean mailEnvoye) {
        this.mailEnvoye = mailEnvoye;
    }

}