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 Aug 8, 2017
1 parent d949683 commit 1eb8766
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.support.project.knowledge.entity.KnowledgeItemValuesEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.ParticipantsEntity;
import org.support.project.knowledge.logic.notification.ParticipateForParticipantNotification;
import org.support.project.knowledge.logic.notification.ParticipateForSponsorNotification;
import org.support.project.knowledge.vo.Participation;
import org.support.project.knowledge.vo.Participations;
import org.support.project.web.bean.LoginedUser;
Expand Down Expand Up @@ -154,10 +156,9 @@ public Boolean participation(Long knowledgeId, Integer userId) {
ParticipantsDao.get().insert(participant);

// 開催者(=登録者)へメール通知
MailLogic.get().notifyAddParticipateForSponsor(knowledgeId, userId, participant.getStatus());

ParticipateForSponsorNotification.get().notify(knowledgeId, userId, participant.getStatus());
// 参加者へメール通知
MailLogic.get().notifyAddParticipateForParticipant(knowledgeId, userId, participant.getStatus());
ParticipateForParticipantNotification.get().notify(knowledgeId, userId, participant.getStatus());

return participant.getStatus().intValue() == STATUS_PARTICIPATION;
}
Expand Down
121 changes: 0 additions & 121 deletions src/main/java/org/support/project/knowledge/logic/MailLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,127 +756,6 @@ public void initialize(String templateId) {
MailLocaleTemplatesDao.get().save(ja);
}

/**
* 参加登録があったことを、開催者へ通知
* @param knowledgeId
* @param userId
* @param status
*/
public void notifyAddParticipateForSponsor(Long knowledgeId, Integer userId, Integer status) {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
LOG.info("mail config is not exists.");
return;
}
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKey(knowledgeId);
if (knowledge == null) {
LOG.info("knowledge [" + knowledgeId + "] is not exists.");
return;
}
// 開催者
UsersEntity sponsor = UsersDao.get().selectOnKey(knowledge.getInsertUser());
// 参加者
UsersEntity participant = UsersDao.get().selectOnKey(userId);
if (sponsor == null || participant == null) {
LOG.warn("sponsor or participant is not exist.");
return;
}
if (!StringUtils.isEmailAddress(sponsor.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + sponsor.getMailAddress() + "] is wrong.");
return;
}

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

MailLocaleTemplatesEntity template = load(sponsor.getLocale(), NOTIFY_ADD_PARTICIPATE);
String title = template.getTitle();
title = title.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(knowledge.getTitle(), 80));
mailsEntity.setTitle(title);
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
contents = contents.replace("{KnowledgeTitle}", knowledge.getTitle());
StringBuilder builder = new StringBuilder();
Resources resources = Resources.getInstance(sponsor.getLocale());
builder.append(participant.getUserName());
if (status == EventsLogic.STATUS_PARTICIPATION) {
builder.append("[").append(resources.getResource("knowledge.view.label.status.participation")).append("]");
} else {
builder.append("[").append(resources.getResource("knowledge.view.label.status.wait.cansel")).append("]");
}
contents = contents.replace("{Participant}", builder.toString());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(knowledge.getKnowledgeId()));

mailsEntity.setContent(contents);
MailsDao.get().insert(mailsEntity);
}
/**
* 参加登録したことを、参加者へ通知
* @param knowledgeId
* @param userId
* @param status
*/
public void notifyAddParticipateForParticipant(Long knowledgeId, Integer userId, Integer status) {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
LOG.info("mail config is not exists.");
return;
}
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKey(knowledgeId);
if (knowledge == null) {
LOG.info("knowledge [" + knowledgeId + "] is not exists.");
return;
}
// 参加者
UsersEntity participant = UsersDao.get().selectOnKey(userId);
if (participant == null) {
LOG.warn("sponsor or participant is not exist.");
return;
}
if (!StringUtils.isEmailAddress(participant.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + participant.getMailAddress() + "] is wrong.");
return;
}

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

