Skip to content

Commit

Permalink
#349 Add notification feature for event information
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Jul 28, 2017
1 parent 390137c commit d949683
Show file tree
Hide file tree
Showing 15 changed files with 493 additions and 217 deletions.
210 changes: 10 additions & 200 deletions src/main/java/org/support/project/knowledge/logic/MailEventLogic.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
package org.support.project.knowledge.logic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import org.support.project.aop.Aspect;
import org.support.project.common.config.Resources;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.StringUtils;
import org.support.project.di.Container;
import org.support.project.di.DI;
import org.support.project.di.Instance;
import org.support.project.knowledge.config.AppConfig;
import org.support.project.knowledge.dao.EventsDao;
import org.support.project.knowledge.dao.KnowledgeItemValuesDao;
import org.support.project.knowledge.dao.KnowledgesDao;
import org.support.project.knowledge.dao.ParticipantsDao;
import org.support.project.knowledge.dao.StockKnowledgesDao;
import org.support.project.knowledge.entity.EventsEntity;
import org.support.project.knowledge.entity.KnowledgeItemValuesEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.entity.ParticipantsEntity;
import org.support.project.knowledge.entity.StockKnowledgesEntity;
import org.support.project.knowledge.logic.notification.EventNotificationByDate;
import org.support.project.knowledge.logic.notification.EventNotificationByMinute;
import org.support.project.knowledge.logic.notification.EventNotificationByWeek;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.dao.MailConfigsDao;
import org.support.project.web.dao.MailsDao;
import org.support.project.web.dao.UsersDao;
import org.support.project.web.entity.MailConfigsEntity;
import org.support.project.web.entity.MailsEntity;
import org.support.project.web.entity.UsersEntity;

@DI(instance = Instance.Singleton)
public class MailEventLogic extends MailLogic {
/** ログ */
private static final Log LOG = LogFactory.getLog(MailEventLogic.class);

public static MailEventLogic get() {
return Container.getComp(MailEventLogic.class);
}

public static final int NOTIFY_STATUS_NOT_SEND = 0;
/** 今週のイベント通知(日曜に通知) */
Expand All @@ -49,25 +41,11 @@ public class MailEventLogic extends MailLogic {
/** ステータス:ストック中 */
public static final int STATUS_STOCKED = 100;


public static MailEventLogic get() {
return Container.getComp(MailEventLogic.class);
}


/**
* 登録されているイベントの開催通知
*/
@Aspect(advice = org.support.project.ormapping.transaction.Transaction.class)
public void notifyEvents() {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
LOG.info("mail config is not exists.");
return;
}

// 登録されているイベントを一週間分取得
// タイムゾーンを考慮して取得するため、少し範囲広めで取得する。
// GMT で先週の土曜〜次週の月曜
Expand Down Expand Up @@ -104,179 +82,11 @@ public void notifyEvents() {
continue;
}
List<KnowledgeItemValuesEntity> values = KnowledgeItemValuesDao.get().selectOnKnowledgeId(event.getKnowledgeId());
notifyMin(event, knowledge, values, now, mailConfigsEntity);
notifyDate(event, knowledge, values, now, mailConfigsEntity);
notifyWeek(event, knowledge, values, now, mailConfigsEntity);
}

}

private void notifyMin(EventsEntity event, KnowledgesEntity knowledge, List<KnowledgeItemValuesEntity> values,
Calendar now, MailConfigsEntity mailConfigsEntity) {
int timing = NOTIFY_STATUS_BEFORE_MIN;
if (event.getNotifyStatus() >= timing) {
// 既に通知済
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("開始までの時間: " + (event.getStartDateTime().getTime() - now.getTimeInMillis()));
}
// 30分を切った?
if (event.getStartDateTime().getTime() <= now.getTimeInMillis() ||
event.getStartDateTime().getTime() - now.getTimeInMillis() > (1000 * 60 * 30)) {
return;
}
LOG.info("イベントのメール通知 [ID] " + knowledge.getKnowledgeId() + " [Timing] 30分前");
sendNotify(event, knowledge, values, timing);
}

