View Javadoc
1   package info.ejava.examples.jaxrs.todos.ejb;
2   
3   import java.util.List;
4   
5   import javax.ejb.Stateless;
6   import javax.ejb.TransactionAttribute;
7   import javax.ejb.TransactionAttributeType;
8   import javax.inject.Inject;
9   import javax.persistence.EntityManager;
10  import javax.persistence.TypedQuery;
11  
12  import info.ejava.examples.jaxrs.todos.bo.TodoItem;
13  import info.ejava.examples.jaxrs.todos.bo.TodoList;
14  import info.ejava.examples.jaxrs.todos.dto.TodoItemDTO;
15  import info.ejava.examples.jaxrs.todos.dto.TodoListDTO;
16  import info.ejava.examples.jaxrs.todos.dto.TodoListListDTO;
17  
18  @Stateless
19  public class TodosMgmtEJB implements TodosMgmtRemote {
20      @Inject
21      DtoMapper dtoMapper;
22      
23      @Inject
24      EntityManager em;
25  
26      @Override
27      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
28      public TodoListListDTO getTodoLists(int offset, int limit) {        
29          List<TodoList> result = getTodoListsLocal(offset, limit);
30          return dtoMapper.map(result);
31      }
32      
33      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
34      public List<TodoList> getTodoListsLocal(int offset, int limit) {
35          TypedQuery<TodoList> query=em.createNamedQuery("TodoList.getTodoLists", TodoList.class);
36          if (offset>0) {
37              query.setFirstResult(offset);
38          }
39          if (limit>0) {
40              query.setMaxResults(limit);
41          }
42          return query.getResultList();
43      }
44  
45  
46      @Override
47      @TransactionAttribute(TransactionAttributeType.REQUIRED)
48      public TodoListDTO createTodoList(TodoListDTO todoList) {
49          try {
50              TodoList result = createTodoList(dtoMapper.map(todoList));
51              return dtoMapper.map(result);
52          } catch (RuntimeException ex) {
53              throw new InternalErrorException("Error creating todoList: %s", ex.toString());            
54          }
55      }
56      
57      @TransactionAttribute(TransactionAttributeType.REQUIRED)
58      public TodoList createTodoList(TodoList todoList) {
59          em.persist(todoList);
60          return todoList;
61      }
62  
63      @Override
64      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
65      public TodoListDTO getTodoList(String listName) throws ResourceNotFoundException {
66          try {
67              TodoList result = getTodoListLocal(listName);
68              if (result==null) {
69                  throw new ResourceNotFoundException("listName[%s] not found", listName);
70              }
71              return dtoMapper.map(result);
72          } catch (RuntimeException ex) {            
73              throw new InternalErrorException("Error getting todoList: %s", ex.toString());
74          }
75      }
76      
77      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
78      public TodoList getTodoListLocal(String listName) {
79          List<TodoList> results = em.createNamedQuery("TodoList.getListByName", 
80                  TodoList.class)
81                   .setParameter("name", listName)
82                  .getResultList();        
83          return results.isEmpty() ? null : results.get(0);
84      }
85      
86      @Override
87      @TransactionAttribute(TransactionAttributeType.REQUIRED)
88      public TodoListDTO renameTodoList(String oldName, String newName) throws ResourceNotFoundException {
89          try {
90              TodoList todoList = getTodoListLocal(oldName);
91              if (todoList==null) {
92                  throw new ResourceNotFoundException("todoList[%s] not found", oldName);
93              }
94              todoList.setName(newName);
95              return dtoMapper.map(todoList);
96          } catch (RuntimeException ex) {
97              throw new InternalErrorException("Error renaming todoList: %s", ex.toString());            
98          }
99      }
100 
101     @Override
102     @TransactionAttribute(TransactionAttributeType.REQUIRED)
103     public void deleteTodoList(String listName) throws ResourceNotFoundException {
104         try {
105             if (deleteTodoListLocal(listName)==0) {
106                 throw new ResourceNotFoundException("todoList[%s] not found", listName);
107             }
108         } catch (RuntimeException ex) {
109             throw new InternalErrorException("Error deleting todoList: %s", ex.toString());            
110         }
111     }
112     
113     @TransactionAttribute(TransactionAttributeType.REQUIRED)
114     public int deleteTodoListLocal(String listName) {
115         TodoList todoList = getTodoListLocal(listName);
116         if (todoList==null) {
117             return 0;
118         } else {
119             em.remove(todoList); //use casecades
120             return 1;
121         }            
122     }
123 
124     @Override
125     @TransactionAttribute(TransactionAttributeType.REQUIRED)
126     public void addTodoListItem(String listName, TodoItemDTO item) 
127             throws ResourceNotFoundException, InvalidRequestException {
128         try {
129             TodoList todoList = getTodoListLocal(listName);
130             if (todoList==null) {
131                 throw new ResourceNotFoundException("todoList[%s] not found", listName);
132             }
133             TodoItem itemBO = dtoMapper.map(item);
134             if (itemBO==null) {
135                 throw new InvalidRequestException("required item not supplied");
136             }
137             
138             addTodoListItem(todoList, itemBO);
139         } catch (RuntimeException ex) {
140             throw new InternalErrorException("Error adding listItem to todoList[%s]: %s", listName, ex.toString());            
141         }
142     }
143     
144     @TransactionAttribute(TransactionAttributeType.REQUIRED)
145     public void addTodoListItem(TodoList todoList, TodoItem item) {
146         item.setTodoList(todoList);
147         todoList.getTodoItems().add(item);
148         em.persist(item);
149         em.flush();
150     }
151 
152     @TransactionAttribute(TransactionAttributeType.SUPPORTS)
153     public TodoItem getTodoListItem(String listName, String itemName) {
154         List<TodoItem> results = em.createNamedQuery("TodoItem.getTodoItem", TodoItem.class)
155                 .setParameter("listName", listName)
156                 .setParameter("itemName", itemName)
157                 .getResultList();
158         return results.isEmpty() ? null : results.get(0);
159     }
160     
161     @Override
162     @TransactionAttribute(TransactionAttributeType.REQUIRED)
163     public TodoItemDTO updateTodoListItem(String listName, String itemName, TodoItemDTO item) 
164             throws ResourceNotFoundException {
165         try {
166             TodoItem dbCopy = getTodoListItem(listName, itemName);
167             if (dbCopy==null) {
168                 throw new ResourceNotFoundException("todoList[%s], todoItem[%s] not found", listName, itemName);
169             }
170                 //assign the PK from the DB copy retrieved by distinct list+item name
171             TodoItem toUpdate = dtoMapper.map(item);
172             toUpdate.setId(dbCopy.getId());
173             
174             return dtoMapper.map(updateTodoListItem(toUpdate));
175         } catch (RuntimeException ex) {
176             throw new InternalErrorException("Error updating todoList: %s", ex.toString());            
177         }
178     }
179     
180     @TransactionAttribute(TransactionAttributeType.REQUIRED)
181     public TodoItem updateTodoListItem(TodoItem item) {
182         return em.merge(item);
183     }
184 
185 
186     @Override
187     @TransactionAttribute(TransactionAttributeType.REQUIRED)
188     public void deleteTodoListItem(String listName, String itemName) throws ResourceNotFoundException {
189         try {
190             TodoItem item = getTodoListItem(listName, itemName);
191             if (item==null) {
192                 throw new ResourceNotFoundException("todoList[%s], todoItem[%s] not found", listName, itemName);
193             }
194             deleteTodoListItem(item);
195         } catch (RuntimeException ex) {
196             throw new InternalErrorException("Error deleting todoList: %s", ex.toString());            
197         }
198     }
199     
200     @TransactionAttribute(TransactionAttributeType.REQUIRED)
201     public void deleteTodoListItem(TodoItem item) {
202         em.createNamedQuery("TodoItem.deleteTodoItem")
203             .setParameter("id", item.getId())
204             .executeUpdate();
205     }
206 
207     
208     @Override
209     public void deleteAll() {
210         for (String entityName : new String[] {"TodoItem","TodoList"}) {
211             em.createQuery(String.format("delete from %s o", entityName)).executeUpdate();
212         }
213     }
214 }