package com.example.cosmos_serversb.models; import java.util.List; import java.util.UUID; import javax.inject.Singleton; import com.example.cosmos_serversb.entities.*; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.hibernate.Session; import org.hibernate.SessionFactory; @Singleton public class Users { private static Users theInstance = null; private static SessionFactory sessionFactory; private static String baseURI = "http://nitta-lab-www.is.konan-u.ac.jp/"; private static String AppName = "cosmos"; private Users() { sessionFactory = SessionFactoryManager.getInstance().getSessionFactory(); } public static Users getInstance() { if (theInstance == null) { theInstance = new Users(); } return theInstance; } public static Token createToken(String uId) { //Tokenを作成しuIdと関連付けしDBに保存し作成したTokenを返す String token = UUID.randomUUID().toString(); Token testToken =new Token(token, uId); /*TODO:Session managerを作成しセッションの二重作成をしない Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(testToken); session.getTransaction().commit(); session.close();*/ return testToken; } public static String createUId() { //uIdを作成し既存していないかチェック後DBに保存し作成したuIdを返す String uId = UUID.randomUUID().toString(); return uId; } public static User createUser(String name, String pw, String iconImage) { String uId = "1234"; String uri = baseURI + AppName +"/users/" +uId; Session session = sessionFactory.openSession(); session.beginTransaction(); createToken(uId); User testUser = new User(name, uId, uri, iconImage);//名前、uId、ユーザーuri、パスワード、アイコンイメージを保存 session.save(testUser); //出力 /* List userResult = session.createQuery( "from User" ).list(); for ( User user : (List<User>) userResult ) { System.out.println( "UserId (" + user.getuId() + ") : " + user.getUri() + ": " + user.getName()); }*/ session.getTransaction().commit(); session.close(); return testUser; } public static User setUser(String uId, String name, String pw, String iconImage) { Session session = sessionFactory.openSession(); session.beginTransaction(); List usersList = session.createQuery( "from User" ).list(); for ( User user : (List<User>) usersList ) { if (user.getuId().equals(uId)) { user.setName(name); if(iconImage != null) { user.setIconUri(iconImage); } session.update(user); break; } } User findUser = (User) session.load(User.class, uId); session.getTransaction().commit(); session.close(); return findUser; } public static User getUserById(String uId) { //与えられたuIdのUser情報を返す Session session = sessionFactory.openSession(); session.beginTransaction(); List usersList = session.createQuery( "from User" ).list(); //uIdを比較し、一致した場合はNameとiconUriを返す。一致しない場合はbreak for ( User user : (List<User>) usersList ) { if (user.getuId().equals(uId)) { User findUser = (User) session.load(User.class, uId); return findUser; // System.out.println( "name: " + user.getName() + "iconUri : " + user.getIconUri()); }else{ break; } } session.getTransaction().commit(); session.close(); //一致しない場合は返す必要がないのでnull return null; } public static User getUserByToken(String token) { //与えられたTokenに紐づくUser情報を返す User testUser = new User("test", getInstance().createUId(), "http://test.com", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); return testUser; } public static DeleteResult deleteUser(String uId) { Session session = sessionFactory.openSession(); session = sessionFactory.openSession(); session.beginTransaction(); Request deleteRequest = (Request) session.load(Request.class, uId); if(deleteRequest != null){ session.delete(deleteRequest); System.out.println("massage: Already Deleted"); } session.getTransaction().commit(); session.close(); return DeleteResult.SUCCESS; } public static Token login(String uId,String pw) { if(checkPassword(uId,pw)) { Token testtoken = getInstance().createToken(uId); //tokenを作成後値を返す // System.out.println("true"); return testtoken;//testToken }else{ return null; } } public static DeleteResult logout(String uId,String token) { Session session = sessionFactory.openSession(); session.beginTransaction(); List usersList = session.createQuery( "from logout" ).list(); for ( User user : (List<User>) usersList ) { if (user.getuId().equals(uId)) { if (Users.getInstance().getUserByToken(token).uId == null) { return DeleteResult.FAILURE; } else { // Users.getInstance().getUserByToken(token).token.remove(Users.getInstance().getUserByToken(token).token.indexOf(token)); return DeleteResult.SUCCESS; } } } return null; } public static boolean checkToken(String uId, String token) { //与えられたtokenとUserのtokenを比較し結果を返す return true; } public static boolean checkPassword(String uId, String pw) { //与えられたパスワードとUserのパスワードを比較し結果を返す return true; } }