Newer
Older
CosmosServer / src / test / java / hibernateTest / models / Groups.java
package hibernateTest.models;

import hibernateTest.entities.Group;
import hibernateTest.entities.Request;
import com.example.cosmos_serversb.models.*;
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;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;


@Singleton
public class Groups {
    private static Groups theInstance = null;
    private static ArrayList<Group> groups = new ArrayList<>();

    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 HashMap<String, String> getGroupsListByUid(String uId){
        return createTestHashMap();
    }

    public static Group createGroup(String name, String uId){
        //先にGidとuriの発行を行う
        String gId = getInstance().createGId();
        String uri = baseURI + AppName + "/groups/" + gId;
        Group testGroup = new Group(gId, uri, name, uId);

//        Session session = sessionFactory.openSession();
//        session.beginTransaction();
//        Session session = SessionManager.getInstance().getCurrentSession();

//        session.save(testGroup);

//        session.getTransaction().commit();
//        session.close();

        return testGroup;
    }

    public static Group getGroupByGid(String gId){
        Group editGroup;
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List groupResult = session.createQuery( "from Group" ).list();
        for ( Group group : (List<Group>) groupResult ) {
            editGroup = group;
            if (editGroup.getgId().equals(gId)) {
                session.getTransaction().commit();
                session.close();
                return editGroup;
            }
        }
        session.getTransaction().commit();
        session.close();
        return null;
    }

    public static DeleteResult deleteGroup(String gId){
        Group editGroup = Groups.getInstance().getGroupByGid(gId);
        if(editGroup == null){
            return DeleteResult.FAILURE;
        }
        //以下に削除処理を行う。
        return DeleteResult.SUCCESS;
    }

    public static HashMap<String, String> getMembersListByGid(String gId){
        return createTestHashMap();
    }

    public static HashMap<String, String> addMember(String gId, String uId){
        return createTestHashMap();
    }

    public static HashMap<String, String> deleteMember(String gId, String uId){
        return createTestHashMap();
    }

    public static HashMap<String, String> getRequestsListByGid(String gId){
        return createTestHashMap();
    }

    public static Request addRequests(String gId, String uId, String product, String deadline, int location){
        //テスト用
        String rId = getInstance().createRId();
        String uri = baseURI + AppName + "/groups/" + gId + "/requests/" + rId;
        Request testRequest = new Request(rId, uri, "1999/01/01/15:00:00",
                uId, product, "1999/01/01/15:00:00", 999);
        return testRequest;
    }

    public static Request getRequestDetailByGidAndRid(String gId, String rId){
        return null;
    }

    public static Request updateRequest(String gId, String rId, String uId, String product, String deadline, int location, boolean done){

        //テスト用
        Request testRequest = new Request(rId, "http://test.com", "1999/01/01/15:00:00",
                uId, product, "1999/01/01/15:00:00", 999);

        return testRequest;
    }

    public static DeleteResult deleteRequest(String gId, String rId){
        return DeleteResult.SUCCESS;
    }

    //テスト用にHashMapの作成
    public static HashMap<String, String> createTestHashMap(){
        HashMap<String, String> testMap = new HashMap<>();
        testMap.put("123456780", "http://test0.com");
        testMap.put("123456781", "http://test1.com");
        testMap.put("123456782", "http://test2.com");
        /*
        //Key, valueの取り出し方例(拡張for文)
        for (String key : TestMap.keySet()) {
            System.out.println(key + " " + TestMap.get(key));
        }
         */

        return testMap;
    }

}