private void notifyDate(EventsEntity event, KnowledgesEntity knowledge, List<KnowledgeItemValuesEntity> values,
Calendar now, MailConfigsEntity mailConfigsEntity) {
int timing = NOTIFY_STATUS_BEFORE_DATE;
if (event.getNotifyStatus() >= timing) {
// 既に通知済
return;
}
// 今日?
TimeZone timezone = TimeZone.getTimeZone(event.getTimeZone());
Calendar today = Calendar.getInstance(timezone);
today.setTimeInMillis(now.getTimeInMillis());

Calendar eventDay = Calendar.getInstance(timezone);
eventDay.setTimeInMillis(event.getStartDateTime().getTime());
if (today.get(Calendar.YEAR) != eventDay.get(Calendar.YEAR) ||
today.get(Calendar.MONTH) != eventDay.get(Calendar.MONTH) ||
today.get(Calendar.DATE) != eventDay.get(Calendar.DATE)) {
// イベント当日以外は通知しない
return;
}
LOG.info("イベントのメール通知 [ID] " + knowledge.getKnowledgeId() + " [Timing] 本日開催");
sendNotify(event, knowledge, values, timing);
}

private void notifyWeek(EventsEntity event, KnowledgesEntity knowledge, List<KnowledgeItemValuesEntity> values,
Calendar now, MailConfigsEntity mailConfigsEntity) {
int timing = NOTIFY_STATUS_BEFORE_WEEK;
if (event.getNotifyStatus() >= timing) {
// 既に通知済
return;
}
//日曜日?(イベントのタイムゾーンで)
TimeZone timezone = TimeZone.getTimeZone(event.getTimeZone());
Calendar today = Calendar.getInstance(timezone);
today.setTimeInMillis(now.getTimeInMillis());
if (today.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
// 日曜日でない場合は通知しない
return;
}
LOG.info("イベントのメール通知 [ID] " + knowledge.getKnowledgeId() + " [Timing] 今週開催");
sendNotify(event, knowledge, values, timing);
}


private void sendNotify(EventsEntity event, KnowledgesEntity knowledge, List<KnowledgeItemValuesEntity> values, int timing) {
List<Integer> userIds = new ArrayList<>();
// 参加者に通知
List<ParticipantsEntity> participants = ParticipantsDao.get().selectOnKnowledgeId(event.getKnowledgeId());
for (ParticipantsEntity participant : participants) {
if (!userIds.contains(participant.getUserId())) {
UsersEntity user = UsersDao.get().selectOnKey(participant.getUserId());
if (user != null) {
this.sendNotifyEvent(event, knowledge, values, user, participant.getStatus(), timing);
userIds.add(participant.getUserId());
}
}
}
// ストックに入れているユーザに通知
List<StockKnowledgesEntity> stocks = StockKnowledgesDao.get().selectOnKnowledgeId(event.getKnowledgeId());
for (StockKnowledgesEntity stockKnowledgesEntity : stocks) {
if (!userIds.contains(stockKnowledgesEntity.getInsertUser())) {
UsersEntity user = UsersDao.get().selectOnKey(stockKnowledgesEntity.getInsertUser());
if (user != null) {
this.sendNotifyEvent(event, knowledge, values, user, STATUS_STOCKED, timing);
userIds.add(stockKnowledgesEntity.getInsertUser());
}
}
}
event.setNotifyStatus(timing);
EventsDao.get().save(event);
}


