diff --git a/config/code_format_on_eclipse.xml b/config/code_format_on_eclipse.xml new file mode 100644 index 000000000..709bda3de --- /dev/null +++ b/config/code_format_on_eclipse.xml @@ -0,0 +1,295 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 170af0c39..43cb61e07 100644 --- a/pom.xml +++ b/pom.xml @@ -1,214 +1,247 @@ - 4.0.0 - org.support-project - knowledge - war - 1.2.1 - webapp for knowledge - https://support-project.org/ - - - UTF-8 - - - - - org.support-project - web - 1.2.0 - - - - org.support-project - markedj - 1.0.7-SNAPSHOT - - - - com.h2database - h2 - 1.4.183 - - - - javax - javaee-web-api - 7.0 - provided - - - javax.servlet - javax.servlet-api - 3.1.0 - provided - - - javax.websocket - javax.websocket-api - 1.1 - provided - - - javax.servlet - jstl - 1.2 - - - - org.apache.lucene - lucene-core - 4.10.2 - - - org.apache.lucene - lucene-analyzers-common - 4.10.2 - - - org.apache.lucene - lucene-analyzers-kuromoji - 4.10.2 - - - org.apache.lucene - lucene-queryparser - 4.10.2 - - - org.apache.lucene - lucene-highlighter - 4.10.2 - - - - org.apache.tika - tika-parsers - 1.9 - - - org.ow2.asm - asm-debug-all - - - - - - javax.mail - mail - 1.4.7 - - - - org.postgresql - postgresql - 9.3-1103-jdbc41 - - - - com.googlecode.java-diff-utils - diffutils - 1.2.1 - - - - org.pegdown - pegdown - 1.5.0 - - - - org.apache.httpcomponents - httpclient - 4.3.1 - - - - - - - private - https://support-project.org/nexus/content/repositories/public/ - - - - - - knowledge - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - UTF-8 - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-war-plugin - 2.6 - - false - - - - - - - - - - - - org.apache.maven.plugins - maven-project-info-reports-plugin - 2.8.1 - - false - - - - - org.codehaus.mojo - findbugs-maven-plugin - 3.0.3 - - true - true - - - - - org.apache.maven.plugins - maven-pmd-plugin - 3.5 - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 2.17 - - - - checkstyle - - - - - config/stylecheck.xml - UTF-8 - true - true - false - - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.support-project + knowledge + 1.3.0 + war + + knowledge + https://github.com/support-project/knowledge + Free Knowledge Base System + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + scm:git:https://github.com/support-project/knowledge.git + scm:git:https://github.com/support-project/knowledge.git + https://github.com/support-project/knowledge + + + GitHub Issues + https://github.com/support-project/knowledge/issues + + + + + Masaru Koda + koda.masaru3_at_gmail.com + support-project.org + https://support-project.org/ + + + + + UTF-8 + + + + + sonatype.oss.snapshots + Sonatype OSS Snapshot Repository + http://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + + org.support-project + web + 1.3.0-SNAPSHOT + + + + org.support-project + markedj + 1.0.7-SNAPSHOT + + + + com.h2database + h2 + 1.4.183 + + + + javax + javaee-web-api + 7.0 + provided + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + javax.websocket + javax.websocket-api + 1.1 + provided + + + javax.servlet + jstl + 1.2 + + + + org.apache.lucene + lucene-core + 4.10.2 + + + org.apache.lucene + lucene-analyzers-common + 4.10.2 + + + org.apache.lucene + lucene-analyzers-kuromoji + 4.10.2 + + + org.apache.lucene + lucene-queryparser + 4.10.2 + + + org.apache.lucene + lucene-highlighter + 4.10.2 + + + + org.apache.tika + tika-parsers + 1.9 + + + org.ow2.asm + asm-debug-all + + + + + + javax.mail + mail + 1.4.7 + + + + org.postgresql + postgresql + 9.3-1103-jdbc41 + + + + com.googlecode.java-diff-utils + diffutils + 1.2.1 + + + + org.pegdown + pegdown + 1.5.0 + + + + org.apache.httpcomponents + httpclient + 4.3.1 + + + + + + knowledge + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + 2.6 + + false + + + + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.8.1 + + false + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.3 + + true + true + + + + + org.apache.maven.plugins + maven-pmd-plugin + 3.5 + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + + checkstyle + + + + + config/stylecheck.xml + UTF-8 + true + true + false + + + + + diff --git a/src/main/java/org/support/project/knowledge/bat/NotifyMailBat.java b/src/main/java/org/support/project/knowledge/bat/NotifyMailBat.java index ff7d31475..1d8c9b3a0 100644 --- a/src/main/java/org/support/project/knowledge/bat/NotifyMailBat.java +++ b/src/main/java/org/support/project/knowledge/bat/NotifyMailBat.java @@ -18,6 +18,7 @@ import org.support.project.common.util.StringUtils; import org.support.project.knowledge.config.AppConfig; import org.support.project.knowledge.config.MailConfig; +import org.support.project.knowledge.config.NotifyType; import org.support.project.knowledge.dao.CommentsDao; import org.support.project.knowledge.dao.ExUsersDao; import org.support.project.knowledge.dao.KnowledgesDao; @@ -35,6 +36,7 @@ import org.support.project.knowledge.entity.WebhookConfigsEntity; import org.support.project.knowledge.entity.WebhooksEntity; import org.support.project.knowledge.logic.KnowledgeLogic; +import org.support.project.knowledge.logic.NotifyCommentLogic; import org.support.project.knowledge.logic.NotifyLogic; import org.support.project.knowledge.logic.WebhookLogic; import org.support.project.knowledge.vo.GroupUser; @@ -49,18 +51,30 @@ import net.arnx.jsonic.JSON; +/** + * メッセージ処理を処理する定期的なバッチプログラム + * @author Koda + */ public class NotifyMailBat extends AbstractBat { /** ログ */ - private static Log LOG = LogFactory.getLog(NotifyMailBat.class); - + private static final Log LOG = LogFactory.getLog(NotifyMailBat.class); + /** config dir for mail */ private static final String MAIL_CONFIG_DIR = "/org/support/project/knowledge/mail/"; - private static final DateFormat getDayFormat() { + /** date format */ + private static DateFormat getDayFormat() { return new SimpleDateFormat("yyyyMMddHHmmss"); } + /** knowledge id what is sended */ private List sendedCommentKnowledgeIds = new ArrayList<>(); + /** like id what is sended */ private List sendedLikeKnowledgeIds = new ArrayList<>(); + /** + * バッチ処理の開始 + * @param args + * @throws Exception + */ public static void main(String[] args) throws Exception { initLogName("NotifyMailBat.log"); configInit(ClassUtils.getShortClassName(NotifyMailBat.class)); @@ -136,12 +150,20 @@ private void notifyLikeInsert(NotifyQueuesEntity notifyQueuesEntity) { if (notifyConfigsEntity != null && INT_FLAG.flagCheck(notifyConfigsEntity.getMyItemLike())) { // 登録者でかつイイネが登録した場合に通知が欲しい Locale locale = user.getLocale(); - MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_like_myitem", locale, MailConfig.class);; + MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_like_myitem", locale, MailConfig.class); sendLikeMail(like, knowledge, likeUser, user, config); } } } - + + /** + * イイネが押されたメールを送る + * @param like + * @param knowledge + * @param likeUser + * @param user + * @param config + */ private void sendLikeMail(LikesEntity like, KnowledgesEntity knowledge, UsersEntity likeUser, UsersEntity user, MailConfig config) { MailConfigsDao mailConfigsDao = MailConfigsDao.get(); MailConfigsEntity mailConfigsEntity = mailConfigsDao.selectOnKey(AppConfig.get().getSystemName()); @@ -220,55 +242,18 @@ private void notifyCommentInsert(NotifyQueuesEntity notifyQueuesEntity) { UsersEntity commentUser = usersDao.selectOnKey(comment.getInsertUser()); // 登録者に通知 - UsersEntity user = usersDao.selectOnKey(knowledge.getInsertUser()); + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); if (user != null) { - NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get(); - NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(user.getUserId()); - if (notifyConfigsEntity != null && INT_FLAG.flagCheck(notifyConfigsEntity.getMyItemComment())) { - // 登録者でかつコメントが登録した場合に通知が欲しい - Locale locale = user.getLocale(); - MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_comment_myitem", locale, MailConfig.class);; - sendCommentMail(comment, knowledge, commentUser, user, config); - } + Locale locale = user.getLocale(); + MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_comment_myitem", locale, MailConfig.class); + sendCommentMail(comment, knowledge, commentUser, user, config); } - // 宛先のナレッジにコメント追加で通知が欲しいユーザに通知 - List users = new ArrayList<>(); - // 宛先の一覧取得 - TargetsDao targetsDao = TargetsDao.get(); - List targetUsers = targetsDao.selectUsersOnKnowledgeId(knowledge.getKnowledgeId()); - users.addAll(targetUsers); - - //グループの一覧 - List targetGroups = targetsDao.selectGroupsOnKnowledgeId(knowledge.getKnowledgeId()); - for (GroupsEntity groupsEntity : targetGroups) { - List groupUsers = ExUsersDao.get().selectGroupUser(groupsEntity.getGroupId(), 0, Integer.MAX_VALUE); - for (GroupUser groupUser : groupUsers) { - if (!contains(users, groupUser)) { - users.add(groupUser); - } - } - } - Iterator iterator = users.iterator(); - while (iterator.hasNext()) { - UsersEntity usersEntity = (UsersEntity) iterator.next(); - // 自分宛てのナレッジ登録/更新で通知するかどうかの判定 - NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get(); - NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(usersEntity.getUserId()); - if (notifyConfigsEntity == null) { - iterator.remove(); - } else if (!INT_FLAG.flagCheck(notifyConfigsEntity.getToItemComment())) { - iterator.remove(); - } else if (user.getUserId().intValue() == knowledge.getInsertUser().intValue()) { - // 登録者には通知しない(登録者に通知する/しないは、上のロジックで処理済) - iterator.remove(); - } - } - + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); for (UsersEntity target : users) { // 宛先にメール送信 Locale locale = target.getLocale(); - MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_comment", locale, MailConfig.class);; + MailConfig config = LocaleConfigLoader.load(MAIL_CONFIG_DIR, "notify_insert_comment", locale, MailConfig.class); sendCommentMail(comment, knowledge, commentUser, target, config); } } @@ -418,7 +403,7 @@ private void notifyProtectKnowledgeUpdate(NotifyQueuesEntity notifyQueuesEntity, * @param groupUser * @return */ - private boolean contains(List users, GroupUser groupUser) { + private boolean contains(List users, UsersEntity groupUser) { for (UsersEntity usersEntity : users) { if (usersEntity.equalsOnKey(groupUser)) { return true; diff --git a/src/main/java/org/support/project/knowledge/config/NotifyType.java b/src/main/java/org/support/project/knowledge/config/NotifyType.java new file mode 100644 index 000000000..02663f8c3 --- /dev/null +++ b/src/main/java/org/support/project/knowledge/config/NotifyType.java @@ -0,0 +1,13 @@ +package org.support.project.knowledge.config; + +/** + * Type of Notify + * @author Koda + * + */ +public enum NotifyType { + /** Type of Notify: Desktop */ + Desktop, + /** Type of Notify: mail */ + Mail +} diff --git a/src/main/java/org/support/project/knowledge/dao/NotifyConfigsDao.java b/src/main/java/org/support/project/knowledge/dao/NotifyConfigsDao.java index 329cac490..4b4ff41cb 100644 --- a/src/main/java/org/support/project/knowledge/dao/NotifyConfigsDao.java +++ b/src/main/java/org/support/project/knowledge/dao/NotifyConfigsDao.java @@ -1,47 +1,73 @@ package org.support.project.knowledge.dao; +import java.util.List; + import org.support.project.di.Container; import org.support.project.di.DI; import org.support.project.di.Instance; - import org.support.project.knowledge.dao.gen.GenNotifyConfigsDao; +import org.support.project.ormapping.common.SQLManager; +import org.support.project.web.entity.UsersEntity; /** * 通知設定 */ -@DI(instance=Instance.Singleton) +@DI(instance = Instance.Singleton) public class NotifyConfigsDao extends GenNotifyConfigsDao { - /** SerialVersion */ - private static final long serialVersionUID = 1L; - - /** - * ID - */ - private int currentId = 0; - - /** - * インスタンス取得 - * AOPに対応 - * @return インスタンス - */ - public static NotifyConfigsDao get() { - return Container.getComp(NotifyConfigsDao.class); - } - - /** - * IDを採番 - * ※コミットしなくても次のIDを採番する為、保存しなければ欠番になる - */ - public Integer getNextId() { - String sql = "SELECT MAX(USER_ID) FROM NOTIFY_CONFIGS;"; - Integer integer = executeQuerySingle(sql, Integer.class); - if (integer != null && currentId < integer) { - currentId = integer; - } - currentId++; - return currentId; - } + /** SerialVersion */ + private static final long serialVersionUID = 1L; + + /** + * ID + */ + private int currentId = 0; + + /** + * インスタンス取得 AOPに対応 + * + * @return インスタンス + */ + public static NotifyConfigsDao get() { + return Container.getComp(NotifyConfigsDao.class); + } + /** + * IDを採番 ※コミットしなくても次のIDを採番する為、保存しなければ欠番になる + */ + public Integer getNextId() { + String sql = "SELECT MAX(USER_ID) FROM NOTIFY_CONFIGS;"; + Integer integer = executeQuerySingle(sql, Integer.class); + if (integer != null && currentId < integer) { + currentId = integer; + } + currentId++; + return currentId; + } + + /** + * 「公開」のナレッジにコメントが追加された際に、「デスクトップ通知」が欲しいユーザの一覧を取得 + * @param limit + * @param offset + * @return + */ + public List getNotifyDesktopUsersOnPublicComment(int limit, int offset) { + String sql = SQLManager.getInstance().getSql( + "/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyDesktopUsersOnPublicComment.sql"); + return executeQueryList(sql, UsersEntity.class, limit, offset); + } + /** + * 「公開」のナレッジにコメントが追加された際に、「メール通知」が欲しいユーザの一覧を取得 + * @param limit + * @param offset + * @return + */ + public List getNotifyMailUsersOnPublicComment(int limit, int offset) { + String sql = SQLManager.getInstance().getSql( + "/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyMailUsersOnPublicComment.sql"); + return executeQueryList(sql, UsersEntity.class, limit, offset); + } + + } diff --git a/src/main/java/org/support/project/knowledge/logic/KnowledgeLogic.java b/src/main/java/org/support/project/knowledge/logic/KnowledgeLogic.java index 7801435c8..be02fc327 100644 --- a/src/main/java/org/support/project/knowledge/logic/KnowledgeLogic.java +++ b/src/main/java/org/support/project/knowledge/logic/KnowledgeLogic.java @@ -3,7 +3,6 @@ import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -58,1259 +57,1276 @@ import org.support.project.web.bean.LoginedUser; import org.support.project.web.entity.GroupsEntity; -@DI(instance=Instance.Singleton) +/** + * Logic class for knowledge data + * @author Koda + */ +@DI(instance = Instance.Singleton) public class KnowledgeLogic { - /** ログ */ - private static Log LOG = LogFactory.getLog(KnowledgeLogic.class); - - public static final int ALL_USER = 0; - - public static final int PUBLIC_FLAG_PUBLIC = 0; - public static final int PUBLIC_FLAG_PRIVATE = 1; - public static final int PUBLIC_FLAG_PROTECT = 2; - - public static final int TEMPLATE_TYPE_KNOWLEDGE = -100; - public static final int TEMPLATE_TYPE_BOOKMARK = -99; - - public static final int TYPE_KNOWLEDGE = IndexType.knowledge.getValue(); - public static final int TYPE_FILE = IndexType.KnowledgeFile.getValue(); - public static final int TYPE_COMMENT = IndexType.KnowledgeComment.getValue(); - - public static final String COMMENT_ID_PREFIX = "COMMENT-"; - - public static KnowledgeLogic get() { - return Container.getComp(KnowledgeLogic.class); - } - - private KnowledgesDao knowledgesDao = Container.getComp(KnowledgesDao.class); - private KnowledgeUsersDao knowledgeUsersDao = KnowledgeUsersDao.get(); - private TagsDao tagsDao = TagsDao.get(); - private KnowledgeTagsDao knowledgeTagsDao = KnowledgeTagsDao.get(); - private UploadedFileLogic fileLogic = UploadedFileLogic.get(); - - /** - * タグの文字列(カンマ区切り)から、登録済のタグであれば、それを取得し、 - * 存在しないものであれば、新たにタグを生成してタグの情報を取得 - * @param tags - * @return - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public List manegeTags(String tags) { - List tagList = new ArrayList<>(); - if (StringUtils.isEmpty(tags)) { - return tagList; - } - String[] splits; - if (tags.indexOf(",") != -1) { - splits = tags.split(","); - } else { - splits = new String[1]; - splits[0] = tags; - } - - for (String tag : splits) { - tag = tag.trim(); - if (tag.startsWith(" ")) { - tag = tag.substring(" ".length()); - } - if (tag.startsWith(" ")) { - tag = tag.substring(" ".length()); - } - - - TagsEntity tagsEntity = tagsDao.selectOnTagName(tag); - if (tagsEntity == null) { - tagsEntity = new TagsEntity(); - tagsEntity.setTagName(tag); - tagsEntity = tagsDao.insert(tagsEntity); - LOG.debug("Tag added." + PropertyUtil.reflectionToString(tagsEntity)); - } - tagList.add(tagsEntity); - } - return tagList; - } - - /** - * ナレッジを登録 - * @param entity - * @param tags - * @param fileNos - * @param targets - * @param editors - * @param template - * @param loginedUser - * @return - * @throws Exception - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public KnowledgesEntity insert(KnowledgesEntity entity, List tags, List fileNos, - List targets, List editors, TemplateMastersEntity template, - LoginedUser loginedUser) throws Exception { - // ナレッジを登録 - KnowledgesEntity insertedEntity = knowledgesDao.insert(entity); - // アクセス権を登録 - saveAccessUser(insertedEntity, loginedUser, targets); - // 編集権を登録 - saveEditorsUser(insertedEntity, loginedUser, editors); - // タグを登録 - setTags(insertedEntity, tags); - // 添付ファイルを更新(紐付けをセット) - fileLogic.setKnowledgeFiles(insertedEntity.getKnowledgeId(), fileNos, loginedUser); - // 拡張項目の保存 - saveTemplateItemValue(insertedEntity.getKnowledgeId(), template, loginedUser); - - // 全文検索エンジンへ登録 - saveIndex(insertedEntity, tags, targets, template, loginedUser.getUserId()); - // 一覧表示用の情報を更新 - updateKnowledgeExInfo(insertedEntity); - // 履歴登録 - insertHistory(insertedEntity); - // 通知(TODO 別スレッド化を検討) - NotifyLogic.get().notifyOnKnowledgeInsert(insertedEntity); - - return insertedEntity; - } - - - - /** - * ナレッジを更新 - * @param entity - * @param fileNos - * @param targets - * @param editors - * @param template - * @param loginedUser - * @return - * @throws Exception - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public KnowledgesEntity update(KnowledgesEntity entity, List tags, List fileNos, - List targets, List editors, TemplateMastersEntity template, - LoginedUser loginedUser) throws Exception { - // ナレッッジを更新 - KnowledgesEntity updatedEntity = knowledgesDao.update(entity); - // ユーザのアクセス権を解除 - knowledgeUsersDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); - // グループとナレッジのヒモ付を解除 - GroupLogic groupLogic = GroupLogic.get(); - groupLogic.removeKnowledgeGroup(updatedEntity.getKnowledgeId()); - // 編集権限を削除 - KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); - KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); - editUsersDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); - editGroupsDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); - - // アクセス権を登録 - saveAccessUser(updatedEntity, loginedUser, targets); - // 編集権を登録 - saveEditorsUser(updatedEntity, loginedUser, editors); - - // タグを登録 - knowledgeTagsDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); - setTags(updatedEntity, tags); - - // 拡張項目の保存 - saveTemplateItemValue(updatedEntity.getKnowledgeId(), template, loginedUser); - - // 添付ファイルを更新(紐付けをセット) - fileLogic.setKnowledgeFiles(updatedEntity.getKnowledgeId(), fileNos, loginedUser); - - // 全文検索エンジンへ登録 - saveIndex(updatedEntity, tags, targets, template, updatedEntity.getInsertUser()); - - // 一覧表示用の情報を更新 - updateKnowledgeExInfo(updatedEntity); - - // 履歴登録 - insertHistory(updatedEntity); - - // 通知(TODO 別スレッド化を検討) - NotifyLogic.get().notifyOnKnowledgeUpdate(updatedEntity); - - return updatedEntity; - } - - /** - * テンプレートにある拡張項目値を保存 - * @param knowledgeId - * @param template - * @param loginedUser - */ - private void saveTemplateItemValue(Long knowledgeId, TemplateMastersEntity template, LoginedUser loginedUser) { - if (template == null) { - return; - } - List items = template.getItems(); - if (items == null) { - return; - } - for (TemplateItemsEntity item : items) { - KnowledgeItemValuesEntity val = new KnowledgeItemValuesEntity(); - val.setKnowledgeId(knowledgeId); - val.setTypeId(template.getTypeId()); - val.setItemNo(item.getItemNo()); - val.setItemValue(item.getItemValue()); - val.setItemStatus(KnowledgeItemValuesEntity.STATUS_SAVED); - KnowledgeItemValuesDao.get().save(val); - } - } - - /** - * ナレッジの更新履歴を登録 - * @param entity - */ - public void insertHistory(KnowledgesEntity entity) { - // 既存のナレッジ情報を履歴へコピー - KnowledgesEntity origin = knowledgesDao.selectOnKey(entity.getKnowledgeId()); - KnowledgeHistoriesEntity history = new KnowledgeHistoriesEntity(); - PropertyUtil.copyPropertyValue(origin, history); - KnowledgeHistoriesDao historiesDao = KnowledgeHistoriesDao.get(); - int max = historiesDao.selectMaxHistoryNo(entity.getKnowledgeId()); - max++; - history.setHistoryNo(max); - historiesDao.physicalInsert(history); - } - - - /** - * タグを登録 - * @param entity - * @param tags - */ - private void setTags(KnowledgesEntity entity, List tags) { - if (tags != null) { - for (TagsEntity tagsEntity : tags) { - KnowledgeTagsEntity knowledgeTagsEntity = new KnowledgeTagsEntity(entity.getKnowledgeId(), tagsEntity.getTagId()); - knowledgeTagsDao.insert(knowledgeTagsEntity); - } - } - } - /** - * 編集権限を登録 - * @param entity - * @param loginedUser - * @param editors - */ - private void saveEditorsUser(KnowledgesEntity entity, LoginedUser loginedUser, List editors) { - KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); - KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); - - // 編集権限を設定 - if (editors != null && !editors.isEmpty()) { - for (int i = 0; i < editors.size(); i++) { - LabelValue labelValue = editors.get(i); - - Integer id = TargetLogic.get().getGroupId(labelValue.getValue()); - if (id != Integer.MIN_VALUE) { - KnowledgeEditGroupsEntity editGroupsEntity = new KnowledgeEditGroupsEntity(); - editGroupsEntity.setKnowledgeId(entity.getKnowledgeId()); - editGroupsEntity.setGroupId(id); - editGroupsDao.save(editGroupsEntity); - } else { - id = TargetLogic.get().getUserId(labelValue.getValue()); - if (id != Integer.MIN_VALUE - && loginedUser.getUserId().intValue() != id.intValue() - && ALL_USER != id.intValue() - ) { - KnowledgeEditUsersEntity editUsersEntity = new KnowledgeEditUsersEntity(); - editUsersEntity.setKnowledgeId(entity.getKnowledgeId()); - editUsersEntity.setUserId(id); - editUsersDao.save(editUsersEntity); - } - } - } - } - } - - - /** - * アクセス権を登録 - * @param entity - * @param loginedUser - * @param targets - */ - private void saveAccessUser(KnowledgesEntity entity, LoginedUser loginedUser, List targets) { - // ナレッジにアクセス可能なユーザに、自分自身をセット - KnowledgeUsersEntity knowledgeUsersEntity = new KnowledgeUsersEntity(); - knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); - knowledgeUsersEntity.setUserId(loginedUser.getLoginUser().getUserId()); - knowledgeUsersDao.insert(knowledgeUsersEntity); - if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { - // 全て公開する情報 - knowledgeUsersEntity = new KnowledgeUsersEntity(); - knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); - knowledgeUsersEntity.setUserId(ALL_USER); - knowledgeUsersDao.insert(knowledgeUsersEntity); - } - if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { - // ナレッジとグループを紐付け - GroupLogic groupLogic = GroupLogic.get(); - groupLogic.saveKnowledgeGroup(entity.getKnowledgeId(), targets); - - // アクセスできるユーザを指定 - if (targets != null && !targets.isEmpty()) { - for (int i = 0; i < targets.size(); i++) { - LabelValue labelValue = targets.get(i); - Integer id = TargetLogic.get().getUserId(labelValue.getValue()); - if (id != Integer.MIN_VALUE - && loginedUser.getUserId().intValue() != id.intValue() - && ALL_USER != id.intValue() - ) { - knowledgeUsersEntity = new KnowledgeUsersEntity(); - knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); - knowledgeUsersEntity.setUserId(id); - knowledgeUsersDao.insert(knowledgeUsersEntity); - } - } - } - } - } - - /** - * 全文検索エンジンへ保存 - * @param entity - * @param tags - * @param template - * @param groups - * @param loginedUser - * @throws Exception - */ - private void saveIndex(KnowledgesEntity entity, List tags, List targets, - TemplateMastersEntity template, Integer creator) throws Exception { - IndexingValue indexingValue = new IndexingValue(); - indexingValue.setType(TYPE_KNOWLEDGE); - indexingValue.setId(String.valueOf(entity.getKnowledgeId())); - indexingValue.setTitle(entity.getTitle()); - - StringBuilder content = new StringBuilder(entity.getContent()); - if (template != null) { - List items = template.getItems(); - for (TemplateItemsEntity item : items) { - content.append(item.getItemName()).append(':').append(item.getItemValue()); - } - } - indexingValue.setContents(content.toString()); - indexingValue.addUser(creator); - if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { - indexingValue.addUser(ALL_USER); - } - if (tags != null) { - for (TagsEntity tagsEntity : tags) { - indexingValue.addTag(tagsEntity.getTagId()); - } - } - if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { - if (targets != null) { - for (LabelValue target : targets) { - Integer id = TargetLogic.get().getGroupId(target.getValue()); - if (id != Integer.MIN_VALUE) { - indexingValue.addGroup(id); - } - id = TargetLogic.get().getUserId(target.getValue()); - if (id != Integer.MIN_VALUE) { - indexingValue.addUser(id); - } - } - } - } - - indexingValue.setCreator(creator); - indexingValue.setTime(entity.getUpdateDatetime().getTime()); // 更新日時をセットするので、更新日時でソート - - IndexLogic.get().save(indexingValue); //全文検索のエンジンにも保存(DBに保存する意味ないかも) - } - - - /** - * 全文検索エンジンからナレッジを取得し、そこにさらに付加情報をつけて返す - * - * @param searchingValue - * @return - * @throws Exception - */ - private List searchKnowledge(SearchingValue searchingValue) throws Exception { - if (LOG.isDebugEnabled()) { - LOG.debug("search params:" + PropertyUtil.reflectionToString(searchingValue)); - } - LOG.trace("検索開始"); - List list = IndexLogic.get().search(searchingValue); - LOG.trace("検索終了"); - LOG.trace("付加情報をセット開始"); - List result = getKnowledgeDatas(list); - LOG.trace("付加情報をセット終了"); - return result; - } - - /** - * ナレッジ検索 - * @param keyword - * @param tags - * @param groups - * @param loginedUser - * @param offset - * @param limit - * @return - * @throws Exception - */ - public List searchKnowledge(String keyword, List tags, - List groups, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { - SearchingValue searchingValue = new SearchingValue(); - searchingValue.setKeyword(keyword); - searchingValue.setOffset(offset); - searchingValue.setLimit(limit); - - // タグが指定されてる場合はユーザに関係なく条件に追加する - if (tags != null && !tags.isEmpty()) { - for (TagsEntity tagsEntity : tags) { - searchingValue.addTag(tagsEntity.getTagId()); - } - } - - // ログインしてない場合はグループ検索ができないので公開記事のみを対象にして検索する - if (loginedUser == null) { - searchingValue.addUser(ALL_USER); - return searchKnowledge(searchingValue); - } - - // グループが指定されてる場合はグループのみ対象にして検索する - if (groups != null) { - for (GroupsEntity groupsEntity : groups) { - searchingValue.addGroup(groupsEntity.getGroupId()); - } - return searchKnowledge(searchingValue); - } - - // 管理者じゃなければ自身が参加してる公開記事、自身の記事、グループの記事を条件に追加する - if (!loginedUser.isAdmin()) { - searchingValue.addUser(ALL_USER); - searchingValue.addUser(loginedUser.getLoginUser().getUserId()); - - List logiedUserGroups = loginedUser.getGroups(); - if (logiedUserGroups != null && !logiedUserGroups.isEmpty()) { - for (GroupsEntity groupsEntity : logiedUserGroups) { - searchingValue.addGroup(groupsEntity.getGroupId()); - } - } - } - - return searchKnowledge(searchingValue); - } - - /** - * ナレッジの検索 - * @param keyword - * @param loginedUser - * @param offset - * @param limit - * @return - * @throws Exception - */ - public List searchKnowledge(String keyword, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { - return searchKnowledge(keyword, null, null, loginedUser, offset, limit); - } - - /** - * ナレッジをタグ指定で表示 - * @param tag - * @param loginedUser - * @param offset - * @param limit - * @return - * @throws Exception - */ - public List showKnowledgeOnTag(String tag, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { - SearchingValue searchingValue = new SearchingValue(); - searchingValue.setOffset(offset); - searchingValue.setLimit(limit); - - if (loginedUser != null && loginedUser.isAdmin()) { - //管理者の場合、ユーザのアクセス権を考慮しない - - } else { - searchingValue.addUser(ALL_USER); - if (loginedUser != null) { - Integer userId = loginedUser.getLoginUser().getUserId(); - searchingValue.addUser(userId); - - List groups = loginedUser.getGroups(); - if (groups != null && !groups.isEmpty()) { - for (GroupsEntity groupsEntity : groups) { - searchingValue.addGroup(groupsEntity.getGroupId()); - } - } - } - } - - if (StringUtils.isInteger(tag)) { - searchingValue.addTag(new Integer(tag)); - } - - return searchKnowledge(searchingValue); - } - - /** - * ナレッジをグループ指定で表示 - * - * @param group - * @param loginedUser - * @param offset - * @param limit - * @return - * @throws Exception - */ - public List showKnowledgeOnGroup(String group, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { - List knowledges = new ArrayList(); - if (loginedUser == null) { - return knowledges; - } - - SearchingValue searchingValue = new SearchingValue(); - searchingValue.setOffset(offset); - searchingValue.setLimit(limit); - - GroupsEntity targetGroup = ExGroupsDao.get().selectOnKey(new Integer(group)); - - if (loginedUser.isAdmin()) { - searchingValue.addGroup(targetGroup.getGroupId()); - return searchKnowledge(searchingValue); - } - - List groups = loginedUser.getGroups(); - for (GroupsEntity groupsEntity : groups) { - if (groupsEntity.getGroupId().intValue() == targetGroup.getGroupId().intValue()) { - searchingValue.addGroup(targetGroup.getGroupId()); - return searchKnowledge(searchingValue); - } - } - - return knowledges; - } - - /** - * 指定ユーザのナレッジを取得 - * @param userId - * @param loginedUser - * @param i - * @param pageLimit - * @return - * @throws Exception - */ - public List showKnowledgeOnUser(int targetUser, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { - SearchingValue searchingValue = new SearchingValue(); - searchingValue.setOffset(offset); - searchingValue.setLimit(limit); - - if (loginedUser != null && loginedUser.isAdmin()) { - //管理者の場合、ユーザのアクセス権を考慮しない - - } else { - searchingValue.addUser(ALL_USER); - if (loginedUser != null) { - Integer userId = loginedUser.getLoginUser().getUserId(); - searchingValue.addUser(userId); - - List groups = loginedUser.getGroups(); - if (groups != null && !groups.isEmpty()) { - for (GroupsEntity groupsEntity : groups) { - searchingValue.addGroup(groupsEntity.getGroupId()); - } - } - } - } - searchingValue.setCreator(targetUser); - - return searchKnowledge(searchingValue); - } - - - - /** - * 全文検索エンジンの結果を元に、DBからデータを取得し、 - * さらにアクセス権のチェックなどを行う - * @param list - * @return - */ - private List getKnowledgeDatas(List list) { - KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); - List knowledgeIds = new ArrayList(); -// List fileIds = new ArrayList<>(); - for (SearchResultValue searchResultValue : list) { - if (searchResultValue.getType() == TYPE_KNOWLEDGE) { - knowledgeIds.add(new Long(searchResultValue.getId())); -// } else if (searchResultValue.getType() == TYPE_FILE) { -// LOG.trace("FILE!!! " + searchResultValue.getId()); -// String id = searchResultValue.getId().substring(FileParseBat.ID_PREFIX.length()); -// fileIds.add(new Long(id)); - } - } - LOG.trace("添付ファイル情報取得完了"); - - List dbs = knowledgesDao.selectKnowledges(knowledgeIds); - Map map = new HashMap(); - for (KnowledgesEntity knowledgesEntity : dbs) { - map.put(knowledgesEntity.getKnowledgeId(), knowledgesEntity); - } - LOG.trace("ナレッジ情報取得完了"); - - List knowledges = new ArrayList<>(); - for (SearchResultValue searchResultValue : list) { - if (searchResultValue.getType() == TYPE_KNOWLEDGE) { - Long key = new Long(searchResultValue.getId()); - if (map.containsKey(key)) { - KnowledgesEntity entity = map.get(key); - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { - entity.setTitle(searchResultValue.getHighlightedTitle()); - } - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { - entity.setContent(searchResultValue.getHighlightedContents()); - } else { - String content = HtmlUtils.escapeHTML(entity.getContent()); - entity.setContent(content); - // entity.setContent(org.apache.commons.lang.StringUtils.abbreviate(entity.getContent(), LuceneSearcher.CONTENTS_LIMIT_LENGTH)); - } - - entity.setScore(searchResultValue.getScore()); - knowledges.add(entity); - } - } else if (searchResultValue.getType() == TYPE_FILE) { - // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 - String id = searchResultValue.getId().substring(FileParseBat.ID_PREFIX.length()); - Long fileNo = new Long(id); - KnowledgeFilesEntity filesEntity = filesDao.selectOnKeyWithoutBinary(fileNo); - if (filesEntity != null && filesEntity.getKnowledgeId() != null) { - KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(filesEntity.getKnowledgeId()); - if (entity == null) { - // 添付ファイルの情報が検索エンジン内にあり、検索にHitしたが、それに紐づくナレッジデータは削除されていた - break; - } - entity.setTitle(entity.getTitle()); - - StringBuilder builder = new StringBuilder(); - builder.append("[FILE] "); - - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { - builder.append(searchResultValue.getHighlightedTitle()); - } else { - builder.append(filesEntity.getFileName()); - } - builder.append("
"); - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { - builder.append(searchResultValue.getHighlightedContents()); - } - entity.setContent(builder.toString()); - entity.setScore(searchResultValue.getScore()); - knowledges.add(entity); - } - } else if (searchResultValue.getType() == TYPE_COMMENT) { - // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 - String id = searchResultValue.getId().substring(COMMENT_ID_PREFIX.length()); - Long commentNo = new Long(id); - CommentsEntity commentsEntity = CommentsDao.get().selectOnKey(commentNo); - if (commentsEntity != null && commentsEntity.getKnowledgeId() != null) { - KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(commentsEntity.getKnowledgeId()); - if (entity == null) { - // コメントの情報が検索エンジン内にあり、検索にHitしたが、それに紐づくナレッジデータは削除されていた - break; - } - entity.setTitle(entity.getTitle()); - - StringBuilder builder = new StringBuilder(); - builder.append("[COMMENT] "); - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { - builder.append(searchResultValue.getHighlightedContents()); - } - entity.setContent(builder.toString()); - entity.setScore(searchResultValue.getScore()); - knowledges.add(entity); - } - } else if (searchResultValue.getType() == IndexType.bookmarkContent.getValue()) { - // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 - String id = searchResultValue.getId().substring(FileParseBat.WEB_ID_PREFIX.length()); - Long knowledgeId = new Long(id); - KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(knowledgeId); - if (entity != null && entity.getKnowledgeId() != null) { - StringBuilder builder = new StringBuilder(); - builder.append("[Bookmark Content] "); - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { - builder.append(searchResultValue.getHighlightedTitle()); - } - builder.append("
"); - if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { - builder.append(searchResultValue.getHighlightedContents()); - } - entity.setContent(builder.toString()); - entity.setScore(searchResultValue.getScore()); - knowledges.add(entity); - } - } - } - -// 以下の付加情報は、ナレッジテーブルに持ち各テーブルに再取得しない -// for (KnowledgesEntity entity : knowledges) { -// // タグを取得(1件づつ処理するのでパフォーマンス悪いかも?) -// setTags(entity); -// // いいねの回数 -// setLikeCount(entity); -// // コメント件数 -// setCommentsCount(entity); -// } - - - LOG.trace("ナレッジ1件づつに、付加情報をセット完了"); - - return knowledges; - } - -// /** -// * コメントの件数を取得 -// * 再度SQLを実行するのでは無く、ナレッジ取得時にカウントもjoinして取得したほうが早い -// * -// * @param entity -// */ -// private void setCommentsCount(KnowledgesEntity entity) { -// CommentsDao commentsDao = CommentsDao.get(); -// Integer count = commentsDao.countOnKnowledgeId(entity.getKnowledgeId()); -// entity.setCommentCount(count); -// } -// -// /** -// * いいねの件数を取得 -// * 再度SQLを実行するのでは無く、ナレッジ取得時にカウントもjoinして取得したほうが早い -// * -// * @param entity -// */ -// private void setLikeCount(KnowledgesEntity entity) { -// LikesDao likesDao = LikesDao.get(); -// Long count = likesDao.countOnKnowledgeId(entity.getKnowledgeId()); -// entity.setLikeCount(count); -// } - - /** - * ナレッジの情報を取得 - * 取得する際にタグ情報も取得 - */ - public KnowledgesEntity selectWithTags(Long knowledgeId, LoginedUser loginedUser) { - KnowledgesEntity entity = select(knowledgeId, loginedUser); - if (entity != null) { - //タグをセット - setTags(entity); - } - return entity; - } - - - /** - * ナレッジを取得(アクセス権のあるもののみ) - * @param knowledgeId - * @param loginedUser - * @return - */ - public KnowledgesEntity select(Long knowledgeId, LoginedUser loginedUser) { - KnowledgesDao dao = Container.getComp(KnowledgesDao.class); - KnowledgesEntity entity = dao.selectOnKeyWithUserName(knowledgeId); - if (entity == null) { - return entity; - } - - if (entity.getPublicFlag() == null || entity.getPublicFlag().intValue() == PUBLIC_FLAG_PUBLIC) { - return entity; - } - Integer userId = Integer.MIN_VALUE; - if (loginedUser != null) { - userId = loginedUser.getLoginUser().getUserId(); - } - if (entity.getInsertUser().intValue() == userId.intValue()) { - // 作成者ならば、アクセス可能 - return entity; - } - if (loginedUser != null && loginedUser.isAdmin()) { - // 管理者は全ての情報にアクセス可能 - return entity; - } - - List usersEntities = knowledgeUsersDao.selectOnKnowledgeId(entity.getKnowledgeId()); - for (KnowledgeUsersEntity knowledgeUsersEntity : usersEntities) { - if (knowledgeUsersEntity.getUserId().intValue() == userId.intValue()) { - // アクセス権限が登録されていれば、取得 - return entity; - } - } - if (loginedUser != null) { - List groups = loginedUser.getGroups(); - if (groups != null) { - List knowledgeGroups = KnowledgeGroupsDao.get().selectOnKnowledgeId(knowledgeId); - for (KnowledgeGroupsEntity knowledgeGroupsEntity : knowledgeGroups) { - for (GroupsEntity groupsEntity : groups) { - if (groupsEntity.getGroupId().intValue() == knowledgeGroupsEntity.getGroupId().intValue()) { - // グループに登録があればアクセス可能 - return entity; - } - } - } - } - } - // アクセス権がなかった - return null; - } - - /** - * ナレッジのタグをセット - * @param entity - */ - private void setTags(KnowledgesEntity entity) { - //タグを取得 - List tagsEntities = tagsDao.selectOnKnowledgeId(entity.getKnowledgeId()); - int count = 0; - StringBuilder builder = new StringBuilder(); - for (TagsEntity tagsEntity : tagsEntities) { - if (count > 0) { - builder.append(","); - } - builder.append(tagsEntity.getTagName()); - count++; - } - entity.setTagNames(builder.toString()); - } - - /** - * ナレッジを取得 - * @param ids - * @param loginedUser - * @return - */ - public List getKnowledges(List ids, LoginedUser loginedUser) { - List knowledgeIds = new ArrayList<>(); - for (String string : ids) { - if (StringUtils.isLong(string)) { - knowledgeIds.add(new Long(string)); - } - } - - //List knowledgesEntities = knowledgesDao.selectKnowledges(knowledgeIds); - //アクセス権を考慮して取得 - List knowledgesEntities = new ArrayList<>(); - List addSuccess = new ArrayList(); - List addFail = new ArrayList(); - for (Long integer : knowledgeIds) { - KnowledgesEntity entity = select(integer, loginedUser); - if (entity != null) { - addSuccess.add(integer.toString()); - knowledgesEntities.add(entity); - } else { - addFail.add(integer.toString()); - } - } - if (addSuccess.isEmpty()) { - LOG.debug("History: add success. [Empty]"); - } else { - LOG.debug("History: add success. " + String.join(",", addSuccess.toArray(new String[0]))); - } - - if (addFail.isEmpty()) { - LOG.debug("History: add fail. [Empty]"); - } else { - LOG.debug("History: add fail. " + String.join(",", addFail.toArray(new String[0]))); - } - - return knowledgesEntities; - } - - /** - * ナレッジを削除 - * @param knowledgeId - * @param loginedUser - * @throws Exception - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public void delete(Long knowledgeId) throws Exception { - LOG.info("delete Knowledge: " + knowledgeId); - //ナレッジ削除(通常のdeleteは、論理削除になる) - knowledgesDao.delete(knowledgeId); - - // アクセス権削除 - knowledgeUsersDao.deleteOnKnowledgeId(knowledgeId); - - // タグを削除 - knowledgeTagsDao.deleteOnKnowledgeId(knowledgeId); - - // 添付ファイルを削除 - fileLogic.deleteOnKnowledgeId(knowledgeId); - - // コメント削除 - this.deleteCommentsOnKnowledgeId(knowledgeId); - - // ナレッジにアクセス可能なグループ削除 - GroupLogic.get().removeKnowledgeGroup(knowledgeId); - - // 編集権限を削除 - KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); - KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); - editUsersDao.deleteOnKnowledgeId(knowledgeId); - editGroupsDao.deleteOnKnowledgeId(knowledgeId); - - //全文検索エンジンから削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete(knowledgeId); - indexLogic.delete("WEB-" + knowledgeId); - } - - /** - * ナレッジに紐づくコメントを削除 - * @param knowledgeId - * @throws Exception - */ - private void deleteCommentsOnKnowledgeId(Long knowledgeId) throws Exception { - CommentsDao commentsDao = CommentsDao.get(); - List comments = commentsDao.selectOnKnowledgeId(knowledgeId); - if (comments != null) { - for (CommentsEntity commentsEntity : comments) { - deleteComment(commentsEntity); - } - } - } - - /** - * ユーザのナレッジを削除 - * TODO ものすごく多くのナレッジを登録したユーザの場合、それを全て削除するのは時間がかかるかも? - * ただ、非同期で実施して、「そのうち消えます」と表示するのも気持ち悪いと感じられるので、 - * いったん同期処理で1件づつ消す(効率的な消し方を検討する) - * @param loginUserId - * @throws Exception - */ - public void deleteOnUser(Integer loginUserId) throws Exception { - // ユーザのナレッジを取得 - List knowledgeIds = knowledgesDao.selectOnUser(loginUserId); - for (Long knowledgeId : knowledgeIds) { - delete(knowledgeId); - } - } - - /** - * 閲覧履歴を保持 - * @param knowledgeId - * @param loginedUser - */ - public void addViewHistory(Long knowledgeId, LoginedUser loginedUser) { - ViewHistoriesDao historiesDao = ViewHistoriesDao.get(); - ViewHistoriesEntity historiesEntity = new ViewHistoriesEntity(); - historiesEntity.setKnowledgeId(knowledgeId); - historiesEntity.setViewDateTime(new Timestamp(new Date().getTime())); - if (loginedUser != null) { - historiesEntity.setInsertUser(loginedUser.getUserId()); - } - historiesDao.insert(historiesEntity); - } - - /** - * いいね!を追加 - * @param knowledgeId - * @param loginedUser - * @return - */ - public Long addLike(Long knowledgeId, LoginedUser loginedUser) { - LikesDao likesDao = LikesDao.get(); - LikesEntity likesEntity = new LikesEntity(); - likesEntity.setKnowledgeId(knowledgeId); - likesDao.insert(likesEntity); - - updateKnowledgeExInfo(knowledgeId); - - Long count = likesDao.countOnKnowledgeId(knowledgeId); - - // 通知(TODO 別スレッド化を検討) - NotifyLogic.get().notifyOnKnowledgeLiked(knowledgeId, likesEntity); - - return count; - } - - - /** - * ナレッジテーブルの - * タグやイイネ件数、コメント件数などの付加情報を - * 更新する(一覧表示用) - * - * @param knowledgeId - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public void updateKnowledgeExInfo(Long knowledgeId) { - // 一覧表示用の情報を更新 - KnowledgesDao knowledgesDao = KnowledgesDao.get(); - KnowledgesEntity entity = knowledgesDao.selectOnKey(knowledgeId); - updateKnowledgeExInfo(entity); - } - - /** - * ナレッジテーブルの - * タグやイイネ件数、コメント件数などの付加情報を - * 更新する(一覧表示用) - * - * @param entity - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public void updateKnowledgeExInfo(KnowledgesEntity entity) { - // タグ情報 - TagsDao tagsDao = TagsDao.get(); - List tags = tagsDao.selectOnKnowledgeId(entity.getKnowledgeId()); - StringJoinBuilder ids = new StringJoinBuilder(); - StringJoinBuilder names = new StringJoinBuilder(); - for (TagsEntity tagsEntity : tags) { - ids.append(tagsEntity.getTagId()); - names.append(tagsEntity.getTagName()); - } - entity.setTagIds(ids.join(",")); - entity.setTagNames(names.join(",")); - // いいね件数 - LikesDao likesDao = LikesDao.get(); - Long likeCount = likesDao.countOnKnowledgeId(entity.getKnowledgeId()); - entity.setLikeCount(likeCount); - // コメント件数 - CommentsDao commentsDao = CommentsDao.get(); - Integer commentCount = commentsDao.countOnKnowledgeId(entity.getKnowledgeId()); - entity.setCommentCount(commentCount); - - // 更新 - KnowledgesDao knowledgesDao = KnowledgesDao.get(); - knowledgesDao.physicalUpdate(entity); - } - - - /** - * コメント保存 - * @param knowledgeId - * @param comment - * @param fileNos - * @throws Exception - */ - public void saveComment(Long knowledgeId, String comment, List fileNos, LoginedUser loginedUser) throws Exception { - CommentsDao commentsDao = CommentsDao.get(); - CommentsEntity commentsEntity = new CommentsEntity(); - commentsEntity.setKnowledgeId(knowledgeId); - commentsEntity.setComment(comment); - commentsEntity = commentsDao.insert(commentsEntity); - // 一覧表示用の情報を更新 - KnowledgeLogic.get().updateKnowledgeExInfo(knowledgeId); - - // 検索エンジンに追加 - addIndexOnComment(commentsEntity); - - // 添付ファイルを更新(紐付けをセット) - fileLogic.setKnowledgeFiles(knowledgeId, fileNos, loginedUser, commentsEntity.getCommentNo()); - - - // 通知(TODO 別スレッド化を検討) - NotifyLogic.get().notifyOnKnowledgeComment(knowledgeId, commentsEntity); - } - - /** - * コメント更新 - * @param commentsEntity - * @param fileNos - * @param loginedUser - * @throws Exception - */ - public void updateComment(CommentsEntity commentsEntity, List fileNos, LoginedUser loginedUser) throws Exception { - CommentsDao commentsDao = CommentsDao.get(); - CommentsEntity updatedCommentsEntity = commentsDao.update(commentsEntity); - // 一覧表示用の情報を更新 - KnowledgeLogic.get().updateKnowledgeExInfo(updatedCommentsEntity.getKnowledgeId()); - - // 検索エンジンに追加 - addIndexOnComment(updatedCommentsEntity); - - // 添付ファイルを更新(紐付けをセット) - fileLogic.setKnowledgeFiles(updatedCommentsEntity.getKnowledgeId(), fileNos, loginedUser, updatedCommentsEntity.getCommentNo()); - } - - - /** - * コメント削除 - * @param commentsEntity - * @param loginedUser - * @throws Exception - */ - public void deleteComment(CommentsEntity commentsEntity) throws Exception { - CommentsDao commentsDao = CommentsDao.get(); - commentsDao.delete(commentsEntity); - // 検索エンジンから削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete(COMMENT_ID_PREFIX + String.valueOf(commentsEntity.getCommentNo())); - } - - - /** - * コメント削除 - * @param commentsEntity - * @param loginedUser - * @throws Exception - */ - public void deleteComment(CommentsEntity commentsEntity, LoginedUser loginedUser) throws Exception { - deleteComment(commentsEntity); - // 一覧表示用の情報を更新 - KnowledgeLogic.get().updateKnowledgeExInfo(commentsEntity.getKnowledgeId()); - // 添付ファイルを更新(紐付けをセット) - fileLogic.setKnowledgeFiles(commentsEntity.getKnowledgeId(), new ArrayList(), loginedUser, commentsEntity.getCommentNo()); - } - - - /** - * コメントを全文検索エンジンへ登録 - * @param commentsEntity - * @throws Exception - */ - public void addIndexOnComment(CommentsEntity commentsEntity) throws Exception { - KnowledgesDao knowledgesDao = KnowledgesDao.get(); - KnowledgesEntity entity = knowledgesDao.selectOnKey(commentsEntity.getKnowledgeId()); - - IndexingValue indexingValue = new IndexingValue(); - indexingValue.setType(TYPE_COMMENT); - indexingValue.setId(COMMENT_ID_PREFIX + String.valueOf(commentsEntity.getCommentNo())); - indexingValue.setTitle(""); - indexingValue.setContents(commentsEntity.getComment()); - indexingValue.addUser(entity.getInsertUser()); - if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { - indexingValue.addUser(ALL_USER); - } - - List tags = TagsDao.get().selectOnKnowledgeId(commentsEntity.getKnowledgeId()); - if (tags != null) { - for (TagsEntity tagsEntity : tags) { - indexingValue.addTag(tagsEntity.getTagId()); - } - } - if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { - TargetLogic targetLogic = TargetLogic.get(); - List targets = targetLogic.selectTargetsOnKnowledgeId(commentsEntity.getKnowledgeId()); - if (targets != null) { - for (LabelValue target : targets) { - Integer id = TargetLogic.get().getGroupId(target.getValue()); - if (id != Integer.MIN_VALUE) { - indexingValue.addGroup(id); - } - id = TargetLogic.get().getUserId(target.getValue()); - if (id != Integer.MIN_VALUE) { - indexingValue.addUser(id); - } - } - } - } - indexingValue.setCreator(commentsEntity.getInsertUser()); - indexingValue.setTime(commentsEntity.getUpdateDatetime().getTime()); // 更新日時をセットするので、更新日時でソート - - IndexLogic.get().save(indexingValue); //全文検索のエンジンにも保存(DBに保存する意味ないかも) - } - - public void reindexing(KnowledgesEntity knowledgesEntity) throws Exception { - // ナレッジの情報を検索エンジンへ更新 - List tags = tagsDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); - List targets = TargetLogic.get().selectTargetsOnKnowledgeId(knowledgesEntity.getKnowledgeId()); - - // 拡張値を取得 - TemplateMastersEntity template = TemplateMastersDao.get().selectWithItems(knowledgesEntity.getTypeId()); - List items = template.getItems(); - List values = KnowledgeItemValuesDao.get().selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); - for (TemplateItemsEntity item : items) { - for (KnowledgeItemValuesEntity val : values) { - if (item.getItemNo().equals(val.getItemNo())) { - item.setItemValue(val.getItemValue()); - } - } - } - // インデックス更新 - saveIndex(knowledgesEntity, tags, targets, template, knowledgesEntity.getInsertUser()); - - // コメントを検索エンジンへ - List comments = CommentsDao.get().selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); - for (CommentsEntity commentsEntity : comments) { - addIndexOnComment(commentsEntity); - } - - //添付ファイルを検索エンジンへ - KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); - List filesEntities = filesDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); - for (KnowledgeFilesEntity knowledgeFilesEntity : filesEntities) { - //添付ファイルのパースは、パースバッチに任せる(ステータスをパース待ちにしておけばバッチが処理する) - filesDao.changeStatus(knowledgeFilesEntity.getFileNo(), FileParseBat.PARSE_STATUS_WAIT, FileParseBat.UPDATE_USER_ID); - } - } - - /** - * ナレッジに対し編集権限があるかチェック - * @param loginedUser - * @param entity - * @param editors - * @return - */ - public boolean isEditor(LoginedUser loginedUser, KnowledgesEntity entity, List editors) { - if (loginedUser == null) { - // ログインしていないユーザに編集権限は無し - return false; - } - if (loginedUser.isAdmin()) { - return true; - } else { - if (entity != null) { - if (entity.getInsertUser().intValue() == loginedUser.getUserId().intValue()) { - return true; - } - } - for (LabelValue labelValue : editors) { - Integer id = TargetLogic.get().getGroupId(labelValue.getValue()); - if (id != Integer.MIN_VALUE) { - List groups = loginedUser.getGroups(); - if (groups != null) { - for (GroupsEntity groupsEntity : groups) { - if (groupsEntity.getGroupId().intValue() == id.intValue()) { - return true; - } - } - } - } else { - id = TargetLogic.get().getUserId(labelValue.getValue()); - if (id != Integer.MIN_VALUE) { - if (id.intValue() == loginedUser.getUserId().intValue()) { - return true; - } - } - } - } - } - return false; - } - - - /** - * 一定期間で、「イイネ」の件数が多いものを一覧で取得 - * (イイネの件数が多い順で並べる) - * - * - * @param loginedUser - * @return - */ - public List getPopularityKnowledges(LoginedUser loginedUser, int offset, int limit) { - long now = new Date().getTime(); - LOG.info(now); - - long term = 1000L * 60L * 60L * 24L * 30L; - LOG.info(term); - long s = now - term; - LOG.info(s); - Timestamp start = new Timestamp(s); - LOG.info(start.getTime()); - LOG.info(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(start)); - - long e = now + (1000L * 60L * 60L * 24L * 1L); - LOG.info(e); - Timestamp end = new Timestamp(e); - LOG.info(end.getTime()); - LOG.info(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(end)); - - if (loginedUser != null && loginedUser.isAdmin()) { - return KnowledgesDao.get().selectPopularity(start, end, offset, limit); - } - - return KnowledgesDao.get().selectPopularityWithAccessControl(loginedUser, start, end, offset, limit); - } + /** LOG */ + private static final Log LOG = LogFactory.getLog(KnowledgeLogic.class); + /** Group id for ALL_USER */ + public static final int ALL_USER = 0; + /** Open range: PUBLIC */ + public static final int PUBLIC_FLAG_PUBLIC = 0; + /** Open range: PRIVATE */ + public static final int PUBLIC_FLAG_PRIVATE = 1; + /** Open range: PROTECT */ + public static final int PUBLIC_FLAG_PROTECT = 2; + + /** Template type: Knowledge */ + public static final int TEMPLATE_TYPE_KNOWLEDGE = -100; + /** Template type: Bookmark */ + public static final int TEMPLATE_TYPE_BOOKMARK = -99; + + /** Index type: Bookmark */ + public static final int TYPE_KNOWLEDGE = IndexType.knowledge.getValue(); + /** Index type: Attached File */ + public static final int TYPE_FILE = IndexType.KnowledgeFile.getValue(); + /** Index type: Comment */ + public static final int TYPE_COMMENT = IndexType.KnowledgeComment.getValue(); + + /** Prefix for Comment */ + public static final String COMMENT_ID_PREFIX = "COMMENT-"; + + /** Get instance */ + public static KnowledgeLogic get() { + return Container.getComp(KnowledgeLogic.class); + } + + /** KnowledgesDao */ + private KnowledgesDao knowledgesDao = Container.getComp(KnowledgesDao.class); + /** KnowledgeUsersDao */ + private KnowledgeUsersDao knowledgeUsersDao = KnowledgeUsersDao.get(); + /** TagsDao */ + private TagsDao tagsDao = TagsDao.get(); + /** KnowledgeTagsDao */ + private KnowledgeTagsDao knowledgeTagsDao = KnowledgeTagsDao.get(); + /** UploadedFileLogic */ + private UploadedFileLogic fileLogic = UploadedFileLogic.get(); + + /** + * タグの文字列(カンマ区切り)から、登録済のタグであれば、それを取得し、 存在しないものであれば、新たにタグを生成してタグの情報を取得 + * + * @param tags + * @return + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public List manegeTags(String tags) { + List tagList = new ArrayList<>(); + if (StringUtils.isEmpty(tags)) { + return tagList; + } + String[] splits; + if (tags.indexOf(",") != -1) { + splits = tags.split(","); + } else { + splits = new String[1]; + splits[0] = tags; + } + + for (String tag : splits) { + tag = tag.trim(); + if (tag.startsWith(" ")) { + tag = tag.substring(" ".length()); + } + if (tag.startsWith(" ")) { + tag = tag.substring(" ".length()); + } + + TagsEntity tagsEntity = tagsDao.selectOnTagName(tag); + if (tagsEntity == null) { + tagsEntity = new TagsEntity(); + tagsEntity.setTagName(tag); + tagsEntity = tagsDao.insert(tagsEntity); + LOG.debug("Tag added." + PropertyUtil.reflectionToString(tagsEntity)); + } + tagList.add(tagsEntity); + } + return tagList; + } + + /** + * ナレッジを登録 + * + * @param entity + * @param tags + * @param fileNos + * @param targets + * @param editors + * @param template + * @param loginedUser + * @return + * @throws Exception + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public KnowledgesEntity insert(KnowledgesEntity entity, List tags, List fileNos, List targets, + List editors, TemplateMastersEntity template, LoginedUser loginedUser) throws Exception { + // ナレッジを登録 + KnowledgesEntity insertedEntity = knowledgesDao.insert(entity); + // アクセス権を登録 + saveAccessUser(insertedEntity, loginedUser, targets); + // 編集権を登録 + saveEditorsUser(insertedEntity, loginedUser, editors); + // タグを登録 + setTags(insertedEntity, tags); + // 添付ファイルを更新(紐付けをセット) + fileLogic.setKnowledgeFiles(insertedEntity.getKnowledgeId(), fileNos, loginedUser); + // 拡張項目の保存 + saveTemplateItemValue(insertedEntity.getKnowledgeId(), template, loginedUser); + + // 全文検索エンジンへ登録 + saveIndex(insertedEntity, tags, targets, template, loginedUser.getUserId()); + // 一覧表示用の情報を更新 + updateKnowledgeExInfo(insertedEntity); + // 履歴登録 + insertHistory(insertedEntity); + // 通知 + NotifyLogic.get().notifyOnKnowledgeInsert(insertedEntity); + + return insertedEntity; + } + + /** + * ナレッジを更新 + * + * @param entity + * @param fileNos + * @param targets + * @param editors + * @param template + * @param loginedUser + * @return + * @throws Exception + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public KnowledgesEntity update(KnowledgesEntity entity, List tags, List fileNos, List targets, + List editors, TemplateMastersEntity template, LoginedUser loginedUser) throws Exception { + // ナレッッジを更新 + KnowledgesEntity updatedEntity = knowledgesDao.update(entity); + // ユーザのアクセス権を解除 + knowledgeUsersDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); + // グループとナレッジのヒモ付を解除 + GroupLogic groupLogic = GroupLogic.get(); + groupLogic.removeKnowledgeGroup(updatedEntity.getKnowledgeId()); + // 編集権限を削除 + KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); + KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); + editUsersDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); + editGroupsDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); + + // アクセス権を登録 + saveAccessUser(updatedEntity, loginedUser, targets); + // 編集権を登録 + saveEditorsUser(updatedEntity, loginedUser, editors); + + // タグを登録 + knowledgeTagsDao.deleteOnKnowledgeId(updatedEntity.getKnowledgeId()); + setTags(updatedEntity, tags); + + // 拡張項目の保存 + saveTemplateItemValue(updatedEntity.getKnowledgeId(), template, loginedUser); + + // 添付ファイルを更新(紐付けをセット) + fileLogic.setKnowledgeFiles(updatedEntity.getKnowledgeId(), fileNos, loginedUser); + + // 全文検索エンジンへ登録 + saveIndex(updatedEntity, tags, targets, template, updatedEntity.getInsertUser()); + + // 一覧表示用の情報を更新 + updateKnowledgeExInfo(updatedEntity); + + // 履歴登録 + insertHistory(updatedEntity); + + // 通知 + NotifyLogic.get().notifyOnKnowledgeUpdate(updatedEntity); + + return updatedEntity; + } + + /** + * テンプレートにある拡張項目値を保存 + * + * @param knowledgeId + * @param template + * @param loginedUser + */ + private void saveTemplateItemValue(Long knowledgeId, TemplateMastersEntity template, LoginedUser loginedUser) { + if (template == null) { + return; + } + List items = template.getItems(); + if (items == null) { + return; + } + for (TemplateItemsEntity item : items) { + KnowledgeItemValuesEntity val = new KnowledgeItemValuesEntity(); + val.setKnowledgeId(knowledgeId); + val.setTypeId(template.getTypeId()); + val.setItemNo(item.getItemNo()); + val.setItemValue(item.getItemValue()); + val.setItemStatus(KnowledgeItemValuesEntity.STATUS_SAVED); + KnowledgeItemValuesDao.get().save(val); + } + } + + /** + * ナレッジの更新履歴を登録 + * + * @param entity + */ + public void insertHistory(KnowledgesEntity entity) { + // 既存のナレッジ情報を履歴へコピー + KnowledgesEntity origin = knowledgesDao.selectOnKey(entity.getKnowledgeId()); + KnowledgeHistoriesEntity history = new KnowledgeHistoriesEntity(); + PropertyUtil.copyPropertyValue(origin, history); + KnowledgeHistoriesDao historiesDao = KnowledgeHistoriesDao.get(); + int max = historiesDao.selectMaxHistoryNo(entity.getKnowledgeId()); + max++; + history.setHistoryNo(max); + historiesDao.physicalInsert(history); + } + + /** + * タグを登録 + * + * @param entity + * @param tags + */ + private void setTags(KnowledgesEntity entity, List tags) { + if (tags != null) { + for (TagsEntity tagsEntity : tags) { + KnowledgeTagsEntity knowledgeTagsEntity = new KnowledgeTagsEntity(entity.getKnowledgeId(), tagsEntity.getTagId()); + knowledgeTagsDao.insert(knowledgeTagsEntity); + } + } + } + + /** + * 編集権限を登録 + * + * @param entity + * @param loginedUser + * @param editors + */ + private void saveEditorsUser(KnowledgesEntity entity, LoginedUser loginedUser, List editors) { + KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); + KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); + + // 編集権限を設定 + if (editors != null && !editors.isEmpty()) { + for (int i = 0; i < editors.size(); i++) { + LabelValue labelValue = editors.get(i); + + Integer id = TargetLogic.get().getGroupId(labelValue.getValue()); + if (id != Integer.MIN_VALUE) { + KnowledgeEditGroupsEntity editGroupsEntity = new KnowledgeEditGroupsEntity(); + editGroupsEntity.setKnowledgeId(entity.getKnowledgeId()); + editGroupsEntity.setGroupId(id); + editGroupsDao.save(editGroupsEntity); + } else { + id = TargetLogic.get().getUserId(labelValue.getValue()); + if (id != Integer.MIN_VALUE && loginedUser.getUserId().intValue() != id.intValue() && ALL_USER != id.intValue()) { + KnowledgeEditUsersEntity editUsersEntity = new KnowledgeEditUsersEntity(); + editUsersEntity.setKnowledgeId(entity.getKnowledgeId()); + editUsersEntity.setUserId(id); + editUsersDao.save(editUsersEntity); + } + } + } + } + } + + /** + * アクセス権を登録 + * + * @param entity + * @param loginedUser + * @param targets + */ + private void saveAccessUser(KnowledgesEntity entity, LoginedUser loginedUser, List targets) { + // ナレッジにアクセス可能なユーザに、自分自身をセット + KnowledgeUsersEntity knowledgeUsersEntity = new KnowledgeUsersEntity(); + knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); + knowledgeUsersEntity.setUserId(loginedUser.getLoginUser().getUserId()); + knowledgeUsersDao.insert(knowledgeUsersEntity); + if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { + // 全て公開する情報 + knowledgeUsersEntity = new KnowledgeUsersEntity(); + knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); + knowledgeUsersEntity.setUserId(ALL_USER); + knowledgeUsersDao.insert(knowledgeUsersEntity); + } + if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { + // ナレッジとグループを紐付け + GroupLogic groupLogic = GroupLogic.get(); + groupLogic.saveKnowledgeGroup(entity.getKnowledgeId(), targets); + + // アクセスできるユーザを指定 + if (targets != null && !targets.isEmpty()) { + for (int i = 0; i < targets.size(); i++) { + LabelValue labelValue = targets.get(i); + Integer id = TargetLogic.get().getUserId(labelValue.getValue()); + if (id != Integer.MIN_VALUE && loginedUser.getUserId().intValue() != id.intValue() && ALL_USER != id.intValue()) { + knowledgeUsersEntity = new KnowledgeUsersEntity(); + knowledgeUsersEntity.setKnowledgeId(entity.getKnowledgeId()); + knowledgeUsersEntity.setUserId(id); + knowledgeUsersDao.insert(knowledgeUsersEntity); + } + } + } + } + } + + /** + * 全文検索エンジンへ保存 + * + * @param entity + * @param tags + * @param template + * @param groups + * @param loginedUser + * @throws Exception + */ + private void saveIndex(KnowledgesEntity entity, List tags, List targets, TemplateMastersEntity template, Integer creator) + throws Exception { + IndexingValue indexingValue = new IndexingValue(); + indexingValue.setType(TYPE_KNOWLEDGE); + indexingValue.setId(String.valueOf(entity.getKnowledgeId())); + indexingValue.setTitle(entity.getTitle()); + + StringBuilder content = new StringBuilder(entity.getContent()); + if (template != null) { + List items = template.getItems(); + for (TemplateItemsEntity item : items) { + content.append(item.getItemName()).append(':').append(item.getItemValue()); + } + } + indexingValue.setContents(content.toString()); + indexingValue.addUser(creator); + if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { + indexingValue.addUser(ALL_USER); + } + if (tags != null) { + for (TagsEntity tagsEntity : tags) { + indexingValue.addTag(tagsEntity.getTagId()); + } + } + if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { + if (targets != null) { + for (LabelValue target : targets) { + Integer id = TargetLogic.get().getGroupId(target.getValue()); + if (id != Integer.MIN_VALUE) { + indexingValue.addGroup(id); + } + id = TargetLogic.get().getUserId(target.getValue()); + if (id != Integer.MIN_VALUE) { + indexingValue.addUser(id); + } + } + } + } + + indexingValue.setCreator(creator); + indexingValue.setTime(entity.getUpdateDatetime().getTime()); // 更新日時をセットするので、更新日時でソート + + IndexLogic.get().save(indexingValue); // 全文検索のエンジンにも保存(DBに保存する意味ないかも) + } + + /** + * 全文検索エンジンからナレッジを取得し、そこにさらに付加情報をつけて返す + * + * @param searchingValue + * @return + * @throws Exception + */ + private List searchKnowledge(SearchingValue searchingValue) throws Exception { + if (LOG.isDebugEnabled()) { + LOG.debug("search params:" + PropertyUtil.reflectionToString(searchingValue)); + } + LOG.trace("検索開始"); + List list = IndexLogic.get().search(searchingValue); + LOG.trace("検索終了"); + LOG.trace("付加情報をセット開始"); + List result = getKnowledgeDatas(list); + LOG.trace("付加情報をセット終了"); + return result; + } + + /** + * ナレッジ検索 + * + * @param keyword + * @param tags + * @param groups + * @param loginedUser + * @param offset + * @param limit + * @return + * @throws Exception + */ + public List searchKnowledge(String keyword, List tags, List groups, LoginedUser loginedUser, + Integer offset, Integer limit) throws Exception { + SearchingValue searchingValue = new SearchingValue(); + searchingValue.setKeyword(keyword); + searchingValue.setOffset(offset); + searchingValue.setLimit(limit); + + // タグが指定されてる場合はユーザに関係なく条件に追加する + if (tags != null && !tags.isEmpty()) { + for (TagsEntity tagsEntity : tags) { + searchingValue.addTag(tagsEntity.getTagId()); + } + } + + // ログインしてない場合はグループ検索ができないので公開記事のみを対象にして検索する + if (loginedUser == null) { + searchingValue.addUser(ALL_USER); + return searchKnowledge(searchingValue); + } + + // グループが指定されてる場合はグループのみ対象にして検索する + if (groups != null) { + for (GroupsEntity groupsEntity : groups) { + searchingValue.addGroup(groupsEntity.getGroupId()); + } + return searchKnowledge(searchingValue); + } + + // 管理者じゃなければ自身が参加してる公開記事、自身の記事、グループの記事を条件に追加する + if (!loginedUser.isAdmin()) { + searchingValue.addUser(ALL_USER); + searchingValue.addUser(loginedUser.getLoginUser().getUserId()); + + List logiedUserGroups = loginedUser.getGroups(); + if (logiedUserGroups != null && !logiedUserGroups.isEmpty()) { + for (GroupsEntity groupsEntity : logiedUserGroups) { + searchingValue.addGroup(groupsEntity.getGroupId()); + } + } + } + + return searchKnowledge(searchingValue); + } + + /** + * ナレッジの検索 + * + * @param keyword + * @param loginedUser + * @param offset + * @param limit + * @return + * @throws Exception + */ + public List searchKnowledge(String keyword, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { + return searchKnowledge(keyword, null, null, loginedUser, offset, limit); + } + + /** + * ナレッジをタグ指定で表示 + * + * @param tag + * @param loginedUser + * @param offset + * @param limit + * @return + * @throws Exception + */ + public List showKnowledgeOnTag(String tag, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { + SearchingValue searchingValue = new SearchingValue(); + searchingValue.setOffset(offset); + searchingValue.setLimit(limit); + + if (loginedUser != null && loginedUser.isAdmin()) { + // 管理者の場合、ユーザのアクセス権を考慮しない + LOG.trace("管理者の場合はアクセス権は考慮しません"); + } else { + searchingValue.addUser(ALL_USER); + if (loginedUser != null) { + Integer userId = loginedUser.getLoginUser().getUserId(); + searchingValue.addUser(userId); + + List groups = loginedUser.getGroups(); + if (groups != null && !groups.isEmpty()) { + for (GroupsEntity groupsEntity : groups) { + searchingValue.addGroup(groupsEntity.getGroupId()); + } + } + } + } + + if (StringUtils.isInteger(tag)) { + searchingValue.addTag(new Integer(tag)); + } + + return searchKnowledge(searchingValue); + } + + /** + * ナレッジをグループ指定で表示 + * + * @param group + * @param loginedUser + * @param offset + * @param limit + * @return + * @throws Exception + */ + public List showKnowledgeOnGroup(String group, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { + List knowledges = new ArrayList(); + if (loginedUser == null) { + return knowledges; + } + + SearchingValue searchingValue = new SearchingValue(); + searchingValue.setOffset(offset); + searchingValue.setLimit(limit); + + GroupsEntity targetGroup = ExGroupsDao.get().selectOnKey(new Integer(group)); + + if (loginedUser.isAdmin()) { + searchingValue.addGroup(targetGroup.getGroupId()); + return searchKnowledge(searchingValue); + } + + List groups = loginedUser.getGroups(); + for (GroupsEntity groupsEntity : groups) { + if (groupsEntity.getGroupId().intValue() == targetGroup.getGroupId().intValue()) { + searchingValue.addGroup(targetGroup.getGroupId()); + return searchKnowledge(searchingValue); + } + } + + return knowledges; + } + + /** + * 指定ユーザのナレッジを取得 + * + * @param userId + * @param loginedUser + * @param i + * @param pageLimit + * @return + * @throws Exception + */ + public List showKnowledgeOnUser(int targetUser, LoginedUser loginedUser, Integer offset, Integer limit) throws Exception { + SearchingValue searchingValue = new SearchingValue(); + searchingValue.setOffset(offset); + searchingValue.setLimit(limit); + + if (loginedUser != null && loginedUser.isAdmin()) { + // 管理者の場合、ユーザのアクセス権を考慮しない + LOG.trace("管理者の場合はアクセス権は考慮しません"); + } else { + searchingValue.addUser(ALL_USER); + if (loginedUser != null) { + Integer userId = loginedUser.getLoginUser().getUserId(); + searchingValue.addUser(userId); + + List groups = loginedUser.getGroups(); + if (groups != null && !groups.isEmpty()) { + for (GroupsEntity groupsEntity : groups) { + searchingValue.addGroup(groupsEntity.getGroupId()); + } + } + } + } + searchingValue.setCreator(targetUser); + + return searchKnowledge(searchingValue); + } + + /** + * 全文検索エンジンの結果を元に、DBからデータを取得し、 さらにアクセス権のチェックなどを行う + * + * @param list + * @return + */ + private List getKnowledgeDatas(List list) { + KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); + List knowledgeIds = new ArrayList(); + // List fileIds = new ArrayList<>(); + for (SearchResultValue searchResultValue : list) { + if (searchResultValue.getType() == TYPE_KNOWLEDGE) { + knowledgeIds.add(new Long(searchResultValue.getId())); + // } else if (searchResultValue.getType() == TYPE_FILE) { + // LOG.trace("FILE!!! " + searchResultValue.getId()); + // String id = searchResultValue.getId().substring(FileParseBat.ID_PREFIX.length()); + // fileIds.add(new Long(id)); + } + } + LOG.trace("添付ファイル情報取得完了"); + + List dbs = knowledgesDao.selectKnowledges(knowledgeIds); + Map map = new HashMap(); + for (KnowledgesEntity knowledgesEntity : dbs) { + map.put(knowledgesEntity.getKnowledgeId(), knowledgesEntity); + } + LOG.trace("ナレッジ情報取得完了"); + + List knowledges = new ArrayList<>(); + for (SearchResultValue searchResultValue : list) { + if (searchResultValue.getType() == TYPE_KNOWLEDGE) { + Long key = new Long(searchResultValue.getId()); + if (map.containsKey(key)) { + KnowledgesEntity entity = map.get(key); + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { + entity.setTitle(searchResultValue.getHighlightedTitle()); + } + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { + entity.setContent(searchResultValue.getHighlightedContents()); + } else { + String content = HtmlUtils.escapeHTML(entity.getContent()); + entity.setContent(content); + // entity.setContent(org.apache.commons.lang.StringUtils.abbreviate(entity.getContent(), + // LuceneSearcher.CONTENTS_LIMIT_LENGTH)); + } + + entity.setScore(searchResultValue.getScore()); + knowledges.add(entity); + } + } else if (searchResultValue.getType() == TYPE_FILE) { + // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 + String id = searchResultValue.getId().substring(FileParseBat.ID_PREFIX.length()); + Long fileNo = new Long(id); + KnowledgeFilesEntity filesEntity = filesDao.selectOnKeyWithoutBinary(fileNo); + if (filesEntity != null && filesEntity.getKnowledgeId() != null) { + KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(filesEntity.getKnowledgeId()); + if (entity == null) { + // 添付ファイルの情報が検索エンジン内にあり、検索にHitしたが、それに紐づくナレッジデータは削除されていた + break; + } + entity.setTitle(entity.getTitle()); + + StringBuilder builder = new StringBuilder(); + builder.append("[FILE] "); + + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { + builder.append(searchResultValue.getHighlightedTitle()); + } else { + builder.append(filesEntity.getFileName()); + } + builder.append("
"); + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { + builder.append(searchResultValue.getHighlightedContents()); + } + entity.setContent(builder.toString()); + entity.setScore(searchResultValue.getScore()); + knowledges.add(entity); + } + } else if (searchResultValue.getType() == TYPE_COMMENT) { + // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 + String id = searchResultValue.getId().substring(COMMENT_ID_PREFIX.length()); + Long commentNo = new Long(id); + CommentsEntity commentsEntity = CommentsDao.get().selectOnKey(commentNo); + if (commentsEntity != null && commentsEntity.getKnowledgeId() != null) { + KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(commentsEntity.getKnowledgeId()); + if (entity == null) { + // コメントの情報が検索エンジン内にあり、検索にHitしたが、それに紐づくナレッジデータは削除されていた + break; + } + entity.setTitle(entity.getTitle()); + + StringBuilder builder = new StringBuilder(); + builder.append("[COMMENT] "); + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { + builder.append(searchResultValue.getHighlightedContents()); + } + entity.setContent(builder.toString()); + entity.setScore(searchResultValue.getScore()); + knowledges.add(entity); + } + } else if (searchResultValue.getType() == IndexType.bookmarkContent.getValue()) { + // TODO 1件づつ処理しているので、パフォーマンスが悪いので後で処理を検討 + String id = searchResultValue.getId().substring(FileParseBat.WEB_ID_PREFIX.length()); + Long knowledgeId = new Long(id); + KnowledgesEntity entity = knowledgesDao.selectOnKeyWithUserName(knowledgeId); + if (entity != null && entity.getKnowledgeId() != null) { + StringBuilder builder = new StringBuilder(); + builder.append("[Bookmark Content] "); + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedTitle())) { + builder.append(searchResultValue.getHighlightedTitle()); + } + builder.append("
"); + if (StringUtils.isNotEmpty(searchResultValue.getHighlightedContents())) { + builder.append(searchResultValue.getHighlightedContents()); + } + entity.setContent(builder.toString()); + entity.setScore(searchResultValue.getScore()); + knowledges.add(entity); + } + } + } + + // 以下の付加情報は、ナレッジテーブルに持ち各テーブルに再取得しない + // for (KnowledgesEntity entity : knowledges) { + // // タグを取得(1件づつ処理するのでパフォーマンス悪いかも?) + // setTags(entity); + // // いいねの回数 + // setLikeCount(entity); + // // コメント件数 + // setCommentsCount(entity); + // } + + LOG.trace("ナレッジ1件づつに、付加情報をセット完了"); + + return knowledges; + } + + // /** + // * コメントの件数を取得 + // * 再度SQLを実行するのでは無く、ナレッジ取得時にカウントもjoinして取得したほうが早い + // * + // * @param entity + // */ + // private void setCommentsCount(KnowledgesEntity entity) { + // CommentsDao commentsDao = CommentsDao.get(); + // Integer count = commentsDao.countOnKnowledgeId(entity.getKnowledgeId()); + // entity.setCommentCount(count); + // } + // + // /** + // * いいねの件数を取得 + // * 再度SQLを実行するのでは無く、ナレッジ取得時にカウントもjoinして取得したほうが早い + // * + // * @param entity + // */ + // private void setLikeCount(KnowledgesEntity entity) { + // LikesDao likesDao = LikesDao.get(); + // Long count = likesDao.countOnKnowledgeId(entity.getKnowledgeId()); + // entity.setLikeCount(count); + // } + + /** + * ナレッジの情報を取得 取得する際にタグ情報も取得 + */ + public KnowledgesEntity selectWithTags(Long knowledgeId, LoginedUser loginedUser) { + KnowledgesEntity entity = select(knowledgeId, loginedUser); + if (entity != null) { + // タグをセット + setTags(entity); + } + return entity; + } + + /** + * ナレッジを取得(アクセス権のあるもののみ) + * + * @param knowledgeId + * @param loginedUser + * @return + */ + public KnowledgesEntity select(Long knowledgeId, LoginedUser loginedUser) { + KnowledgesDao dao = Container.getComp(KnowledgesDao.class); + KnowledgesEntity entity = dao.selectOnKeyWithUserName(knowledgeId); + if (entity == null) { + return entity; + } + + if (entity.getPublicFlag() == null || entity.getPublicFlag().intValue() == PUBLIC_FLAG_PUBLIC) { + return entity; + } + Integer userId = Integer.MIN_VALUE; + if (loginedUser != null) { + userId = loginedUser.getLoginUser().getUserId(); + } + if (entity.getInsertUser().intValue() == userId.intValue()) { + // 作成者ならば、アクセス可能 + return entity; + } + if (loginedUser != null && loginedUser.isAdmin()) { + // 管理者は全ての情報にアクセス可能 + return entity; + } + + List usersEntities = knowledgeUsersDao.selectOnKnowledgeId(entity.getKnowledgeId()); + for (KnowledgeUsersEntity knowledgeUsersEntity : usersEntities) { + if (knowledgeUsersEntity.getUserId().intValue() == userId.intValue()) { + // アクセス権限が登録されていれば、取得 + return entity; + } + } + if (loginedUser != null) { + List groups = loginedUser.getGroups(); + if (groups != null) { + List knowledgeGroups = KnowledgeGroupsDao.get().selectOnKnowledgeId(knowledgeId); + for (KnowledgeGroupsEntity knowledgeGroupsEntity : knowledgeGroups) { + for (GroupsEntity groupsEntity : groups) { + if (groupsEntity.getGroupId().intValue() == knowledgeGroupsEntity.getGroupId().intValue()) { + // グループに登録があればアクセス可能 + return entity; + } + } + } + } + } + // アクセス権がなかった + return null; + } + + /** + * ナレッジのタグをセット + * + * @param entity + */ + private void setTags(KnowledgesEntity entity) { + // タグを取得 + List tagsEntities = tagsDao.selectOnKnowledgeId(entity.getKnowledgeId()); + int count = 0; + StringBuilder builder = new StringBuilder(); + for (TagsEntity tagsEntity : tagsEntities) { + if (count > 0) { + builder.append(","); + } + builder.append(tagsEntity.getTagName()); + count++; + } + entity.setTagNames(builder.toString()); + } + + /** + * ナレッジを取得 + * + * @param ids + * @param loginedUser + * @return + */ + public List getKnowledges(List ids, LoginedUser loginedUser) { + List knowledgeIds = new ArrayList<>(); + for (String string : ids) { + if (StringUtils.isLong(string)) { + knowledgeIds.add(new Long(string)); + } + } + + // List knowledgesEntities = knowledgesDao.selectKnowledges(knowledgeIds); + // アクセス権を考慮して取得 + List knowledgesEntities = new ArrayList<>(); + List addSuccess = new ArrayList(); + List addFail = new ArrayList(); + for (Long integer : knowledgeIds) { + KnowledgesEntity entity = select(integer, loginedUser); + if (entity != null) { + addSuccess.add(integer.toString()); + knowledgesEntities.add(entity); + } else { + addFail.add(integer.toString()); + } + } + if (addSuccess.isEmpty()) { + LOG.debug("History: add success. [Empty]"); + } else { + LOG.debug("History: add success. " + String.join(",", addSuccess.toArray(new String[0]))); + } + + if (addFail.isEmpty()) { + LOG.debug("History: add fail. [Empty]"); + } else { + LOG.debug("History: add fail. " + String.join(",", addFail.toArray(new String[0]))); + } + + return knowledgesEntities; + } + + /** + * ナレッジを削除 + * + * @param knowledgeId + * @param loginedUser + * @throws Exception + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public void delete(Long knowledgeId) throws Exception { + LOG.info("delete Knowledge: " + knowledgeId); + // ナレッジ削除(通常のdeleteは、論理削除になる) + knowledgesDao.delete(knowledgeId); + + // アクセス権削除 + knowledgeUsersDao.deleteOnKnowledgeId(knowledgeId); + + // タグを削除 + knowledgeTagsDao.deleteOnKnowledgeId(knowledgeId); + + // 添付ファイルを削除 + fileLogic.deleteOnKnowledgeId(knowledgeId); + + // コメント削除 + this.deleteCommentsOnKnowledgeId(knowledgeId); + + // ナレッジにアクセス可能なグループ削除 + GroupLogic.get().removeKnowledgeGroup(knowledgeId); + + // 編集権限を削除 + KnowledgeEditUsersDao editUsersDao = KnowledgeEditUsersDao.get(); + KnowledgeEditGroupsDao editGroupsDao = KnowledgeEditGroupsDao.get(); + editUsersDao.deleteOnKnowledgeId(knowledgeId); + editGroupsDao.deleteOnKnowledgeId(knowledgeId); + + // 全文検索エンジンから削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete(knowledgeId); + indexLogic.delete("WEB-" + knowledgeId); + } + + /** + * ナレッジに紐づくコメントを削除 + * + * @param knowledgeId + * @throws Exception + */ + private void deleteCommentsOnKnowledgeId(Long knowledgeId) throws Exception { + CommentsDao commentsDao = CommentsDao.get(); + List comments = commentsDao.selectOnKnowledgeId(knowledgeId); + if (comments != null) { + for (CommentsEntity commentsEntity : comments) { + deleteComment(commentsEntity); + } + } + } + + /** + * ユーザのナレッジを削除 TODO ものすごく多くのナレッジを登録したユーザの場合、それを全て削除するのは時間がかかるかも? ただ、非同期で実施して、「そのうち消えます」と表示するのも気持ち悪いと感じられるので、 いったん同期処理で1件づつ消す(効率的な消し方を検討する) + * + * @param loginUserId + * @throws Exception + */ + public void deleteOnUser(Integer loginUserId) throws Exception { + // ユーザのナレッジを取得 + List knowledgeIds = knowledgesDao.selectOnUser(loginUserId); + for (Long knowledgeId : knowledgeIds) { + delete(knowledgeId); + } + } + + /** + * 閲覧履歴を保持 + * + * @param knowledgeId + * @param loginedUser + */ + public void addViewHistory(Long knowledgeId, LoginedUser loginedUser) { + ViewHistoriesDao historiesDao = ViewHistoriesDao.get(); + ViewHistoriesEntity historiesEntity = new ViewHistoriesEntity(); + historiesEntity.setKnowledgeId(knowledgeId); + historiesEntity.setViewDateTime(new Timestamp(new Date().getTime())); + if (loginedUser != null) { + historiesEntity.setInsertUser(loginedUser.getUserId()); + } + historiesDao.insert(historiesEntity); + } + + /** + * いいね!を追加 + * + * @param knowledgeId + * @param loginedUser + * @return + */ + public Long addLike(Long knowledgeId, LoginedUser loginedUser) { + LikesDao likesDao = LikesDao.get(); + LikesEntity likesEntity = new LikesEntity(); + likesEntity.setKnowledgeId(knowledgeId); + likesDao.insert(likesEntity); + + updateKnowledgeExInfo(knowledgeId); + + Long count = likesDao.countOnKnowledgeId(knowledgeId); + + // 通知 + NotifyLogic.get().notifyOnKnowledgeLiked(knowledgeId, likesEntity); + + return count; + } + + /** + * ナレッジテーブルの タグやイイネ件数、コメント件数などの付加情報を 更新する(一覧表示用) + * + * @param knowledgeId + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public void updateKnowledgeExInfo(Long knowledgeId) { + // 一覧表示用の情報を更新 + KnowledgesDao knowledgesDao = KnowledgesDao.get(); + KnowledgesEntity entity = knowledgesDao.selectOnKey(knowledgeId); + updateKnowledgeExInfo(entity); + } + + /** + * ナレッジテーブルの タグやイイネ件数、コメント件数などの付加情報を 更新する(一覧表示用) + * + * @param entity + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public void updateKnowledgeExInfo(KnowledgesEntity entity) { + // タグ情報 + TagsDao tagsDao = TagsDao.get(); + List tags = tagsDao.selectOnKnowledgeId(entity.getKnowledgeId()); + StringJoinBuilder ids = new StringJoinBuilder(); + StringJoinBuilder names = new StringJoinBuilder(); + for (TagsEntity tagsEntity : tags) { + ids.append(tagsEntity.getTagId()); + names.append(tagsEntity.getTagName()); + } + entity.setTagIds(ids.join(",")); + entity.setTagNames(names.join(",")); + // いいね件数 + LikesDao likesDao = LikesDao.get(); + Long likeCount = likesDao.countOnKnowledgeId(entity.getKnowledgeId()); + entity.setLikeCount(likeCount); + // コメント件数 + CommentsDao commentsDao = CommentsDao.get(); + Integer commentCount = commentsDao.countOnKnowledgeId(entity.getKnowledgeId()); + entity.setCommentCount(commentCount); + + // 更新 + KnowledgesDao knowledgesDao = KnowledgesDao.get(); + knowledgesDao.physicalUpdate(entity); + } + + /** + * コメント保存 + * + * @param knowledgeId + * @param comment + * @param fileNos + * @throws Exception + */ + public void saveComment(Long knowledgeId, String comment, List fileNos, LoginedUser loginedUser) throws Exception { + CommentsDao commentsDao = CommentsDao.get(); + CommentsEntity commentsEntity = new CommentsEntity(); + commentsEntity.setKnowledgeId(knowledgeId); + commentsEntity.setComment(comment); + commentsEntity = commentsDao.insert(commentsEntity); + // 一覧表示用の情報を更新 + KnowledgeLogic.get().updateKnowledgeExInfo(knowledgeId); + + // 検索エンジンに追加 + addIndexOnComment(commentsEntity); + + // 添付ファイルを更新(紐付けをセット) + fileLogic.setKnowledgeFiles(knowledgeId, fileNos, loginedUser, commentsEntity.getCommentNo()); + + // 通知 + NotifyLogic.get().notifyOnKnowledgeComment(knowledgeId, commentsEntity); + } + + /** + * コメント更新 + * + * @param commentsEntity + * @param fileNos + * @param loginedUser + * @throws Exception + */ + public void updateComment(CommentsEntity commentsEntity, List fileNos, LoginedUser loginedUser) throws Exception { + CommentsDao commentsDao = CommentsDao.get(); + CommentsEntity updatedCommentsEntity = commentsDao.update(commentsEntity); + // 一覧表示用の情報を更新 + KnowledgeLogic.get().updateKnowledgeExInfo(updatedCommentsEntity.getKnowledgeId()); + + // 検索エンジンに追加 + addIndexOnComment(updatedCommentsEntity); + + // 添付ファイルを更新(紐付けをセット) + fileLogic.setKnowledgeFiles(updatedCommentsEntity.getKnowledgeId(), fileNos, loginedUser, updatedCommentsEntity.getCommentNo()); + } + + /** + * コメント削除 + * + * @param commentsEntity + * @param loginedUser + * @throws Exception + */ + public void deleteComment(CommentsEntity commentsEntity) throws Exception { + CommentsDao commentsDao = CommentsDao.get(); + commentsDao.delete(commentsEntity); + // 検索エンジンから削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete(COMMENT_ID_PREFIX + String.valueOf(commentsEntity.getCommentNo())); + } + + /** + * コメント削除 + * + * @param commentsEntity + * @param loginedUser + * @throws Exception + */ + public void deleteComment(CommentsEntity commentsEntity, LoginedUser loginedUser) throws Exception { + deleteComment(commentsEntity); + // 一覧表示用の情報を更新 + KnowledgeLogic.get().updateKnowledgeExInfo(commentsEntity.getKnowledgeId()); + // 添付ファイルを更新(紐付けをセット) + fileLogic.setKnowledgeFiles(commentsEntity.getKnowledgeId(), new ArrayList(), loginedUser, commentsEntity.getCommentNo()); + } + + /** + * コメントを全文検索エンジンへ登録 + * + * @param commentsEntity + * @throws Exception + */ + public void addIndexOnComment(CommentsEntity commentsEntity) throws Exception { + KnowledgesDao knowledgesDao = KnowledgesDao.get(); + KnowledgesEntity entity = knowledgesDao.selectOnKey(commentsEntity.getKnowledgeId()); + + IndexingValue indexingValue = new IndexingValue(); + indexingValue.setType(TYPE_COMMENT); + indexingValue.setId(COMMENT_ID_PREFIX + String.valueOf(commentsEntity.getCommentNo())); + indexingValue.setTitle(""); + indexingValue.setContents(commentsEntity.getComment()); + indexingValue.addUser(entity.getInsertUser()); + if (entity.getPublicFlag() == null || PUBLIC_FLAG_PUBLIC == entity.getPublicFlag()) { + indexingValue.addUser(ALL_USER); + } + + List tags = TagsDao.get().selectOnKnowledgeId(commentsEntity.getKnowledgeId()); + if (tags != null) { + for (TagsEntity tagsEntity : tags) { + indexingValue.addTag(tagsEntity.getTagId()); + } + } + if (entity.getPublicFlag() != null && entity.getPublicFlag().intValue() == PUBLIC_FLAG_PROTECT) { + TargetLogic targetLogic = TargetLogic.get(); + List targets = targetLogic.selectTargetsOnKnowledgeId(commentsEntity.getKnowledgeId()); + if (targets != null) { + for (LabelValue target : targets) { + Integer id = TargetLogic.get().getGroupId(target.getValue()); + if (id != Integer.MIN_VALUE) { + indexingValue.addGroup(id); + } + id = TargetLogic.get().getUserId(target.getValue()); + if (id != Integer.MIN_VALUE) { + indexingValue.addUser(id); + } + } + } + } + indexingValue.setCreator(commentsEntity.getInsertUser()); + indexingValue.setTime(commentsEntity.getUpdateDatetime().getTime()); // 更新日時をセットするので、更新日時でソート + + IndexLogic.get().save(indexingValue); // 全文検索のエンジンにも保存(DBに保存する意味ないかも) + } + + /** + * Re indexing + * @param knowledgesEntity + * @throws Exception + */ + public void reindexing(KnowledgesEntity knowledgesEntity) throws Exception { + // ナレッジの情報を検索エンジンへ更新 + List tags = tagsDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); + List targets = TargetLogic.get().selectTargetsOnKnowledgeId(knowledgesEntity.getKnowledgeId()); + + // 拡張値を取得 + TemplateMastersEntity template = TemplateMastersDao.get().selectWithItems(knowledgesEntity.getTypeId()); + List items = template.getItems(); + List values = KnowledgeItemValuesDao.get().selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); + for (TemplateItemsEntity item : items) { + for (KnowledgeItemValuesEntity val : values) { + if (item.getItemNo().equals(val.getItemNo())) { + item.setItemValue(val.getItemValue()); + } + } + } + // インデックス更新 + saveIndex(knowledgesEntity, tags, targets, template, knowledgesEntity.getInsertUser()); + + // コメントを検索エンジンへ + List comments = CommentsDao.get().selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); + for (CommentsEntity commentsEntity : comments) { + addIndexOnComment(commentsEntity); + } + + // 添付ファイルを検索エンジンへ + KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); + List filesEntities = filesDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId()); + for (KnowledgeFilesEntity knowledgeFilesEntity : filesEntities) { + // 添付ファイルのパースは、パースバッチに任せる(ステータスをパース待ちにしておけばバッチが処理する) + filesDao.changeStatus(knowledgeFilesEntity.getFileNo(), FileParseBat.PARSE_STATUS_WAIT, FileParseBat.UPDATE_USER_ID); + } + } + + /** + * ナレッジに対し編集権限があるかチェック + * + * @param loginedUser + * @param entity + * @param editors + * @return + */ + public boolean isEditor(LoginedUser loginedUser, KnowledgesEntity entity, List editors) { + if (loginedUser == null) { + // ログインしていないユーザに編集権限は無し + return false; + } + if (loginedUser.isAdmin()) { + return true; + } else { + if (entity != null) { + if (entity.getInsertUser().intValue() == loginedUser.getUserId().intValue()) { + return true; + } + } + for (LabelValue labelValue : editors) { + Integer id = TargetLogic.get().getGroupId(labelValue.getValue()); + if (id != Integer.MIN_VALUE) { + List groups = loginedUser.getGroups(); + if (groups != null) { + for (GroupsEntity groupsEntity : groups) { + if (groupsEntity.getGroupId().intValue() == id.intValue()) { + return true; + } + } + } + } else { + id = TargetLogic.get().getUserId(labelValue.getValue()); + if (id != Integer.MIN_VALUE) { + if (id.intValue() == loginedUser.getUserId().intValue()) { + return true; + } + } + } + } + } + return false; + } + + /** + * 一定期間で、「イイネ」の件数が多いものを一覧で取得 (イイネの件数が多い順で並べる) + * + * + * @param loginedUser + * @return + */ + public List getPopularityKnowledges(LoginedUser loginedUser, int offset, int limit) { + long now = new Date().getTime(); + LOG.info(now); + + long term = 1000L * 60L * 60L * 24L * 30L; + LOG.info(term); + long s = now - term; + LOG.info(s); + Timestamp start = new Timestamp(s); + LOG.info(start.getTime()); + LOG.info(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(start)); + + long e = now + (1000L * 60L * 60L * 24L * 1L); + LOG.info(e); + Timestamp end = new Timestamp(e); + LOG.info(end.getTime()); + LOG.info(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(end)); + + if (loginedUser != null && loginedUser.isAdmin()) { + return KnowledgesDao.get().selectPopularity(start, end, offset, limit); + } + + return KnowledgesDao.get().selectPopularityWithAccessControl(loginedUser, start, end, offset, limit); + } } diff --git a/src/main/java/org/support/project/knowledge/logic/NotifyCommentLogic.java b/src/main/java/org/support/project/knowledge/logic/NotifyCommentLogic.java new file mode 100644 index 000000000..7e15c69ca --- /dev/null +++ b/src/main/java/org/support/project/knowledge/logic/NotifyCommentLogic.java @@ -0,0 +1,188 @@ +package org.support.project.knowledge.logic; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.support.project.common.config.INT_FLAG; +import org.support.project.common.log.Log; +import org.support.project.common.log.LogFactory; +import org.support.project.di.Container; +import org.support.project.di.DI; +import org.support.project.di.Instance; +import org.support.project.knowledge.config.NotifyType; +import org.support.project.knowledge.dao.CommentsDao; +import org.support.project.knowledge.dao.ExUsersDao; +import org.support.project.knowledge.dao.NotifyConfigsDao; +import org.support.project.knowledge.dao.TargetsDao; +import org.support.project.knowledge.entity.CommentsEntity; +import org.support.project.knowledge.entity.KnowledgesEntity; +import org.support.project.knowledge.entity.NotifyConfigsEntity; +import org.support.project.knowledge.vo.GroupUser; +import org.support.project.web.dao.UsersDao; +import org.support.project.web.entity.GroupsEntity; +import org.support.project.web.entity.UsersEntity; + +/** + * Notification on comment. + * + * @author Koda + * + */ +@DI(instance = Instance.Singleton) +public class NotifyCommentLogic extends NotifyLogic { + /** ログ */ + private static final Log LOG = LogFactory.getLog(NotifyCommentLogic.class); + + /** + * インスタンスを取得 + * + * @return + */ + public static NotifyCommentLogic get() { + return Container.getComp(NotifyCommentLogic.class); + } + + + + + /** + * コメントが更新された際に、自分宛てのコメントが更新された場合に通知するを選択しているユーザの一覧を取得する + * @param notifyType + * + * @param comment + * @param knowledge + * @return + */ + public List getTargetUsersOnComment(NotifyType notifyType, CommentsEntity comment, KnowledgesEntity knowledge) { + CommentsDao commentsDao = CommentsDao.get(); + UsersDao usersDao = UsersDao.get(); + List users = new ArrayList<>(); // 宛先候補 + + if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PRIVATE) { + // 非公開のナレッジにコメントが登録された + // 登録者がコメントを登録しただけなので、通知の対象外で良い + return users; // 空のリストを返す + } else if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PUBLIC) { + // 公開なので、全てのユーザが宛先の対象 + // 自分が宛先でかつ、公開を除外しないユーザの一覧を取得 + int limit = 999999; // リミットは充分に大きい数にしておく + // (TODO 全てリストに入れるので、ユーザ数が膨大になったときには、OutOfMemoryが発生するが、そこまで大きな組織を想定していないのでいったんはこのまま) + if (notifyType == NotifyType.Desktop) { + List alluser = NotifyConfigsDao.get().getNotifyDesktopUsersOnPublicComment(limit, 0); + users.addAll(alluser); + } else if (notifyType == NotifyType.Mail) { + List alluser = NotifyConfigsDao.get().getNotifyMailUsersOnPublicComment(limit, 0); + users.addAll(alluser); + } + Iterator iterator = users.iterator(); + while (iterator.hasNext()) { + UsersEntity usersEntity = (UsersEntity) iterator.next(); + if (usersEntity.getUserId().intValue() == knowledge.getInsertUser().intValue()) { + // Knowledgeの登録者には通知しない(自分が登録したナレッジにコメントを登録したら通知するかどうかは別の設定) + iterator.remove(); + } + } + + // 公開を除外していても、自分が「コメント」を登録したナレッジの場合、通知する + List addCommentUsers = new ArrayList<>(); + List comments = commentsDao.selectOnKnowledgeId(knowledge.getKnowledgeId()); + for (CommentsEntity commentsEntity : comments) { + UsersEntity writedUser = usersDao.selectOnKey(commentsEntity.getInsertUser()); + if (!contains(addCommentUsers, writedUser)) { + addCommentUsers.add(writedUser); + } + } + iterator = addCommentUsers.iterator(); + while (iterator.hasNext()) { + UsersEntity usersEntity = (UsersEntity) iterator.next(); + // 宛先候補のユーザが、「自分宛てのナレッジ登録/更新で通知するか」の設定を確認 + NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get(); + NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(usersEntity.getUserId()); + if (notifyConfigsEntity == null) { + iterator.remove(); + } else if (!INT_FLAG.flagCheck(notifyConfigsEntity.getToItemComment())) { + iterator.remove(); + } else if (usersEntity.getUserId().intValue() == knowledge.getInsertUser().intValue()) { + // Knowledgeの登録者には通知しない(自分が登録したナレッジにコメントを登録したら通知するかどうかは別の設定) + iterator.remove(); + } else { + if (!contains(users, usersEntity)) { + users.add(usersEntity); + } + } + } + return users; + } else if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PROTECT) { + // 非公開の区分で、宛先が指定されている + + // ユーザ指定の宛先の一覧取得は全て宛先候補に入れる + TargetsDao targetsDao = TargetsDao.get(); + List targetUsers = targetsDao.selectUsersOnKnowledgeId(knowledge.getKnowledgeId()); + users.addAll(targetUsers); + + // グループ指定の宛先の場合、グループに所属しているユーザを宛先候補に入れる + List targetGroups = targetsDao.selectGroupsOnKnowledgeId(knowledge.getKnowledgeId()); + for (GroupsEntity groupsEntity : targetGroups) { + List groupUsers = ExUsersDao.get().selectGroupUser(groupsEntity.getGroupId(), 0, Integer.MAX_VALUE); + for (GroupUser groupUser : groupUsers) { + if (!contains(users, groupUser)) { + users.add(groupUser); + } + } + } + Iterator iterator = users.iterator(); + while (iterator.hasNext()) { + UsersEntity usersEntity = (UsersEntity) iterator.next(); + // 宛先候補のユーザが、「自分宛てのナレッジ登録/更新で通知するか」の設定を確認 + NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get(); + NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(usersEntity.getUserId()); + if (notifyConfigsEntity == null) { + iterator.remove(); + } else if (!INT_FLAG.flagCheck(notifyConfigsEntity.getToItemComment())) { + iterator.remove(); + } else if (usersEntity.getUserId().intValue() == knowledge.getInsertUser().intValue()) { + // Knowledgeの登録者には通知しない(自分が登録したナレッジにコメントを登録したら通知するかどうかは別の設定) + iterator.remove(); + } + } + + if (LOG.isDebugEnabled()) { + for (UsersEntity usersEntity : users) { + LOG.debug("notify on added comment." + usersEntity.getUserId()); + } + } + return users; + } + return users; + } + + /** + * 自分が登録されたナレッジにコメントが追加されたら通知するになっていた場合に、ナレッジ登録者のユーザを取得する + * + * @param knowledge + * @return + */ + public UsersEntity getInsertUserOnComment(NotifyType notifyType, CommentsEntity comment, KnowledgesEntity knowledge) { + if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PRIVATE) { + if (knowledge.getInsertUser() == comment.getInsertUser()) { + // ナレッジが非公開で、コメント登録したユーザがナレッジ登録ユーザであれば通知する必要は無い + return null; + } + } + UsersDao usersDao = UsersDao.get(); + UsersEntity user = usersDao.selectOnKey(knowledge.getInsertUser()); + if (user != null) { + NotifyConfigsDao notifyConfigsDao = NotifyConfigsDao.get(); + NotifyConfigsEntity notifyConfigsEntity = notifyConfigsDao.selectOnKey(user.getUserId()); + if (notifyConfigsEntity != null && INT_FLAG.flagCheck(notifyConfigsEntity.getMyItemComment())) { + if (notifyType == NotifyType.Desktop && INT_FLAG.flagCheck(notifyConfigsEntity.getNotifyDesktop())) { + return user; + } else if (notifyType == NotifyType.Mail && INT_FLAG.flagCheck(notifyConfigsEntity.getNotifyMail())) { + return user; + } + } + } + return null; + } +} diff --git a/src/main/java/org/support/project/knowledge/logic/NotifyLogic.java b/src/main/java/org/support/project/knowledge/logic/NotifyLogic.java index 6bf478132..ee1990d82 100644 --- a/src/main/java/org/support/project/knowledge/logic/NotifyLogic.java +++ b/src/main/java/org/support/project/knowledge/logic/NotifyLogic.java @@ -32,296 +32,301 @@ import org.support.project.web.dao.SystemConfigsDao; import org.support.project.web.entity.GroupsEntity; import org.support.project.web.entity.SystemConfigsEntity; - -@DI(instance=Instance.Singleton) +import org.support.project.web.entity.UsersEntity; +/** + * 通知を処理するロジック + * @author Koda + * + */ +@DI(instance = Instance.Singleton) public class NotifyLogic { - /** ログ */ - private static Log LOG = LogFactory.getLog(NotifyLogic.class); - - public static NotifyLogic get() { - return Container.getComp(NotifyLogic.class); - } - - /** - * 指定のナレッジにアクセスするURLを作成 - * @param knowledge - * @return - */ - public String makeURL(Long knowledgeId) { - SystemConfigsDao dao = SystemConfigsDao.get(); - SystemConfigsEntity config = dao.selectOnKey(SystemConfig.SYSTEM_URL, AppConfig.get().getSystemName()); - if (config == null) { - return ""; - } - - StringBuilder builder = new StringBuilder(); - builder.append(config.getConfigValue()); - if (!config.getConfigValue().endsWith("/")) { - builder.append("/"); - } - builder.append("open.knowledge/view/"); - builder.append(knowledgeId); - return builder.toString(); - } - - - /** - * 通知を処理 - * @param notify - */ - private void notify(Notify notify) { - // Mail通知 - NotifyQueuesDao notifyQueuesDao = NotifyQueuesDao.get(); - NotifyQueuesEntity notifyQueuesEntity = notify.getQueue(); - // 重複チェックし - if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_INSERT)) { - // ナレッジの新規登録は必ず通知のキューに入れる - notifyQueuesDao.insert(notifyQueuesEntity); - } else if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_UPDATE)) { - // ナレッジが更新された場合、キューに「登録通知」もしくは「更新通知」が存在しているのであれば登録しない - NotifyQueuesEntity exist = notifyQueuesDao.selectOnTypeAndId(notifyQueuesEntity.getType(), notifyQueuesEntity.getId()); - if (exist == null) { - exist = notifyQueuesDao.selectOnTypeAndId(Notify.TYPE_KNOWLEDGE_INSERT, notifyQueuesEntity.getId()); - if (exist == null) { - notifyQueuesDao.insert(notifyQueuesEntity); - } - } - } else if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_LIKE) - || NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_COMMENT)) { - NotifyQueuesEntity exist = notifyQueuesDao.selectOnTypeAndId(notifyQueuesEntity.getType(), notifyQueuesEntity.getId()); - if (exist == null) { - notifyQueuesDao.insert(notifyQueuesEntity); - } - } - - // Desktop通知 - NotifyAction notifyAction = Container.getComp(NotifyAction.class); - notifyAction.notifyObservers(notify); - } - - /** - * ナレッジが登録された際の通知 - * @param knowledgesEntity - */ - public void notifyOnKnowledgeInsert(KnowledgesEntity knowledgesEntity) { - Notify notify = new Notify(); - notify.inserted(knowledgesEntity); - notify(notify); - } + /** ログ */ + private static final Log LOG = LogFactory.getLog(NotifyLogic.class); + /** + * インスタンスを取得 + * @return + */ + public static NotifyLogic get() { + return Container.getComp(NotifyLogic.class); + } + + /** + * 指定のナレッジにアクセスするURLを作成 + * + * @param knowledge + * @return + */ + public String makeURL(Long knowledgeId) { + SystemConfigsDao dao = SystemConfigsDao.get(); + SystemConfigsEntity config = dao.selectOnKey(SystemConfig.SYSTEM_URL, AppConfig.get().getSystemName()); + if (config == null) { + return ""; + } + + StringBuilder builder = new StringBuilder(); + builder.append(config.getConfigValue()); + if (!config.getConfigValue().endsWith("/")) { + builder.append("/"); + } + builder.append("open.knowledge/view/"); + builder.append(knowledgeId); + return builder.toString(); + } + + /** + * 通知を処理 + * + * @param notify + */ + private void notify(Notify notify) { + // Mail通知 + NotifyQueuesDao notifyQueuesDao = NotifyQueuesDao.get(); + NotifyQueuesEntity notifyQueuesEntity = notify.getQueue(); + // 重複チェックし + if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_INSERT)) { + // ナレッジの新規登録は必ず通知のキューに入れる + notifyQueuesDao.insert(notifyQueuesEntity); + } else if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_UPDATE)) { + // ナレッジが更新された場合、キューに「登録通知」もしくは「更新通知」が存在しているのであれば登録しない + NotifyQueuesEntity exist = notifyQueuesDao.selectOnTypeAndId(notifyQueuesEntity.getType(), notifyQueuesEntity.getId()); + if (exist == null) { + exist = notifyQueuesDao.selectOnTypeAndId(Notify.TYPE_KNOWLEDGE_INSERT, notifyQueuesEntity.getId()); + if (exist == null) { + notifyQueuesDao.insert(notifyQueuesEntity); + } + } + } else if (NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_LIKE) + || NumberUtils.is(notifyQueuesEntity.getType(), Notify.TYPE_KNOWLEDGE_COMMENT)) { + NotifyQueuesEntity exist = notifyQueuesDao.selectOnTypeAndId(notifyQueuesEntity.getType(), notifyQueuesEntity.getId()); + if (exist == null) { + notifyQueuesDao.insert(notifyQueuesEntity); + } + } + + // Desktop通知は、多数のユーザを処理するので、別スレッドで処理する + Thread t = new Thread(new Runnable() { + @Override + public void run() { + LOG.debug("start notify"); + NotifyAction notifyAction = Container.getComp(NotifyAction.class); + notifyAction.notifyObservers(notify); + LOG.debug("end notify"); + } + }); + t.setDaemon(true); + t.start(); + } + + /** + * ナレッジが登録された際の通知 + * + * @param knowledgesEntity + */ + public void notifyOnKnowledgeInsert(KnowledgesEntity knowledgesEntity) { + Notify notify = new Notify(); + notify.inserted(knowledgesEntity); + notify(notify); + } + + /** + * ナレッジが更新された際の通知 + * + * @param knowledgesEntity + */ + public void notifyOnKnowledgeUpdate(KnowledgesEntity knowledgesEntity) { + Notify notify = new Notify(); + notify.updated(knowledgesEntity); + + notify(notify); + } + + /** + * ナレッジに「イイネ」が登録された際の通知 + * + * @param knowledgeId + */ + public void notifyOnKnowledgeLiked(Long knowledgeId, LikesEntity likesEntity) { + Notify notify = new Notify(); + notify.liked(likesEntity); + + notify(notify); + } + + /** + * ナレッジにコメントが追加された + * + * @param knowledgeId + */ + public void notifyOnKnowledgeComment(Long knowledgeId, CommentsEntity commentsEntity) { + Notify notify = new Notify(); + notify.commented(commentsEntity); + + notify(notify); + } + + /** + * 指定のナレッジは、自分宛てのナレッジかどうかを判定し、メッセージを取得する + * + * @param loginuser + * @param knowledge + * @return + */ + public MessageResult getInsertKnowledgeMessage(LoginedUser loginuser, Locale locale, KnowledgesEntity knowledge) { + if (isToKnowledgeSave(loginuser, knowledge)) { + MessageResult messageResult = new MessageResult(); + messageResult.setMessage( + Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.insert", String.valueOf(knowledge.getKnowledgeId()))); + messageResult.setResult(makeURL(knowledge.getKnowledgeId())); // Knowledgeへのリンク + return messageResult; + } + return null; + } + + /** + * 指定のナレッジは、自分宛てのナレッジかどうかを判定し、メッセージを取得する + * + * @param loginuser + * @param knowledge + * @return + */ + public MessageResult getUpdateKnowledgeMessage(LoginedUser loginuser, Locale locale, KnowledgesEntity knowledge) { + // TODO ストック機能ができたら、ストックしたナレッジの更新かどうかを判定する + if (isToKnowledgeSave(loginuser, knowledge)) { + MessageResult messageResult = new MessageResult(); + messageResult.setMessage( + Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.update", String.valueOf(knowledge.getKnowledgeId()))); + messageResult.setResult(makeURL(knowledge.getKnowledgeId())); // Knowledgeへのリンク + return messageResult; + } + return null; + } + + /** + * 自分宛てのナレッジが登録/更新されたか判定する + * + * @param loginuser + * @param knowledge + * @return + */ + private boolean isToKnowledgeSave(LoginedUser loginuser, KnowledgesEntity knowledge) { + NotifyConfigsDao dao = NotifyConfigsDao.get(); + NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); + if (!flagCheck(entity.getNotifyDesktop())) { + // デスクトップ通知対象外 + return false; + } +// 自分が登録した場合は、通知する必要無し?いったんは通知する +// if (knowledge.getInsertUser().intValue() == loginuser.getUserId().intValue()) { +// return false; +// } + if (!flagCheck(entity.getToItemSave())) { + // 自分宛てのナレッジが登録/更新されたら通知するがOFF + return false; + } + return isToKnowledge(loginuser, knowledge, entity); + } + + /** + * 指定のナレッジは、自分宛てのナレッジかどうかを判定する 自分宛ては以下の場合を言う ・公開区分が「公開」でかつ、「公開」でも通知する設定になっている場合 ・公開区分が「保護」でかつ、宛先に自分が入っている場合 + * + * @param loginuser + * @param knowledge + * @param entity + * @return + */ + private boolean isToKnowledge(LoginedUser loginuser, KnowledgesEntity knowledge, NotifyConfigsEntity entity) { + if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PUBLIC) { + // 公開のナレッジ + if (!flagCheck(entity.getToItemIgnorePublic())) { + // 公開も除外しない + return true; + } + } else if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PROTECT) { + // 保護のナレッジ + KnowledgeGroupsDao knowledgeGroupsDao = KnowledgeGroupsDao.get(); + List groupsEntities = knowledgeGroupsDao.selectOnKnowledgeId(knowledge.getKnowledgeId()); + List groups = loginuser.getGroups(); + for (KnowledgeGroupsEntity knowledgeGroupsEntity : groupsEntities) { + for (GroupsEntity groupsEntity : groups) { + if (knowledgeGroupsEntity.getGroupId().intValue() == groupsEntity.getGroupId().intValue()) { + return true; + } + } + } + + KnowledgeUsersDao knowledgeUsersDao = KnowledgeUsersDao.get(); + List usersEntities = knowledgeUsersDao.selectOnKnowledgeId(knowledge.getKnowledgeId()); + for (KnowledgeUsersEntity knowledgeUsersEntity : usersEntities) { + if (knowledgeUsersEntity.getUserId().intValue() == loginuser.getUserId().intValue()) { + return true; + } + } + } + return false; + } + + /** + * Integer型のフラグをチェック + * + * @param check + * @return + */ + public boolean flagCheck(Integer check) { + if (check == null) { + return false; + } + if (check.intValue() == INT_FLAG.ON.getValue()) { + return true; + } + return false; + } + + /** + * 指定の「イイネ」の追加で通知するかどうかを判定し、通知する場合はメッセージを取得する + * + * @param loginuser + * @param locale + * @param like + * @return + */ + public MessageResult getSaveLikeMessage(LoginedUser loginuser, Locale locale, LikesEntity like) { + NotifyConfigsDao dao = NotifyConfigsDao.get(); + NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); + if (!flagCheck(entity.getNotifyDesktop())) { + // デスクトップ通知対象外 + return null; + } + KnowledgesDao knowledgesDao = KnowledgesDao.get(); + KnowledgesEntity knowledge = knowledgesDao.selectOnKey(like.getKnowledgeId()); - /** - * ナレッジが更新された際の通知 - * @param knowledgesEntity - */ - public void notifyOnKnowledgeUpdate(KnowledgesEntity knowledgesEntity) { - Notify notify = new Notify(); - notify.updated(knowledgesEntity); - - notify(notify); - } - - - /** - * ナレッジに「イイネ」が登録された際の通知 - * @param knowledgeId - */ - public void notifyOnKnowledgeLiked(Long knowledgeId, LikesEntity likesEntity) { - Notify notify = new Notify(); - notify.liked(likesEntity); - - notify(notify); - } - - /** - * ナレッジにコメントが追加された - * @param knowledgeId - */ - public void notifyOnKnowledgeComment(Long knowledgeId, CommentsEntity commentsEntity) { - Notify notify = new Notify(); - notify.commented(commentsEntity); - - notify(notify); - } + if (flagCheck(entity.getMyItemLike()) && knowledge.getInsertUser().intValue() == loginuser.getUserId().intValue()) { + // 自分で投稿したナレッジにイイネが押されたので通知 + MessageResult messageResult = new MessageResult(); + messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.myitem.like", + String.valueOf(knowledge.getKnowledgeId()))); + messageResult.setResult(makeURL(knowledge.getKnowledgeId())); // Knowledgeへのリンク + return messageResult; + } - - - - /** - * 指定のナレッジは、自分宛てのナレッジかどうかを判定し、メッセージを取得する - * @param loginuser - * @param knowledge - * @return - */ - public MessageResult getInsertKnowledgeMessage(LoginedUser loginuser, Locale locale, KnowledgesEntity knowledge) { - if (isToKnowledgeSave(loginuser, knowledge)) { - MessageResult messageResult = new MessageResult(); - messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.insert", String.valueOf(knowledge.getKnowledgeId()))); - messageResult.setResult(makeURL(knowledge.getKnowledgeId()));// Knowledgeへのリンク - return messageResult; - } - return null; - } - - /** - * 指定のナレッジは、自分宛てのナレッジかどうかを判定し、メッセージを取得する - * @param loginuser - * @param knowledge - * @return - */ - public MessageResult getUpdateKnowledgeMessage(LoginedUser loginuser, Locale locale, KnowledgesEntity knowledge) { - //TODO ストック機能ができたら、ストックしたナレッジの更新かどうかを判定する - if (isToKnowledgeSave(loginuser, knowledge)) { - MessageResult messageResult = new MessageResult(); - messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.update", String.valueOf(knowledge.getKnowledgeId()))); - messageResult.setResult(makeURL(knowledge.getKnowledgeId()));// Knowledgeへのリンク - return messageResult; - } - return null; - } - - - /** - * 自分宛てのナレッジが登録/更新されたか判定する - * - * @param loginuser - * @param knowledge - * @return - */ - private boolean isToKnowledgeSave(LoginedUser loginuser, KnowledgesEntity knowledge) { - NotifyConfigsDao dao = NotifyConfigsDao.get(); - NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); - if (!flagCheck(entity.getNotifyDesktop())) { - //デスクトップ通知対象外 - return false; - } - if (knowledge.getInsertUser().intValue() == loginuser.getUserId().intValue()) { - //TODO 自分が登録した場合は、通知する必要無し?いったんは通知する - // return false; - } - if (!flagCheck(entity.getToItemSave())) { - // 自分宛てのナレッジが登録/更新されたら通知するがOFF - return false; - } - return isToKnowledge(loginuser, knowledge, entity); - } - - /** - * 指定のナレッジは、自分宛てのナレッジかどうかを判定する - * 自分宛ては以下の場合を言う - * ・公開区分が「公開」でかつ、「公開」でも通知する設定になっている場合 - * ・公開区分が「保護」でかつ、宛先に自分が入っている場合 - * - * @param loginuser - * @param knowledge - * @param entity - * @return - */ - private boolean isToKnowledge(LoginedUser loginuser, KnowledgesEntity knowledge, NotifyConfigsEntity entity) { - if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PUBLIC) { - // 公開のナレッジ - if (!flagCheck(entity.getToItemIgnorePublic())) { - // 公開も除外しない - return true; - } - } else if (knowledge.getPublicFlag() == KnowledgeLogic.PUBLIC_FLAG_PROTECT) { - // 保護のナレッジ - KnowledgeGroupsDao knowledgeGroupsDao = KnowledgeGroupsDao.get(); - List groupsEntities = knowledgeGroupsDao.selectOnKnowledgeId(knowledge.getKnowledgeId()); - List groups = loginuser.getGroups(); - for (KnowledgeGroupsEntity knowledgeGroupsEntity : groupsEntities) { - for (GroupsEntity groupsEntity : groups) { - if (knowledgeGroupsEntity.getGroupId().intValue() == groupsEntity.getGroupId().intValue()) { - return true; - } - } - } - - KnowledgeUsersDao knowledgeUsersDao = KnowledgeUsersDao.get(); - List usersEntities = knowledgeUsersDao.selectOnKnowledgeId(knowledge.getKnowledgeId()); - for (KnowledgeUsersEntity knowledgeUsersEntity : usersEntities) { - if (knowledgeUsersEntity.getUserId().intValue() == loginuser.getUserId().intValue()) { - return true; - } - } - } - return false; - } - /** - * Integer型のフラグをチェック - * @param check - * @return - */ - public boolean flagCheck(Integer check) { - if (check == null) { - return false; - } - if (check.intValue() == INT_FLAG.ON.getValue()) { - return true; - } - return false; - } - - - /** - * 指定のコメントの更新で通知するかどうかを判定し、通知する場合は通知するメッセージを取得する - * @param loginuser - * @param locale - * @param comment - * @return - */ - public MessageResult getSaveCommentMessage(LoginedUser loginuser, Locale locale, CommentsEntity comment) { - NotifyConfigsDao dao = NotifyConfigsDao.get(); - NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); - if (!flagCheck(entity.getNotifyDesktop())) { - //デスクトップ通知対象外 - return null; - } - KnowledgesDao knowledgesDao = KnowledgesDao.get(); - KnowledgesEntity knowledge = knowledgesDao.selectOnKey(comment.getKnowledgeId()); - if (flagCheck(entity.getMyItemComment()) && knowledge.getInsertUser().intValue() == loginuser.getUserId().intValue()) { - // 自分で投稿したナレッジにコメントがついたので通知 - MessageResult messageResult = new MessageResult(); - messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.myitem.comment", String.valueOf(knowledge.getKnowledgeId()))); - messageResult.setResult(makeURL(knowledge.getKnowledgeId()));// Knowledgeへのリンク - return messageResult; - } - - if (flagCheck(entity.getToItemComment()) && isToKnowledge(loginuser, knowledge, entity)) { - // 自分宛てのナレッジにコメントがついたので通知 - MessageResult messageResult = new MessageResult(); - messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.comment", String.valueOf(knowledge.getKnowledgeId()))); - messageResult.setResult(makeURL(knowledge.getKnowledgeId()));// Knowledgeへのリンク - return messageResult; - } - return null; - } - /** - * 指定の「イイネ」の追加で通知するかどうかを判定し、通知する場合はメッセージを取得する - * @param loginuser - * @param locale - * @param like - * @return - */ - public MessageResult getSaveLikeMessage(LoginedUser loginuser, Locale locale, LikesEntity like) { - NotifyConfigsDao dao = NotifyConfigsDao.get(); - NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); - if (!flagCheck(entity.getNotifyDesktop())) { - //デスクトップ通知対象外 - return null; - } - KnowledgesDao knowledgesDao = KnowledgesDao.get(); - KnowledgesEntity knowledge = knowledgesDao.selectOnKey(like.getKnowledgeId()); - - if (flagCheck(entity.getMyItemLike()) && knowledge.getInsertUser().intValue() == loginuser.getUserId().intValue()) { - // 自分で投稿したナレッジにイイネが押されたので通知 - MessageResult messageResult = new MessageResult(); - messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.myitem.like", String.valueOf(knowledge.getKnowledgeId()))); - messageResult.setResult(makeURL(knowledge.getKnowledgeId()));// Knowledgeへのリンク - return messageResult; - } - - return null; - } + return null; + } + + + + /** + * 既に指定のユーザが追加されているのか確認 + * @param users + * @param groupUser + * @return + */ + protected boolean contains(List users, UsersEntity groupUser) { + for (UsersEntity usersEntity : users) { + if (usersEntity.equalsOnKey(groupUser)) { + return true; + } + } + return false; + } + - } diff --git a/src/main/java/org/support/project/knowledge/logic/UploadedFileLogic.java b/src/main/java/org/support/project/knowledge/logic/UploadedFileLogic.java index 740202fcd..90e77b5a9 100644 --- a/src/main/java/org/support/project/knowledge/logic/UploadedFileLogic.java +++ b/src/main/java/org/support/project/knowledge/logic/UploadedFileLogic.java @@ -16,276 +16,277 @@ import org.support.project.di.Instance; import org.support.project.knowledge.dao.KnowledgeFilesDao; import org.support.project.knowledge.entity.KnowledgeFilesEntity; -import org.support.project.knowledge.indexer.IndexingValue; import org.support.project.knowledge.vo.UploadFile; import org.support.project.web.bean.LoginedUser; -@DI(instance=Instance.Singleton) +/** + * Logic for attached file. + * @author Koda + */ +@DI(instance = Instance.Singleton) public class UploadedFileLogic { - /** ログ */ - private static Log LOG = LogFactory.getLog(UploadedFileLogic.class); + /** ログ */ + private static final Log LOG = LogFactory.getLog(UploadedFileLogic.class); + /** KnowledgeFilesDao */ + private KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); + /** Get instance */ + public static UploadedFileLogic get() { + return Container.getComp(UploadedFileLogic.class); + } - private KnowledgeFilesDao filesDao = KnowledgeFilesDao.get(); - - public static UploadedFileLogic get() { - return Container.getComp(UploadedFileLogic.class); - } + /** + * ファイルを保存する + * + * @param fileItem + * @param loginedUser + * @param context + * @return + * @throws IOException + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public UploadFile saveFile(FileItem fileItem, LoginedUser loginedUser, String context) throws IOException { + LOG.trace("saveFile()"); + KnowledgeFilesEntity entity = new KnowledgeFilesEntity(); + entity.setFileName(fileItem.getName()); + entity.setFileSize(new Double(fileItem.getSize())); + entity.setFileBinary(fileItem.getInputStream()); + entity.setParseStatus(0); + entity = filesDao.insert(entity); + UploadFile file = convUploadFile(context, entity); + // 処理が完了したら、テンポラリのファイルを削除 + fileItem.delete(); + return file; + } - /** - * ファイルを保存する - * @param fileItem - * @param loginedUser - * @param context - * @return - * @throws IOException - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public UploadFile saveFile(FileItem fileItem, LoginedUser loginedUser, String context) throws IOException { - LOG.trace("saveFile()"); - KnowledgeFilesEntity entity = new KnowledgeFilesEntity(); - entity.setFileName(fileItem.getName()); - entity.setFileSize(new Double(fileItem.getSize())); - entity.setFileBinary(fileItem.getInputStream()); - entity.setParseStatus(0); - entity = filesDao.insert(entity); - UploadFile file = convUploadFile(context, entity); - //処理が完了したら、テンポラリのファイルを削除 - fileItem.delete(); - return file; - } - - /** - * KnowledgeFilesEntity の情報から、画面に戻す UploadFile の情報を生成する - * @param context - * @param entity - * @return - */ - private UploadFile convUploadFile(String context, KnowledgeFilesEntity entity) { - UploadFile file = new UploadFile(); - file.setFileNo(entity.getFileNo()); - file.setUrl(context + "/open.file/download?fileNo=" + entity.getFileNo()); - //file.setThumbnailUrl(context + "/open.file/thumbnai?fileNo=" + entity.getFileNo()); - file.setThumbnailUrl(context + "/bower/teambox.free-file-icons/32px/_blank.png"); - file.setName(entity.getFileName()); - file.setType("-"); - file.setSize(entity.getFileSize()); - file.setDeleteUrl(context + "/protect.file/delete?fileNo=" + entity.getFileNo()); - file.setDeleteType("DELETE"); - if (entity.getKnowledgeId() != null && 0 != entity.getKnowledgeId().longValue()) { - file.setKnowlegeId(entity.getKnowledgeId()); - } - file.setCommentNo(entity.getCommentNo()); - return file; - } + /** + * KnowledgeFilesEntity の情報から、画面に戻す UploadFile の情報を生成する + * + * @param context + * @param entity + * @return + */ + private UploadFile convUploadFile(String context, KnowledgeFilesEntity entity) { + UploadFile file = new UploadFile(); + file.setFileNo(entity.getFileNo()); + file.setUrl(context + "/open.file/download?fileNo=" + entity.getFileNo()); + // file.setThumbnailUrl(context + "/open.file/thumbnai?fileNo=" + entity.getFileNo()); + file.setThumbnailUrl(context + "/bower/teambox.free-file-icons/32px/_blank.png"); + file.setName(entity.getFileName()); + file.setType("-"); + file.setSize(entity.getFileSize()); + file.setDeleteUrl(context + "/protect.file/delete?fileNo=" + entity.getFileNo()); + file.setDeleteType("DELETE"); + if (entity.getKnowledgeId() != null && 0 != entity.getKnowledgeId().longValue()) { + file.setKnowlegeId(entity.getKnowledgeId()); + } + file.setCommentNo(entity.getCommentNo()); + return file; + } - - - /** - * ナレッジに紐付く添付ファイルの情報を更新 - * - * 1. 現在のナレッジに紐づくファイルを一覧取得する - * 2. 紐づけるファイルの番号のファイルが存在しない場合、ファイル番号でファイル情報を取得 - * 2-1. 取得したファイル情報に、ナレッジ番号をセットし更新 - * 3. 1で取得したファイル一覧から、処理済(紐付け済)ファイル番号を削除する - * 4. 1で取得したファイルの一覧で、残っているものがあれば、そのファイルは削除(紐付けがきれている) - * - * @param entity - * @param fileNos - * @param loginedUser - * @param commentNo - * @throws Exception - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public void setKnowledgeFiles(Long knowledgeId, List fileNos, LoginedUser loginedUser) throws Exception { - // 現在、すでに紐づいている添付ファイルを取得 - List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); - Map filemap = new HashMap<>(); - for (KnowledgeFilesEntity entity : filesEntities) { - if (entity.getCommentNo() == null) { - filemap.put(entity.getFileNo(), entity); - } - } - - // 画面で設定されている添付ファイルの番号で紐付けを作成 - for (Long fileNo : fileNos) { - KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); - if (entity != null) { - if (entity.getKnowledgeId() == null || 0 == entity.getKnowledgeId().longValue()) { - filesDao.connectKnowledge(fileNo, knowledgeId, null, loginedUser); - } - } - filemap.remove(fileNo); - } - - // 始めに取得した一覧で、紐付けが作成されなかった(=紐付けが切れた)ファイルを削除 - Iterator iterator = filemap.keySet().iterator(); - while (iterator.hasNext()) { - Long fileNo = (Long) iterator.next(); - //filesDao.delete(fileNo); - filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する - // 全文検索エンジンから情報を削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete("FILE-" + fileNo); - } - } - - /** - * コメントでナレッジに紐付く添付ファイルを更新 - * - * 1. 現在のナレッジに紐づくファイルを一覧取得する - * 2. 紐づけるファイルの番号のファイルが存在しない場合、ファイル番号でファイル情報を取得 - * 2-1. 取得したファイル情報に、ナレッジ番号をセットし更新 - * 3. 1で取得したファイル一覧から、処理済(紐付け済)ファイル番号を削除する - * 4. 1で取得したファイルの一覧で、残っているものがあれば、そのファイルは削除(紐付けがきれている) - * - * @param entity - * @param fileNos - * @param loginedUser - * @param commentNo - * @throws Exception - */ - @Aspect(advice=org.support.project.ormapping.transaction.Transaction.class) - public void setKnowledgeFiles(Long knowledgeId, List fileNos, LoginedUser loginedUser, Long commentNo) throws Exception { - // 現在、すでに紐づいている添付ファイルを取得 - List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); - Map filemap = new HashMap<>(); - for (KnowledgeFilesEntity entity : filesEntities) { - if (entity.getCommentNo() != null && entity.getCommentNo().equals(commentNo)) { - filemap.put(entity.getFileNo(), entity); - } - } - // 画面で設定されている添付ファイルの番号で紐付けを作成 - for (Long fileNo : fileNos) { - KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); - if (entity != null) { - if (entity.getKnowledgeId() == null || 0 == entity.getKnowledgeId().longValue()) { - filesDao.connectKnowledge(fileNo, knowledgeId, commentNo, loginedUser); - } - } - filemap.remove(fileNo); - } - - // 始めに取得した一覧で、紐付けが作成されなかった(=紐付けが切れた)ファイルを削除 - Iterator iterator = filemap.keySet().iterator(); - while (iterator.hasNext()) { - Long fileNo = (Long) iterator.next(); - //filesDao.delete(fileNo); - filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する - - // 全文検索エンジンから情報を削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete("FILE-" + fileNo); - } - } - - - /** - * ファイルを削除する - * @param fileNo - * @param loginedUser - * @throws Exception - */ - public void removeFile(Long fileNo, LoginedUser loginedUser) throws Exception { - // DBのデータを削除 - filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する - - // 全文検索エンジンから情報を削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete("FILE-" + fileNo); - } + /** + * ナレッジに紐付く添付ファイルの情報を更新 + * + * 1. 現在のナレッジに紐づくファイルを一覧取得する 2. 紐づけるファイルの番号のファイルが存在しない場合、ファイル番号でファイル情報を取得 2-1. 取得したファイル情報に、ナレッジ番号をセットし更新 3. 1で取得したファイル一覧から、処理済(紐付け済)ファイル番号を削除する 4. + * 1で取得したファイルの一覧で、残っているものがあれば、そのファイルは削除(紐付けがきれている) + * + * @param entity + * @param fileNos + * @param loginedUser + * @param commentNo + * @throws Exception + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public void setKnowledgeFiles(Long knowledgeId, List fileNos, LoginedUser loginedUser) throws Exception { + // 現在、すでに紐づいている添付ファイルを取得 + List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); + Map filemap = new HashMap<>(); + for (KnowledgeFilesEntity entity : filesEntities) { + if (entity.getCommentNo() == null) { + filemap.put(entity.getFileNo(), entity); + } + } - /** - * 指定のナレッジに紐づく添付ファイルの情報を取得 - * @param knowledgeId - * @param context - * @return - */ - public List selectOnKnowledgeId(Long knowledgeId, String context) { - List files = new ArrayList(); - List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); - for (KnowledgeFilesEntity entity : filesEntities) { - files.add(convUploadFile(context, entity)); - } - return files; - } + // 画面で設定されている添付ファイルの番号で紐付けを作成 + if (fileNos != null) { + for (Long fileNo : fileNos) { + KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); + if (entity != null) { + if (entity.getKnowledgeId() == null || 0 == entity.getKnowledgeId().longValue()) { + filesDao.connectKnowledge(fileNo, knowledgeId, null, loginedUser); + } + } + filemap.remove(fileNo); + } + } - /** - * 指定のナレッジに紐づく添付ファイルの情報を取得 - * @param knowledgeId - * @param context - * @return - */ - public List selectOnKnowledgeIdWithoutCommentFiles(Long knowledgeId, String context) { - List files = new ArrayList(); - List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); - for (KnowledgeFilesEntity entity : filesEntities) { - if (entity.getCommentNo() == null || entity.getCommentNo() == 0) { - files.add(convUploadFile(context, entity)); - } - } - return files; - } + // 始めに取得した一覧で、紐付けが作成されなかった(=紐付けが切れた)ファイルを削除 + Iterator iterator = filemap.keySet().iterator(); + while (iterator.hasNext()) { + Long fileNo = (Long) iterator.next(); + // filesDao.delete(fileNo); + filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する + // 全文検索エンジンから情報を削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete("FILE-" + fileNo); + } + } - - /** - * 指定の添付ファイル番号の情報を取得 - * @param fileNos - * @param context - * @return - */ - public List selectOnFileNos(List fileNos, String context) { - List files = new ArrayList(); - for (Long fileNo : fileNos) { - KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); - files.add(convUploadFile(context, entity)); - } - return files; - } + /** + * コメントでナレッジに紐付く添付ファイルを更新 + * + * 1. 現在のナレッジに紐づくファイルを一覧取得する 2. 紐づけるファイルの番号のファイルが存在しない場合、ファイル番号でファイル情報を取得 2-1. 取得したファイル情報に、ナレッジ番号をセットし更新 3. 1で取得したファイル一覧から、処理済(紐付け済)ファイル番号を削除する 4. + * 1で取得したファイルの一覧で、残っているものがあれば、そのファイルは削除(紐付けがきれている) + * + * @param entity + * @param fileNos + * @param loginedUser + * @param commentNo + * @throws Exception + */ + @Aspect(advice = org.support.project.ormapping.transaction.Transaction.class) + public void setKnowledgeFiles(Long knowledgeId, List fileNos, LoginedUser loginedUser, Long commentNo) throws Exception { + // 現在、すでに紐づいている添付ファイルを取得 + List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); + Map filemap = new HashMap<>(); + for (KnowledgeFilesEntity entity : filesEntities) { + if (entity.getCommentNo() != null && entity.getCommentNo().equals(commentNo)) { + filemap.put(entity.getFileNo(), entity); + } + } + // 画面で設定されている添付ファイルの番号で紐付けを作成 + for (Long fileNo : fileNos) { + KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); + if (entity != null) { + if (entity.getKnowledgeId() == null || 0 == entity.getKnowledgeId().longValue()) { + filesDao.connectKnowledge(fileNo, knowledgeId, commentNo, loginedUser); + } + } + filemap.remove(fileNo); + } - /** - * ナレッジを削除する際に、添付ファイルを削除 - * @param knowledgeId - * @throws Exception - */ - public void deleteOnKnowledgeId(Long knowledgeId) throws Exception { - List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); - for (KnowledgeFilesEntity entity : filesEntities) { - filesDao.physicalDelete(entity.getFileNo()); - // 全文検索エンジンから情報を削除 - IndexLogic indexLogic = IndexLogic.get(); - indexLogic.delete("FILE-" + entity.getFileNo()); - } - } - - - /** - * ダウンロードする添付ファイルを取得 - * (取得して良い権限が無い場合はNULLを返す) - * @param fileNo - * @param loginedUser - * @return - */ - public KnowledgeFilesEntity getFile(Long fileNo, LoginedUser loginedUser) { - KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); - if (entity == null) { - return null; - } - if (entity.getKnowledgeId() != null && 0 != entity.getKnowledgeId().longValue()) { - // ナレッジに紐づいている場合、そのナレッジにアクセスできれば添付ファイルにアクセス可能 - KnowledgeLogic knowledgeLogic = KnowledgeLogic.get(); - if (knowledgeLogic.select(entity.getKnowledgeId(), loginedUser) == null) { - return null; - } - } else { - // ナレッジに紐づいていない場合、登録者のみがアクセス可能 - if (loginedUser == null) { - return null; - } - if (entity.getInsertUser().intValue() != loginedUser.getUserId().intValue()) { - return null; - } - } - return filesDao.selectOnKey(fileNo); - } + // 始めに取得した一覧で、紐付けが作成されなかった(=紐付けが切れた)ファイルを削除 + Iterator iterator = filemap.keySet().iterator(); + while (iterator.hasNext()) { + Long fileNo = (Long) iterator.next(); + // filesDao.delete(fileNo); + filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する + + // 全文検索エンジンから情報を削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete("FILE-" + fileNo); + } + } + + /** + * ファイルを削除する + * + * @param fileNo + * @param loginedUser + * @throws Exception + */ + public void removeFile(Long fileNo, LoginedUser loginedUser) throws Exception { + // DBのデータを削除 + filesDao.physicalDelete(fileNo); // バイナリは大きいので、物理削除する + + // 全文検索エンジンから情報を削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete("FILE-" + fileNo); + } + + /** + * 指定のナレッジに紐づく添付ファイルの情報を取得 + * + * @param knowledgeId + * @param context + * @return + */ + public List selectOnKnowledgeId(Long knowledgeId, String context) { + List files = new ArrayList(); + List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); + for (KnowledgeFilesEntity entity : filesEntities) { + files.add(convUploadFile(context, entity)); + } + return files; + } + + /** + * 指定のナレッジに紐づく添付ファイルの情報を取得 + * + * @param knowledgeId + * @param context + * @return + */ + public List selectOnKnowledgeIdWithoutCommentFiles(Long knowledgeId, String context) { + List files = new ArrayList(); + List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); + for (KnowledgeFilesEntity entity : filesEntities) { + if (entity.getCommentNo() == null || entity.getCommentNo() == 0) { + files.add(convUploadFile(context, entity)); + } + } + return files; + } + + /** + * 指定の添付ファイル番号の情報を取得 + * + * @param fileNos + * @param context + * @return + */ + public List selectOnFileNos(List fileNos, String context) { + List files = new ArrayList(); + for (Long fileNo : fileNos) { + KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); + files.add(convUploadFile(context, entity)); + } + return files; + } + + /** + * ナレッジを削除する際に、添付ファイルを削除 + * + * @param knowledgeId + * @throws Exception + */ + public void deleteOnKnowledgeId(Long knowledgeId) throws Exception { + List filesEntities = filesDao.selectOnKnowledgeId(knowledgeId); + for (KnowledgeFilesEntity entity : filesEntities) { + filesDao.physicalDelete(entity.getFileNo()); + // 全文検索エンジンから情報を削除 + IndexLogic indexLogic = IndexLogic.get(); + indexLogic.delete("FILE-" + entity.getFileNo()); + } + } + + /** + * ダウンロードする添付ファイルを取得 (取得して良い権限が無い場合はNULLを返す) + * + * @param fileNo + * @param loginedUser + * @return + */ + public KnowledgeFilesEntity getFile(Long fileNo, LoginedUser loginedUser) { + KnowledgeFilesEntity entity = filesDao.selectOnKeyWithoutBinary(fileNo); + if (entity == null) { + return null; + } + if (entity.getKnowledgeId() != null && 0 != entity.getKnowledgeId().longValue()) { + // ナレッジに紐づいている場合、そのナレッジにアクセスできれば添付ファイルにアクセス可能 + KnowledgeLogic knowledgeLogic = KnowledgeLogic.get(); + if (knowledgeLogic.select(entity.getKnowledgeId(), loginedUser) == null) { + return null; + } + } else { + // ナレッジに紐づいていない場合、登録者のみがアクセス可能 + if (loginedUser == null) { + return null; + } + if (entity.getInsertUser().intValue() != loginedUser.getUserId().intValue()) { + return null; + } + } + return filesDao.selectOnKey(fileNo); + } } diff --git a/src/main/java/org/support/project/knowledge/vo/Notify.java b/src/main/java/org/support/project/knowledge/vo/Notify.java index 855c8aa6a..ff40e4d09 100644 --- a/src/main/java/org/support/project/knowledge/vo/Notify.java +++ b/src/main/java/org/support/project/knowledge/vo/Notify.java @@ -1,83 +1,162 @@ package org.support.project.knowledge.vo; +import java.util.List; import java.util.Locale; +import org.support.project.common.config.Resources; import org.support.project.common.util.RandomUtil; +import org.support.project.common.util.StringUtils; +import org.support.project.knowledge.config.NotifyType; +import org.support.project.knowledge.dao.KnowledgesDao; +import org.support.project.knowledge.dao.NotifyConfigsDao; import org.support.project.knowledge.entity.CommentsEntity; import org.support.project.knowledge.entity.KnowledgesEntity; import org.support.project.knowledge.entity.LikesEntity; +import org.support.project.knowledge.entity.NotifyConfigsEntity; import org.support.project.knowledge.entity.NotifyQueuesEntity; +import org.support.project.knowledge.logic.NotifyCommentLogic; import org.support.project.knowledge.logic.NotifyLogic; import org.support.project.web.bean.LoginedUser; import org.support.project.web.bean.MessageResult; +import org.support.project.web.entity.UsersEntity; +/** + * Class for notify + * @author Koda + */ public class Notify { - - public static final int TYPE_KNOWLEDGE_INSERT = 1; - public static final int TYPE_KNOWLEDGE_UPDATE = 2; - public static final int TYPE_KNOWLEDGE_COMMENT = 11; - public static final int TYPE_KNOWLEDGE_LIKE = 21; - - private int type; - - private KnowledgesEntity knowledge; - private CommentsEntity comment; - private LikesEntity like; - - public void inserted(KnowledgesEntity entity) { - type = TYPE_KNOWLEDGE_INSERT; - knowledge = entity; - } - public void updated(KnowledgesEntity entity) { - type = TYPE_KNOWLEDGE_UPDATE; - knowledge = entity; - } - public void commented(CommentsEntity commentsEntity) { - type = TYPE_KNOWLEDGE_COMMENT; - comment = commentsEntity; - } - public void liked(LikesEntity entity) { - type = TYPE_KNOWLEDGE_LIKE; - like = entity; - } - - /** - * 通知するメッセージを取得 - * @param loginuser - * @param locale - * @return - */ - public MessageResult getMessage(LoginedUser loginuser, Locale locale) { - if (type == TYPE_KNOWLEDGE_INSERT) { - return NotifyLogic.get().getInsertKnowledgeMessage(loginuser, locale, knowledge); - } else if (type == TYPE_KNOWLEDGE_UPDATE) { - return NotifyLogic.get().getUpdateKnowledgeMessage(loginuser, locale, knowledge); - } else if (type == TYPE_KNOWLEDGE_COMMENT) { - return NotifyLogic.get().getSaveCommentMessage(loginuser, locale, comment); - } else if (type == TYPE_KNOWLEDGE_LIKE) { - return NotifyLogic.get().getSaveLikeMessage(loginuser, locale, like); - } - return null; - } - /** - * メール通知のキューを取得 - * @return - */ - public NotifyQueuesEntity getQueue() { - NotifyQueuesEntity entity = new NotifyQueuesEntity(); - entity.setHash(RandomUtil.randamGen(30)); - entity.setType(type); - - if (type == TYPE_KNOWLEDGE_INSERT || type == TYPE_KNOWLEDGE_UPDATE) { - entity.setId(knowledge.getKnowledgeId()); - } else if (type == TYPE_KNOWLEDGE_COMMENT) { - entity.setId(comment.getCommentNo()); - } else if (type == TYPE_KNOWLEDGE_LIKE) { - entity.setId(like.getNo()); - } - return entity; - } - - - + /** notify type: insert knowledge */ + public static final int TYPE_KNOWLEDGE_INSERT = 1; + /** notify type: update knowledge */ + public static final int TYPE_KNOWLEDGE_UPDATE = 2; + /** notify type: add comment */ + public static final int TYPE_KNOWLEDGE_COMMENT = 11; + /** notify type: add like */ + public static final int TYPE_KNOWLEDGE_LIKE = 21; + + /** notify type */ + private int type; + + /** Updated KnowledgesEntity */ + private KnowledgesEntity knowledge; + /** Added CommentsEntity */ + private CommentsEntity comment; + /** Added LikesEntity */ + private LikesEntity like; + + /** + * Knowledge inserted. + * @param entity + */ + public void inserted(KnowledgesEntity entity) { + type = TYPE_KNOWLEDGE_INSERT; + knowledge = entity; + } + /** + * Knowledge updated. + * @param entity + */ + public void updated(KnowledgesEntity entity) { + type = TYPE_KNOWLEDGE_UPDATE; + knowledge = entity; + } + /** + * Comment added. + * @param commentsEntity + */ + public void commented(CommentsEntity commentsEntity) { + type = TYPE_KNOWLEDGE_COMMENT; + comment = commentsEntity; + } + /** + * Like added. + * @param entity + */ + public void liked(LikesEntity entity) { + type = TYPE_KNOWLEDGE_LIKE; + like = entity; + } + /** + * メール通知のキューを取得 + * + * @return + */ + public NotifyQueuesEntity getQueue() { + NotifyQueuesEntity entity = new NotifyQueuesEntity(); + entity.setHash(RandomUtil.randamGen(30)); + entity.setType(type); + + if (type == TYPE_KNOWLEDGE_INSERT || type == TYPE_KNOWLEDGE_UPDATE) { + entity.setId(knowledge.getKnowledgeId()); + } else if (type == TYPE_KNOWLEDGE_COMMENT) { + entity.setId(comment.getCommentNo()); + } else if (type == TYPE_KNOWLEDGE_LIKE) { + entity.setId(like.getNo()); + } + return entity; + } + + /** + * デスクトップ通知するメッセージを取得 + * + * @param loginuser + * @param locale + * @return + */ + public MessageResult getMessage(LoginedUser loginuser, Locale locale) { + if (type == TYPE_KNOWLEDGE_INSERT) { + return NotifyLogic.get().getInsertKnowledgeMessage(loginuser, locale, knowledge); + } else if (type == TYPE_KNOWLEDGE_UPDATE) { + return NotifyLogic.get().getUpdateKnowledgeMessage(loginuser, locale, knowledge); + } else if (type == TYPE_KNOWLEDGE_COMMENT) { + return this.getSaveCommentMessage(loginuser, locale, comment); + } else if (type == TYPE_KNOWLEDGE_LIKE) { + return NotifyLogic.get().getSaveLikeMessage(loginuser, locale, like); + } + return null; + } + /** + * コメント更新時のデスクトップメッセージを取得 + * @param loginuser + * @param locale + * @param comment + * @return + */ + private MessageResult getSaveCommentMessage(LoginedUser loginuser, Locale locale, CommentsEntity comment) { + NotifyConfigsDao dao = NotifyConfigsDao.get(); + NotifyConfigsEntity entity = dao.selectOnKey(loginuser.getUserId()); // ログインユーザのデスクトップ通知設定 + if (!NotifyLogic.get().flagCheck(entity.getNotifyDesktop())) { + // デスクトップ通知対象外 + return null; + } + + KnowledgesDao knowledgesDao = KnowledgesDao.get(); + KnowledgesEntity knowledge = knowledgesDao.selectOnKey(comment.getKnowledgeId()); + // 登録者に通知 + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + if (user != null) { + if (user.getUserId().intValue() == loginuser.getUserId().intValue()) { + // ログインユーザはナレッジ登録者で、自分の登録したナレッジにコメントがついたら通知を希望 + MessageResult messageResult = new MessageResult(); + messageResult.setMessage(Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.myitem.comment", + StringUtils.abbreviate(knowledge.getTitle(), 80))); + messageResult.setResult(NotifyLogic.get().makeURL(knowledge.getKnowledgeId())); // Knowledgeへのリンク + return messageResult; + } + } + // 宛先のナレッジにコメント追加で通知が欲しいユーザに通知 + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + for (UsersEntity target : users) { + if (target.getUserId().intValue() == loginuser.getUserId().intValue()) { + // 自分宛てのナレッジにコメントがついたので通知 + MessageResult messageResult = new MessageResult(); + messageResult.setMessage( + Resources.getInstance(locale).getResource("knowledge.notify.msg.desktop.to.comment", + StringUtils.abbreviate(knowledge.getTitle(), 80))); + messageResult.setResult(NotifyLogic.get().makeURL(knowledge.getKnowledgeId())); // Knowledgeへのリンク + return messageResult; + } + } + return null; + } } diff --git a/src/main/java/org/support/project/knowledge/websocket/SessionObserver.java b/src/main/java/org/support/project/knowledge/websocket/SessionObserver.java index 41f98dc83..d97b6fa6a 100644 --- a/src/main/java/org/support/project/knowledge/websocket/SessionObserver.java +++ b/src/main/java/org/support/project/knowledge/websocket/SessionObserver.java @@ -7,8 +7,6 @@ import javax.websocket.Session; -import net.arnx.jsonic.JSON; - import org.support.project.common.log.Log; import org.support.project.common.log.LogFactory; import org.support.project.knowledge.vo.Notify; @@ -16,62 +14,73 @@ import org.support.project.web.bean.MessageResult; import org.support.project.web.websocket.EndpointConfigurator; +import net.arnx.jsonic.JSON; + +/** + * Websocketのセッションに通知を行うObserverクラス + * @author Koda + */ public class SessionObserver implements Observer { - /** ログ */ - private static Log LOG = LogFactory.getLog(SessionObserver.class); + /** ログ */ + private static final Log LOG = LogFactory.getLog(SessionObserver.class); + + /** + * セッション + */ + private Session session; + /** + * コンストラクタ + * @param session + */ + public SessionObserver(Session session) { + super(); + this.session = session; + } + + @Override + public void update(Observable o, Object message) { + try { + if (!session.isOpen()) { + return; + } + + Locale locale = Locale.getDefault(); + Map prop = session.getUserProperties(); + LoginedUser loginuser = null; + if (prop != null) { + if (prop.containsKey(EndpointConfigurator.LOCALE_KEY)) { + locale = (Locale) prop.get(EndpointConfigurator.LOCALE_KEY); + } + if (prop.containsKey(EndpointConfigurator.LOCALE_KEY)) { + loginuser = (LoginedUser) prop.get(EndpointConfigurator.LOGIN_USER_KEY); + } + } - private Session session; - - public SessionObserver(Session session) { - super(); - this.session = session; - } + if (message instanceof Notify && loginuser != null) { + Notify notify = (Notify) message; + MessageResult result = notify.getMessage(loginuser, locale); + if (result != null) { + if (LOG.isInfoEnabled()) { + StringBuilder builder = new StringBuilder(); + builder.append("[Notify] ").append("User id: ").append(session.getUserPrincipal().getName()).append(" "); + builder.append("Session id: ").append(session.getId()).append(" ").append(result.getMessage()); + LOG.info(builder.toString()); + } + session.getBasicRemote().sendText(JSON.encode(result)); + } + } + } catch (Exception e) { + LOG.error("error", e); + } + } - @Override - public void update(Observable o, Object message) { - try { - if (!session.isOpen()) { - return; - } - - Locale locale = Locale.getDefault(); - Map prop = session.getUserProperties(); - LoginedUser loginuser = null; - if (prop != null) { - if (prop.containsKey(EndpointConfigurator.LOCALE_KEY)) { - locale = (Locale) prop.get(EndpointConfigurator.LOCALE_KEY); - } - if (prop.containsKey(EndpointConfigurator.LOCALE_KEY)) { - loginuser = (LoginedUser) prop.get(EndpointConfigurator.LOGIN_USER_KEY); - } - } - - if (message instanceof Notify && loginuser != null) { - Notify notify = (Notify) message; - MessageResult result = notify.getMessage(loginuser, locale); - if (result != null) { - if (LOG.isInfoEnabled()) { - LOG.info("[Notify] "); - LOG.info("Session id: " + session.getId()); - LOG.info("User id: " + session.getUserPrincipal().getName()); - //LOG.info("User key: " + loginuser.getLoginUser().getUserKey()); - //LOG.info("Locale: " + locale.toString()); - LOG.info(result.getMessage()); - } - session.getBasicRemote().sendText(JSON.encode(result)); - } - } - } catch (Exception e) { - LOG.error("error", e); - } - } - - /** - * Sessionが開いているかチェック - * @return - */ - public boolean isOpen() { - return session.isOpen(); - } + /** + * Sessionが開いているかチェック + * + * @return + */ + public boolean isOpen() { + return session.isOpen(); + } } diff --git a/src/main/resources/appconfig.xml b/src/main/resources/appconfig.xml index 5e68173cc..cdaa6d818 100644 --- a/src/main/resources/appconfig.xml +++ b/src/main/resources/appconfig.xml @@ -1,24 +1,25 @@ - knowledge - Asia/Tokyo - {user.home}/.knowledge - {base.path}/index/ - {base.path}/tmp/ - {base.path}/db/ - {base.path}/logs/ - 500 - 100 - - - - ja - - - - en - - + knowledge + KNOWLEDGE_HOME + Asia/Tokyo + {user.home}/.knowledge + {base.path}/index/ + {base.path}/tmp/ + {base.path}/db/ + {base.path}/logs/ + 500 + 100 + + + + ja + + + + en + + diff --git a/src/main/resources/appresource.properties b/src/main/resources/appresource.properties index d4a96e98d..4aeb89529 100644 --- a/src/main/resources/appresource.properties +++ b/src/main/resources/appresource.properties @@ -55,7 +55,7 @@ message.allready.started=Allready started. message.confirm.delete=Are you sure you want to delete? # Common Label -label.version=v1.2.1 +label.version=v1.3.0 label.login=Sign in label.previous = Previous label.next=Next @@ -217,6 +217,7 @@ knowledge.view.like=Like! knowledge.view.fav=Stock knowledge.view.edit=Edit knowledge.view.edit.with.login=Edit(Sign in) +knowledge.view.edit.disable=You do not have permission to edit knowledge.view.back.list=Back to list knowledge.view.comment.label=Comment knowledge.view.comment=Add Comment @@ -313,7 +314,7 @@ knowledge.withdrawal.label.name=Removed User knowledge.registration.result.title=Registration Result knowledge.registration.msg.wait=I accept the registration.
use the start of the service, has become necessary to confirm the administrator. Please wait until the confirmation of
administrator. knowledge.registration.msg.mail=The invitation to the service I was sent. from the
received mail, you will be able to log in and complete the user registration.
-knowledge.registration.msg.mail.info=※ Note: Depending on the mailer, you may e-mail invitation will enter the junk e-mail. Please make sure that you are not reaching as junk e-mail. +knowledge.registration.msg.mail.info=※ Note: Depending on the mailer, you may e-mail invitation will enter the junk e-mail. Please make sure that you are not reaching as junk e-mail. knowledge.config.title=User registration method specified knowledge.config.label.registration.method=The choice of procedure of user registration @@ -419,7 +420,7 @@ knowledge.data.label.restore.msg.danger=When you run the restore will overwrite knowledge.data.label.restore.msg.info=A data base of default is made the target for the data which is being managed by this page. When changing the stock destination of data by connection previous change in the data base, it isn't made the target. Please manage a backup according to the use environment by yourself in that case. knowledge.data.label.active=database server status knowledge.data.label.active.msg=You can change the operating status of the database server. And when you want to back up and restore, please stop the database before shutting down the service. -knowledge.data.label.active.msg2=※Please refer to the stop when you shut down in the case of use in Docker. +knowledge.data.label.active.msg2=※Please refer to the stop when you shut down in the case of use in Docker. knowledge.data.label.active.msg3=And I have stopped DB when Tomcat stops, but will be Docker is stopped before DB stop, it will become a state that DB could not be closed properly. knowledge.data.label.active.msg4=Then you may such as the value of automatic numbering of the automatic next is funny. knowledge.data.label.active.status=Current state @@ -446,6 +447,8 @@ knowledge.connection.msg.custom.enable=Custom setting becomes effective at prese knowledge.connection.msg.custom.disable=It's default because custom setting doesn't exist, it's included and DB is used. knowledge.connection.msg.custom.transfer=Of data, it's being copied. Please wait a moment until it's completed. knowledge.connection.msg.custom.transfer.request=A request of a copy of data was accepted. +knowledge.connection.msg.recommend.postgres=Recommend change to Postgres for Stable operation. +knowledge.connection.msg.info.url=jdbc:postgresql://(hostname):(port)/(database_name) ex)jdbc:postgresql://localhost:5432/postgres knowledge.reindexing.title=Re-making of an index for searches knowledge.reindexing.msg1=I have an index for searches (Lucene) in addition to Database for data storages in Knowledge. knowledge.reindexing.msg2=An index for the searches is made on this screen. @@ -491,8 +494,8 @@ knowledge.export.msg3=After the execution, Please wait knowledge.export.msg.start=We have to generate the export data. knowledge.export.msg.end=Generation of export data has been completed. -knowledge.sample.markdown.1=- Copy the text that has been written here in the knowledge Then "Preview", -knowledge.sample.markdown.2=\u0020\u0020You can image the display of Markdown +knowledge.sample.markdown.1=- This Markdown text is dispay on 'Display sample' area, +knowledge.sample.markdown.2=\u0020\u0020You can check the display of Markdown knowledge.sample.markdown.11=#### Heading -> "#" knowledge.sample.markdown.12=- "#" is heading knowledge.sample.markdown.13=\u0020\u0020\u0020- After the "#", by opening a space, write the title of the character diff --git a/src/main/resources/appresource_ja.properties b/src/main/resources/appresource_ja.properties index b46d4729d..96949efd8 100644 --- a/src/main/resources/appresource_ja.properties +++ b/src/main/resources/appresource_ja.properties @@ -55,7 +55,7 @@ message.allready.started=すでに開始済です message.confirm.delete=本当に削除しますか? # Common Label -label.version=v1.2.1 +label.version=v1.3.0 label.login=サインイン label.previous = 前へ label.next = 次へ @@ -217,6 +217,7 @@ knowledge.view.like=いいね! knowledge.view.fav=ストック knowledge.view.edit=投稿を編集 knowledge.view.edit.with.login=投稿を編集(サインイン) +knowledge.view.edit.disable=編集する権限がありません knowledge.view.back.list=一覧に戻る knowledge.view.comment.label=コメント knowledge.view.comment=コメントを追加 @@ -446,6 +447,8 @@ knowledge.connection.msg.custom.enable=現在、カスタム設定が有効に knowledge.connection.msg.custom.disable=カスタム設定は存在しないため、デフォルトの組み込みDBを使っています knowledge.connection.msg.custom.transfer=データのコピー中です。完了するまでしばらくおまちください。(このメッセージがきえるまで、操作しないでください) knowledge.connection.msg.custom.transfer.request=データのコピーのリクエストを受け付けました +knowledge.connection.msg.recommend.postgres=長期の安定稼働のためには、DatabaseをPostgresに変更することをおすすめします +knowledge.connection.msg.info.url=jdbc:postgresql://(hostname):(port)/(database_name) ex)jdbc:postgresql://localhost:5432/postgres knowledge.reindexing.title=検索用のインデックスの再作成 knowledge.reindexing.msg1=Knowledgeでは、データ保存用のDatabaseの他に、検索用のインデックス(Lucene)を持っています。 knowledge.reindexing.msg2=本画面では、その検索用のインデックスを再作成します。 @@ -491,7 +494,7 @@ knowledge.export.msg3=このため、エクスポートを実行した後、ダ knowledge.export.msg.start=エクスポートデータを生成しています。 knowledge.export.msg.end=エクスポートデータの生成が完了しました。 -knowledge.sample.markdown.1=- ここに書かれている文章をナレッジにコピーして「プレビュー」すると、 +knowledge.sample.markdown.1=- ここに書かれているMarkdownのテキストが、下のDispley sampleに表示されるので、 knowledge.sample.markdown.2=\u0020\u0020Markdownがどのように表示されるかのイメージが湧くと思います knowledge.sample.markdown.11=#### 見出し -> 「#」 knowledge.sample.markdown.12=- 「#」はタイトル diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularity.sql b/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularity.sql index a2f0d359d..7dc7e59f1 100644 --- a/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularity.sql +++ b/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularity.sql @@ -1,5 +1,7 @@ SELECT KNOWLEDGES.* + ,USERS.USER_NAME AS INSERT_USER_NAME + ,UP_USER.USER_NAME AS UPDATE_USER_NAME ,COUNT(LIKES.NO) AS LIKE_COUNT_ON_TERM FROM KNOWLEDGES @@ -8,10 +10,14 @@ SELECT KNOWLEDGES.KNOWLEDGE_ID = LIKES.KNOWLEDGE_ID AND LIKES.INSERT_DATETIME BETWEEN ? AND ? ) + LEFT OUTER JOIN USERS + ON USERS.USER_ID = KNOWLEDGES.INSERT_USER + LEFT OUTER JOIN USERS AS UP_USER + ON UP_USER.USER_ID = KNOWLEDGES.UPDATE_USER WHERE KNOWLEDGES.DELETE_FLAG = 0 GROUP BY - KNOWLEDGES.KNOWLEDGE_ID + KNOWLEDGES.KNOWLEDGE_ID, USERS.USER_NAME, UP_USER.USER_NAME ORDER BY COUNT(LIKES.NO) DESC ,KNOWLEDGES.UPDATE_DATETIME DESC diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularityWithAccessControl.sql b/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularityWithAccessControl.sql index b55456705..db9f27ca7 100644 --- a/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularityWithAccessControl.sql +++ b/src/main/resources/org/support/project/knowledge/dao/sql/KnowledgesDao/KnowledgesDao_selectPopularityWithAccessControl.sql @@ -1,5 +1,7 @@ SELECT KNOWLEDGES.* + ,USERS.USER_NAME AS INSERT_USER_NAME + ,UP_USER.USER_NAME AS UPDATE_USER_NAME ,COUNT(LIKES.NO) AS LIKE_COUNT_ON_TERM FROM KNOWLEDGES @@ -8,6 +10,10 @@ SELECT KNOWLEDGES.KNOWLEDGE_ID = LIKES.KNOWLEDGE_ID AND LIKES.INSERT_DATETIME BETWEEN ? AND ? ) + LEFT OUTER JOIN USERS + ON USERS.USER_ID = KNOWLEDGES.INSERT_USER + LEFT OUTER JOIN USERS AS UP_USER + ON UP_USER.USER_ID = KNOWLEDGES.UPDATE_USER WHERE KNOWLEDGES.DELETE_FLAG = 0 AND ( @@ -42,7 +48,7 @@ SELECT ) ) GROUP BY - KNOWLEDGES.KNOWLEDGE_ID + KNOWLEDGES.KNOWLEDGE_ID, USERS.USER_NAME, UP_USER.USER_NAME ORDER BY COUNT(LIKES.NO) DESC ,KNOWLEDGES.UPDATE_DATETIME DESC diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyDesktopUsersOnPublicComment.sql b/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyDesktopUsersOnPublicComment.sql new file mode 100644 index 000000000..107b787ea --- /dev/null +++ b/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyDesktopUsersOnPublicComment.sql @@ -0,0 +1,14 @@ +SELECT * FROM ( +SELECT + USERS.* + FROM + USERS INNER JOIN NOTIFY_CONFIGS + ON (USERS.USER_ID = NOTIFY_CONFIGS.USER_ID) +WHERE + NOTIFY_CONFIGS.TO_ITEM_COMMENT = 1 + AND (NOTIFY_CONFIGS.TO_ITEM_IGNORE_PUBLIC != 1 OR NOTIFY_CONFIGS.TO_ITEM_IGNORE_PUBLIC IS NULL) + AND NOTIFY_CONFIGS.NOTIFY_DESKTOP = 1 + AND USERS.DELETE_FLAG = 0 +ORDER BY USERS.INSERT_USER +) +LIMIT ? OFFSET ? diff --git a/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyMailUsersOnPublicComment.sql b/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyMailUsersOnPublicComment.sql new file mode 100644 index 000000000..4f9b987ad --- /dev/null +++ b/src/main/resources/org/support/project/knowledge/dao/sql/NotifyConfigsDao/GetNotifyMailUsersOnPublicComment.sql @@ -0,0 +1,14 @@ +SELECT * FROM ( +SELECT + USERS.* + FROM + USERS INNER JOIN NOTIFY_CONFIGS + ON (USERS.USER_ID = NOTIFY_CONFIGS.USER_ID) +WHERE + NOTIFY_CONFIGS.TO_ITEM_COMMENT = 1 + AND (NOTIFY_CONFIGS.TO_ITEM_IGNORE_PUBLIC != 1 OR NOTIFY_CONFIGS.TO_ITEM_IGNORE_PUBLIC IS NULL) + AND NOTIFY_CONFIGS.NOTIFY_MAIL = 1 + AND USERS.DELETE_FLAG = 0 +ORDER BY USERS.INSERT_USER +) +LIMIT ? OFFSET ? diff --git a/src/main/webapp/WEB-INF/views/admin/database/connection.jsp b/src/main/webapp/WEB-INF/views/admin/database/connection.jsp index 9f3b6fc7c..3d8c09a40 100644 --- a/src/main/webapp/WEB-INF/views/admin/database/connection.jsp +++ b/src/main/webapp/WEB-INF/views/admin/database/connection.jsp @@ -7,136 +7,162 @@ <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> -<% JspUtil jspUtil = new JspUtil(request, pageContext); %> +<% + JspUtil jspUtil = new JspUtil(request, pageContext); +%> - - - - - -<% if (jspUtil.is(Boolean.TRUE, "transfer")) { %> - -<% } %> - - - - - -

