In the previous section we prepared the module for the back-end of the to-do list application. Now let's write the interface for our main service. Create an "org.epseelon.samples.todolist.business" package in src/main/java, and then create a TodoService interface inside, using the following code:
package org.epseelon.samples.todolist.business;
import java.util.List;
import org.epseelon.samples.todolist.domain.TodoItem;
public interface TodoService {
void remove(TodoItem todoItem) throws Exception;
TodoItem save(TodoItem todoItem) throws Exception;
TodoItem findById(TodoItem todoItem) throws Exception;
List<TodoItem> getList() throws Exception;
}
The TodoService interface above is a very classic example of a CRUD service. Notice that the TodoItem class is being used as both a parameter and a return type for some of these methods. Normally I would never do this in a project, because when you have a situation like this where several entities have links between them, you run the risk of encountering LazyInitializationException problems on the client side. This occurs when you attempt to access linked entities while outside of a Hibernate transaction. This is not considered a best practice and should be avoided. But for the purposes of this sample project, it will work for the short term.
In production projects I like to use the value-object pattern, where I define data transfer objects for each entity and only use these objects as return types and parameters. I'm skipping over that part for the sake of clarity in this sample project—since there is only one entity in our domain. This is something you can add in later, after completing the steps to build the initial to-do list application.
Now we're ready to implement this service in the same package:
package org.epseelon.samples.todolist.business;
import java.util.List;
import org.epseelon.samples.todolist.domain.TodoItem;
import org.epseelon.samples.todolist.domain.TodoItemRepository;
public class TodoServiceImpl implements TodoService {
private TodoItemRepository todoItemRepository;
public void setTodoItemRepository(TodoItemRepository todoItemRepository) {
this.todoItemRepository = todoItemRepository;
}
public TodoItem save(TodoItem item) throws Exception {
try {
this.todoItemRepository.save(item);
return item;
} catch (Exception e) {
throw new Exception("Could not save item because: " + e.getCause());
}
}
public void remove(TodoItem item) throws Exception {
try {
this.todoItemRepository.remove(item);
} catch (Exception e) {
throw new Exception("Could not delete item because " + e.getMessage());
}
}
public TodoItem findById(TodoItem item) throws Exception {
try {
return this.todoItemRepository.findById(item);
} catch (Exception e) {
throw new Exception("Could not find item because " + e.getMessage());
}
}
public List<TodoItem> getList() throws Exception {
try {
return this.todoItemRepository.getList();
} catch (Exception e) {
throw new Exception("Could not list items because " + e.getMessage());
}
}
}
As you can see, this implementation accesses a TodoItemRepository in package "org.epseelon.samples.todolist.domain". Here is the code used to create the interface:
package org.epseelon.samples.todolist.domain;
import java.util.List;
public interface TodoItemRepository {
void remove(TodoItem todoItem);
TodoItem save(TodoItem todoItem);
TodoItem findById(TodoItem todoItem) throws Exception;
List<TodoItem> getList();
}
This is its Hibernate-based implementation, in package "org.epseelon.samples.todolist.domain.hibernate":
package org.epseelon.samples.todolist.domain.hibernate;
import java.util.List;
import org.epseelon.samples.todolist.domain.TodoItem;
import org.epseelon.samples.todolist.domain.TodoItemRepository;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class TodoItemHibernateDao extends HibernateDaoSupport implements TodoItemRepository {
public TodoItem save(TodoItem todoItem) {
getHibernateTemplate().saveOrUpdate(todoItem);
return todoItem;
}
public void remove(TodoItem todoItem) {
getHibernateTemplate().delete(todoItem);
}
public TodoItem findById(TodoItem todoItem) throws Exception {
long id = todoItem.getId();
todoItem = (TodoItem) getHibernateTemplate().get(TodoItem.class, todoItem.getId());
if (todoItem == null)
throw new Exception("Could not find an item with id " + id);
return todoItem;
}
@SuppressWarnings("unchecked")
public List<TodoItem> getList() {
return (List<TodoItem>) getHibernateTemplate().loadAll(TodoItem.class);
}
}
And finally, the last piece of the Java puzzle is the code needed to create the org.epseelon.samples.todolist.domain.TodoItem class:
package org.epseelon.samples.todolist.domain;
public class TodoItem {
private long id;
private String title;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
The TodoItem class shown above is a standard implementation for returning the data from the repository.
Make sure to run 'mvn install' to ensure that the code compiles before moving on to the next section. In the next part, we'll configure the MySQL database and set up all of the pieces to work together.