0

Example I have a JSF managed bean, and in I have method to create user:

 public String createNewAccount() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        try {
            String userRole = requestParameterMap.get("userRole").toString();
            String active = requestParameterMap.get("active").toString();
            Boolean act = Boolean.parseBoolean(active);
            user.setRole(userRole);
            user.setActive(act);
            if ((user.getEmail() != null) && (userDao.findEmailExist(user.getEmail()))) {
                sendErrorMessageToUser("A user with the given email address exists in the databasee");
                return null;
            } else {
                userDao.create(user);
            }
        } catch (EJBException e) {
            sendErrorMessageToUser("Register error");
            return null;
        }
        return "newAccountCreated";
    }

I read that JSF managedBean should be only used to navigation, and here my question is whether such doing validation in JSF beanie is correct? Should we create a service layer for example, in which the verification will be accomplished? For example, like this:

@Stateless
public class UserDao implements UserDaoLocal {

    @PersistenceContext
    private EntityManager em;
    private User user;

    @Override
    public void create(User user) {
        em.persist(user);
    }

@Stateless
public class UserDaoService implements UserDaoServiceLocal {

    @EJB
    private UserDaoLocal userDao
    private User user;


    @Override
    public String create(User user) {
        if(findEmailExist(user.email) {
            return "emailExist";
        } else {
             userDao.create(user);
              return "create";
       }
    }

    @Override
    public boolean findEmailExist(String email) {
        try {
            Query q = em.createNamedQuery("User.findByEmail");
            q.setParameter("email", email);
            user = (User) q.getSingleResult();
            return true;
        } catch (Exception e) {
            System.out.println("Błąd email: " + e.getMessage());
            return false;
        }
    }

and then in jsf bean only have this:

public String createNewAccount() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParameterMap = (Map) context.getExternalContext().getRequestParameterMap();
        try {
            String userRole = requestParameterMap.get("userRole").toString();
            String active = requestParameterMap.get("active").toString();
            Boolean act = Boolean.parseBoolean(active);
            user.setRole(userRole);
            user.setActive(act);
            String result userDaoService.create(user);
            if (result = "emailExist") {
                sendErrorMessageToUser("A user with the given email address exists in the database");
                return null;
            } else
                return result;
            }
        } catch (EJBException e) {
            sendErrorMessageToUser("Error registration");
            return null;
        }
    }

That is a good? What is the best way out to partition code in Java EE?

The Nightmare
  • 631
  • 3
  • 16
  • 34
  • That's basic MVC architecture. View layer (in your case JSF managed bean) has to do only the job of converting the form into manageable data. Processing such data and DAO access has nothing to do with that layer. – Xtreme Biker Aug 19 '13 at 07:45

1 Answers1

3

Validation normally belongs to the service layer, as there is no need to tie it to a specific view implementation. So in your case try to put it in the EJB.

  • If you have to implement another application which is to use another view implementation like Swing for example, it will benefit from the validation as well.

  • It's normally easier to write tests against the service layer; and this way you can test the validation without having to mock the view layer.

  • Using JSF, if you have two use cases using different managed beans, and both use cases create a user, chances are that you end up with writing the validation code twice.

Beryllium
  • 12,164
  • 9
  • 52
  • 81
  • Ok. Thanks for answer. But If my managedBean is ok? There is too high, the operation? – The Nightmare Aug 19 '13 at 10:16
  • 1
    You forgot to tell the most important thing OP needs to know in case of performing validation in JSF: it should be done in a validator, not in an action method. – BalusC Aug 19 '13 at 11:27
  • I have validator. Validator check the correct entered e-mail. But in service layer I check if one does not already have such email. – The Nightmare Aug 19 '13 at 14:32
  • @Orange91 [How to use `@EJB`, `@Inject` and/or `@Autowired` in `@FacesValidator`](http://stackoverflow.com/q/7572335/1065197). Even if you *hate it*, you must do the things in the right way. – Luiggi Mendoza Aug 19 '13 at 15:47
  • I have validator in the page. Validator check pattern email. In service layer i check if someone has does not have such an email. This is the wrong? So what should be placed in the service layer? – The Nightmare Aug 20 '13 at 09:58