Skip to content

Commit

Permalink
#695 Add a internal processing for survey copy
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed May 11, 2017
1 parent b47f644 commit 21e72be
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 7 deletions.
64 changes: 63 additions & 1 deletion src/main/java/org/support/project/knowledge/dao/SurveysDao.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.support.project.knowledge.dao;

import java.util.ArrayList;
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.GenSurveysDao;
import org.support.project.knowledge.entity.SurveysEntity;
import org.support.project.ormapping.common.SQLManager;
import org.support.project.web.bean.LoginedUser;
import org.support.project.web.entity.GroupsEntity;

/**
* アンケート
Expand All @@ -21,7 +27,63 @@ public class SurveysDao extends GenSurveysDao {
public static SurveysDao get() {
return Container.getComp(SurveysDao.class);
}

/**
* IDのプレフィックスで絞り込んだアンケートの一覧を取得
* @param idPrefix
* @param limit
* @param offset
* @return
*/
public List<SurveysEntity> selectWithKnowledgeTitle(String idPrefix, int limit, int offset) {
String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/SurveysDao/SurveysDao_select_with_knowledge_title.sql");
return executeQueryList(sql, SurveysEntity.class, idPrefix, limit, offset);
}

/**
* IDのプレフィックスで絞り込んだアンケートの一覧を取得
* アクセス権のあるKnowledgeに紐付いたもののみ
* @param loginedUser
* @param idPrefix
* @param limit
* @param offset
* @return
*/
public List<SurveysEntity> selectWithKnowledgeTitleOnlyAccessAble(LoginedUser loginedUser, String idPrefix, int limit, int offset) {
String sql = SQLManager.getInstance().getSql("/org/support/project/knowledge/dao/sql/SurveysDao/SurveysDao_select_on_accessable.sql");
List<Object> params = new ArrayList<>();
params.add(idPrefix);
Integer loginuserId = Integer.MIN_VALUE;
if (loginedUser != null) {
loginuserId = loginedUser.getUserId();
}
params.add(loginuserId);
params.add(loginuserId);

List<Integer> groups = new ArrayList<>();
groups.add(0); // ALL Groups
if (loginedUser != null && loginedUser.getGroups() != null) {
List<GroupsEntity> userGroups = loginedUser.getGroups();
for (GroupsEntity groupsEntity : userGroups) {
groups.add(groupsEntity.getGroupId());
}
}
StringBuilder groupParams = new StringBuilder();
int cnt = 0;
for (Integer integer : groups) {
if (cnt > 0) {
groupParams.append(", ");
}
cnt++;
params.add(integer);
groupParams.append("?");
}
sql = sql.replace("%GROUPS%", groupParams.toString());
params.add(limit);
params.add(offset);

return executeQueryList(sql, SurveysEntity.class, params.toArray(new Object[0]));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class SurveysEntity extends GenSurveysEntity {

private boolean editable;

private String knowledgeTitle;

/** SerialVersion */
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -74,4 +76,18 @@ public void setEditable(boolean editable) {
this.editable = editable;
}

/**
* @return the knowledgeTitle
*/
public String getKnowledgeTitle() {
return knowledgeTitle;
}

/**
* @param knowledgeTitle the knowledgeTitle to set
*/
public void setKnowledgeTitle(String knowledgeTitle) {
this.knowledgeTitle = knowledgeTitle;
}

}
29 changes: 29 additions & 0 deletions src/main/java/org/support/project/knowledge/logic/SurveyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,36 @@ public SurveysEntity saveSurvey(SurveysEntity survey, LoginedUser loginedUser) {
}
return new SurveysEntity();
}

/**
* 登録されているアンケートの一覧を取得(ページ制御あり)
* @param user ログインユーザ
* @param idPrefix IDのプレフィックス(絞込条件)
* @param page ページ番号
* @return
*/
public List<SurveysEntity> listSurveys(LoginedUser user, String idPrefix, int page) {
int limit = 20;
int offset = page * limit;
if (user == null) {
// アンケート一覧はコピーする対象を選択する機能なので、Knowledge編集者のはずで、userがnullはありえない
return new ArrayList<>();
} else if (user.isAdmin()) {
// アクセス権関係なく、全てのアンケート情報を取得
return SurveysDao.get().selectWithKnowledgeTitle(idPrefix, limit, offset);
} else {
// アクセス可能なアンケートの情報を取得
return SurveysDao.get().selectWithKnowledgeTitleOnlyAccessAble(user, idPrefix, limit, offset);
}
}


