package cn.updev.Events.Event;

import cn.updev.Database.HibernateSessionFactory;
import cn.updev.Events.Static.IEvent;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.Date;
import java.util.List;

/**
 * Created by hypo on 15-9-30.
 */
public class EventDAO {

    private Session session;

    public EventDAO() {

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

    public IEvent getEventById(Long eventId){
        Query query =session.createQuery("from Event e where e.eventId=" + eventId);

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

    public List<IEvent> getSingleEventByUserId(Integer userId){

        Query query =session.createQuery("from Event e where e.doerId=" + userId + " and e.groupId = NULL");

        return (List<IEvent>) query.list();
    }

    public List<IEvent> getEventByUserId(Integer userId){

        Query query =session.createQuery("from Event e where e.doerId=" + userId);

        return (List<IEvent>) query.list();
    }

    public List<IEvent> getEventByEventGroupId(Integer eventGroupId){

        Query query =session.createQuery("from Event e where e.groupId=" + eventGroupId);

        return (List<IEvent>) query.list();
    }

    public List<IEvent> getEventByUserGroupId(Integer userGroupId){

        return null;
    }

    public Boolean eventFinish(Long eventId){

        IEvent event = getEventById(eventId);
        if(event.isFinish()){
            event.setFinishTime(new Date());

            session.update(event);

            return true;
        }else {
            event.setFinishTime(null);

            session.update(event);

            return false;
        }
    }

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

@@ -292,31 +292,33 @@
    p
-rivate Session session;
+ublic EventDAO() %7B%0A%0A    %7D
 %0A%0A  
@@ -322,35 +322,57 @@
 %0A    public 
+I
 Event
-DAO() 
+ getEventById(Long eventId)
 %7B%0A%0A        t
@@ -366,29 +366,32 @@
 )%7B%0A%0A        
-this.
+Session 
 session = Hi
@@ -434,91 +434,8 @@
 ();%0A
-        this.session.clear();%0A    %7D%0A%0A    public IEvent getEventById(Long eventId)%7B%0A
     
@@ -640,16 +640,114 @@
 get(0);%0A
+        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A
         
@@ -822,32 +822,100 @@
 teger userId)%7B%0A%0A
+        Session session = HibernateSessionFactory.currentSession();%0A
         Query qu
@@ -1007,25 +1007,24 @@
 NULL%22);%0A
-%0A
         
 return (
@@ -1011,32 +1011,24 @@
 %22);%0A        
-return (
 List%3CIEvent%3E
@@ -1019,33 +1019,39 @@
     List%3CIEvent%3E
-)
+ list =
  query.list();%0A 
@@ -1040,32 +1040,153 @@
  = query.list();
+%0A%0A        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A%0A        return list;
 %0A    %7D%0A%0A    publ
@@ -1228,32 +1228,100 @@
 teger userId)%7B%0A%0A
+        Session session = HibernateSessionFactory.currentSession();%0A
         Query qu
@@ -1383,33 +1383,32 @@
  + userId);%0A
-%0A
         
 return (List
@@ -1391,32 +1391,24 @@
 d);%0A        
-return (
 List%3CIEvent%3E
@@ -1399,33 +1399,39 @@
     List%3CIEvent%3E
-)
+ list =
  query.list();%0A 
@@ -1420,32 +1420,153 @@
  = query.list();
+%0A%0A        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A%0A        return list;
 %0A    %7D%0A%0A    publ
@@ -1620,32 +1620,100 @@
 eventGroupId)%7B%0A%0A
+        Session session = HibernateSessionFactory.currentSession();%0A
         Query qu
@@ -1786,25 +1786,24 @@
 oupId);%0A
-%0A
         
 return (
@@ -1798,16 +1798,8 @@
     
-return (
 List
@@ -1806,17 +1806,23 @@
 %3CIEvent%3E
-)
+ list =
  query.l
@@ -1836,79 +1836,365 @@
     
-%7D%0A%0A    public List%3CIEvent%3E getEventByUserGroupId(Integer userGroupId)%7B%0A
+    session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();%0A%0A        return list;%0A    %7D%0A%0A    public List%3CIEvent%3E getEventByUserGroupId(Integer userGroupId)%7B%0A%0A        Session session = HibernateSessionFactory.currentSession();%0A        session.clear();%0A        session.flush();%0A        HibernateSessionFactory.closeSession();
 %0A   
@@ -2257,32 +2257,100 @@
 Long eventId)%7B%0A%0A
+        Session session = HibernateSessionFactory.currentSession();%0A
         IEvent e
@@ -2483,32 +2483,143 @@
 update(event);%0A%0A
+            session.clear();%0A            session.flush();%0A            HibernateSessionFactory.closeSession();%0A%0A
             retu
@@ -2735,20 +2735,23 @@
     
-return false
+session.clear()
 ;%0A  
@@ -2760,48 +2760,33 @@
     
-%7D%0A
     
-%7D%0A%0A    protected void finalize()%7B%0A
+session.flush();%0A    
     
@@ -2825,20 +2825,58 @@
 eSession();%0A
+%0A            return false;%0A        %7D%0A%0A
     %7D%0A%7D%0A

update EventDAO session