/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package action;
import bo.Geolog;
import dao.GeologDao;
import daoI.IGeologDao;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class InregistrareActionBean extends BaseActionBean {
private String username;
private String parola;
private String nume;
private String prenume;
private String adresa;
private String email;
private String telefon;
private String vizibilitate;
private Boolean emailInvalid = false;
private Boolean telefonInvalid = false;
private Boolean geologAdaugat= false;
public Resolution inregistrare() {
return new ForwardResolution("/WEB-INF/jsp/inregistrare.jsp");
}
public Resolution submit() {
IGeologDao geoDao = new GeologDao();
Geolog geolog = geoDao.geologExistent(username, parola);
if (geolog != null) {
getContext().getRequest().getSession(true).setAttribute("user", geolog.getIdGeolog());
return new ForwardResolution("/WEB-INF/jsp/geologLogat.jsp");
} else {
String confirmareVizibilitate;
String acceptare = "in asteptare";
if (getVizibilitate() == null) {
confirmareVizibilitate = "nu";
} else {
confirmareVizibilitate = "da";
}
byte[] parolaB = parola.getBytes();
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(parolaB);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
parola = hexString + "";
} catch (Exception e) {
}
if (!isEmailValid(email)) {
setEmailInvalid(true);
return new ForwardResolution("/WEB-INF/jsp/inregistrare.jsp");
}
if (!isNumeric(telefon) || telefon.length() != 10) {
setTelefonInvalid(true);
return new ForwardResolution("/WEB-INF/jsp/inregistrare.jsp");
}
setGeologAdaugat(true);
geoDao.saveOrUpdate(new Geolog(nume, prenume, telefon, email, adresa, username, parola, confirmareVizibilitate, acceptare));
return new ForwardResolution("/WEB-INF/jsp/inregistrare.jsp");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getParola() {
return parola;
}
public void setParola(String parola) {
this.parola = parola;
}
public String getAdresa() {
return adresa;
}
public void setAdresa(String adresa) {
this.adresa = adresa;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume = nume;
}
public String getPrenume() {
return prenume;
}
public void setPrenume(String prenume) {
this.prenume = prenume;
}
public String getTelefon() {
return telefon;
}
public void setTelefon(String telefon) {
this.telefon = telefon;
}
public String getVizibilitate() {
return vizibilitate;
}
public void setVizibilitate(String vizibilitate) {
this.vizibilitate = vizibilitate;
}
public Boolean getEmailInvalid() {
return emailInvalid;
}
public void setEmailInvalid(Boolean emailInvalid) {
this.emailInvalid = emailInvalid;
}
public Boolean getTelefonInvalid() {
return telefonInvalid;
}
public void setTelefonInvalid(Boolean telefonInvalid) {
this.telefonInvalid = telefonInvalid;
}
public Boolean getGeologAdaugat() {
return geologAdaugat;
}
public void setGeologAdaugat(Boolean geologAdaugat) {
this.geologAdaugat = geologAdaugat;
}
private boolean isEmailValid(String email) {
boolean isValid = false;
// [\\w\\.-]+ - include caractere ,punct, cratima
// ([\\w\\-]+\\.)+ - include caractere, cratima urmat de punct pt a separa domeniul de subdomeniu
// [A-Z]{2,4}$ - se termina in 2, 3, sau 4 litere (com, yahoo)
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
}