<%= jspUtil.label("knowledge.navbar.data.connect") %>

- -<% if(jspUtil.is(Boolean.TRUE, "custom")) { %> - - - <% if (jspUtil.is(Boolean.TRUE, "transfer")) { %> - - <% } %> - -<% } else { %> - -<% } %> - -
-
- -
- -
- -
-
- - " /> -
-
- - " /> -
-
- - " /> -
-
- - " /> -
-
- - " /> -
-
- - " /> -
- - - - - -  <%= jspUtil.label("label.delete") %> - - -<% if(jspUtil.is(Boolean.TRUE, "custom") && !(jspUtil.is(Boolean.TRUE, "transfer"))) { %> - -  <%= jspUtil.label("knowledge.connection.label.transfer") %> - - - -  <%= jspUtil.label("knowledge.connection.label.transfer.back") %> - -<% } %> - -
- - - -
+ + + + <% + if (jspUtil.is(Boolean.TRUE, "transfer")) { + %> + + <% + } + %> + + + + + +

<%=jspUtil.label("knowledge.navbar.data.connect")%>

+ + + + <% + if (jspUtil.is(Boolean.TRUE, "custom")) { + %> + + + <% + if (jspUtil.is(Boolean.TRUE, "transfer")) { + %> + + <% + } + %> + + <% + } else { + %> + + <% + } + %> + +
+
+

