/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beanControl;
import EntityBean.Users;
import helper.MD5;
import SessionBean.UsersSSBLocal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import helper.messages;
import java.util.Date;
import java.util.Properties;
import javax.faces.application.FacesMessage;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.primefaces.component.growl.GrowlRenderer;
//import helper.operationSession;
/**
*
*/
@ManagedBean(name = "insertLogin")
@RequestScoped
public class createAccount {
@EJB
private UsersSSBLocal loginFacade;
private Users login;
/** Creates a new instance of createAccount */
public createAccount() {
// userName = (String) operationSession.layGTSession("user");
}
private String user;
private String pass;
private String fullname;
private String gender;
private String city;
private String address;
private int cardnumber;
private int telephone;
private String email;
private String variableEmail;
private Date birth;
public void createUser(String role) {
try{
if(user.length() == 0)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "User Not null", "Error User");
}else if(pass.length() ==0)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "Password not null", "Error User");
}else
{
if(user.length() <6)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "UserName have more than 5 characters", "Error User");
} else if (pass.length() < 6)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "Password have more than 5 characters", "Error pass");
}else
{
this.login = this.loginFacade.getUserID(user);
if(login != null)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "User already exist", "Error User");
}else
{
String mail= email+variableEmail;
String password = pass;
Users login1 = new Users();
login1.setUsername(user);
login1.setPassword(password);
login1.setRoles(role);
login1.setAddress(address);
login1.setCardnumber(cardnumber);
login1.setCity(city);
login1.setDayofbirth(birth);
login1.setEmail(mail);
login1.setFullname(fullname);
login1.setGender(gender);
login1.setTelephone(telephone);
login1.setStatus("on");
this.loginFacade.create(login1);
sendAuto(mail);
messages.taoTB(FacesMessage.SEVERITY_INFO, "Register Successful", "Successful");
}
}
}
}catch(Exception ex)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "Error", "Error User");
}
}
public void sendAuto(String to) throws Exception {
// java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
try{
if(to.length() == 0 )
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "Email To not null .", "Error");
}else{
Properties props = System.getProperties();
// –
//Thiết lập smtp
props.put("mail.smtp.host", "smtp.gmail.com");
//Port 587 của gmail, set port khác nếu không dùng gmail
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
//Thiết lập account đăng nhập smtp
final String login = "nguyenduong111090@gmail.com";//usermail
final String pwd = "duongbum";//”password của bạn ở đây”;
Authenticator pa = null; //default: no authentication
if (login != null && pwd != null) { //authentication required?
props.put("mail.smtp.auth", "true");
pa = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, pwd);
}
};
}//else: no authentication
Session session = Session.getInstance(props, pa);
// — Tạo đối tượng message
Message msg = new MimeMessage(session);
// — Set các field FROM và TO
msg.setFrom(new InternetAddress("nguyenduong111090@gmail.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
// — Set tiêu đề và body (nội dung thư) –
msg.setSubject("Welcome to KSC Website !");
msg.setText("Congratulation ! You have register success ");
// — Set header information
msg.setHeader("X-Mailer", "KSC-Email");
msg.setSentDate(new Date());
msg.saveChanges();
// — Gởi tin nhắn
Transport.send(msg);
// Thông báo khi gởi thư thành công
messages.taoTB(FacesMessage.SEVERITY_INFO, "Send mail Successful .", "Successful");
}
}catch(Exception ex)
{
messages.taoTB(FacesMessage.SEVERITY_ERROR, "Send mail Error .", "Error");
}
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Users getLogin() {
return login;
}
public void setLogin(Users login) {
this.login = login;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCity() {
return city;
}
public String getVariableEmail() {
return variableEmail;
}
public void setVariableEmail(String variableEmail) {
this.variableEmail = variableEmail;
}
public void setCity(String city) {
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getCardnumber() {
return cardnumber;
}
public void setCardnumber(int cardnumber) {
this.cardnumber = cardnumber;
}
public int getTelephone() {
return telephone;
}
public void setTelephone(int telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}