package com.ourlinc.helloworld.model;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.ourlinc.helloworld.service.impl.ActivityPodi;
import com.ourlinc.helloworld.util.WebUtils;
import com.ourlinc.omni.persistence.PersistenceAbstract;
import com.ourlinc.omni.persistence.Persister;
import com.ourlinc.swift.exception.UnsupportedException;
import com.ourlinc.swift.util.Array;
import com.ourlinc.swift.util.UniteId;
/**
* 业务对象活动类
*
* @author lipeiying
*
*/
public class Activity extends PersistenceAbstract<ActivityPodi>
implements
Comparable<Activity> {
public final static int STATUS_CANCEL = 0;// 取消状态
public final static int STATUS_AVAILIABLE = 1;// 可用状态
@Resource(type = String.class)
private UniteId m_Id = UniteId.nil;// 联合ID
@Resource
private String m_Title;// 活动标题
@Resource
private Date m_Time;// 活动时间
@Resource
private String m_Place;// 活动地点
@Resource
private String m_Content;// 活动内容
@Resource
private String m_Note;// 备注
@Resource
private Date m_StartDate = new Date();// 报名开始时间,防止出现空指针已成
@Resource
private Date m_OverDate = new Date();// 报名截止时间
@Resource
private Date m_ReleaseDate = new Date();// 发布时间
@Resource
private int m_Status = STATUS_AVAILIABLE;// 活动状态,默认为可用
@Resource
private Array<String> m_Members = new Array<String>();// 参加者列表,记录id
/**
* 构造函数
*
* @param podi
*/
public Activity(ActivityPodi podi) {
super(podi);
markUpdate();
}
public Activity(ActivityPodi podi, String title, Date time, String place,
String content, Date startDate, Date overDate, String note) {
super(podi);
m_Title = title;
m_Time = time;
m_Place = place;
m_Content = content;
m_Note = note;
m_StartDate = startDate;
m_OverDate = overDate;
m_ReleaseDate = new Date();
markUpdate();
}
/**
* 用户报名参加活动
*
* @param username
*/
public void addUser(String id) {
if (null != m_Members && !m_Members.contains(id)) {
m_Members.add(id);
markUpdate();
}
}
/**
* 用户取消参加
*
* @param username
*/
public void removeUser(String userId) {
m_Members.remove(userId);
markUpdate();
}
/**
* 取消活动
*/
public void cancel() {
m_Status = STATUS_CANCEL;
markUpdate();
}
/**
* 修改活动明细
*
* @param title
* @param time
* @param place
* @param content
* @param note
* @param startDate
* @param overTime
* @param release
*/
public void edit(String title, Date time, String place, String content,
String note, Date startDate, Date overDate) {
m_Title = title;
m_Time = time;
m_Place = place;
m_Content = content;
m_Note = note;
m_StartDate = startDate;
m_OverDate = overDate;
m_ReleaseDate = new Date();
markUpdate();
}
// get方法开始
public String getId() {
return m_Id.toString();
}
public String getTitle() {
return m_Title;
}
/**
* 返回活动时间
*
* @return
*/
public Date getTime() {
return m_Time;
}
public String getPlace() {
return m_Place;
}
public String getContent() {
return m_Content;
}
public String getNote() {
return m_Note;
}
/**
* 返回活动报名开始时间
*
* @return
*/
public Date getStartDate() {
return m_StartDate;
}
/**
* 返回活动报名截止时间
*
* @return
*/
public Date getOverDate() {
return m_OverDate;
}
/**
* 返回发布活动时的时间
*
* @return
*/
public Date getReleaseDate() {
return m_ReleaseDate;
}
/**
* 返回活动的状态,为已取消,可用
*
* @return
*/
public int getStatus() {
return m_Status;
}
/**
*
* @return
*/
public String getStatusString() {
if (STATUS_CANCEL == m_Status) {
return "已取消";
}
Date now = new Date();
if (now.after(m_StartDate) && now.before(m_OverDate)) {
return "报名中.....";
} else if (now.after(m_OverDate) && now.before(m_Time)) {
return "已截止";
} else if (now.after(m_Time)) {
return "已结束";
} else {
return "未开始报名";
}
}
public List<User> getMembers() {
List<User> result = new ArrayList<User>();
for (String id : m_Members) {
User user = getPodi().getUserById(id);
if (null != user) {
result.add(user);
}
}
return result;
}
/**
* 返回参加此活动的人数
*
* @return
*/
public int getMembersCount() {
return m_Members.size();
}
/**
* 返回报名开始时间的字符串表示,用yyyy-MM-dd HH:mm格式表示
*
* @return
*/
public String getStartDateString() {
return WebUtils.dateAndTimeFormatToStr(m_StartDate);
}
/**
* 返回报名截止时间的字符串表示,用yyyy-MM-dd HH:mm格式表示
*
* @return
*/
public String getOverDateString() {
return WebUtils.dateAndTimeFormatToStr(m_OverDate);
}
/**
* 查看活动是什么时候发布的,用yyyy-MM-dd HH:mm格式表示
*
* @return
*/
public String getReleaseDateString() {
return WebUtils.dateAndTimeFormatToStr(m_ReleaseDate);
}
/**
* 返回活动时间的字符串表示,用yyyy-MM-dd HH:mm格式表示
*
* @return
*/
public String getTimeString() {
return WebUtils.dateAndTimeFormatToStr(m_Time);
}
/**
* 返回参加该活动的人员邮件地址
*
* @return
*/
public List<String> listMembersEmail() {
List<String> result = new ArrayList<String>();
for (String id : m_Members) {
User user = getPodi().getUserById(id);
if (null != user) {
result.add(user.getEmail());
}
}
return result;
}
/**
* 这个活动对用户来说能不能报名,只有在报名阶段并且没有取消的活动用户才能报名
*
* @return
*/
public boolean getJoinAuthority() {
return canEditByUser();
}
/**
* 管理员有没有权限修改活动,已经发生或取消的活动不能修改
*
* @return
*/
public boolean getEditAuthority() {
return canEditByAdmin();
}
// /**
// * 这个活动管理员能不能帮用户报名,只有未开始并且没有取消的活动才能报名
// *
// * @return
// */
// public boolean canJoinForUserByAdmin() {
// return canEditByAdmin();
// }
/**
* 这个活动对管理员来说能不能修改,已经发生或取消的活动不能修改
*
* @return
*/
public boolean canEditByAdmin() {
return (STATUS_AVAILIABLE == m_Status && new Date().before(m_Time))
? true
: false;
}
/**
* 这个活动对用户来说能不能操作,只有处于报名中并且没有被取消的活动才能操作
*
* @return
*/
public boolean canEditByUser() {
return (new Date().before(m_OverDate) && new Date().after(m_StartDate) && STATUS_AVAILIABLE == m_Status)
? true
: false;
}
/**
* 返回id的URL编码
*
* @return
*/
public String getURLEncodeForId() {
String result = "";
try {
result = URLEncoder.encode(getId(), "utf-8");
} catch (UnsupportedEncodingException e) {
}
return result;
}
/**
* 发表评论
*
* @param act
*/
public Comment postComment(User user, String content) {
return getPodi().postComment(this, user, content);
}
// get方法结束
protected void genUniteId() {
if (UniteId.nil != m_Id) {
throw new UnsupportedException("不能重复初始化ID:" + m_Id);
}
m_Id = UniteId.valueOf(getPodi().getPersister(getClass()).getNewId());
setPersistenceState(STATE_REINDEX | STATE_NEW);
}
protected void initPersistenceId(Persister<?> arg0) {
genUniteId();
}
public String getPersistenceId() {
return getId();
}
public int compareTo(Activity act) {
return m_Time.after(act.m_Time) ? -1 : 1;
}
}