Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  3. <persistence-unit name="GestorPU" transaction-type="RESOURCE_LOCAL">
  4. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  5. <class>Entity.Cadgru</class>
  6. <exclude-unlisted-classes>true</exclude-unlisted-classes>
  7. <properties>
  8. <property name="hibernate.connection.driver_class" value="org.firebirdsql.jdbc.FBDriver"/>
  9. <property name="hibernate.connection.url" value="jdbc:firebirdsql:localhost/3050:C:BANCOSGESTOR.FDB"/>
  10. <property name="hibernate.connection.username" value="SYSDBA"/>
  11. <property name="hibernate.connection.password" value="masterkey"/>
  12. <property name="hibernate.dialect" value="org.hibernate.dialect.FirebirdDialect"/>
  13. </properties>
  14. </persistence-unit>
  15. <persistence-unit name="MitryusPU" transaction-type="RESOURCE_LOCAL">
  16. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  17. <class>Entity.Cadusr</class>
  18. <class>Entity.Cadloj</class>
  19. <class>Entity.Cadfun</class>
  20. <class>Entity.Tipcli</class>
  21. <class>Entity.Tipven</class>
  22. <class>Entity.Vendas</class>
  23. <class>Entity.Venda_Sintetico</class>
  24. <class>Entity.CodigoPin</class>
  25. <class>Entity.Codloc</class>
  26. <class>Entity.VendaEvolucao</class>
  27. <exclude-unlisted-classes>true</exclude-unlisted-classes>
  28. <properties>
  29. <property name="hibernate.connection.driver_class" value="org.firebirdsql.jdbc.FBDriver"/>
  30. <property name="hibernate.connection.username" value="SYSDBA"/>
  31. <property name="hibernate.connection.password" value="masterkey"/>
  32. <property name="hibernate.dialect" value="org.hibernate.dialect.FirebirdDialect"/>
  33. </properties>
  34. </persistence-unit>
  35. </persistence>
  36.  
  37. package DAO;
  38.  
  39. import java.util.Properties;
  40. import javax.persistence.EntityManager;
  41. import javax.persistence.EntityManagerFactory;
  42. import javax.persistence.Persistence;
  43.  
  44. public class Conexao {
  45.  
  46. private static EntityManagerFactory emf ;
  47.  
  48. public static EntityManager getEntityManager(String PU, String Local) {
  49.  
  50. if(PU.equals("0")){
  51. emf = Persistence.createEntityManagerFactory("GestorPU");
  52. }else{
  53. Properties props = new Properties();
  54. props.setProperty("hibernate.connection.url", "jdbc:firebirdsql:localhost/3050:" + Local);
  55. emf = Persistence.createEntityManagerFactory(PU, props);
  56. }
  57.  
  58.  
  59. return emf.createEntityManager();
  60. }
  61. public static EntityManager getEntity() {
  62. return emf.createEntityManager();
  63. }
  64.  
  65. }
  66.  
  67. /*
  68. * To change this license header, choose License Headers in Project Properties.
  69. * To change this template file, choose Tools | Templates
  70. * and open the template in the editor.
  71. */
  72. package DAO;
  73.  
  74. import Entity.Cadgru;
  75. import Entity.Cadusr;
  76. import java.io.UnsupportedEncodingException;
  77. import java.security.MessageDigest;
  78. import java.security.NoSuchAlgorithmException;
  79. import javax.ejb.Stateless;
  80. import javax.persistence.EntityManager;
  81. import javax.persistence.EntityTransaction;
  82. import javax.persistence.NoResultException;
  83. import javax.persistence.Query;
  84.  
  85. /**
  86. *
  87. * @author Felipee
  88. */
  89. @Stateless
  90. public class GestorDAO {
  91.  
  92. Cadusr retorno;
  93. public static Cadusr usu = new Cadusr();
  94.  
  95. public Cadusr buscaPorId(String gr, String usr, String senha) {
  96. EntityManager em = Conexao.getEntityManager("0", null);
  97. EntityTransaction tx = em.getTransaction();
  98. tx.begin();
  99. Cadgru cadgru = em.find(Cadgru.class, gr);
  100. tx.commit();
  101. em.close();
  102. if (cadgru != null) {
  103. EntityManager em2 = Conexao.getEntityManager("MitryusPU", cadgru.getEndfdb());
  104. EntityTransaction tx2 = em2.getTransaction();
  105. tx2.begin();
  106. String jpql = "select a from Cadusr a where a.nomusr = :nomusr and a.pasusr = :pasusr";
  107. Query query = em2.createQuery(jpql, Cadusr.class);
  108. query.setParameter("nomusr", usr);
  109. query.setParameter("pasusr", senha);
  110. try {
  111. retorno = (Cadusr) query.getSingleResult();
  112. tx2.commit();
  113. em2.close();
  114. if (retorno == null) {
  115. em = Conexao.getEntityManager("0", cadgru.getEndfdb());
  116. tx = em.getTransaction();
  117. tx.begin();
  118. tx.commit();
  119. em.close();
  120.  
  121. }
  122. } catch (NoResultException nre) {
  123.  
  124. }
  125.  
  126. } else {
  127. retorno = null;
  128. }
  129.  
  130.  
  131. return retorno;
  132. }
  133.  
  134. public String usuarioLogado() {
  135. String Usuario = usu.getCodusr() + "-" + usu.getNomusr();
  136. return Usuario;
  137. }
  138.  
  139. private String convertStringToMd5(String valor) {
  140. MessageDigest mDigest;
  141. try {
  142. //Instanciamos o nosso HASH MD5, poderíamos usar outro como
  143. //SHA, por exemplo, mas optamos por MD5.
  144. mDigest = MessageDigest.getInstance("MD5");
  145.  
  146. //Convert a String valor para um array de bytes em MD5
  147. byte[] valorMD5 = mDigest.digest(valor.getBytes("UTF-8"));
  148.  
  149. //Convertemos os bytes para hexadecimal, assim podemos salvar
  150. //no banco para posterior comparação se senhas
  151. StringBuffer sb = new StringBuffer();
  152. for (byte b : valorMD5) {
  153. sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
  154.  
  155. }
  156.  
  157. return sb.toString();
  158.  
  159. } catch (NoSuchAlgorithmException e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. return null;
  163. } catch (UnsupportedEncodingException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. return null;
  167. }
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement