Skip to content

Commit

Permalink
#349 Add notification list for user
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Jul 26, 2017
1 parent 034bdcb commit f50074a
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.support.project.knowledge.entity.NotifyConfigsEntity;
import org.support.project.web.boundary.ForwardBoundary;
import org.support.project.web.common.HttpUtil;
import org.support.project.web.logic.NotificationLogic;
import org.support.project.web.logic.SanitizingLogic;

@DI(instance = Instance.Prototype)
Expand All @@ -30,6 +31,7 @@ public abstract class Control extends org.support.project.web.control.Control {
public static final String MSG_SUCCESS = "NOTIFY_MSG_SUCCESS";
public static final String MSG_WARN = "NOTIFY_MSG_WARN";
public static final String MSG_ERROR = "NOTIFY_MSG_ERROR";
public static final String NOTIFY_UNREAD_COUNT = "NOTIFY_UNREAD_COUNT";

private List<String> infos = null;
private List<String> successes = null;
Expand All @@ -48,6 +50,12 @@ public void setRequest(HttpServletRequest request) {
request.setAttribute(MSG_SUCCESS, successes);
request.setAttribute(MSG_WARN, warns);
request.setAttribute(MSG_ERROR, errors);

// 通知の件数を取得
if (getLoginedUser() != null) {
int count = NotificationLogic.get().countUnRead(getLoginUserId());
request.setAttribute(NOTIFY_UNREAD_COUNT, count);
}
}

protected String getResource(String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.support.project.knowledge.control.protect;

import java.util.List;

import org.support.project.knowledge.control.Control;
import org.support.project.knowledge.logic.NotificationLogic;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.control.service.Get;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.exception.InvalidParamException;

public class NotificationControl extends Control {

@Get
public Boundary list() throws InvalidParamException {
int offset = getPathInteger(0);
List<NotificationsEntity> notifications = NotificationLogic.get().getNotification(getLoginUserId(), offset);

for (NotificationsEntity notificationsEntity : notifications) {
NotificationLogic.get().convNotification(notificationsEntity, getLoginedUser(), NotificationLogic.TARGET.list);
}

setAttribute("notifications", notifications);

int previous = offset - 1;
if (previous < 0) {
previous = 0;
}
setAttribute("offset", offset);
setAttribute("previous", previous);
setAttribute("next", offset + 1);

return forward("list.jsp");
}



}
17 changes: 9 additions & 8 deletions src/main/java/org/support/project/knowledge/logic/MailLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@
import org.support.project.web.dao.MailConfigsDao;
import org.support.project.web.dao.MailsDao;
import org.support.project.web.dao.SystemConfigsDao;
import org.support.project.web.dao.UserNotificationsDao;
import org.support.project.web.dao.UsersDao;
import org.support.project.web.entity.ConfirmMailChangesEntity;
import org.support.project.web.entity.GroupsEntity;
import org.support.project.web.entity.MailConfigsEntity;
import org.support.project.web.entity.MailsEntity;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.entity.PasswordResetsEntity;
import org.support.project.web.entity.ProvisionalRegistrationsEntity;
import org.support.project.web.entity.SystemConfigsEntity;
import org.support.project.web.entity.UserNotificationsEntity;
import org.support.project.web.entity.UsersEntity;

import net.arnx.jsonic.JSON;
Expand Down Expand Up @@ -1037,14 +1040,6 @@ private void notifyPublicKnowledgeUpdate(NotifyQueuesEntity notifyQueuesEntity,
*/
private void notifyKnowledgeUpdateToUsers(NotifyQueuesEntity notifyQueuesEntity, KnowledgesEntity knowledge, List<UsersEntity> users)
throws Exception {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
LOG.info("mail config is not exists.");
return;
}

StringBuilder content = new StringBuilder();
// テンプレートの種類をセット
TemplateMastersEntity templateMaster = TemplateMastersDao.get().selectWithItems(knowledge.getTypeId());
Expand All @@ -1066,7 +1061,10 @@ private void notifyKnowledgeUpdateToUsers(NotifyQueuesEntity notifyQueuesEntity,
}
content.append(knowledge.getContent());

NotificationsEntity notification = NotificationLogic.get().insertNotificationOnKnowledgeUpdate(knowledge);

for (UsersEntity usersEntity : users) {
NotificationLogic.get().insertUserNotification(notification, usersEntity);
if (!StringUtils.isEmailAddress(usersEntity.getMailAddress())) {
// 送信先のメールアドレスが不正なのでこのユーザにはメール送信しない
LOG.warn("mail targget [" + usersEntity.getMailAddress() + "] is wrong.");
Expand All @@ -1082,10 +1080,13 @@ private void notifyKnowledgeUpdateToUsers(NotifyQueuesEntity notifyQueuesEntity,
} else {
template = load(locale, MailLogic.NOTIFY_UPDATE_KNOWLEDGE);
}
// メール送信
insertNotifyKnowledgeUpdateMailQue(knowledge, usersEntity, template, content.toString());
}
}



/**
* 記事の追加・更新のWebhookの登録を行う
* @param comment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.support.project.knowledge.logic;

import org.support.project.common.util.StringUtils;
import org.support.project.di.Container;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.vo.notification.KnowledgeUpdate;
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.entity.NotificationsEntity;
import org.support.project.web.entity.UserNotificationsEntity;
import org.support.project.web.entity.UsersEntity;

import net.arnx.jsonic.JSON;

public class NotificationLogic extends org.support.project.web.logic.NotificationLogic {
public static enum TARGET {
list, detail
}

/**
* Get instance
* @return instance
*/
public static NotificationLogic get() {
return Container.getComp(NotificationLogic.class);
}

/**
* ユーザに通知をセット
* @param notification
* @param usersEntity
*/
public void insertUserNotification(NotificationsEntity notification, UsersEntity usersEntity) {
UserNotificationsEntity userNotification = new UserNotificationsEntity(notification.getNo(), usersEntity.getUserId());
userNotification.setStatus(NotificationLogic.STATUS_UNREAD);
UserNotificationsDao.get().insert(userNotification);
}

/**
* Knowledgeの登録/更新時の通知情報を作成
* @param knowledge
* @return
*/
public NotificationsEntity insertNotificationOnKnowledgeUpdate(KnowledgesEntity knowledge) {
// 通知情報を作成
NotificationsEntity notification = new NotificationsEntity();
if (knowledge.getNotifyStatus() == null || knowledge.getNotifyStatus().intValue() == 0) {
notification.setTitle(MailLogic.NOTIFY_INSERT_KNOWLEDGE);
} else {
notification.setTitle(MailLogic.NOTIFY_UPDATE_KNOWLEDGE);
}
KnowledgeUpdate update = new KnowledgeUpdate();
update.setKnowledgeId(knowledge.getKnowledgeId());
update.setKnowledgeTitle(knowledge.getTitle());
update.setUpdateUser(knowledge.getUpdateUserName());
notification.setContent(JSON.encode(update));
notification = NotificationsDao.get().insert(notification);
return notification;
}


/**
* ユーザへの通知を意味のある形へ変換
* @param notificationsEntity
* @param loginedUser
*/
public void convNotification(NotificationsEntity notificationsEntity, LoginedUser loginedUser, TARGET target) {
String category = notificationsEntity.getTitle();
if (MailLogic.NOTIFY_INSERT_KNOWLEDGE.equals(category) || MailLogic.NOTIFY_UPDATE_KNOWLEDGE.equals(category)) {
KnowledgeUpdate update = JSON.decode(notificationsEntity.getContent(), KnowledgeUpdate.class);
MailLocaleTemplatesEntity template = MailLogic.get().load(loginedUser.getLocale(), MailLogic.NOTIFY_INSERT_KNOWLEDGE);

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

if (target == TARGET.detail) {
String contents = template.getContent();
contents = contents.replace("{KnowledgeId}", String.valueOf(update.getKnowledgeId()));
contents = contents.replace("{KnowledgeTitle}", update.getKnowledgeTitle());
contents = contents.replace("{User}", update.getUpdateUser());
contents = contents.replace("{URL}", NotifyLogic.get().makeURL(update.getKnowledgeId()));
KnowledgesEntity entity = KnowledgeLogic.get().select(update.getKnowledgeId(), loginedUser);
if (entity != null) {
contents = contents.replace("{Contents}", entity.getContent());
} else {
contents = contents.replace("{Contents}", "");
}
notificationsEntity.setContent(contents);
}
}

}




}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.NumberUtils;
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;
Expand All @@ -23,16 +24,21 @@
import org.support.project.knowledge.entity.KnowledgeUsersEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.LikesEntity;
import org.support.project.knowledge.entity.MailLocaleTemplatesEntity;
import org.support.project.knowledge.entity.NotifyConfigsEntity;
import org.support.project.knowledge.entity.NotifyQueuesEntity;
import org.support.project.knowledge.vo.Notify;
import org.support.project.knowledge.vo.notification.KnowledgeUpdate;
import org.support.project.knowledge.websocket.NotifyAction;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.bean.MessageResult;
import org.support.project.web.dao.SystemConfigsDao;
import org.support.project.web.entity.GroupsEntity;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.entity.SystemConfigsEntity;
import org.support.project.web.entity.UsersEntity;

import net.arnx.jsonic.JSON;
/**
* 通知を処理するロジック
* @author Koda
Expand Down Expand Up @@ -328,5 +334,7 @@ protected boolean contains(List<UsersEntity> users, UsersEntity groupUser) {
return false;
}




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

public class KnowledgeUpdate {
private long KnowledgeId;
private String KnowledgeTitle;
private String updateUser;
/**
* @return the knowledgeId
*/
public long getKnowledgeId() {
return KnowledgeId;
}
/**
* @param knowledgeId the knowledgeId to set
*/
public void setKnowledgeId(long knowledgeId) {
KnowledgeId = knowledgeId;
}
/**
* @return the knowledgeTitle
*/
public String getKnowledgeTitle() {
return KnowledgeTitle;
}
/**
* @param knowledgeTitle the knowledgeTitle to set
*/
public void setKnowledgeTitle(String knowledgeTitle) {
KnowledgeTitle = knowledgeTitle;
}
/**
* @return the updateUser
*/
public String getUpdateUser() {
return updateUser;
}
/**
* @param updateUser the updateUser to set
*/
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
}
1 change: 1 addition & 0 deletions src/main/resources/appresource.properties
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,4 @@ knowledge.token.label.update=Update expiration date
knowledge.token.label.delete=Delete Token
knowledge.token.msg.copy=Copied

knowledge.notification.title=Notifications
3 changes: 3 additions & 0 deletions src/main/resources/appresource_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,6 @@ knowledge.token.label.create=Tokenを生成する
knowledge.token.label.update=有効期限を更新する
knowledge.token.label.delete=Tokenを削除する
knowledge.token.msg.copy=コピーしました

knowledge.notification.title=通知

8 changes: 8 additions & 0 deletions src/main/webapp/WEB-INF/views/commons/layout/commonNavbar.jsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%@page import="org.support.project.knowledge.control.Control"%>
<%@page import="org.support.project.knowledge.entity.ServiceConfigsEntity"%>
<%@page pageEncoding="UTF-8" isELIgnored="false" session="false" errorPage="/WEB-INF/views/commons/errors/jsp_error.jsp"%>

Expand Down Expand Up @@ -131,6 +132,7 @@
<div class="btn-group">
<a href="#" class="btn btn-success" id="navMenuButtonLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<img src="<%= request.getContextPath()%>/open.account/icon/<%= jspUtil.id() %>" alt="icon" width="15" height="15"/>
<span class="badge"><%= jspUtil.out(Control.NOTIFY_UNREAD_COUNT) %></span>
</a>
<a href="#" class="btn btn-success dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
Expand All @@ -142,6 +144,12 @@
</a>
</li>
<li class="divider"></li>
<li >
<a href="<%= request.getContextPath() %>/protect.notification/list" >
<i class="fa fa-bullhorn"></i>&nbsp;<%= jspUtil.label("knowledge.notification.title") %>
</a>
</li>
<li class="divider"></li>
<li >
<a href="<%= request.getContextPath() %>/open.knowledge/search" >
<i class="fa fa-search"></i>&nbsp;<%= jspUtil.label("knowledge.navbar.search") %>
Expand Down
Loading

0 comments on commit f50074a

Please sign in to comment.