[jboss-user] [JBoss Seam] - Re: binding attribute doesnt work with Seam?

mnrz do-not-reply at jboss.com
Tue Oct 9 03:20:05 EDT 2007


the xhtml is already posted at first thread above and here is the backing bean:


  | @Stateful
  | @Name("userRegister")
  | @Scope(ScopeType.SESSION)
  | @Restrict("#{s:hasRole('Admin')}")
  | public class UserRegisterAction implements UserRegister   {
  | 
  | 	private Log logger = LogFactory.getLog(UserRegisterAction.class);
  | 
  | 	private User tempUser;
  | 	
  | 	private HtmlSelectOneMenu selectOneUser;
  | 	
  | 	private SelectItem[] allUsers;
  | 	private ArrayList<SelectItem> allRoles;
  | 	private SelectItem[] allGroups;
  | 	private boolean newUser;
  | 	private Long selectedGroup = 0L; 
  | 	private String selectedUsername = "0";
  | 	private boolean reload = false;
  | 	private UIInput username;
  | 	private UIInput address;
  | 	private UIInput contactNo;
  | 	private UIInput mobilePhone;
  | 	private String verifyPassword; 
  | 	private String password;
  | 	
  | 	@EJB
  | 	private UserDao userDao;
  | 	@EJB
  | 	private GroupDao groupDao ;
  | 	
  | 
  | 	private List<String> selectedRoles;
  | 	
  | 
  | 	//constructors....
  | 	public UserRegisterAction() {
  | 		logger.debug("constructing UserRegisterAction...");
  | 
  | 	}
  | 	
  | 	@Factory(value = "tempUser", scope = ScopeType.STATELESS)
  | 	public User loadUser() {
  | 		if(reload || tempUser == null) {
  | 		if(selectedUsername.equals("0")) {
  | 			logger.debug("instantiating a new User");
  | 			tempUser = new User();
  | 			verifyPassword = password = "";
  | 			selectedGroup = 0L;
  | 		}else{
  | 				logger.debug("loading User with username '"+selectedUsername+"'");
  | 				tempUser = userDao.load(selectedUsername);
  | 				if(tempUser.getGroup() != null)
  | 					selectedGroup = tempUser.getGroup().getId();
  | 				verifyPassword = password = tempUser.getPassword();
  | 				selectedRoles = new ArrayList<String>();				
  | 				for(UserRole r : tempUser.getRoles()) {
  | 					selectedRoles.add(r.getRoleName());
  | 				}
  | 				//setInputValuesWithUser();
  | 		}
  | 		}
  | 		return tempUser;
  | 	}
  | 	
  | 	//private methods...
  | 	private void loadAllRoles() {
  | 		List<ObjectMap> list = null;
  | 		try {
  | 			list = ActionUtil.getClassResourceBundle().getList(AccessRole.class, new java.util.Locale("en"));
  | 		} catch (Exception e) {
  | 			e.printStackTrace();
  | 		}
  | 		
  | 		if(list != null) {
  | 			allRoles = new ArrayList<SelectItem>();
  | 			for(int i = 0;i< list.size();i++) {
  | 				if(list.get(i).getValue() == null || list.get(i).getValue().equals(AccessRole.Unknown.name())) {
  | 					continue;
  | 				}
  | 				SelectItem item = new SelectItem();
  | 				item.setLabel(list.get(i).getMeaning());
  | 				item.setValue(list.get(i).getValue());
  | 				allRoles.add(item);
  | 			}
  | 		}
  | 	}
  | 	
  | 	@SuppressWarnings("unchecked")
  | 	private void loadAllGroups() {
  | 		List<Group> groups = groupDao.loadAll();
  | 		if(groups != null) {
  | 		allGroups = new SelectItem[groups.size()+1];
  | 		allGroups[0] = new SelectItem(new Long(0),"--not-specified--");
  | 			for(int i = 0;i< groups.size();++i) {
  | 				SelectItem item = new SelectItem();
  | 				item.setLabel(groups.get(i).getName());
  | 				item.setValue(groups.get(i).getId());
  | 				allGroups[i+1] = item;
  | 			}
  | 		}
  | 	}
  | 	
  | 	private void loadAllUsers() {
  | 		logger.debug("loading... " + userDao);
  | 		List users = userDao.load();
  | 		if(users != null) {
  | 			allUsers = new SelectItem[users.size()];
  | 			for(int i = 0; i < users.size();++i) {
  | 				SelectItem item = new SelectItem();
  | 				User u = (User) users.get(i);
  | 				item.setLabel(u.getUsername());
  | 				item.setValue(u.getUsername());
  | 				allUsers = item;
  | 			}
  | 		}
  | 	}
  | 	
  | 	private void saveNewUser() {
  | 		logger.debug("inserting....");
  | 		User existing =userDao.load(tempUser.getUsername());
  | 		if (existing == null) {
  | 			tempUser.setPassword(password);
  | 			tempUser.setGroup(groupDao.load(selectedGroup));
  | 			userDao.save(tempUser);
  | 			loadAllUsers();
  | 			FacesMessages.instance().add(FacesMessage.SEVERITY_INFO,"tempUser '"+tempUser.getUsername()+"' has been created successfuly");
  | 			tempUser = new User();
  | 			setNewUser(true);
  | 			selectedUsername = "0";
  | 			selectedRoles.clear();
  | 
  | 		} else {
  | 			FacesMessages.instance().add(FacesMessage.SEVERITY_ERROR,"User '#{userRegister.user.username}' already exists. please choose another username");
  | 			return;
  | 		}
  | 	}
  | 	
  | 	private void updateUser() {
  | 		logger.debug("merging....");
  | 		try {
  | 			tempUser.setPassword(password);
  | 			tempUser.setGroup(groupDao.load(selectedGroup));
  | 			tempUser.getRoles().clear();
  | 			for(String role: selectedRoles) {
  | 				tempUser.addRole(role);
  | 			}
  | 			userDao.merge(tempUser);
  | 			FacesMessages.instance().add(FacesMessage.SEVERITY_INFO,"'"+tempUser.getUsername()+"' has been updated successfuly");
  | 		} catch (Exception e) {
  | 			e.printStackTrace();
  | 		}
  | 	}
  | 	
  | 	private void setUserWithSubmittedValues() {
  | 		logger.debug(tempUser.getUsername()+" - "+tempUser.getAddress()+" - "+tempUser.getPhoneNo1());
  | 		logger.debug(username.getLocalValue()+" - "+username.getSubmittedValue()+" - "+username.getValue());
  | 		logger.debug(contactNo.getLocalValue()+" - "+contactNo.getSubmittedValue()+" - "+contactNo.getValue());
  | 		tempUser.setUsername((String)username.getSubmittedValue());
  | 		tempUser.setAddress((String)address.getSubmittedValue());
  | 		tempUser.setPhoneNo1((String)contactNo.getSubmittedValue());
  | 		tempUser.setMobileContact((String)mobilePhone.getSubmittedValue());
  | 	}
  | 	
  | 	private void setInputValuesWithUser() {
  | 		username.setValue((String)tempUser.getUsername());
  | 		address.setValue((String)tempUser.getAddress());
  | 		contactNo.setValue((String)tempUser.getPhoneNo1());
  | 		mobilePhone.setValue((String)tempUser.getMobileContact());
  | 	}
  | 	
  | 	//action methods...
  | 	public String save() {
  | 		//FacesContext f;f.getContext().getCurrentInstance().get
  | 		//setUserWithSubmittedValues();
  | 		logger.debug("saving.... '"+tempUser.getUsername()+"' with address: "+tempUser.getAddress());
  | 		logger.debug("Phone: "+tempUser.getPhoneNo1() + " Mobile: "+tempUser.getMobileContact());
  | 		if(tempUser.getUsername().equals("")) {
  | 			FacesMessages.instance().add(FacesMessage.SEVERITY_ERROR,"username can not be empty");
  | 			return null;
  | 		}
  | 		logger.debug("pass:"+password + " verify:"+verifyPassword);
  | 		if(password.equals("") || verifyPassword.equals("") || !password.equals(verifyPassword)) {
  | 			FacesMessages.instance().add(FacesMessage.SEVERITY_ERROR,"password is empty or password and verification don't match");
  | 			return null;
  | 		}
  | 		if(isNewUser()) {
  | 			saveNewUser();
  | 		}else {
  | 			updateUser();
  | 		}
  | 		
  | 		return null;
  | 	}
  | 	
  | 	public void userListValueChanged(ValueChangeEvent event) {
  | 
  | 		try {
  | 			selectedUsername = (String) event.getNewValue();
  | 			reload = true;
  | 		} catch (Exception e) {
  | 			e.printStackTrace();
  | 		}
  | 	}
  | 
  | 
  | 	
  | 	public String back(){
  | 		return "back";
  | 	}
  | 
  | 	@Remove @Destroy
  | 	public void destroy() {
  | 		
  | 	}
  | 
  | 	// getters and setters....
  | 	public List<String> getSelectedRoles() {
  | 		return selectedRoles;
  | 	}
  | 
  | 	public void setSelectedRoles(List<String> selectedRoles) {
  | 		this.selectedRoles = selectedRoles;
  | 	}
  | 
  | 	public User getTempUser() {
  | 		if(tempUser == null) {
  | 			tempUser = new User();
  | 		}
  | 		return tempUser;
  | 	}
  | 
  | 	public void setTempUser(User user) {
  | 		this.tempUser = user;
  | 	}
  | 
  | 	public String getVerifyPassword() {
  | 		return verifyPassword;
  | 	}
  | 
  | 	public void setVerifyPassword(String verify) {
  | 		this.verifyPassword = verify;
  | 	}
  | 
  | 
  | 	public HtmlSelectOneMenu getSelectOneUser() {
  | 		return selectOneUser;
  | 	}
  | 
  | 	public void setSelectOneUser(HtmlSelectOneMenu selectOneUser) {
  | 		this.selectOneUser = selectOneUser;
  | 	}
  | 
  | 	public SelectItem[] getAllUsers() {
  | 		if(allUsers == null) {
  | 			loadAllUsers();
  | 		}
  | 		return allUsers;
  | 	}
  | 
  | 	public void setAllUsers(SelectItem[] allUsers) {
  | 		this.allUsers = allUsers;
  | 	}
  | 
  | 	public String getSelectedUsername() {
  | 		return selectedUsername;
  | 	}
  | 
  | 	public void setSelectedUsername(String selectedUsername) {
  | 		this.selectedUsername = selectedUsername;
  | 	}
  | 	
  | 	public SelectItem[] getAllGroups() {
  | 		if(allGroups == null) {
  | 			loadAllGroups();
  | 		}
  | 		return allGroups;
  | 	}
  | 
  | 	public void setAllGroups(SelectItem[] allGroups) {
  | 		this.allGroups = allGroups;
  | 	}
  | 
  | 	public ArrayList<SelectItem> getAllRoles() {
  | 		if(allRoles == null) {
  | 			loadAllRoles();
  | 		}
  | 		return allRoles;
  | 	}
  | 
  | 	public void setAllRoles(ArrayList<SelectItem> allRoles) {
  | 		this.allRoles = allRoles;
  | 	}
  | 
  | 	public Long getSelectedGroup() {
  | 		return selectedGroup;
  | 	}
  | 
  | 	public void setSelectedGroup(Long selectedGroup) {
  | 		this.selectedGroup = selectedGroup;
  | 	}
  | 
  | 	public boolean isNewUser() {
  | 		if(selectedUsername.equals("0")) {
  | 			newUser = true;
  | 		}else {
  | 			newUser = false;
  | 		}
  | 		return newUser;
  | 	}
  | 
  | 	public void setNewUser(boolean newUser) {
  | 		this.newUser = newUser;
  | 	}
  | 
  | 	public String getPassword() {
  | 		return password;
  | 	}
  | 
  | 	public void setPassword(String password) {
  | 		this.password = password;
  | 	}
  | 
  | 	public UIInput getAddress() {
  | 		return address;
  | 	}
  | 
  | 	public void setAddress(UIInput address) {
  | 		this.address = address;
  | 	}
  | 
  | 	public UIInput getContactNo() {
  | 		return contactNo;
  | 	}
  | 
  | 	public void setContactNo(UIInput contactNo) {
  | 		this.contactNo = contactNo;
  | 	}
  | 
  | 	public UIInput getMobilePhone() {
  | 		return mobilePhone;
  | 	}
  | 
  | 	public void setMobilePhone(UIInput mobilePhone) {
  | 		this.mobilePhone = mobilePhone;
  | 	}
  | 
  | 	public UIInput getUsername() {
  | 		return username;
  | 	}
  | 
  | 	public void setUsername(UIInput username) {
  | 		this.username = username;
  | 	}
  | 
  | 
  | }
  | 
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4092864#4092864

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4092864



More information about the jboss-user mailing list