/**
* アンケート情報の取得
* @param knowledgeId
* @param userId
* @return
*/
public SurveysEntity loadSurvey(Long knowledgeId, Integer userId) {
LOG.trace("loadSurvey");
SurveysEntity entity = SurveysDao.get().selectOnKey(knowledgeId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
SELECT
KNOWLEDGES.KNOWLEDGE_ID,
KNOWLEDGES.TITLE AS KNOWLEDGE_TITLE,
SURVEYS.TITLE AS TITLE
FROM
KNOWLEDGES
INNER JOIN SURVEYS ON (KNOWLEDGES.KNOWLEDGE_ID = SURVEYS.KNOWLEDGE_ID)
WHERE
KNOWLEDGES.DELETE_FLAG = 0
AND CAST(KNOWLEDGES.KNOWLEDGE_ID AS VARCHAR(20)) LIKE ? || '%'
AND (
KNOWLEDGES.PUBLIC_FLAG = 0
OR (
KNOWLEDGES.PUBLIC_FLAG = 1
AND KNOWLEDGES.INSERT_USER = ?
)
OR (
KNOWLEDGES.PUBLIC_FLAG = 2
AND EXISTS (
SELECT
*
FROM
KNOWLEDGE_USERS
WHERE
KNOWLEDGES.KNOWLEDGE_ID = KNOWLEDGE_USERS.KNOWLEDGE_ID
AND KNOWLEDGE_USERS.USER_ID = ?
)
)
OR (
KNOWLEDGES.PUBLIC_FLAG = 2
AND EXISTS (
SELECT
*
FROM
KNOWLEDGE_GROUPS
WHERE
KNOWLEDGES.KNOWLEDGE_ID = KNOWLEDGE_GROUPS.KNOWLEDGE_ID
AND KNOWLEDGE_GROUPS.GROUP_ID IN (%GROUPS%)
)
)
)
ORDER BY
KNOWLEDGES.KNOWLEDGE_ID ASC
LIMIT ? OFFSET ?;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
KNOWLEDGES.KNOWLEDGE_ID,
KNOWLEDGES.TITLE AS KNOWLEDGE_TITLE,
SURVEYS.TITLE AS TITLE
FROM
KNOWLEDGES
INNER JOIN SURVEYS ON (KNOWLEDGES.KNOWLEDGE_ID = SURVEYS.KNOWLEDGE_ID)
WHERE
KNOWLEDGES.DELETE_FLAG = 0
AND CAST(KNOWLEDGES.KNOWLEDGE_ID AS VARCHAR(20)) LIKE ? || '%'
ORDER BY
KNOWLEDGES.KNOWLEDGE_ID ASC
LIMIT ? OFFSET ?;
14 changes: 12 additions & 2 deletions src/test/java/org/support/project/knowledge/TestCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.support.project.knowledge.config.AppConfig;
import org.support.project.knowledge.dao.gen.DatabaseControlDao;
import org.support.project.knowledge.deploy.InitDB;
import org.support.project.ormapping.common.DBUserPool;
import org.support.project.ormapping.connection.ConnectionManager;
import org.support.project.ormapping.tool.config.ORmappingToolConfig;
import org.support.project.web.bean.LoginedUser;
Expand Down Expand Up @@ -48,6 +49,8 @@ public abstract class TestCommon {
public static LoginedUser loginedUser2 = null;
/** login user for test */
public static LoginedUser loginedUser3 = null;
/** login user for test */
public static LoginedUser loginedAdmin = null;

/** group for test */
public static GroupsEntity group = null;
Expand All @@ -64,17 +67,20 @@ public abstract class TestCommon {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
AppConfig.initEnvKey(KNOWLEDGE_TEST_HOME);
H2DBServerLogic.get().start();
if (!H2DBServerLogic.get().isActive()) {
H2DBServerLogic.get().start();
}
testConnection();
initData();
DBUserPool.get().setUser(loginedUser.getUserId());
}
/**
* @AfterClass
* @throws Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
H2DBServerLogic.get().stop();
// H2DBServerLogic.get().stop();
}
/**
* @Before
Expand Down Expand Up @@ -112,12 +118,14 @@ public static void initData() throws Exception {
loginedUser = new LoginedUser();
loginedUser2 = new LoginedUser();
loginedUser3 = new LoginedUser();
loginedAdmin = new LoginedUser();
groupuser1 = new LoginedUser();
groupuser2 = new LoginedUser();

loginedUser.setLocale(Locale.ENGLISH);
loginedUser2.setLocale(Locale.ENGLISH);
loginedUser3.setLocale(Locale.ENGLISH);
loginedAdmin.setLocale(Locale.ENGLISH);
groupuser1.setLocale(Locale.ENGLISH);
groupuser2.setLocale(Locale.ENGLISH);

Expand All @@ -133,10 +141,12 @@ public static void initData() throws Exception {
File indexDir = new File(appConfig.getIndexPath());
FileUtil.delete(indexDir);

Integer[] rolesAdmin = {1}; // 2はユーザ、1はAdmin
Integer[] roles = {2}; // 2はユーザ、1はAdmin
addUser(loginedUser, "テストユーザ1", roles);
addUser(loginedUser2, "テストユーザ2", roles);
addUser(loginedUser3, "テストユーザ3", roles);
addUser(loginedAdmin, "管理者", rolesAdmin);

addUser(groupuser1, "GroupUser1", roles);
addUser(groupuser2, "GroupUser2", roles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public class AccountImagesDaoTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
AppConfig.initEnvKey(TestCommon.KNOWLEDGE_TEST_HOME);
H2DBServerLogic.get().start();
if (!H2DBServerLogic.get().isActive()) {
H2DBServerLogic.get().start();
}
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
H2DBServerLogic.get().stop();
// H2DBServerLogic.get().stop();
}

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public class LikesDaoTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
AppConfig.initEnvKey(TestCommon.KNOWLEDGE_TEST_HOME);
H2DBServerLogic.get().start();
if (!H2DBServerLogic.get().isActive()) {
H2DBServerLogic.get().start();
}
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
H2DBServerLogic.get().stop();
// H2DBServerLogic.get().stop();
}

@Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.support.project.knowledge.dao;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;
import org.support.project.common.test.Order;
import org.support.project.knowledge.TestCommon;
import org.support.project.knowledge.entity.KnowledgesEntity;
import org.support.project.knowledge.entity.SurveysEntity;
import org.support.project.knowledge.logic.KnowledgeLogic;
import org.support.project.knowledge.vo.KnowledgeData;
import org.support.project.ormapping.common.DBUserPool;
import org.support.project.web.bean.LoginedUser;

public class SurveysDaoTest extends TestCommon {

@Test
@Order(order = 1)
public void testSelectWithKnowledgeTitle() throws Exception {
LoginedUser admin = SurveysDaoTest.loginedAdmin; //AdminUser

DBUserPool.get().setUser(admin.getUserId());

// Knowledge登録(非公開)
KnowledgesEntity entity = new KnowledgesEntity();
entity.setTitle("Test1");
entity.setContent("テストだよ");
entity.setTypeId(-100);
KnowledgeLogic logic = KnowledgeLogic.get();
KnowledgeData data = new KnowledgeData();
data.setKnowledge(entity);
entity.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PRIVATE);
entity = logic.insert(data, admin);

// Survey登録
SurveysEntity survey = new SurveysEntity(entity.getKnowledgeId());
survey.setTitle("テストアンケート");
SurveysDao.get().save(survey);

// 検索
List<SurveysEntity> surveysEntities = SurveysDao.get().selectWithKnowledgeTitle("", 10, 0);

// 結果確認(1件取得)
assertEquals(1, surveysEntities.size());
}

@Test
@Order(order = 2)
public void testSelectWithKnowledgeTitleOnlyAccessAble() throws Exception {
LoginedUser admin = SurveysDaoTest.loginedAdmin; //AdminUser
LoginedUser user = SurveysDaoTest.loginedUser2;
KnowledgeLogic logic = KnowledgeLogic.get();

DBUserPool.get().setUser(admin.getUserId());

// 検索
List<SurveysEntity> surveysEntities = SurveysDao.get().selectWithKnowledgeTitleOnlyAccessAble(user, "", 10, 0);
// 結果確認(Privateは取得されない/test1で登録したものが取得される)
assertEquals(0, surveysEntities.size());

// Knowledge登録(公開)
KnowledgesEntity entity = new KnowledgesEntity();
entity.setTitle("Test2");
entity.setContent("テストだよ");
entity.setTypeId(-100);
KnowledgeData data = new KnowledgeData();
data.setKnowledge(entity);
entity.setPublicFlag(KnowledgeLogic.PUBLIC_FLAG_PUBLIC);
entity = logic.insert(data, admin);

// Survey登録
SurveysEntity survey = new SurveysEntity(entity.getKnowledgeId());
survey.setTitle("テストアンケート");
SurveysDao.get().save(survey);

// 検索
surveysEntities = SurveysDao.get().selectWithKnowledgeTitleOnlyAccessAble(user, "", 10, 0);
// 結果確認(1件取得)
assertEquals(1, surveysEntities.size());
}




}
Loading

0 comments on commit 21e72be

Please sign in to comment.