MailLocaleTemplatesEntity template = load(participant.getLocale(), NOTIFY_REGISTRATION_EVENT);
String title = template.getTitle();
title = title.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(knowledge.getTitle(), 80));
mailsEntity.setTitle(title);
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
contents = contents.replace("{KnowledgeTitle}", knowledge.getTitle());
StringBuilder builder = new StringBuilder();
Resources resources = Resources.getInstance(participant.getLocale());
if (status == EventsLogic.STATUS_PARTICIPATION) {
builder.append(resources.getResource("knowledge.view.label.status.participation"));
} else {
builder.append(resources.getResource("knowledge.view.label.status.wait.cansel"));
}
contents = contents.replace("{Status}", builder.toString());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(knowledge.getKnowledgeId()));

mailsEntity.setContent(contents);
MailsDao.get().insert(mailsEntity);
}
/**
* 参加キャンセルがあったことを、開催者へ通知
* @param knowledgeId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.support.project.knowledge.logic.notification.LikeInsertNotification;
import org.support.project.knowledge.logic.notification.Notification;
import org.support.project.knowledge.logic.notification.Notification.TARGET;
import org.support.project.knowledge.logic.notification.ParticipateForParticipantNotification;
import org.support.project.knowledge.logic.notification.ParticipateForSponsorNotification;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.dao.NotificationsDao;
import org.support.project.web.dao.UserNotificationsDao;
Expand Down Expand Up @@ -46,6 +48,10 @@ public Notification getNotification(String category) {
return LikeInsertNotification.get();
} else if (MailLogic.NOTIFY_EVENT.equals(category)) {
return EventNotificationByWeek.get();
} else if (MailLogic.NOTIFY_REGISTRATION_EVENT.equals(category)) {
return ParticipateForParticipantNotification.get();
} else if (MailLogic.NOTIFY_ADD_PARTICIPATE.equals(category)) {
return ParticipateForSponsorNotification.get();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.support.project.knowledge.logic.notification;

import org.support.project.common.config.Resources;
import org.support.project.common.util.StringUtils;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.logic.EventsLogic;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.NotificationLogic;
import org.support.project.knowledge.logic.NotifyLogic;
import org.support.project.knowledge.vo.notification.Participate;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.dao.NotificationsDao;
import org.support.project.web.dao.UserNotificationsDao;
import org.support.project.web.dao.UsersDao;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.entity.UserNotificationsEntity;
import org.support.project.web.entity.UsersEntity;

import net.arnx.jsonic.JSON;

public abstract class AbstractParticipateNotification extends AbstractNotification implements ParticipateNotification {


protected void insertNotification(KnowledgesEntity knowledge, Integer participant, Integer status, String template) {
NotificationsEntity notification = new NotificationsEntity();
notification.setTitle(template);

Participate info = new Participate();
info.setKnowledgeId(knowledge.getKnowledgeId());
info.setKnowledgeTitle(knowledge.getTitle());
info.setUpdateUser(knowledge.getUpdateUserName());

info.setParticipant(participant);
info.setStatus(status);

notification.setContent(JSON.encode(info));
notification = NotificationsDao.get().insert(notification);

if (template.equals(MailLogic.NOTIFY_ADD_PARTICIPATE)) {
// 参加登録があったことを、開催者へ通知
UserNotificationsEntity userNotification = new UserNotificationsEntity(notification.getNo(), knowledge.getInsertUser());
userNotification.setStatus(NotificationLogic.STATUS_UNREAD);
UserNotificationsDao.get().insert(userNotification);
} else {
// 参加登録したことを、参加者へ通知
UserNotificationsEntity userNotification = new UserNotificationsEntity(notification.getNo(), participant);
userNotification.setStatus(NotificationLogic.STATUS_UNREAD);
UserNotificationsDao.get().insert(userNotification);
}
}


@Override
public void convNotification(NotificationsEntity notificationsEntity, LoginedUser loginedUser, TARGET target) {
Participate info = JSON.decode(notificationsEntity.getContent(), Participate.class);
String templateString = notificationsEntity.getTitle();
MailLocaleTemplatesEntity template = MailLogic.get().load(loginedUser.getLocale(), notificationsEntity.getTitle());

String title = template.getTitle();
title = title.replace("{KnowledgeId}", String.valueOf(info.getKnowledgeId()));
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(info.getKnowledgeTitle(), 80));
notificationsEntity.setTitle(title);

if (target == TARGET.detail) {
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", String.valueOf(info.getKnowledgeId()));
contents = contents.replace("{KnowledgeTitle}", info.getKnowledgeTitle());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(info.getKnowledgeId()));
UsersEntity participant = UsersDao.get().selectOnKey(info.getParticipant());
if (participant != null) {
Resources resources = Resources.getInstance(loginedUser.getLocale());
StringBuilder builder = new StringBuilder();
int status = info.getStatus();
if (MailLogic.NOTIFY_ADD_PARTICIPATE.equals(templateString)) {
// 参加登録があったことを、開催者へ通知
builder.append(participant.getUserName());
if (status == EventsLogic.STATUS_PARTICIPATION) {
builder.append("[").append(resources.getResource("knowledge.view.label.status.participation")).append("]");
} else {
builder.append("[").append(resources.getResource("knowledge.view.label.status.wait.cansel")).append("]");
}
contents = contents.replace("{Participant}", builder.toString());
} else {
// 参加登録したことを、参加者へ通知
if (status == EventsLogic.STATUS_PARTICIPATION) {
builder.append(resources.getResource("knowledge.view.label.status.participation"));
} else {
builder.append(resources.getResource("knowledge.view.label.status.wait.cansel"));
}
contents = contents.replace("{Status}", builder.toString());
}
}
notificationsEntity.setContent(contents);
}
}


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

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.KnowledgesDao;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.logic.EventsLogic;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.NotifyLogic;
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;

/**
* 参加登録したことを、参加者へ通知
* @author koda
*/
@DI(instance = Instance.Singleton)
public class ParticipateForParticipantNotification extends AbstractParticipateNotification{
/** ログ */
private static final Log LOG = LogFactory.getLog(ParticipateForParticipantNotification.class);
/** インスタンス取得 */
public static ParticipateForParticipantNotification get() {
return Container.getComp(ParticipateForParticipantNotification.class);
}

@Override
public void notify(Long knowledgeId, Integer userId, Integer status) {
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKey(knowledgeId);
if (knowledge == null) {
LOG.info("knowledge [" + knowledgeId + "] is not exists.");
return;
}
// 参加者
UsersEntity participant = UsersDao.get().selectOnKey(userId);
if (participant == null) {
LOG.warn("sponsor or participant is not exist.");
return;
}

// 通知情報生成
insertNotification(knowledge, userId, status, MailLogic.NOTIFY_REGISTRATION_EVENT);

MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
LOG.info("mail config is not exists.");
return;
}
if (!StringUtils.isEmailAddress(participant.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + participant.getMailAddress() + "] is wrong.");
return;
}

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

MailLocaleTemplatesEntity template = MailLogic.get().load(participant.getLocale(), MailLogic.NOTIFY_REGISTRATION_EVENT);
String title = template.getTitle();
title = title.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
title = title.replace("{KnowledgeTitle}", StringUtils.abbreviate(knowledge.getTitle(), 80));
mailsEntity.setTitle(title);
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", knowledge.getKnowledgeId().toString());
contents = contents.replace("{KnowledgeTitle}", knowledge.getTitle());
StringBuilder builder = new StringBuilder();
Resources resources = Resources.getInstance(participant.getLocale());
if (status == EventsLogic.STATUS_PARTICIPATION) {
builder.append(resources.getResource("knowledge.view.label.status.participation"));
} else {
builder.append(resources.getResource("knowledge.view.label.status.wait.cansel"));
}
contents = contents.replace("{Status}", builder.toString());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(knowledge.getKnowledgeId()));

mailsEntity.setContent(contents);
MailsDao.get().insert(mailsEntity);
}

}
Loading

0 comments on commit 1eb8766

Please sign in to comment.