package hibernateTest.models; import com.example.cosmos_serversb.entities.Token; import com.example.cosmos_serversb.models.DeleteResult; import javax.inject.Singleton; import java.util.Date; import java.util.List; import java.util.UUID; import hibernateTest.entities.Event; import hibernateTest.entities.Group; import hibernateTest.entities.Request; import hibernateTest.entities.User; import org.hibernate.Criteria; import org.hibernate.SessionBuilder; import org.hibernate.criterion.Restrictions; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @Singleton public class Users { private static Users theInstance = null; private static SessionFactory sessionFactory; public static void setUp() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } } public static void tearDown() { try { if (sessionFactory != null) { sessionFactory.close(); } } catch (Exception e) { System.out.println("Exception!"); } } private Users() { } public static Users getInstance() { if (theInstance == null) { theInstance = new Users(); } return theInstance; } public static String createToken(String uId) { //Tokenを作成しuIdと関連付けしDBに保存し作成したTokenを返す //String token = UUID.randomUUID().toString(); //return token; return null; } public static String createUId() { //uIdを作成し既存していないかチェック後DBに保存し作成したuIdを返す String uId = UUID.randomUUID().toString(); return uId; } public static User createUser(String name, String pw, String iconImage) { Session session = sessionFactory.openSession(); session.beginTransaction(); //名前、パスワード、アイコンイメージを保存 User testUser = new User(name,getInstance().createUId(), "http://test.com",pw,iconImage); System.out.println(testUser.getUri()); session.save(testUser); //hibernateテスト出力用 List userResult = session.createQuery( "from User" ).list(); for ( User user : (List<User>) userResult ) { System.out.println( "User (" + user.getuId() + ") : " + user.getUri() + ": " + user.getName()); } session.getTransaction().commit(); session.close(); //testUser.token.add(getInstance().createToken(testUser.uId)); //testUser.token.add(getInstance().createToken(testUser.uId)); return testUser; } public static User setUser(String uId, String name, String pw, String iconImage) { Session session = sessionFactory.openSession(); session.beginTransaction(); User testUser = new User(name,uId,"http://test.com",pw,iconImage); Criteria criteria = session.createCriteria(Request.class).add(Restrictions.eq("rId", "333")); Request editRequest = (Request) criteria.uniqueResult(); if(editRequest != null) { editRequest.setProduct("Chocolate ice !!"); session.update(editRequest); } session.getTransaction().commit(); session.close(); //testUser.token.add(getInstance().createToken(testUser.uId)); //testUser.token.add(getInstance().createToken(testUser.uId)); return testUser; } public static User getUserById(String uId) { //与えられたuIdのUser情報を返す User editUser; Session session = sessionFactory.openSession(); session.beginTransaction(); List userResult = session.createQuery("from User").list(); for ( hibernateTest.entities.User user : (List<hibernateTest.entities.User>) userResult){ editUser = user; if (editUser.getuId().equals(uId)){ return editUser; } } session.getTransaction().commit(); session.close(); //testUser.token.add(getInstance().createToken(testUser.uId)); //testUser.token.add(getInstance().createToken(testUser.uId)); return null; } public static User getUserByToken(String token) { //与えられたTokenに紐づくUser情報を返す User testUser = new User("test", getInstance().createUId(), "http://test.com", "testpw", "http://localhost:8080/iconImage/b4bb7db0-cc0c-4ee1-816f-8304add12da8.jpg"); //testUser.token.add(getInstance().createToken(testUser.uId)); //testUser.token.add(getInstance().createToken(testUser.uId)); return testUser; } public static DeleteResult deleteUser(String uId) { Session session = sessionFactory.openSession(); session = sessionFactory.openSession(); session.beginTransaction(); Request deleteRequest = (Request) session.load(Request.class, "222"); if(deleteRequest != null){ session.delete(deleteRequest); } session.getTransaction().commit(); session.close(); return DeleteResult.SUCCESS; } public static Token login(String uId) { //Token testtoken = new Token(getInstance().createToken(uId)); //tokenを作成後値を返す //return testtoken;//testToken return null; } public static DeleteResult logout(String token) { 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; } } public static boolean checkToken(String uId, String token) { //与えられたtokenとUserのtokenを比較し結果を返す return true; } public static boolean checkPassword(String uId, String pw) { //与えられたパスワードとUserのパスワードを比較し結果を返す return true; } }