Hello everyone,
I'm working on a simple application in which any user can be in charge of other Users. Thus, I created a field in my Model containing the email of those people, which is automatically updated when a new user is created. But this List is always null, even if I try to fill it before creating the User.
Here is the relevant code (my User class) :
-----------------------------------------------------------------------
package models;
//Import declarations here
@Entity
public class User extends Model {
@Id
@Required
public String email;
@Required
public String lastname;
public String firstname;
@Required
public String passphrase;
@Column(nullable=true)
public String superieur;
@Column(nullable=false)
public List<String> subordonnes;
public boolean admin;
public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);
private final static Logger LOGGER = Logger.getLogger(User.class.getName());
public static final int EMAIL_SEARCH = 1;
public static final int NAME_SEARCH = 2;
public User(String lastname, String firstname, String passphrase, String email, String superieur, List<String> subordonnes) {
super();
this.lastname = lastname;
this.firstname = firstname;
this.passphrase = passphrase;
this.email = email;
this.superieur = superieur;
this.subordonnes = subordonnes;
}
public static List<User> all(){
return find.all();
}
public static String add(User u){
if(validateUser(u)){
u.subordonnes=new ArrayList<String>();
u.subordonnes.add("no...@none.com");
u.save();
if(u.superieur==null){
return "User added";
}else{
if(!(u.superieur.equals("no...@none.com"))){
User x = User.find.byId(u.superieur);
if(x.subordonnes==null){
LOGGER.log(Log.VERBOSE, "List created (again ?)");
x.subordonnes = new ArrayList<String>();
}
x.subordonnes.add(u.email);
x.update((Object)x.email);
LOGGER.log(Log.VERBOSE, "User "+u.email+" added to "+x.email+" list");
}
return "User added";
}
}else{
return "Invalid data for user";
}
}
public static void remove(String e){
find.ref(e).delete();
}
public static String update(String id, User u){
u.update((Object)id);
return "Update successful";
}
public static boolean validateUser(User u){
Pattern pattern = Pattern.compile("[A-Z][a-z]+(-[A-Z][a-z]+)?");
Matcher matcher;
EmailValidator valid = EmailValidator.getInstance();
if(valid.isValid(u.email))
{
if(User.find.byId(u.email)==null){
matcher = pattern.matcher((CharSequence)u.lastname);
if(matcher.find())
{
matcher = pattern.matcher((CharSequence)u.firstname);
if(matcher.find()){
pattern=Pattern.compile(".{8,}");
matcher = pattern.matcher((CharSequence)u.passphrase);
if(matcher.find())
{
return true;
}
}
}
}
}
return false;
}
public static User authenticate(String email, String passphrase) {
return find.where().eq("email", email)
.eq("passphrase", passphrase).findUnique();
}
public static List<User> search(String s, int i){
Pattern p = Pattern.compile(".*"+s+".*");
Matcher m;
List<User> elu = all();
List<User> ok = new ArrayList<User>();
for(User u : elu){
if(i==NAME_SEARCH){
m = p.matcher((CharSequence)u.lastname);
}else{
m = p.matcher((CharSequence)u.email);
}
if(m.find()){
ok.add(u);
}
}
return ok;
}
}
I tried to use the method saveManyToManyAssociations("subordonnes"), but it resulted in a NullPointerException while testing.
What am I doing wrong ? (Amongst other things, of course...)
Thanks for your help.