/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.rubis.admin.controller.user;
import java.util.ArrayList;
import java.util.Collection;
import javax.faces.event.AbortProcessingException;
import javax.faces.model.SelectItem;
import org.xulfaces.rubis.Controller;
import org.xulfaces.rubis.admin.dao.RegionDAO;
import org.xulfaces.rubis.admin.dao.UserDAO;
import org.xulfaces.rubis.model.Region;
import org.xulfaces.rubis.model.User;
public class UserController extends Controller {
private static final String ERROR_MESG = "Can not save user : user first name and last name must not be empty !";
private User user = new User();
private UsersTreeController usersTreeCtrl;
private UserDAO userDAO;
private RegionDAO regionDAO;
private Collection<SelectItem> regions = new ArrayList<SelectItem>();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UsersTreeController getUsersTreeCtrl() {
return usersTreeCtrl;
}
public void setUsersTreeCtrl(UsersTreeController usersTreeCtrl) {
this.usersTreeCtrl = usersTreeCtrl;
}
protected boolean validate(){
boolean valid = true;
valid = !this.user.getFirstname().equals("");
valid = valid && (!this.user.getLastname().equals(""));
return valid;
}
public void save(){
if(!validate()){
throw new AbortProcessingException(ERROR_MESG);
}
this.userDAO.saveUser(user);
}
public void editUser(){
this.user = (User) this.usersTreeCtrl.getSelectedObject();
if(this.user != null){
populateRegionCombo();
}
}
protected void populateRegionCombo(){
this.regions.clear();
Collection<Region> collection = this.regionDAO.getRegions();
for (Region region : collection) {
SelectItem selectItem = new SelectItem();
selectItem.setValue(region);
selectItem.setLabel(region.getName());
this.regions.add(selectItem);
}
}
public void addUser(){
this.user = new User();
populateRegionCombo();
}
public void saveNewUser(){
if(!validate()){
throw new AbortProcessingException(ERROR_MESG);
}
else {
this.userDAO.saveNewUser(user);
}
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public RegionDAO getRegionDAO() {
return regionDAO;
}
public void setRegionDAO(RegionDAO regionDAO) {
this.regionDAO = regionDAO;
}
public Collection getRegions() {
return regions;
}
public void setRegions(Collection regions) {
this.regions = regions;
}
}