package cn.updev.Events.Group;

import cn.updev.Database.HibernateSessionFactory;
import cn.updev.Events.Event.EventDAO;
import cn.updev.Events.Static.IEvent;
import cn.updev.Events.Static.ITeamEvents;
import cn.updev.Events.Static.IUserEvents;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hypo on 15-11-18.
 */
public class EventGroupDAO {

    private Session session;

    public EventGroupDAO() {

        this.session = HibernateSessionFactory.currentSession();
        this.session.clear();
    }

    public List<UserEventGroup> getAllUserEventGroup(Integer userId){

        Query query = session.createQuery("from EventGroupInfo info where info.ownerId=" + userId);
        List<EventGroupInfo> list = (List<EventGroupInfo>)query.list();

        if(list.size() == 0){
            return null;
        }

        List<UserEventGroup> rnt = new ArrayList<UserEventGroup>();
        for(EventGroupInfo groupInfo : list){
            List<IEvent> events = new EventDAO().getEventByEventGroupId(groupInfo.getGroupId());
            if(events != null){

                rnt.add(new UserEventGroup(groupInfo, events));
            }
        }
        return rnt;
    }

    public ITeamEvents getTeamEventGroup(Integer groupId){
        Query query =session.createQuery("from EventGroupInfo info where info.groupId=" + groupId);

        if(query.list().size() == 0){
            return null;
        }

        List<IEvent> events = new EventDAO().getEventByEventGroupId(groupId);
        EventGroupInfo group = (EventGroupInfo)query.list().get(0);
        return new TeamEventGroup(group, events);
    }

    public IUserEvents getUserEventGroup(Integer groupId){
        Query query =session.createQuery("from EventGroupInfo info where info.groupId=" + groupId);

        if(query.list().size() == 0){
            return null;
        }

        List<IEvent> events = new EventDAO().getEventByEventGroupId(groupId);
        EventGroupInfo group = (EventGroupInfo)query.list().get(0);
        return new UserEventGroup(group, events);
    }

    protected void finalize(){
        HibernateSessionFactory.closeSession();
    }

}

@@ -428,157 +428,32 @@
    p
-rivate Session session;%0A%0A    public EventGroupDAO() %7B%0A%0A        this.session = HibernateSessionFactory.currentSession();%0A        this.session.clear();
+ublic EventGroupDAO() %7B%0A
 %0A   
@@ -523,24 +523,92 @@
 r userId)%7B%0A%0A
+        Session session = HibernateSessionFactory.currentSession();%0A
         Quer
@@ -948,16 +948,17 @@
  list)%7B%0A
+%0A
         
@@ -965,68 +965,69 @@
     
-List%3CIEvent%3E events = new EventDAO().getEventByEventG
+query =session.createQuery(%22from Event e where e.g
 roupId
-(
+=%22 + 
 grou
@@ -1043,24 +1043,72 @@
 GroupId());%0A
+            List%3CIEvent%3E events = query.list();%0A
             
@@ -1109,16 +1109,17 @@
       if
+ 
 (events 
@@ -1126,16 +1126,17 @@
 != null)
+ 
 %7B%0A%0A     
@@ -1210,32 +1210,130 @@
     %7D%0A        %7D%0A
+        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A
         return r
@@ -1393,32 +1393,101 @@
 nteger groupId)%7B
+%0A%0A        Session session = HibernateSessionFactory.currentSession();
 %0A        Query q
@@ -1658,68 +1658,69 @@
     
-List%3CIEvent%3E events = new EventDAO().getEventByEventG
+query =session.createQuery(%22from Event e where e.g
 roupId
-(
+=%22 + 
 grou
@@ -1717,32 +1717,77 @@
 d=%22 + groupId);%0A
+        List%3CIEvent%3E events = query.list();%0A%0A
         EventGro
@@ -1830,32 +1830,130 @@
 .list().get(0);%0A
+        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A
         return n
@@ -2051,16 +2051,85 @@
 roupId)%7B
+%0A%0A        Session session = HibernateSessionFactory.currentSession();
 %0A       
@@ -2308,68 +2308,79 @@
     
-List%3CIEvent%3E events = new EventDAO().getEventByEventG
+Query getEvents =session.createQuery(%22from Event e where e.g
 roupId
-(
+=%22 + 
 grou
@@ -2377,32 +2377,81 @@
 d=%22 + groupId);%0A
+        List%3CIEvent%3E events = getEvents.list();%0A%0A
         EventGro
@@ -2514,87 +2514,49 @@
     
-return new UserEventGroup(group, events
+session.clear(
 );%0A    
-%7D%0A%0A
     
-protected void finalize()%7B
+session.flush();
 %0A   
@@ -2604,13 +2604,62 @@
 ();%0A    
+    return new UserEventGroup(group, events);%0A    
 %7D
-%0A
 %0A%7D%0A

update EventGroupDAO session