Where should I locate the code for validating an employee ID (badge) that will be entered in multiple html forms through out my application?
Currently I have it in the STKUserForm.java which is used when people login (authenticateUser) or request their password (requestPassword). This works great so far. I have to send my many thanks to BalusC. A lot of what I have working so far is based on his DAO/Servlets blog. Thanks BalusC!!!!!!!!!!
But now, I am creating another use case besides logging in, where a supervisor assigns a task (CommitmentItemForm.java) to an employee by entering the employee's badge. I'd rather not duplicate my business logic (someday a valid badge may have 7 digits)there so I feel I need to move it out of STKUserForm.
STKUserForm.java - called from the login page (by a servlet) and both methods mentioned above call the processBadge which then calls the validateBadge method.
public final class STKUserForm extends Form {
public STKUser authenticateUser(STKUser LoginUser) {
<snip>
processBadge(LoginUser.getBadge());
<snip>
return authenticatedUser;
}
public void requestPassword(STKUser loginUser) {
<snip>
processBadge(LoginUser.getBadge());
<snip>
}
public void processBadge(String badge) throws DAOException {
try {
validateBadge(badge);
} catch (ValidatorException e) {
setError(FIELD_USERBADGE, e.getMessage());
}
}
public void validateBadge(String badge) throws ValidatorException, DAOException {
if (badge != null) {
if (!FormUtil.isBadge(badge)) {
throw new ValidatorException("Please enter valid badge (6 digits, numbers only, and no 'E').");
} else if (!STKUserDAO.isValidEmployee(badge)) {
throw new ValidatorException("This is not a valid badge of any EB Employee.");
}
}
}
}
So where should I move the validateBadge method?? STKUser bean?? FormUtil??? Some other utility class??? I'm unsure because it makes a call to STKUserDAO. I'll want to validate an employee badge for many use cases through out this and other applications.