package com.example.cosmos_serversb.models; import java.lang.reflect.Member; import java.util.*; import javax.inject.Singleton; import java.util.Date; import com.example.cosmos_serversb.entities.*; import com.example.cosmos_serversb.jsons.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import static com.example.cosmos_serversb.models.Users.getUserById; @Singleton public class Groups { private static Groups theInstance = null; private static ArrayList<Group> groups = new ArrayList<>(); //private static HashMap<String, String> groupsMap = new HashMap<>(); private static SessionFactory sessionFactory; private static String baseURI = "http://nitta-lab-www.is.konan-u.ac.jp/"; private static String AppName = "cosmos"; private Groups(){ //sessionFactory = SessionFactoryManager.getInstance().getSessionFactory(); } public static Groups getInstance(){ if(theInstance == null){ theInstance = new Groups(); } return theInstance; } public static String createGId() { //gIdを作成し既存していないかチェック後DBに保存し作成したgIdを返す String gId = UUID.randomUUID().toString(); return gId; } public static String createRId() { //rIdを作成し既存していないかチェック後DBに保存し作成したrIdを返す String rId = UUID.randomUUID().toString(); return rId; } public static GroupsMapJson getGroupsListByUid(String uId){ return getGroupsHashMap(uId); } public static Group createGroup(String name, String uId){ if(name == null || uId == null){ return null; //not-null制約 } User user = getUserById(uId); if(user == null){ return null; //存在しないユーザがグループを作成しようとした。 } //先にGidとuriの発行を行う String gId = getInstance().createGId(); String uri = baseURI + AppName + "/groups/" + gId; Group testGroup = new Group(gId, uri, name, uId); Session session = SessionManager.getInstance().getSession(); session.save(testGroup); return testGroup; } public static Group getGroupByGid(String gId){ Group editGroup; Session session = SessionManager.getInstance().getSession(); List groupResult = session.createQuery( "from Group" ).list(); for ( Group group : (List<Group>) groupResult ) { editGroup = group; if (editGroup.getgId().equals(gId)) { return editGroup; } } return null; } public static DeleteResult deleteGroup(String gId){ Session session = SessionManager.getInstance().getSession(); Group editGroup = session.get(Group.class, gId); if(editGroup == null){ return DeleteResult.FAILURE; } //削除するグループに所属しているユーザーに、退会してもらう。 for(User member : editGroup.getMembers()){ member.exitGroup(editGroup); } //削除するグループが持っているユーザの情報を全て削除する。 editGroup.clearAllMembersForDeleteGroup(); session.delete(editGroup); return DeleteResult.SUCCESS; } public static MembersJson getMembersListByGid(String gId){ Session session = SessionManager.getInstance().getSession(); Group editGroup = session.get(Group.class, gId); if(editGroup != null){ MembersJson members = new MembersJson(editGroup); return members; } return null; } public static Group addMember(String gId, String uId){ Session session = SessionManager.getInstance().getSession(); Group editGroup = session.get(Group.class, gId); if(editGroup != null) { editGroup.addMember(uId); session.update(editGroup); //UsersMapJson usersMapJson = new UsersMapJson(editGroup); return editGroup; } //例外処理 return null; } public static DeleteResult deleteMember(String gId, String uId){ Session session = SessionManager.getInstance().getSession(); Group editGroup = session.get(Group.class, gId); User editUser = session.get(User.class, uId); if(editGroup != null){ if(editGroup.getMembers().contains(editUser)){ editUser.exitGroup(editGroup); editGroup.deleteMember(editUser); if(editGroup.getMembers().size() == 0){ //ユーザーが存在しないグループは削除 session.delete(editGroup); } return DeleteResult.SUCCESS; } } return DeleteResult.FAILURE; } /* public static Group getGroupByGid(String gId){ Group editGroup; Session session = SessionManager.getInstance().getSession(); List groupResult = session.createQuery( "from Group" ).list(); for ( Group group : (List<Group>) groupResult ) { editGroup = group; if (editGroup.getgId().equals(gId)) { return editGroup; } } return null; } */ public static RequestsJson getRequestsListByGid(String gId, boolean detail, int quantity){ Session session = SessionManager.getInstance().getSession(); Group group = session.get(Group.class, gId); if(group != null) { RequestsJson requestsJson = new RequestsJson(group.getRequests(), quantity); return requestsJson; } //例外処理 return null; } public static Request addRequests(String gId, String uId, String product, String deadline, int location){ if(product == null || uId == null){ return null; //not-null制約 } String rId = getInstance().createRId(); String uri = baseURI + AppName + "/groups/" + gId + "/requests/" + rId; Request request = new Request(rId, uri, new Date(), uId, product, deadline, location); Session session = SessionManager.getInstance().getSession(); Group editGroup = session.get(Group.class, gId); if(editGroup != null) { editGroup.addRequests(request); session.update(editGroup); return request; } //例外処理 return null; } public static Request getRequestDetailByGidAndRid(String gId, String rId){ Session session = SessionManager.getInstance().getSession(); Request request = session.get(Request.class, rId); Group group = session.get(Group.class, gId); if(group.getRequests().contains(request)) { return request; } //違うグループのリクエストは返せない。 return null; } public static Request updateRequest(String gId, String rId, String uId, String product, String deadline, int location, boolean done){ if(product == null){ return null; //not-null制約 } Session session = SessionManager.getInstance().getSession(); Request editRequest = session.get(Request.class, rId); if(editRequest != null){ editRequest.update(uId, product, deadline, location, done); session.update(editRequest); } //例外処理 return null; // //テスト用 // Request testRequest = new Request(rId, "http://test.com", new Date(), uId, product, new Date(), 999); // // return testRequest; } public static DeleteResult deleteRequest(String gId, String rId){ Session session = SessionManager.getInstance().getSession(); Request request = session.get(Request.class, rId); if(request != null) { session.delete(request); return DeleteResult.SUCCESS; } return DeleteResult.FAILURE; } /* //テスト用にHashMapの作成 public static void createTestHashMap(String gId, String uri){ testMap.put(gId, uri); /* //Key, valueの取り出し方例(拡張for文) for (String key : TestMap.keySet()) { System.out.println(key + " " + TestMap.get(key)); } } */ public static GroupsMapJson getGroupsHashMap(String uId){ Session session = SessionManager.getInstance().getSession(); GroupsMapJson groupsjson = new GroupsMapJson(session.load(User.class, uId)); return groupsjson; } }