Skip to content

Commit

Permalink
#840 Add add point process at knowledge show
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Aug 31, 2017
1 parent 331c05e commit a54e586
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public List<Long> selectViewdKnowledgeIds(List<StockKnowledge> stocks, Integer u
return executeQueryList(sql.toString(), Long.class, params.toArray(new Object[0]));
}

public long selectCountOnKnowledgeId(Long knowledgeId) {
String sql = "SELECT COUNT(*) FROM VIEW_HISTORIES WHERE KNOWLEDGE_ID = ?";
return executeQuerySingle(sql, Long.class, knowledgeId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.knowledge.dao.KnowledgesDao;
import org.support.project.knowledge.dao.ViewHistoriesDao;
import org.support.project.knowledge.deploy.Migrate;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.ViewHistoriesEntity;
import org.support.project.knowledge.logic.activity.Activity;
import org.support.project.knowledge.logic.activity.ActivityLogic;
import org.support.project.ormapping.tool.dao.InitializeDao;
Expand All @@ -31,39 +33,112 @@ public boolean doMigrate() throws Exception {
};
initializeDao.initializeDatabase(sqlpaths);

// 参照回数を集計
doSetViewCountToKnowledge();

// Knowledgeを登録したイベントによりポイントを集計
doAddPointByKnowledge();

// Knowledgeを参照したイベントによりポイントを集計
doAddPointByKnowledgeShow();

// 参照回数を集計


return true;
}



private void doAddPointByKnowledgeShow() throws InterruptedException {
List<ViewHistoriesEntity> list;
int offset = 0;
int limit = 50;
do {
list = doAddPointByKnowledgeShow(offset, limit);
offset = offset + limit;
synchronized (this) {
wait(200);
}
} while (list.size() > 0);
}

@Aspect(advice = org.support.project.ormapping.transaction.Transaction.class)
public void doAddPointByKnowledge() {
// ポイント修正
LOG.info("Aggregate point by knowledge insert");
private List<ViewHistoriesEntity> doAddPointByKnowledgeShow(int offset, int limit) {
LOG.info("Aggregate point by knowledge show");
List<ViewHistoriesEntity> list;
list = ViewHistoriesDao.get().selectAllWidthPager(limit, offset);
for (ViewHistoriesEntity viewHistoriesEntity : list) {
KnowledgesEntity knowledge = KnowledgesDao.get().selectOnKey(viewHistoriesEntity.getKnowledgeId());
if (knowledge == null) {
LOG.info(" knowledge [" + viewHistoriesEntity.getKnowledgeId() + "] is not found. so skip add point by knowledge show.");
continue;
}
LoginedUser user = new LoginedUser();
UsersEntity account = UsersDao.get().selectOnKey(viewHistoriesEntity.getInsertUser());
if (account == null) {
LOG.info(" event user [" + knowledge.getInsertUser() + "] is not found. so skip add point by knowledge show.");
continue;
}
user.setLoginUser(account);
LOG.info(" knowledge [" + knowledge.getKnowledgeId() + "] ");
ActivityLogic.get().processActivity(Activity.KNOWLEDGE_SHOW, user, knowledge);
}
return list;
}

public void doAddPointByKnowledge() throws InterruptedException {
List<KnowledgesEntity> knowledges;
int offset = 0;
int limit = 50;
do {
knowledges = KnowledgesDao.get().selectAllWidthPager(limit, offset);
for (KnowledgesEntity knowledge : knowledges) {
LoginedUser user = new LoginedUser();
UsersEntity account = UsersDao.get().selectOnKey(knowledge.getInsertUser());
if (account == null) {
LOG.info("insert user [" + knowledge.getInsertUser() + "] is not found. so skip add point by knowledge insert.");
continue;
}
user.setLoginUser(account);
LOG.info(" knowledge [" + knowledge.getKnowledgeId() + "] ");
ActivityLogic.get().processActivity(Activity.KNOWLEDGE_INSERT, user, knowledge);
}
knowledges = doAddPointByKnowledge(offset, limit);
offset = offset + limit;
synchronized (this) {
wait(200);
}
} while (knowledges.size() > 0);
}
@Aspect(advice = org.support.project.ormapping.transaction.Transaction.class)
private List<KnowledgesEntity> doAddPointByKnowledge(int offset, int limit) {
LOG.info("Aggregate point by knowledge insert");
List<KnowledgesEntity> knowledges;
knowledges = KnowledgesDao.get().selectAllWidthPager(limit, offset);
for (KnowledgesEntity knowledge : knowledges) {
LoginedUser user = new LoginedUser();
UsersEntity account = UsersDao.get().selectOnKey(knowledge.getInsertUser());
if (account == null) {
LOG.info(" insert user [" + knowledge.getInsertUser() + "] is not found. so skip add point by knowledge insert.");
continue;
}
user.setLoginUser(account);
LOG.info(" knowledge [" + knowledge.getKnowledgeId() + "] ");
ActivityLogic.get().processActivity(Activity.KNOWLEDGE_INSERT, user, knowledge);
}
return knowledges;
}

private void doSetViewCountToKnowledge() throws InterruptedException {
List<KnowledgesEntity> knowledges;
int offset = 0;
int limit = 50;
do {
knowledges = doSetViewCountToKnowledge(offset, limit);
offset = offset + limit;
synchronized (this) {
wait(200);
}
} while (knowledges.size() > 0);
}
@Aspect(advice = org.support.project.ormapping.transaction.Transaction.class)
private List<KnowledgesEntity> doSetViewCountToKnowledge(int offset, int limit) {
LOG.info("Set view count to Knowledge");
List<KnowledgesEntity> knowledges;
knowledges = KnowledgesDao.get().selectAllWidthPager(limit, offset);
for (KnowledgesEntity knowledge : knowledges) {
LOG.info(" knowledge [" + knowledge.getKnowledgeId() + "] ");
long count = ViewHistoriesDao.get().selectCountOnKnowledgeId(knowledge.getKnowledgeId());
KnowledgesDao.get().updateViewCount(count, knowledge.getKnowledgeId());
}
return knowledges;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public LoginedUser getUser() {
public void setUser(LoginedUser user) {
this.user = user;
}

/**
* ポイントと種類を保持する内部クラス
* @author koda
*/
protected class TypeAndPoint {
int type;
int point;
TypeAndPoint(int type, int point) {
this.type = type;
this.point = point;
}
}

/**
* 対象のactivityが既に登録されているかチェック
* @param userId アクティビティを実行したユーザ
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.support.project.knowledge.logic.activity;

import org.support.project.knowledge.entity.ActivitiesEntity;
import org.support.project.knowledge.entity.KnowledgesEntity;

public abstract class AbstractAddPointForKnowledgeProcessor extends AbstractActivityProcessor {
Expand All @@ -16,4 +17,65 @@ public KnowledgesEntity getKnowledge() {
public void setKnowledge(KnowledgesEntity knowledge) {
this.knowledge = knowledge;
}

protected abstract Activity getActivity();
protected abstract TypeAndPoint getTypeAndPointForActivityExecuter();
protected abstract TypeAndPoint getTypeAndPointForKnowledgeOwner();
protected abstract TypeAndPoint getTypeAndPointForKnowledge();

@Override
public void execute() throws Exception {
if (getKnowledge() == null || getUser() == null) {
// ありえないけど念のため確認
return;
}
if (isExistsActivity(getUser().getUserId(), getActivity(), String.valueOf(getKnowledge().getKnowledgeId()))) {
// 既に指定のKnowledge登録済
return;
}
// ポイント発行アクティビティを生成
ActivitiesEntity activity = addActivity(
getUser().getUserId(),
getActivity(),
String.valueOf(getKnowledge().getKnowledgeId()),
getKnowledge().getInsertDatetime());



// 実行したユーザのポイントアップ
TypeAndPoint exec = getTypeAndPointForActivityExecuter();
if (exec != null) {
addPointForUser(
getUser().getUserId(),
getKnowledge().getInsertDatetime(),
getUser().getUserId(), // ターゲットは、実行したユーザ
activity.getActivityNo(),
exec.type,
exec.point);
}
// 記事の登録者のポイントをアップ
TypeAndPoint owner = getTypeAndPointForKnowledgeOwner();
if (owner != null) {
addPointForUser(
getUser().getUserId(),
getKnowledge().getInsertDatetime(),
getKnowledge().getInsertUser(), // ターゲットは登録者
activity.getActivityNo(),
exec.type,
exec.point);
}
// 記事のポイントアップ
TypeAndPoint knowledge = getTypeAndPointForKnowledge();
if (knowledge != null) {
addPointForKnowledge(
getUser().getUserId(),
getKnowledge().getInsertDatetime(),
getKnowledge().getKnowledgeId(),
activity.getActivityNo(),
exec.type,
exec.point);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface ActivityProcessor {
*/
public static final int TYPE_KNOWLEDGE_DO_INSERT = 11;
public static final int TYPE_KNOWLEDGE_INSERTED = 12;
public static final int TYPE_KNOWLEDGE_DO_SHOW = 21;
public static final int TYPE_KNOWLEDGE_SHOWN_BY_OHER = 22;
public static final int TYPE_KNOWLEDGE_SHOWN = 23;

void execute() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.support.project.di.Container;
import org.support.project.di.DI;
import org.support.project.di.Instance;
import org.support.project.knowledge.entity.ActivitiesEntity;

/**
* ナレッジ登録時のポイント付与
Expand All @@ -21,43 +20,21 @@ public class KnowledgeInsertActivity extends AbstractAddPointForKnowledgeProcess
public static KnowledgeInsertActivity get() {
return Container.getComp(KnowledgeInsertActivity.class);
}

@Override
public void execute() throws Exception {
if (getKnowledge() == null || getUser() == null) {
// ありえないけど念のため確認
return;
}
if (isExistsActivity(getUser().getUserId(), Activity.KNOWLEDGE_INSERT, String.valueOf(getKnowledge().getKnowledgeId()))) {
// 既に指定のKnowledge登録済
return;
}
//int point = RandomUtil.randamNum(30, 50);
int point = 50;

LOG.debug("Add point by knowledge insert:" + point);

// ポイント発行アクティビティを生成
ActivitiesEntity activity = addActivity(
getUser().getUserId(),
Activity.KNOWLEDGE_INSERT,
String.valueOf(getKnowledge().getKnowledgeId()),
getKnowledge().getInsertDatetime());
// 登録者のポイントをアップ
addPointForUser(
getUser().getUserId(),
getKnowledge().getInsertDatetime(),
getUser().getUserId(),
activity.getActivityNo(),
TYPE_KNOWLEDGE_DO_INSERT,
point);
// 記事のポイントアップ
addPointForKnowledge(
getUser().getUserId(),
getKnowledge().getInsertDatetime(),
getKnowledge().getKnowledgeId(),
activity.getActivityNo(),
TYPE_KNOWLEDGE_INSERTED,
point);
protected Activity getActivity() {
LOG.info("Start add point process on insert knowledge.");
return Activity.KNOWLEDGE_INSERT;
}
@Override
protected TypeAndPoint getTypeAndPointForActivityExecuter() {
return new TypeAndPoint(TYPE_KNOWLEDGE_DO_INSERT, 50);
}
@Override
protected TypeAndPoint getTypeAndPointForKnowledgeOwner() {
return null;
}
@Override
protected TypeAndPoint getTypeAndPointForKnowledge() {
return new TypeAndPoint(TYPE_KNOWLEDGE_INSERTED, 50);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.support.project.knowledge.logic.activity;

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;

/**
*
* 種類 | 獲得のタイプ | ポイント付与先 | ポイント | 獲得タイプの意味
* 2 | 21 | 参照者 | 1 | 記事を参照するアクションを行うと、参照者にポイント追加(一つの記事に付き1回のみ)
* 2 | 22 | 記事登録者 | 1 | 自分が登録された記事が参照されたら、登録者にポイント追加(一つの記事に対し、参照者毎に1回のみ)
* 2 | 23 | 記事 | 1 | 記事が参照されると、その記事のポイントが追加(一つの記事に対し、参照者毎に1回のみ)
*
* @author koda
*/
@DI(instance = Instance.Prototype)
public class KnowledgeShowActivity extends AbstractAddPointForKnowledgeProcessor {
private static final Log LOG = LogFactory.getLog(KnowledgeShowActivity.class);
public static KnowledgeShowActivity get() {
return Container.getComp(KnowledgeShowActivity.class);
}

@Override
protected Activity getActivity() {
LOG.info("Start add point process on show knowledge.");
return Activity.KNOWLEDGE_SHOW;
}
@Override
protected TypeAndPoint getTypeAndPointForActivityExecuter() {
return new TypeAndPoint(TYPE_KNOWLEDGE_DO_SHOW, 1);
}
@Override
protected TypeAndPoint getTypeAndPointForKnowledgeOwner() {
return new TypeAndPoint(TYPE_KNOWLEDGE_SHOWN_BY_OHER, 1);
}
@Override
protected TypeAndPoint getTypeAndPointForKnowledge() {
return new TypeAndPoint(TYPE_KNOWLEDGE_SHOWN, 1);
}

}

0 comments on commit a54e586

Please sign in to comment.