private void sendNotifyEvent(EventsEntity event, KnowledgesEntity knowledge, List<KnowledgeItemValuesEntity> values,
UsersEntity user, int status, int timing) {
if (!StringUtils.isEmailAddress(user.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + user.getMailAddress() + "] is wrong.");
return;
}
MailLocaleTemplatesEntity template = load(user.getLocale(), MailLogic.NOTIFY_EVENT);

String date = "";
String start = "";
String end = "";
String place = "";
for (KnowledgeItemValuesEntity item : values) {
if (item.getItemNo() == EventsLogic.ITEM_NO_DATE) {
date = item.getItemValue();
} else if (item.getItemNo() == EventsLogic.ITEM_NO_START) {
start = item.getItemValue();
} else if (item.getItemNo() == EventsLogic.ITEM_NO_END) {
end = item.getItemValue();
} else if (item.getItemNo() == EventsLogic.ITEM_NO_PLACE) {
place = item.getItemValue();
}
}
Resources resources = Resources.getInstance(user.getLocale());
String statusLabel = "";
if (status == EventsLogic.STATUS_PARTICIPATION) {
statusLabel = resources.getResource("knowledge.event.label.status.participation");
} else if (status == EventsLogic.STATUS_WAIT_CANSEL) {
statusLabel = resources.getResource("knowledge.event.label.status.wait.cansel");
} else if (status == STATUS_STOCKED) {
statusLabel = resources.getResource("knowledge.event.label.status.stocked");
}
String timingLabel = "";
if (timing == NOTIFY_STATUS_BEFORE_WEEK) {
timingLabel = resources.getResource("knowledge.event.label.timing.week");
} else if (timing == NOTIFY_STATUS_BEFORE_DATE) {
timingLabel = resources.getResource("knowledge.event.label.timing.tody");
} else if (timing == NOTIFY_STATUS_BEFORE_MIN) {
timingLabel = resources.getResource("knowledge.event.label.timing.soon");

EventNotificationByMinute.get().notify(event, knowledge, values, now);
EventNotificationByDate.get().notify(event, knowledge, values, now);
EventNotificationByWeek.get().notify(event, knowledge, values, now);
}

MailsEntity mailsEntity = new MailsEntity();
String mailId = idGenu("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(user.getMailAddress());
mailsEntity.setToName(user.getUserName());

String title = template.getTitle();
title = title.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(knowledge.getTitle(), 80));
title = title.replace("{Date}", date);
title = title.replace("{Start}", start);
title = title.replace("{End}", end);
title = title.replace("{Place}", place);
mailsEntity.setTitle(title);

String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
contents = contents.replace("{KnowledgeTitle}", StringUtils.abbreviate(knowledge.getTitle(), 80));
contents = contents.replace("{Date}", date);
contents = contents.replace("{Start}", start);
contents = contents.replace("{End}", end);
contents = contents.replace("{Place}", place);
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(knowledge.getKnowledgeId()));
contents = contents.replace("{Status}", statusLabel);
contents = contents.replace("{Timing}", timingLabel);
contents = contents.replace("{UserName}", user.getUserName());
mailsEntity.setContent(contents);
MailsDao.get().insert(mailsEntity);
}




}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.support.project.knowledge.logic;

import java.util.Iterator;
import java.util.List;

import org.support.project.di.Container;
import org.support.project.knowledge.logic.notification.CommentInsertNotification;
import org.support.project.knowledge.logic.notification.EventNotificationByWeek;
import org.support.project.knowledge.logic.notification.KnowledgeUpdateNotification;
import org.support.project.knowledge.logic.notification.LikeInsertNotification;
import org.support.project.knowledge.logic.notification.Notification;
Expand Down Expand Up @@ -42,16 +44,23 @@ public Notification getNotification(String category) {
return CommentInsertNotification.get();
} else if (MailLogic.NOTIFY_INSERT_LIKE_MYITEM.equals(category)) {
return LikeInsertNotification.get();
} else if (MailLogic.NOTIFY_EVENT.equals(category)) {
return EventNotificationByWeek.get();
}
return null;
}


public List<NotificationsEntity> getNotification(LoginedUser loginedUser, int offset) {
List<NotificationsEntity> notifications = super.getNotification(loginedUser.getUserId(), offset);
for (NotificationsEntity notificationsEntity : notifications) {
for (Iterator<NotificationsEntity> iterator = notifications.iterator(); iterator.hasNext();) {
NotificationsEntity notificationsEntity = (NotificationsEntity) iterator.next();
Notification notification = getNotification(notificationsEntity.getTitle());
notification.convNotification(notificationsEntity, loginedUser, Notification.TARGET.list);
if (notification == null) {
iterator.remove();
} else {
notification.convNotification(notificationsEntity, loginedUser, Notification.TARGET.list);
}
}
return notifications;
}
Expand Down
Loading

0 comments on commit d949683

Please sign in to comment.