I have a project to write with JavaEE, but I am new with the technology.
I have EJBs which rely on parameters received from a client front-end.
At the moment, I'm validating parameters within the EJBs, e.g., I have methods in my EJBs to validate the parameters, but I assume this is bad practice because it leads to code duplication, and adds more responsibilities to my EJBs.
I'd like to know what are the best practices to perform parameters validations.
EDIT: My JavaEE back-end is not directly reachable by the client. Instead, we have an interoperability layer written in Java, and following the SOAP architecture, that lies between the client front-end, and the J2E back-end.
An example of my code can be found below :
// Method inherited from the EJB interface
// The eventOrganizer variable is another EJB that is injected into this very EJB
@Override
public boolean registerEvent(String name, int participantNumber, Calendar date, Coordinator coordinator) {
l.log(Level.INFO, "Received request for event creation");
if (!areParametersValid(name, participantNumber, date, coordinator)) {
return false;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date.getTime());
cal.add(Calendar.HOUR_OF_DAY, 12);
Event event = new Event(coordinator, date.getTime(), cal.getTime(), participantNumber, name);
return eventOrganizer.bookRoom(event);
}
// Parameters validation methods
/**
* Returns true if the given name is non null and
* is not empty
* @param name the name of the {@link Event}
* @return true if the name semantically of the {@link Event} is valid
*/
private boolean nameIsGood(String name) {
return name != null && !name.trim().equals("");
}
/**
* Returns true if the number of participant is strictly positive
* @param participantNumber the number of people in the {@link Event}
* @return true if the number of participant is valid
*/
private boolean participantNumberIsGood(int participantNumber) {
return participantNumber > 0;
}
/**
* Checks if the given date is a date in the future,
* and returns true if it is
* @param date the date to check
* @return true if the provided start date for the {@link Event} is valid
*/
private boolean dateIsGood(Calendar date) {
return date.after(Calendar.getInstance());
}
/**
* Checks if the given {@link Coordinator} is a valid coordinator,
* i.e., if he's not null, and returns true if he is not
* @param coordinator the {@link Coordinator} to check
* @return true if the {@link Coordinator} is valid
*/
private boolean coordinatorIsGood(Coordinator coordinator) {
return coordinator != null;
}
/**
* Checks that all the parameters received for an {@link Event} creation are valid
* and returns true if they are, or false if they're not
* @param name the name of the {@link Event}, as a {@link String}
* @param participantNumber the estimated number of people for the
* {@link Event} as a {@link Integer}
* @param date the date at which the {@link Event} is scheduled,
* as a {@link Calendar}
* @param coordinator the {@link Coordinator} that created the {@link Event}
* @return true if all the given parameters are valid, and the event creation shall be processed
*/
private boolean areParametersValid(String name, int participantNumber, Calendar date, Coordinator coordinator) {
return nameIsGood(name) && participantNumberIsGood(participantNumber) && dateIsGood(date) && coordinatorIsGood(coordinator);
}
// Event object
public class Event {
private Coordinator coordinator;
private Date startDate;
private Date endDate;
private int nbPeople;
private String name;
private List<Room> rooms;
private List<RoomType> desiredRoomTypes;
public Event(int nbPeople, String name, List<RoomType> roomTypes) {
this.nbPeople = nbPeople;
this.name = name;
this.desiredRoomTypes = roomTypes;
this.rooms = new ArrayList<>();
}
public Event(Coordinator coordinator, Date startDate, Date endDate, int nbPeople, String name) {
this.coordinator = coordinator;
this.startDate = startDate;
this.endDate = endDate;
this.nbPeople = nbPeople;
this.name = name;
this.rooms = new ArrayList<>();
}
public List<Room> getRooms() {
return rooms;
}
public void addRooms(List<Room> rooms) {
this.rooms.addAll(rooms);
}
public void addRoom(Room room) {
this.rooms.add(room);
}
public Coordinator getCoordinator() {
return coordinator;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public int getNbPeople() {
return nbPeople;
}
public String getName() {
return name;
}
}
// Coordinator object
public class Coordinator {
private String firstName;
private String lastName;
private String email;
private List<Event> eventsCreated;
public Coordinator(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public List<Event> getEventsCreated() {
return eventsCreated;
}
public void setEventsCreated(List<Event> eventsCreated) {
this.eventsCreated = eventsCreated;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
}