Skip to content

Commit

Permalink
#349 Add notification feature for user added
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Aug 8, 2017
1 parent 4cd61a4 commit a200f3a
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public Boundary save() {
String type = getParam("userAddType");
String notify = getParam("userAddNotify");
// メール送信の場合、メールの設定が完了しているかチェック
if ((type != null && type.equals(SystemConfig.USER_ADD_TYPE_VALUE_MAIL))
|| (notify != null && notify.equals(SystemConfig.USER_ADD_NOTIFY_ON))) {
if (type != null && type.equals(SystemConfig.USER_ADD_TYPE_VALUE_MAIL)) {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.support.project.knowledge.config.SystemConfig;
import org.support.project.knowledge.control.Control;
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.notification.AcceptCheckUserNotification;
import org.support.project.knowledge.logic.notification.AddUserNotification;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
Expand Down Expand Up @@ -116,8 +118,7 @@ public Boundary save() {
// 仮登録を行う
ProvisionalRegistrationsEntity entity = addProvisionalRegistration();
// 管理者へメール通知
MailLogic mailLogic = MailLogic.get();
mailLogic.sendNotifyAcceptUser(entity);
AcceptCheckUserNotification.get().sendNotifyAcceptUser(entity);

return forward("provisional_registration.jsp");
}
Expand Down Expand Up @@ -154,9 +155,8 @@ private void addUser() {
user = UserLogic.get().insert(user, roles);
setAttributeOnProperty(user);

// 管理者へユーザが追加されたことを通知
MailLogic mailLogic = MailLogic.get();
mailLogic.sendNotifyAddUser(user);
// 管理者にユーザが追加されたことを通知
AddUserNotification.get().sendNotifyAddUser(user);

// ログイン処理
AuthenticationLogic<LoginedUser> logic = Container.getComp(DefaultAuthenticationLogicImpl.class);
Expand Down Expand Up @@ -221,9 +221,8 @@ public Boundary activate() {
UsersEntity user = UserLogic.get().activate(entity);
// 管理者へユーザが追加されたことを通知
if (user != null) {
MailLogic mailLogic = MailLogic.get();
mailLogic.sendNotifyAddUser(user);

// 管理者にユーザが追加されたことを通知
AddUserNotification.get().sendNotifyAddUser(user);
}
// ログイン処理
AuthenticationLogic<LoginedUser> logic = Container.getComp(DefaultAuthenticationLogicImpl.class);
Expand Down
127 changes: 1 addition & 126 deletions src/main/java/org/support/project/knowledge/logic/MailLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -48,18 +46,15 @@
import org.support.project.knowledge.parser.ParserFactory;
import org.support.project.knowledge.vo.ParseResult;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.config.WebConfig;
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.UsersDao;
import org.support.project.web.entity.ConfirmMailChangesEntity;
import org.support.project.web.entity.MailConfigsEntity;
import org.support.project.web.entity.MailsEntity;
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.UsersEntity;

@DI(instance = Instance.Singleton)
public class MailLogic {
Expand Down Expand Up @@ -110,13 +105,6 @@ public class MailLogic {

// private static final DateFormat DAY_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
private static final String MAIL_CONFIG_DIR = "/org/support/project/knowledge/mail/";


/** date format */
private static DateFormat getDayFormat() {
return new SimpleDateFormat("yyyyMMddHHmmss");
}


public static MailLogic get() {
return Container.getComp(MailLogic.class);
Expand Down Expand Up @@ -328,7 +316,6 @@ private void mailSend(Session session, MailConfigsEntity config, MailsEntity ent

/**
* メール送信のIDを生成
*
* @param label 11桁まで
* @return
*/
Expand All @@ -342,7 +329,6 @@ public String idGen(String label) {
return builder.toString();
}


/**
* URLを生成
*
Expand Down Expand Up @@ -448,102 +434,7 @@ public void sendAcceptedAddRequest(ProvisionalRegistrationsEntity entity, String
}

/**
* ユーザ登録通知
*
* @param user
*/
public void sendNotifyAddUser(UsersEntity user) {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(org.support.project.knowledge.config.AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
return;
}

LOG.trace("sendNotifyAddUser");
SystemConfigsDao configsDao = SystemConfigsDao.get();
SystemConfigsEntity configsEntity = configsDao.selectOnKey(SystemConfig.USER_ADD_NOTIFY, AppConfig.get().getSystemName());
if (configsEntity != null && SystemConfig.USER_ADD_NOTIFY_ON.equals(configsEntity.getConfigValue())) {
// 管理者へのメール通知がONなので、メールを送信する
UsersDao usersDao = UsersDao.get();
List<UsersEntity> users = usersDao.selectOnRoleKey(WebConfig.ROLE_ADMIN);
for (UsersEntity entity : users) {
if (!StringUtils.isEmailAddress(entity.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + entity.getMailAddress() + "] is wrong.");
continue;
}
Locale locale = entity.getLocale();
MailLocaleTemplatesEntity mailConfig = load(locale, NOTIFY_ADD_USER);

String contents = mailConfig.getContent();
contents = contents.replace("{UserId}", String.valueOf(user.getUserId()));
contents = contents.replace("{UserName}", user.getUserName());
contents = contents.replace("{UserMail}", user.getMailAddress());
String title = mailConfig.getTitle();

MailsDao mailsDao = MailsDao.get();
MailsEntity mailsEntity = new MailsEntity();
String mailId = idGen("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(entity.getMailAddress());
mailsEntity.setToName(entity.getUserName());
mailsEntity.setTitle(title);
mailsEntity.setContent(contents);
mailsDao.insert(mailsEntity);
}
}
}

/**
* ユーザの仮登録通知
*
* @param entity
*/
public void sendNotifyAcceptUser(ProvisionalRegistrationsEntity registrationsEntity) {
MailConfigsDao mailConfigsDao = MailConfigsDao.get();
MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(org.support.project.knowledge.config.AppConfig.get().getSystemName());
if (mailConfigsEntity == null) {
// メールの設定が登録されていなければ、送信処理は終了
return;
}

LOG.trace("sendNotifyAcceptUser");
SystemConfigsDao configsDao = SystemConfigsDao.get();
SystemConfigsEntity configsEntity = configsDao.selectOnKey(SystemConfig.USER_ADD_NOTIFY, AppConfig.get().getSystemName());
if (configsEntity != null && SystemConfig.USER_ADD_NOTIFY_ON.equals(configsEntity.getConfigValue())) {
// 管理者へのメール通知がONなので、メールを送信する
UsersDao usersDao = UsersDao.get();
List<UsersEntity> users = usersDao.selectOnRoleKey(WebConfig.ROLE_ADMIN);
for (UsersEntity entity : users) {
if (!StringUtils.isEmailAddress(entity.getMailAddress())) {
// 送信先のメールアドレスが不正なので、送信処理は終了
LOG.warn("mail targget [" + entity.getMailAddress() + "] is wrong.");
continue;
}
Locale locale = entity.getLocale();
MailLocaleTemplatesEntity mailConfig = load(locale, NOTIFY_ACCEPT_USER);

String contents = mailConfig.getContent();
String title = mailConfig.getTitle();

MailsDao mailsDao = MailsDao.get();
MailsEntity mailsEntity = new MailsEntity();
String mailId = idGen("Notify");
mailsEntity.setMailId(mailId);
mailsEntity.setStatus(MailLogic.MAIL_STATUS_UNSENT);
mailsEntity.setToAddress(entity.getMailAddress());
mailsEntity.setToName(entity.getUserName());
mailsEntity.setTitle(title);
mailsEntity.setContent(contents);
mailsDao.insert(mailsEntity);
}
}
}

/**
* メール変更のリクエストを受付
* パスワード変更のリクエストを受付
*
* @param email
* @param locale
Expand Down Expand Up @@ -651,22 +542,6 @@ public String getMailContent(String content) throws Exception {
return content;
}

/**
* メール送信のIDを生成
* @param string
* @return
*/
protected String idGenu(String label) {
StringBuilder builder = new StringBuilder();
builder.append(label);
builder.append("-");
builder.append(getDayFormat().format(new Date()));
builder.append("-");
builder.append(UUID.randomUUID().toString());
return builder.toString();
}



/**
* メールのテンプレート設定を取得
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

import org.support.project.di.Container;
import org.support.project.knowledge.logic.notification.AcceptCheckUserNotification;
import org.support.project.knowledge.logic.notification.AddUserNotification;
import org.support.project.knowledge.logic.notification.CommentInsertNotification;
import org.support.project.knowledge.logic.notification.EventNotificationByWeek;
import org.support.project.knowledge.logic.notification.KnowledgeUpdateNotification;
Expand Down Expand Up @@ -58,6 +60,10 @@ public Notification getNotification(String category) {
return ParticipateChangeStatusForParticipantNotification.get();
} else if (MailLogic.NOTIFY_REMOVE_PARTICIPATE.equals(category)) {
return ParticipateRemoveForSponsorNotification.get();
} else if (MailLogic.NOTIFY_ADD_USER.equals(category)) {
return AddUserNotification.get();
} else if (MailLogic.NOTIFY_ACCEPT_USER.equals(category)) {
return AcceptCheckUserNotification.get();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package org.support.project.knowledge.logic.notification;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public abstract class AbstractNotification {
/** date format */
private static DateFormat getDayFormat() {
return new SimpleDateFormat("yyyyMMddHHmmss");
}
import org.support.project.knowledge.logic.MailLogic;
import org.support.project.knowledge.logic.NotificationLogic;
import org.support.project.web.dao.UserNotificationsDao;
import org.support.project.web.entity.NotificationsEntity;
import org.support.project.web.entity.UserNotificationsEntity;

public abstract class AbstractNotification implements Notification {
/**
* メール送信のIDを生成
* @param string
* @return
*/
protected String idGen(String label) {
StringBuilder builder = new StringBuilder();
builder.append(label);
builder.append("-");
builder.append(getDayFormat().format(new Date()));
builder.append("-");
builder.append(UUID.randomUUID().toString());
return builder.toString();
return MailLogic.get().idGen(label);
}

/**
* 指定の通知をユーザに紐付け
* @param notification
* @param userId
*/
protected void insertUserNotification(NotificationsEntity notification, int userId) {
UserNotificationsEntity userNotification = new UserNotificationsEntity(notification.getNo(), userId);
userNotification.setStatus(NotificationLogic.STATUS_UNREAD);
UserNotificationsDao.get().insert(userNotification);
}

}
Loading

0 comments on commit a200f3a

Please sign in to comment.