+
+
+ " value="<%=jspUtil.out("uRL")%>" /> +
+
+ " /> +
+
+ " /> +
+
+ " /> +
+
+ " /> +
+
+ " /> +
+ + + + +  <%=jspUtil.label("label.delete")%> + + + <% + if (jspUtil.is(Boolean.TRUE, "custom") && !(jspUtil.is(Boolean.TRUE, "transfer"))) { + %> +  <%=jspUtil.label("knowledge.connection.label.transfer")%> +  <%=jspUtil.label("knowledge.connection.label.transfer.back")%> + + <% + } + %> + +
+ + + +
diff --git a/src/main/webapp/WEB-INF/views/open/knowledge/view.jsp b/src/main/webapp/WEB-INF/views/open/knowledge/view.jsp index 9147b2621..75a004803 100644 --- a/src/main/webapp/WEB-INF/views/open/knowledge/view.jsp +++ b/src/main/webapp/WEB-INF/views/open/knowledge/view.jsp @@ -149,6 +149,10 @@ var _SET_IMAGE_LABEL= '<%= jspUtil.label("knowledge.edit.set.image.path") %>'; class="btn btn-primary btn_edit" role="button">  <%= jspUtil.label("knowledge.view.edit") %> + <% } else { %> + <% } %> <% } else { %> " diff --git a/src/main/webapp/WEB-INF/views/protect/knowledge/markdown.jsp b/src/main/webapp/WEB-INF/views/protect/knowledge/markdown.jsp index 85378184b..18aff0e56 100644 --- a/src/main/webapp/WEB-INF/views/protect/knowledge/markdown.jsp +++ b/src/main/webapp/WEB-INF/views/protect/knowledge/markdown.jsp @@ -1,85 +1,95 @@ +<%@page pageEncoding="UTF-8" isELIgnored="false" session="false" errorPage="/WEB-INF/views/commons/errors/jsp_error.jsp"%> <%@page import="org.support.project.web.util.JspUtil"%> -<%@page pageEncoding="UTF-8" isELIgnored="false" session="false" - errorPage="/WEB-INF/views/commons/errors/jsp_error.jsp"%> <% JspUtil jspUtil = new JspUtil(request, pageContext); %> - + +
+
+

Display sample

+
+
  • +
    +
    +
    + + + + + + diff --git a/src/main/webapp/css/knowledge-edit.css b/src/main/webapp/css/knowledge-edit.css index f34a5904e..229a07123 100644 --- a/src/main/webapp/css/knowledge-edit.css +++ b/src/main/webapp/css/knowledge-edit.css @@ -89,3 +89,10 @@ .tag_list > a { text-decoration: none; } + +.markdown_sample{ + border: 2px solid silver; + padding: 5px; + height: 300px; + overflow: auto; +} diff --git a/src/main/webapp/js/knowledge-edit.js b/src/main/webapp/js/knowledge-edit.js index 21c041cf2..ce6c5a4b4 100644 --- a/src/main/webapp/js/knowledge-edit.js +++ b/src/main/webapp/js/knowledge-edit.js @@ -184,15 +184,27 @@ $(document).ready(function() { $('#emojiSymbolsModal').on('loaded.bs.modal', function (event) { emojiSelect('#emojiSymbolsModal'); }); - - $('#sampleMarkdownCheck').click(function() { - var text = $('#sampleMarkdownText').val(); - var textarea = $('#content'); - textarea.val(text); - preview(); - $('#helpMarkdownModal').modal('hide'); - var p = $("#preview").offset().top - 60; - $('html,body').animate({ scrollTop: p }, 'fast'); + $('#helpMarkdownModal').on('shown.bs.modal', function (event) { + $.post(_CONTEXT + '/open.knowledge/marked', { + title : 'Markdown Sample', + content : $('#sampleMarkdownText').val() + }, function(data) { + var html = '
    '; + var content = data.content; + html += content; + html += '
    '; + + var jqObj = $('#markdownSamplePreview'); + jqObj.html(html); + jqObj.find('code').addClass('hljs'); + codeHighlight(jqObj) + .then(function() { + var content = emoji(jqObj.html().trim(), _CONTEXT + '/bower/emoji-parser/emoji', {classes: 'emoji-img'}); + jqObj.html(content); + }).then(function () { + jqObj.find('a.oembed').oembed(); + }); + }); }); setUpTagSelect(); diff --git a/src/test/java/org/support/project/knowledge/TestCommon.java b/src/test/java/org/support/project/knowledge/TestCommon.java index 440b4bf03..54d6db5b7 100644 --- a/src/test/java/org/support/project/knowledge/TestCommon.java +++ b/src/test/java/org/support/project/knowledge/TestCommon.java @@ -4,9 +4,16 @@ import java.io.IOException; import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; import org.support.project.common.config.ConfigLoader; import org.support.project.common.exception.SerializeException; +import org.support.project.common.logic.H2DBServerLogic; import org.support.project.common.serialize.SerializeUtils; +import org.support.project.common.test.OrderedRunner; import org.support.project.common.util.FileUtil; import org.support.project.common.util.RandomUtil; import org.support.project.knowledge.config.AppConfig; @@ -15,58 +22,155 @@ import org.support.project.ormapping.tool.config.ORmappingToolConfig; import org.support.project.ormapping.tool.dao.InitializeDao; import org.support.project.web.bean.LoginedUser; +import org.support.project.web.dao.GroupsDao; import org.support.project.web.dao.RolesDao; +import org.support.project.web.dao.UserGroupsDao; +import org.support.project.web.dao.UserRolesDao; import org.support.project.web.dao.UsersDao; +import org.support.project.web.entity.GroupsEntity; import org.support.project.web.entity.RolesEntity; +import org.support.project.web.entity.UserGroupsEntity; +import org.support.project.web.entity.UserRolesEntity; import org.support.project.web.entity.UsersEntity; -public class TestCommon { - public static LoginedUser loginedUser = null; - public static LoginedUser loginedUser2 = null; - - public static void testConnection() throws SerializeException, IOException { - // コネクション設定 - String configFileName = "/ormappingtool.xml"; - ORmappingToolConfig config = SerializeUtils.bytesToObject( - TestCommon.class.getResourceAsStream(configFileName), - ORmappingToolConfig.class); - config.getConnectionConfig().convURL(); - ConnectionManager.getInstance().addConnectionConfig(config.getConnectionConfig()); - } - - public static void initData() throws Exception { - loginedUser = new LoginedUser(); - loginedUser2 = new LoginedUser(); - - //DBを完全初期化 - InitializeDao initializeDao = InitializeDao.get(); - initializeDao.dropAllTable(); - InitDB.main(null); - // 全文検索エンジンのインデックスの消去 - AppConfig appConfig = ConfigLoader.load(AppConfig.APP_CONFIG, AppConfig.class); - File indexDir = new File(appConfig.getIndexPath()); - FileUtil.delete(indexDir); - - // テスト用のユーザを登録 - UsersEntity entity = new UsersEntity(); - entity.setUserKey(RandomUtil.randamGen(64)); - entity.setUserName("テストユーザ"); - entity.setPassword(RandomUtil.randamGen(64)); - entity = UsersDao.get().insert(entity); - loginedUser.setLoginUser(entity); - - RolesDao rolesDao = RolesDao.get(); - List rolesEntities = rolesDao.selectOnUserKey(entity.getUserKey()); - loginedUser.setRoles(rolesEntities); - - UsersEntity entity2 = new UsersEntity(); - entity2.setUserKey(RandomUtil.randamGen(64)); - entity2.setUserName("テストユーザ2"); - entity2.setPassword(RandomUtil.randamGen(64)); - entity2 = UsersDao.get().insert(entity2); - loginedUser2.setLoginUser(entity2); - loginedUser.setRoles(rolesEntities); - } - - +/** + * Abstract class for test. + * @author Koda + * + */ +@RunWith(OrderedRunner.class) +public abstract class TestCommon { + /** login user for test */ + public static LoginedUser loginedUser = null; + /** login user for test */ + public static LoginedUser loginedUser2 = null; + /** login user for test */ + public static LoginedUser loginedUser3 = null; + + /** group for test */ + public static GroupsEntity group = null; + /** login user for test */ + public static LoginedUser groupuser1 = null; + /** login user for test */ + public static LoginedUser groupuser2 = null; + + + /** + * @BeforeClass + * @throws Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + H2DBServerLogic.get().start(); + testConnection(); + initData(); + } + /** + * @AfterClass + * @throws Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + H2DBServerLogic.get().stop(); + } + /** + * @Before + * @throws Exception + */ + @Before + public void setUp() throws Exception { + } + /** + * @After + * @throws Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * connection change for test + * @throws SerializeException + * @throws IOException + */ + public static void testConnection() throws SerializeException, IOException { + // コネクション設定 + String configFileName = "/ormappingtool.xml"; + ORmappingToolConfig config = SerializeUtils.bytesToObject(TestCommon.class.getResourceAsStream(configFileName), ORmappingToolConfig.class); + config.getConnectionConfig().convURL(); + ConnectionManager.getInstance().addConnectionConfig(config.getConnectionConfig()); + } + + /** + * Insert common data for test. + * @throws Exception + */ + public static void initData() throws Exception { + loginedUser = new LoginedUser(); + loginedUser2 = new LoginedUser(); + loginedUser3 = new LoginedUser(); + groupuser1 = new LoginedUser(); + groupuser2 = new LoginedUser(); + + // DBを完全初期化 + InitializeDao initializeDao = InitializeDao.get(); + initializeDao.dropAllTable(); + InitDB.main(null); + // 全文検索エンジンのインデックスの消去 + AppConfig appConfig = ConfigLoader.load(AppConfig.APP_CONFIG, AppConfig.class); + File indexDir = new File(appConfig.getIndexPath()); + FileUtil.delete(indexDir); + + Integer[] roles = {2}; // 2はユーザ、1はAdmin + addUser(loginedUser, "テストユーザ1", roles); + addUser(loginedUser2, "テストユーザ2", roles); + addUser(loginedUser3, "テストユーザ3", roles); + + addUser(groupuser1, "GroupUser1", roles); + addUser(groupuser2, "GroupUser2", roles); + + GroupsEntity groupsEntity = new GroupsEntity(); + groupsEntity.setGroupName("テストグループ"); + groupsEntity.setGroupKey("TestGroup"); + group = GroupsDao.get().save(groupsEntity); + + UserGroupsEntity usergroup = new UserGroupsEntity(); + usergroup.setGroupId(group.getGroupId()); + usergroup.setUserId(groupuser1.getUserId()); + UserGroupsDao.get().save(usergroup); + + usergroup = new UserGroupsEntity(); + usergroup.setGroupId(group.getGroupId()); + usergroup.setUserId(groupuser2.getUserId()); + UserGroupsDao.get().save(usergroup); + } + + /** + * テストユーザの情報を生成 + * @param user + * @param userName + * @param roleIds + * @return + */ + protected static List addUser(LoginedUser user, String userName, Integer[] roleIds) { + // テスト用のユーザを登録 + UsersEntity entity = new UsersEntity(); + entity.setUserKey(RandomUtil.randamGen(64)); + entity.setUserName(userName); + entity.setPassword(RandomUtil.randamGen(64)); + entity = UsersDao.get().insert(entity); + user.setLoginUser(entity); + + RolesDao rolesDao = RolesDao.get(); + for (Integer roleId : roleIds) { + UserRolesEntity role = new UserRolesEntity(); + role.setRoleId(roleId); + role.setUserId(entity.getUserId()); + UserRolesDao.get().save(role); + } + List rolesEntities = rolesDao.selectOnUserKey(entity.getUserKey()); + user.setRoles(rolesEntities); + return rolesEntities; + } + } diff --git a/src/test/java/org/support/project/knowledge/dao/KnowledgesDaoTest.java b/src/test/java/org/support/project/knowledge/dao/KnowledgesDaoTest.java index d0137482e..aa8e44177 100644 --- a/src/test/java/org/support/project/knowledge/dao/KnowledgesDaoTest.java +++ b/src/test/java/org/support/project/knowledge/dao/KnowledgesDaoTest.java @@ -25,25 +25,7 @@ public class KnowledgesDaoTest extends TestCommon { /** ログ */ private static Log LOG = LogFactory.getLog(KnowledgesDaoTest.class); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - H2DBServerLogic.get().start(); - initData(); - } - @AfterClass - public static void tearDownAfterClass() throws Exception { - H2DBServerLogic.get().stop(); - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - + @Test public void testSelectKnowledges() throws Exception { java.util.List knowledgeIds = new ArrayList(); diff --git a/src/test/java/org/support/project/knowledge/dao/NotifyConfigsDaoTest.java b/src/test/java/org/support/project/knowledge/dao/NotifyConfigsDaoTest.java new file mode 100644 index 000000000..1fb31895c --- /dev/null +++ b/src/test/java/org/support/project/knowledge/dao/NotifyConfigsDaoTest.java @@ -0,0 +1,151 @@ +package org.support.project.knowledge.dao; + +import java.util.List; + +import org.junit.Test; +import org.support.project.common.config.INT_FLAG; +import org.support.project.common.test.AssertEx; +import org.support.project.common.test.Order; +import org.support.project.knowledge.TestCommon; +import org.support.project.knowledge.entity.NotifyConfigsEntity; +import org.support.project.web.entity.UsersEntity; + +/** + * 通知のDaoのテスト + * @author Koda + */ +public class NotifyConfigsDaoTest extends TestCommon { + private static final int LIMIT = 100; + private static final int OFFSET = 0; + + @Test + @Order(order = 1) + public void testNotify1() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyDesktopUsersOnPublicComment(LIMIT, OFFSET); + AssertEx.eq(1, users.size()); + } + @Test + @Order(order = 2) + public void testNotify2() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemIgnorePublic(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyDesktopUsersOnPublicComment(LIMIT, OFFSET); + AssertEx.eq(0, users.size()); + } + @Test + @Order(order = 3) + public void testNotify3() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyDesktopUsersOnPublicComment(1, OFFSET); + AssertEx.eq(1, users.size()); + AssertEx.eq(loginedUser.getUserId(), users.get(0).getUserId()); + } + @Test + @Order(order = 4) + public void testNotify4() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyDesktopUsersOnPublicComment(1, 1); + AssertEx.eq(1, users.size()); + AssertEx.eq(loginedUser2.getUserId(), users.get(0).getUserId()); + } + + + + @Test + @Order(order = 5) + public void testNotify5() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyMailUsersOnPublicComment(LIMIT, OFFSET); + AssertEx.eq(1, users.size()); + } + @Test + @Order(order = 6) + public void testNotify6() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemIgnorePublic(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyMailUsersOnPublicComment(LIMIT, OFFSET); + AssertEx.eq(0, users.size()); + } + @Test + @Order(order = 7) + public void testNotify7() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyMailUsersOnPublicComment(1, OFFSET); + AssertEx.eq(1, users.size()); + AssertEx.eq(loginedUser.getUserId(), users.get(0).getUserId()); + } + @Test + @Order(order = 8) + public void testNotify8() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + List users = NotifyConfigsDao.get().getNotifyMailUsersOnPublicComment(1, 1); + AssertEx.eq(1, users.size()); + AssertEx.eq(loginedUser2.getUserId(), users.get(0).getUserId()); + } + +} diff --git a/src/test/java/org/support/project/knowledge/logic/KnowledgeLogicTest.java b/src/test/java/org/support/project/knowledge/logic/KnowledgeLogicTest.java index c9552678b..1f85c8f5a 100644 --- a/src/test/java/org/support/project/knowledge/logic/KnowledgeLogicTest.java +++ b/src/test/java/org/support/project/knowledge/logic/KnowledgeLogicTest.java @@ -28,28 +28,9 @@ public class KnowledgeLogicTest extends TestCommon { /** ログ */ private static Log LOG = LogFactory.getLog(KnowledgeLogicTest.class); - private static List list = new ArrayList<>(); - @BeforeClass - public static void setUpBeforeClass() throws Exception { - H2DBServerLogic.get().start(); - initData(); - } - @AfterClass - public static void tearDownAfterClass() throws Exception { - H2DBServerLogic.get().stop(); - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - @Test @Order(order= 1) public void testInsert() throws Exception { diff --git a/src/test/java/org/support/project/knowledge/logic/MarkdownLogicTest.java b/src/test/java/org/support/project/knowledge/logic/MarkdownLogicTest.java index c8a858210..e148e50a3 100644 --- a/src/test/java/org/support/project/knowledge/logic/MarkdownLogicTest.java +++ b/src/test/java/org/support/project/knowledge/logic/MarkdownLogicTest.java @@ -29,23 +29,6 @@ public class MarkdownLogicTest extends TestCommon { /** ログ */ private static Log LOG = LogFactory.getLog(MarkdownLogicTest.class); - - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - /** * 改行コードは無視して値比較をするために、文字列(Line)の配列で取得 diff --git a/src/test/java/org/support/project/knowledge/logic/NotifyCommentLogicTest.java b/src/test/java/org/support/project/knowledge/logic/NotifyCommentLogicTest.java new file mode 100644 index 000000000..1c3157a9b --- /dev/null +++ b/src/test/java/org/support/project/knowledge/logic/NotifyCommentLogicTest.java @@ -0,0 +1,296 @@ +package org.support.project.knowledge.logic; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.support.project.common.config.INT_FLAG; +import org.support.project.common.log.Log; +import org.support.project.common.log.LogFactory; +import org.support.project.common.test.Order; +import org.support.project.knowledge.TestCommon; +import org.support.project.knowledge.config.NotifyType; +import org.support.project.knowledge.dao.CommentsDao; +import org.support.project.knowledge.dao.NotifyConfigsDao; +import org.support.project.knowledge.entity.CommentsEntity; +import org.support.project.knowledge.entity.KnowledgesEntity; +import org.support.project.knowledge.entity.NotifyConfigsEntity; +import org.support.project.ormapping.common.DBUserPool; +import org.support.project.web.bean.LabelValue; +import org.support.project.web.entity.UsersEntity; + +/** + * Test for Notification logic. + * @author Koda + */ +public class NotifyCommentLogicTest extends TestCommon { + /** ログ */ + private static Log LOG = LogFactory.getLog(NotifyCommentLogicTest.class); + + /** テスト用ナレッジ1 */ + private static KnowledgesEntity knowledge1; + /** テスト用ナレッジ2 */ + private static KnowledgesEntity knowledge2; + /** テスト用ナレッジ3 */ + private static KnowledgesEntity knowledge3; + /** テスト用ナレッジ4 */ + private static KnowledgesEntity knowledge4; + + /** + * @BeforeClass + * @throws Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TestCommon.setUpBeforeClass(); + DBUserPool.get().setUser(loginedUser.getUserId()); + + LOG.info("テストユーザ1でKnowledge登録"); + KnowledgesEntity knowledge = new KnowledgesEntity(); + knowledge.setTitle("テスト1"); + knowledge.setContent("テスト"); + knowledge.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PUBLIC); // 公開 + knowledge1 = KnowledgeLogic.get().insert(knowledge, null, null, null, null, null, loginedUser); + + LOG.info("テストユーザ1でKnowledge登録"); + knowledge = new KnowledgesEntity(); + knowledge.setTitle("テスト2"); + knowledge.setContent("テスト2"); + knowledge.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PRIVATE); // 非公開 + knowledge2 = KnowledgeLogic.get().insert(knowledge, null, null, null, null, null, loginedUser); + + LOG.info("テストユーザ1でKnowledge登録"); + knowledge = new KnowledgesEntity(); + knowledge.setTitle("テスト3"); + knowledge.setContent("テスト3"); + knowledge.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PROTECT); // 非公開 + List targets = new ArrayList<>(); + LabelValue labelValue = new LabelValue(); + labelValue.setLabel(TargetLogic.ID_PREFIX_USER + loginedUser2.getUserId()); + labelValue.setValue(TargetLogic.ID_PREFIX_USER + loginedUser2.getUserId()); + targets.add(labelValue); + knowledge3 = KnowledgeLogic.get().insert(knowledge, null, null, targets, null, null, loginedUser); + + LOG.info("テストユーザ1でKnowledge登録"); + knowledge = new KnowledgesEntity(); + knowledge.setTitle("テスト4"); + knowledge.setContent("テスト4"); + knowledge.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PROTECT); // 非公開 + targets = new ArrayList<>(); + labelValue = new LabelValue(); + labelValue.setLabel(TargetLogic.ID_PREFIX_GROUP + group.getGroupId()); + labelValue.setValue(TargetLogic.ID_PREFIX_GROUP + group.getGroupId()); + targets.add(labelValue); + knowledge4 = KnowledgeLogic.get().insert(knowledge, null, null, targets, null, null, loginedUser); + } + + @Test + @Order(order = 1) + public void testNotify1() throws Exception { + KnowledgesEntity knowledge = knowledge1; + // 公開のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertNull(user); // 通知設定がセットされていないので、通知先は無し + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertNull(user); // 通知設定がセットされていないので、通知先は無し + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(0, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(0, users.size()); + + knowledge = knowledge3; + + // 保護のナレッジにコメント登録 + comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertNull(user); // 通知設定がセットされていないので、通知先は無し + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertNull(user); // 通知設定がセットされていないので、通知先は無し + + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(0, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(0, users.size()); + } + + + + + @Test + @Order(order = 2) + public void testNotify2() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setMyItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setMyItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser3.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setMyItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + KnowledgesEntity knowledge = knowledge1; + + // 公開のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(2, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(2, users.size()); + } + + + @Test + @Order(order = 3) + public void testNotify3() throws Exception { + KnowledgesEntity knowledge = knowledge2; + // 非公開のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertNull(user); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertNull(user); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(0, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(0, users.size()); + } + + @Test + @Order(order = 4) + public void testNotify4() throws Exception { + KnowledgesEntity knowledge = knowledge3; + // 保護のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(1, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(1, users.size()); + } + + + @Test + @Order(order = 5) + public void testNotify5() throws Exception { + DBUserPool.get().setUser(loginedUser2.getUserId()); + KnowledgesEntity knowledge = knowledge3; + // 保護のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(1, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(1, users.size()); + } + + + @Test + @Order(order = 6) + public void testNotify6() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(loginedUser2.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setMyItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.OFF.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + KnowledgesEntity knowledge = knowledge3; + // 公開のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(0, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(0, users.size()); + } + + + @Test + @Order(order = 7) + public void testNotify7() throws Exception { + NotifyConfigsEntity notifyConfigs = new NotifyConfigsEntity(); + notifyConfigs.setUserId(groupuser1.getUserId()); + notifyConfigs.setNotifyMail(INT_FLAG.ON.getValue()); + notifyConfigs.setNotifyDesktop(INT_FLAG.ON.getValue()); + notifyConfigs.setMyItemComment(INT_FLAG.ON.getValue()); + notifyConfigs.setToItemComment(INT_FLAG.ON.getValue()); + NotifyConfigsDao.get().save(notifyConfigs); + + KnowledgesEntity knowledge = knowledge4; + // 公開のナレッジにコメント登録 + CommentsEntity comment = new CommentsEntity(); + comment.setKnowledgeId(knowledge.getKnowledgeId()); + CommentsDao.get().save(comment); + + UsersEntity user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + user = NotifyCommentLogic.get().getInsertUserOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(loginedUser.getUserId(), user.getUserId()); + + List users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Desktop, comment, knowledge); + Assert.assertEquals(1, users.size()); + users = NotifyCommentLogic.get().getTargetUsersOnComment(NotifyType.Mail, comment, knowledge); + Assert.assertEquals(1, users.size()); + } + + +} diff --git a/src/test/resources/ormappingtool.xml b/src/test/resources/ormappingtool.xml index 050d552af..3365c3ddc 100644 --- a/src/test/resources/ormappingtool.xml +++ b/src/test/resources/ormappingtool.xml @@ -3,7 +3,7 @@ connection org.h2.Driver - jdbc:h2:~/.knowledge/knowledgeTestDB + jdbc:h2:tcp://localhost/./knowledgeTestDB sa public