Newer
Older
CosmosServer / src / test / java / hibernateTest / models / Groups.java
  1. package hibernateTest.models;
  2.  
  3. import hibernateTest.entities.Group;
  4. import hibernateTest.entities.Request;
  5. import com.example.cosmos_serversb.models.DeleteResult;
  6. import org.hibernate.Session;
  7. import org.hibernate.SessionFactory;
  8. import org.hibernate.boot.MetadataSources;
  9. import org.hibernate.boot.registry.StandardServiceRegistry;
  10. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  11.  
  12. import javax.inject.Singleton;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.UUID;
  17.  
  18.  
  19. @Singleton
  20. public class Groups {
  21. private static Groups theInstance = null;
  22. private static ArrayList<Group> groups = new ArrayList<>();
  23.  
  24. private static SessionFactory sessionFactory;
  25. private static String baseURI = "http://nitta-lab-www.is.konan-u.ac.jp/";
  26. private static String AppName = "cosmos";
  27.  
  28. private Groups(){
  29. sessionFactory = SessionFactoryManager.getInstance().getSessionFactory();
  30. }
  31.  
  32. public static Groups getInstance(){
  33. if(theInstance == null){
  34. theInstance = new Groups();
  35. }
  36. return theInstance;
  37. }
  38.  
  39. public static String createGId() {
  40. //gIdを作成し既存していないかチェック後DBに保存し作成したgIdを返す
  41. String gId = UUID.randomUUID().toString();
  42. return gId;
  43. }
  44.  
  45. public static String createRId() {
  46. //rIdを作成し既存していないかチェック後DBに保存し作成したrIdを返す
  47. String rId = UUID.randomUUID().toString();
  48. return rId;
  49. }
  50.  
  51. public static HashMap<String, String> getGroupsListByUid(String uId){
  52. return createTestHashMap();
  53. }
  54.  
  55. public static Group createGroup(String name, String uId){
  56. //先にGidとuriの発行を行う
  57. String gId = getInstance().createGId();
  58. String uri = baseURI + AppName + "/groups/" + gId;
  59. Group testGroup = new Group(gId, uri, name, uId);
  60.  
  61. // Session session = sessionFactory.openSession();
  62. // session.beginTransaction();
  63. // Session session = SessionManager.getInstance().getCurrentSession();
  64.  
  65. // session.save(testGroup);
  66.  
  67. // session.getTransaction().commit();
  68. // session.close();
  69.  
  70. return testGroup;
  71. }
  72.  
  73. public static Group getGroupByGid(String gId){
  74. Group editGroup;
  75. Session session = sessionFactory.openSession();
  76. session.beginTransaction();
  77. List groupResult = session.createQuery( "from Group" ).list();
  78. for ( Group group : (List<Group>) groupResult ) {
  79. editGroup = group;
  80. if (editGroup.getgId().equals(gId)) {
  81. session.getTransaction().commit();
  82. session.close();
  83. return editGroup;
  84. }
  85. }
  86. session.getTransaction().commit();
  87. session.close();
  88. return null;
  89. }
  90.  
  91. public static DeleteResult deleteGroup(String gId){
  92. Group editGroup = Groups.getInstance().getGroupByGid(gId);
  93. if(editGroup == null){
  94. return DeleteResult.FAILURE;
  95. }
  96. //以下に削除処理を行う。
  97. return DeleteResult.SUCCESS;
  98. }
  99.  
  100. public static HashMap<String, String> getMembersListByGid(String gId){
  101. return createTestHashMap();
  102. }
  103.  
  104. public static HashMap<String, String> addMember(String gId, String uId){
  105. return createTestHashMap();
  106. }
  107.  
  108. public static HashMap<String, String> deleteMember(String gId, String uId){
  109. return createTestHashMap();
  110. }
  111.  
  112. public static HashMap<String, String> getRequestsListByGid(String gId){
  113. return createTestHashMap();
  114. }
  115.  
  116. public static Request addRequests(String gId, String uId, String product, String deadline, int location){
  117. //テスト用
  118. String rId = getInstance().createRId();
  119. String uri = baseURI + AppName + "/groups/" + gId + "/requests/" + rId;
  120. Request testRequest = new Request(rId, uri, "1999/01/01/15:00:00",
  121. uId, product, "1999/01/01/15:00:00", 999);
  122. return testRequest;
  123. }
  124.  
  125. public static Request getRequestDetailByGidAndRid(String gId, String rId){
  126. return null;
  127. }
  128.  
  129. public static Request updateRequest(String gId, String rId, String uId, String product, String deadline, int location, boolean done){
  130.  
  131. //テスト用
  132. Request testRequest = new Request(rId, "http://test.com", "1999/01/01/15:00:00",
  133. uId, product, "1999/01/01/15:00:00", 999);
  134.  
  135. return testRequest;
  136. }
  137.  
  138. public static DeleteResult deleteRequest(String gId, String rId){
  139. return DeleteResult.SUCCESS;
  140. }
  141.  
  142. //テスト用にHashMapの作成
  143. public static HashMap<String, String> createTestHashMap(){
  144. HashMap<String, String> testMap = new HashMap<>();
  145. testMap.put("123456780", "http://test0.com");
  146. testMap.put("123456781", "http://test1.com");
  147. testMap.put("123456782", "http://test2.com");
  148. /*
  149. //Key, valueの取り出し方例(拡張for文)
  150. for (String key : TestMap.keySet()) {
  151. System.out.println(key + " " + TestMap.get(key));
  152. }
  153. */
  154.  
  155. return testMap;
  156. }
  157.  
  158. }