tn.insat.controllers.InscriptionController.java Source code

Java tutorial

Introduction

Here is the source code for tn.insat.controllers.InscriptionController.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 tn.insat.controllers;

import tn.insat.controllers.utilities.Utilities;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Random;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import tn.insat.business.IClientService;
import tn.insat.model.Client;

@Controller
@ManagedBean
public class InscriptionController {

    @Autowired
    private Client client;
    private String relativeImagePath = null;
    private String realImagePath = null;
    private String passwordConf;
    @Autowired
    IClientService clientService;

    public InscriptionController() {
    }

    public void handleFileUpload(FileUploadEvent event) {
        InputStream inputStream = null;
        BigInteger random = new BigInteger(64, new Random());
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/accountPics/");
        System.out.println(path);
        try {
            inputStream = event.getFile().getInputstream();
            FileOutputStream outputStream = new FileOutputStream(
                    path + "\\" + random + event.getFile().getFileName());
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while (true) {
                bytesRead = inputStream.read(buffer);
                if (bytesRead > 0) {
                    outputStream.write(buffer, 0, bytesRead);
                } else {
                    break;
                }
            }
            outputStream.close();
            inputStream.close();
            realImagePath = path + "\\" + random + event.getFile().getFileName();
            relativeImagePath = random + event.getFile().getFileName();
            sendMessage(Utilities.getBundleMessage("phr", "success.download"), FacesMessage.SEVERITY_INFO);

        } catch (IOException ex) {
            sendMessage(Utilities.getBundleMessage("err", "error.download"), FacesMessage.SEVERITY_ERROR);
            System.out.println(ex.getMessage());
        }
    }

    public void signin(ActionEvent e) {
        if (relativeImagePath != null)
            client.setImage(relativeImagePath);
        else {
            if (client.getSexe().equals("M"))
                client.setImage("avatar-male.png");
            else if (client.getSexe().equals("F"))
                client.setImage("avatar-male.png");
            else
                client.setImage("avatar-unknown.png");
        }
        if (clientService.noConstraintBroken(client)) {
            client.setPassword(Utilities.MD5(client.getPassword()));
            clientService.addClient(client);
            try {
                redirect("index.xhtml");
            } catch (IOException ex) {
            }
            sendMessage(Utilities.getBundleMessage("phr", "success.signup"), FacesMessage.SEVERITY_INFO);
        } else {
            sendMessage(Utilities.getBundleMessage("err", "error.emailCinExist"), FacesMessage.SEVERITY_ERROR);
        }
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    private void sendMessage(String msg, FacesMessage.Severity severity) {
        FacesMessage m = new FacesMessage(severity, msg, null);
        FacesContext.getCurrentInstance().addMessage(null, m);
    }

    public String getPasswordConf() {
        return passwordConf;
    }

    public void setPasswordConf(String passwordConf) {
        this.passwordConf = passwordConf;
    }

    private void redirect(String url) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect(url);
    }

}