/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.managers; import ee.tlu.htk.dippler.entities.Client; import javax.ejb.Stateless; import javax.persistence.Query; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.NonUniqueResultException; import javax.persistence.PersistenceContext; /** * * @author metz */ @Stateless public class ClientManager implements ClientManagerLocal { @PersistenceContext private EntityManager em; @Override public Long validate(String username, String password) { Query q = em.createNamedQuery("Client.findClient"); q.setParameter("username", username); q.setParameter("password", password); try { Client client = (Client) q.getSingleResult(); // post validate if (client.getUsername().equals(username) && client.getPassword().equals(password)) { return client.getId(); } } catch (NoResultException nre) { nre.printStackTrace(System.err); } catch (NonUniqueResultException nur) { nur.printStackTrace(System.err); } catch (Exception e) { e.printStackTrace(System.err); } return null; } @Override public Client getClient(Long id) { if ( id > 0 ) { return em.find(Client.class, id); } return null; } @Override public String getOrganization(Client client) { return client.getOrganization().getName(); } @Override public boolean isTrusted(Long id) { Client client = getClient(id); if ( client != null ) { return client.getTrusted(); } return Boolean.FALSE; } }