/Banque/Banque-ejb/src/java/org/banque/managers/ClientManager.java

https://github.com/bjurkovski/car-j2ee · Java · 304 lines · 258 code · 33 blank · 13 comment · 37 complexity · b034169075f41eaddeb585df7b00c42d MD5 · raw file

  1. package org.banque.managers;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import javax.ejb.Stateless;
  11. import javax.persistence.EntityManager;
  12. import javax.persistence.PersistenceContext;
  13. import javax.persistence.Query;
  14. import org.banque.dtos.ClientDTO;
  15. import org.banque.entities.Client;
  16. import org.banque.exceptions.BanqueException;
  17. import org.banque.managers.interfaces.IClientManagerLocal;
  18. /**
  19. *
  20. * @author wasser
  21. */
  22. @Stateless
  23. public class ClientManager extends PersonManager implements IClientManagerLocal {
  24. @PersistenceContext(unitName = "BanquePU")
  25. protected EntityManager em;
  26. @Override
  27. public ClientDTO createClient(ClientDTO client) throws BanqueException {
  28. return createClient(client.getName(), client.getLastName(), client.getPassword(), client.getGender(), client.getDateOfBirth(), client.getAddress(), client.getEmail(), client.isAdmin());
  29. }
  30. @Override
  31. public ClientDTO createClient(String name, String lastName, String password, ClientDTO.Gender gender, Date dateOfBirth, String address, String email) throws BanqueException {
  32. return createClient(name, lastName, password, gender, dateOfBirth, address, email, false);
  33. }
  34. @Override
  35. public ClientDTO createClient(String name, String lastName, String password, ClientDTO.Gender gender, Date dateOfBirth, String address, String email, boolean admin) throws BanqueException {
  36. Client clientDB = new Client(name, lastName, PersonManager.hashPassword(password), PersonManager.getGenderEntity(gender), dateOfBirth, address, email, admin);
  37. validateClient(clientDB);
  38. try {
  39. em.persist(clientDB);
  40. return createClientDTO(clientDB);
  41. } catch (Exception e) {
  42. System.out.println("Original Error Message: " + e.getMessage());
  43. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  44. }
  45. }
  46. @Override
  47. public void deleteClient(Long id) throws BanqueException {
  48. Client client = findClientDB(id);
  49. try {
  50. em.remove(em.merge(client));
  51. } catch (Exception e) {
  52. System.out.println("Original Error Message: " + e.getMessage());
  53. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  54. }
  55. }
  56. @Override
  57. public ClientDTO updateClient(ClientDTO client) throws BanqueException {
  58. Client clientDB = findClientDB(client.getId());
  59. try {
  60. clientDB.setAddress(client.getAddress());
  61. clientDB.setDateOfBirth(client.getDateOfBirth());
  62. clientDB.setGender(PersonManager.getGenderEntity(client.getGender()));
  63. clientDB.setLastName(client.getLastName());
  64. clientDB.setName(client.getName());
  65. clientDB.setPassword(client.getPassword());
  66. clientDB.setEmail(client.getEmail());
  67. clientDB.setAdmin(client.isAdmin());
  68. return createClientDTO(em.merge(clientDB));
  69. } catch (Exception e) {
  70. System.out.println("Original Error Message: " + e.getMessage());
  71. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  72. }
  73. }
  74. @Override
  75. public ClientDTO findClient(Long id) throws BanqueException {
  76. return createClientDTO(findClientDB(id));
  77. }
  78. protected Client findClientDB(Long id) throws BanqueException {
  79. if (id == null) {
  80. throw new BanqueException(BanqueException.ErrorType.CLIENT_NOT_FOUND);
  81. }
  82. Client c = em.find(Client.class, id);
  83. if (c == null) {
  84. throw new BanqueException(BanqueException.ErrorType.CLIENT_NOT_FOUND);
  85. }
  86. return c;
  87. }
  88. @Override
  89. public List<ClientDTO> findAllClients() throws BanqueException {
  90. LinkedList<ClientDTO> _return = new LinkedList<ClientDTO>();
  91. try {
  92. List<Client> results = em.createNamedQuery(Client.FIND_ALL).getResultList();
  93. for (Client c : results) {
  94. _return.add(createClientDTO(c));
  95. }
  96. return _return;
  97. } catch (Exception e) {
  98. System.out.println("Original Error Message: " + e.getMessage());
  99. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  100. }
  101. }
  102. @Override
  103. public List<ClientDTO> findClientsByCriteria(String searchString, int criteria) throws BanqueException {
  104. switch(criteria) {
  105. case ID:
  106. List<ClientDTO> _return = new LinkedList<ClientDTO>();
  107. _return.add(findClient(Long.decode(searchString)));
  108. return _return;
  109. case PRENOM:
  110. return findClientsByName(searchString);
  111. case NOM:
  112. return findClientsByLastName(searchString);
  113. case EMAIL:
  114. return findClientsByEmail(searchString);
  115. }
  116. throw new BanqueException(BanqueException.ErrorType.INVALID_SEARCH_CRITERIA);
  117. }
  118. @Override
  119. public List<ClientDTO> findClients(String searchString) throws BanqueException {
  120. List<ClientDTO> _return = new LinkedList<ClientDTO>();
  121. try {
  122. Query query = em.createNamedQuery(Client.FIND_PARTIALLY).setParameter("partial", "%" + searchString + "%");
  123. List<Client> results = query.getResultList();
  124. for (Client c : results) {
  125. _return.add(createClientDTO(c));
  126. }
  127. return _return;
  128. } catch (Exception e) {
  129. System.out.println("Original Error Message: " + e.getMessage());
  130. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  131. }
  132. }
  133. @Override
  134. public List<ClientDTO> findClientsByName(String searchString) throws BanqueException {
  135. List<ClientDTO> _return = new LinkedList<ClientDTO>();
  136. try {
  137. Query query = em.createNamedQuery(Client.FIND_BY_NAME).setParameter("name", "%" + searchString + "%");
  138. List<Client> results = query.getResultList();
  139. for (Client c : results) {
  140. _return.add(createClientDTO(c));
  141. }
  142. return _return;
  143. } catch (Exception e) {
  144. System.out.println("Original Error Message: " + e.getMessage());
  145. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  146. }
  147. }
  148. @Override
  149. public List<ClientDTO> findClientsByLastName(String searchString) throws BanqueException {
  150. List<ClientDTO> _return = new LinkedList<ClientDTO>();
  151. try {
  152. Query query = em.createNamedQuery(Client.FIND_BY_LAST_NAME).setParameter("lastName", "%" + searchString + "%");
  153. List<Client> results = query.getResultList();
  154. for (Client c : results) {
  155. _return.add(createClientDTO(c));
  156. }
  157. return _return;
  158. } catch (Exception e) {
  159. System.out.println("Original Error Message: " + e.getMessage());
  160. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  161. }
  162. }
  163. @Override
  164. public List<ClientDTO> findClientsByEmail(String searchString, boolean exactMatch) throws BanqueException {
  165. List<ClientDTO> _return = new LinkedList<ClientDTO>();
  166. try {
  167. Query query;
  168. if(exactMatch)
  169. query = em.createNamedQuery(Client.FIND_BY_EMAIL_EQUAL).setParameter("email", searchString);
  170. else
  171. query = em.createNamedQuery(Client.FIND_BY_EMAIL).setParameter("email", "%" + searchString + "%");
  172. List<Client> results = query.getResultList();
  173. for (Client c : results) {
  174. _return.add(createClientDTO(c));
  175. }
  176. return _return;
  177. } catch (Exception e) {
  178. System.out.println("Original Error Message: " + e.getMessage());
  179. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  180. }
  181. }
  182. @Override
  183. public List<ClientDTO> findClientsByEmail(String searchString) throws BanqueException {
  184. return findClientsByEmail(searchString, false);
  185. }
  186. @Override
  187. public List<ClientDTO> findClientsByDateOfSubscription(Date date) throws BanqueException {
  188. LinkedList<ClientDTO> _return = new LinkedList<ClientDTO>();
  189. try {
  190. Query query = em.createNamedQuery(Client.FIND_BY_SUBSCRIPTION_DATE).setParameter("subsdate", date);
  191. List<Client> results = query.getResultList();
  192. for (Client c : results) {
  193. _return.add(createClientDTO(c));
  194. }
  195. return _return;
  196. } catch (Exception e) {
  197. System.out.println("Original Error Message: " + e.getMessage());
  198. throw new BanqueException(BanqueException.ErrorType.DATABASE_ERROR);
  199. }
  200. }
  201. @Override
  202. public ClientDTO authenticateClient(Long id, String password) throws BanqueException {
  203. ClientDTO client = findClient(id);
  204. if (PersonManager.hashPassword(password).equals(client.getPassword())) {
  205. return client;
  206. }
  207. return null;
  208. }
  209. @Override
  210. public ClientDTO changePassword(Long id, String password) throws BanqueException {
  211. ClientDTO client = findClient(id);
  212. client.setPassword(PersonManager.hashPassword(password));
  213. return updateClient(client);
  214. }
  215. protected static ClientDTO createClientDTO(Client client) {
  216. ClientDTO clientDTO = new ClientDTO(client.getName(), client.getLastName(), client.getPassword(), PersonManager.getGenderDTO(client.getGender()), client.getDateOfBirth(), client.getAddress(), client.getEmail(), client.isAdmin());
  217. clientDTO.setId(client.getId());
  218. return clientDTO;
  219. }
  220. /**
  221. * Validates a client before trying to store it or update it on the database
  222. * @param client the client to be analyzed
  223. * @return true if its possible to save the client or an exception otherwise
  224. * @throws BanqueException
  225. */
  226. protected boolean validateClient(Client client) throws BanqueException {
  227. //Fields cannot be null
  228. if (client.getName() == null || client.getName().isEmpty()) {
  229. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_NAME);
  230. }
  231. if (client.getLastName() == null || client.getLastName().isEmpty()) {
  232. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_LAST_NAME);
  233. }
  234. if (client.getPassword() == null || client.getPassword().isEmpty()) {
  235. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_PASSWORD);
  236. }
  237. if (client.getAddress() == null || client.getAddress().isEmpty()) {
  238. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_ADDRESS);
  239. }
  240. if (client.getDateOfBirth() == null) {
  241. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_DATE_OF_BIRTH);
  242. }
  243. if (client.getEmail() == null || client.getEmail().isEmpty()) {
  244. throw new BanqueException(BanqueException.ErrorType.CLIENT_NULL_EMAIL);
  245. }
  246. List<ClientDTO> lc = null;
  247. try {
  248. lc = findClientsByEmail(client.getEmail(), true);
  249. } catch(BanqueException e) {
  250. }
  251. if(lc != null && lc.size() > 0) {
  252. throw new BanqueException(BanqueException.ErrorType.CLIENT_EMAIL_ALREADY_USED);
  253. }
  254. ClientDTO c = null;
  255. try {
  256. c = findClient(client.getId());
  257. } catch(BanqueException e) {
  258. }
  259. //ID cannot exist and if it exists, must be the same as us
  260. if (c != null && c.getId() != client.getId()) {
  261. throw new BanqueException(BanqueException.ErrorType.CLIENT_ID_ALREADY_EXISTS);
  262. }
  263. //Email needs to have an arroba and a domain
  264. Pattern pattern = Pattern.compile("\\w+@\\w+\\.\\w+");
  265. Matcher matcher = pattern.matcher(client.getEmail());
  266. if (!matcher.find()) {
  267. throw new BanqueException(BanqueException.ErrorType.INVALID_EMAIL);
  268. }
  269. return true;
  270. }
  271. }