Newer
Older
CosmosServer / src / test / java / hibernateTest / models / Users.java
  1. package hibernateTest.models;
  2.  
  3. import com.example.cosmos_serversb.entities.Token;
  4. import com.example.cosmos_serversb.models.DeleteResult;
  5.  
  6. import javax.inject.Singleton;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.UUID;
  10.  
  11. import hibernateTest.entities.Event;
  12. import hibernateTest.entities.Group;
  13. import hibernateTest.entities.Request;
  14. import hibernateTest.entities.User;
  15. import org.hibernate.Criteria;
  16. import org.hibernate.SessionBuilder;
  17. import org.hibernate.criterion.Restrictions;
  18. import org.hibernate.Session;
  19. import org.hibernate.SessionFactory;
  20. import org.hibernate.boot.MetadataSources;
  21. import org.hibernate.boot.registry.StandardServiceRegistry;
  22. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  23.  
  24. @Singleton
  25. public class Users {
  26. private static Users theInstance = null;
  27. private static SessionFactory sessionFactory;
  28.  
  29. public static void setUp() {
  30. // A SessionFactory is set up once for an application!
  31. final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
  32. .configure() // configures settings from hibernate.cfg.xml
  33. .build();
  34. try {
  35. sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
  36. }
  37. catch (Exception e) {
  38. // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
  39. // so destroy it manually.
  40. StandardServiceRegistryBuilder.destroy( registry );
  41. }
  42. }
  43.  
  44. public static void tearDown() {
  45. try {
  46. if (sessionFactory != null) {
  47. sessionFactory.close();
  48. }
  49. } catch (Exception e) {
  50. System.out.println("Exception!");
  51. }
  52. }
  53.  
  54. private Users() {
  55. }
  56.  
  57. public static Users getInstance() {
  58. if (theInstance == null) {
  59. theInstance = new Users();
  60. }
  61. return theInstance;
  62. }
  63.  
  64. public static String createToken(String uId) {
  65. //Tokenを作成しuIdと関連付けしDBに保存し作成したTokenを返す
  66. //String token = UUID.randomUUID().toString();
  67. //return token;
  68. return null;
  69. }
  70.  
  71. public static String createUId() {
  72. //uIdを作成し既存していないかチェック後DBに保存し作成したuIdを返す
  73. String uId = UUID.randomUUID().toString();
  74. return uId;
  75. }
  76.  
  77. public static User createUser(String name, String pw, String iconImage) {
  78.  
  79. Session session = sessionFactory.openSession();
  80. session.beginTransaction();
  81. //名前、パスワード、アイコンイメージを保存
  82. User testUser = new User(name,getInstance().createUId(), "http://test.com",pw,iconImage);
  83. System.out.println(testUser.getUri());
  84. session.save(testUser);
  85. //hibernateテスト出力用
  86. List userResult = session.createQuery( "from User" ).list();
  87. for ( User user : (List<User>) userResult ) {
  88. System.out.println( "User (" + user.getuId() + ") : " + user.getUri() + ": " + user.getName());
  89. }
  90. session.getTransaction().commit();
  91. session.close();
  92.  
  93. //testUser.token.add(getInstance().createToken(testUser.uId));
  94. //testUser.token.add(getInstance().createToken(testUser.uId));
  95.  
  96. return testUser;
  97. }
  98.  
  99. public static User setUser(String uId, String name, String pw, String iconImage) {
  100.  
  101. Session session = sessionFactory.openSession();
  102. session.beginTransaction();
  103. User testUser = new User(name,uId,"http://test.com",pw,iconImage);
  104. Criteria criteria = session.createCriteria(Request.class).add(Restrictions.eq("rId", "333"));
  105. Request editRequest = (Request) criteria.uniqueResult();
  106. if(editRequest != null) {
  107. editRequest.setProduct("Chocolate ice !!");
  108. session.update(editRequest);
  109. }
  110. session.getTransaction().commit();
  111. session.close();
  112.  
  113. //testUser.token.add(getInstance().createToken(testUser.uId));
  114. //testUser.token.add(getInstance().createToken(testUser.uId));
  115.  
  116. return testUser;
  117. }
  118.  
  119. public static User getUserById(String uId) {
  120. //与えられたuIdのUser情報を返す
  121. User editUser;
  122. Session session = sessionFactory.openSession();
  123. session.beginTransaction();
  124. List userResult = session.createQuery("from User").list();
  125. for ( hibernateTest.entities.User user : (List<hibernateTest.entities.User>) userResult){
  126. editUser = user;
  127. if (editUser.getuId().equals(uId)){
  128. return editUser;
  129. }
  130. }
  131. session.getTransaction().commit();
  132. session.close();
  133.  
  134. //testUser.token.add(getInstance().createToken(testUser.uId));
  135. //testUser.token.add(getInstance().createToken(testUser.uId));
  136.  
  137. return null;
  138. }
  139.  
  140. public static User getUserByToken(String token) {
  141. //与えられたTokenに紐づくUser情報を返す
  142. User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg");
  143.  
  144. //testUser.token.add(getInstance().createToken(testUser.uId));
  145. //testUser.token.add(getInstance().createToken(testUser.uId));
  146.  
  147. return testUser;
  148. }
  149.  
  150. public static DeleteResult deleteUser(String uId) {
  151.  
  152. Session session = sessionFactory.openSession();
  153. session = sessionFactory.openSession();
  154. session.beginTransaction();
  155. Request deleteRequest = (Request) session.load(Request.class, "222");
  156. if(deleteRequest != null){
  157. session.delete(deleteRequest);
  158. }
  159. session.getTransaction().commit();
  160. session.close();
  161.  
  162. return DeleteResult.SUCCESS;
  163. }
  164.  
  165. public static Token login(String uId) {
  166. //Token testtoken = new Token(getInstance().createToken(uId));
  167. //tokenを作成後値を返す
  168. //return testtoken;//testToken
  169. return null;
  170. }
  171.  
  172. public static DeleteResult logout(String token) {
  173. if(Users.getInstance().getUserByToken(token).uId == null) {
  174. return DeleteResult.FAILURE;
  175. }else{
  176. //Users.getInstance().getUserByToken(token).token.remove(Users.getInstance().getUserByToken(token).token.indexOf(token));
  177. return DeleteResult.SUCCESS;
  178. }
  179. }
  180.  
  181. public static boolean checkToken(String uId, String token) {
  182. //与えられたtokenとUserのtokenを比較し結果を返す
  183. return true;
  184. }
  185.  
  186. public static boolean checkPassword(String uId, String pw) {
  187. //与えられたパスワードとUserのパスワードを比較し結果を返す
  188. return true;
  189. }
  190. }