package cn.updev.Action.Event;

import cn.updev.Events.Event.EventDAO;
import cn.updev.Events.Event.EventFactory;
import cn.updev.Events.Static.EventWeight;
import cn.updev.Events.Static.IEvent;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;

/**
 * Created by hypo on 15-11-19.
 */
public class EventAction extends ActionSupport {

    private Long eventId;
    private String eventTitle;
    private Date expectTime;
    private Integer weight;
    private Integer groupId;

    public String execute() throws Exception {

        if(this.eventTitle == null || this.eventTitle.trim().length() == 0){
            return INPUT;
        }

        // 获得Session下的已经登录用户的ID
        int ownerId = 1;

        //权重
        EventWeight eventWeight = EventWeight.values()[this.weight];

        //创建事件
        EventFactory factory = new EventFactory(this.eventTitle, this.expectTime, ownerId, eventWeight);
        IEvent event = factory.getEvent();

        if(event != null){
            return SUCCESS;
        }

        return ERROR;
    }

    public String eventFinish(){

        if(this.eventId == null || this.eventId < 1){
            return ERROR;
        }

        IEvent event = new EventDAO().getEventById(this.eventId);
        this.groupId = event.getGroupId();
        if(this.groupId == null){
            this.groupId = 0;
        }


        if(event.isFinish()){
            event.setFinishTime(null);
        }else {
            event.setFinishTime(new Date());
        }

        EventFactory factory = new EventFactory();
        factory.update(event);

        return SUCCESS;
    }

    public String addEventToGroup(){

        if(this.groupId == null || this.groupId < 1){
            return ERROR;
        }

        if(this.eventTitle == null || this.eventTitle.trim().length() == 0){
            return SUCCESS;
        }

        // 获得Session下的已经登录用户的ID
        int ownerId = 1;

        //权重
        EventWeight eventWeight = EventWeight.values()[this.weight];

        //创建事件
        EventFactory factory = new EventFactory(this.eventTitle, this.expectTime, ownerId, eventWeight, groupId);
        IEvent event = factory.getEvent();

        if(event != null){
            return SUCCESS;
        }

        return ERROR;
    }

    public String updateEvent(){

        if(this.eventId == null || this.eventId < 1){
            return ERROR;
        }

        EventDAO dao = new EventDAO();
        IEvent event = dao.getEventById(eventId);

        if(event == null){
            return ERROR;
        }

        this.groupId = event.getGroupId();
        if(this.groupId == null){
            this.groupId = 0;
        }

        event.setEventTitle(this.eventTitle);
        event.setExpectTime(this.expectTime);
        event.setWeight(EventWeight.values()[this.weight]);

        EventFactory factory = new EventFactory();
        factory.update(event);

        return SUCCESS;
    }

    public String delEvent(){

        if(this.eventId == null || this.eventId < 1){
            return ERROR;
        }

        EventDAO dao = new EventDAO();
        IEvent event = dao.getEventById(eventId);

        if(event == null){
            return ERROR;
        }

        this.groupId = event.getGroupId();
        if(this.groupId == null){
            this.groupId = 0;
        }

        EventFactory factory = new EventFactory();
        factory.delete(event);

        return SUCCESS;
    }

    public Long getEventId() {
        return eventId;
    }

    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }

    public String getEventTitle() {
        return eventTitle;
    }

    public void setEventTitle(String eventTitle) {
        this.eventTitle = eventTitle;
    }

    public Date getExpectTime() {
        return expectTime;
    }

    public void setExpectTime(Date expectTime) {
        this.expectTime = expectTime;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public Integer getGroupId() {
        return groupId;
    }
}

@@ -188,16 +188,65 @@
 IEvent;%0A
+import cn.updev.Users.Static.FuctionClass.Login;%0A
 import c
@@ -283,16 +283,16 @@
 upport;%0A
-
 %0Aimport 
@@ -586,24 +586,130 @@
 xception %7B%0A%0A
+        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D%0A%0A
         if(t
@@ -856,33 +856,72 @@
   int ownerId = 
-1
+new Login().getLoginedUser().getUserId()
 ;%0A%0A        //%E6%9D%83%E9%87%8D%0A
@@ -1279,24 +1279,130 @@
 tFinish()%7B%0A%0A
+        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D%0A%0A
         if(t
@@ -1954,24 +1954,130 @@
 ToGroup()%7B%0A%0A
+        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D%0A%0A
         if(t
@@ -2325,17 +2325,56 @@
 nerId = 
-1
+new Login().getLoginedUser().getUserId()
 ;%0A%0A     
@@ -2745,32 +2745,138 @@
 updateEvent()%7B%0A%0A
+        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D%0A%0A
         if(this.
@@ -3481,32 +3481,32 @@
 SUCCESS;%0A    %7D%0A%0A
-
     public Strin
@@ -3512,32 +3512,138 @@
 ng delEvent()%7B%0A%0A
+        Login login = new Login();%0A        if(login.isNotLogined())%7B%0A            return LOGIN;%0A        %7D%0A%0A
         if(this.

update EventAction