Skip to content

Commit

Permalink
#829 Add base class to internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
koda-masaru committed Oct 23, 2017
1 parent 1c7e3e3 commit baabbdf
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.support.project.web.bean.ApiParams;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.control.ApiControl;
import org.support.project.web.control.GetApiControl;
import org.support.project.web.control.service.Get;

public class AttachControl extends ApiControl {
public class AttachControl extends GetApiControl {
/** ログ */
private static final Log LOG = LogFactory.getLog(AttachControl.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.support.project.knowledge.control.api;

import java.util.ArrayList;
import java.util.List;

import org.support.project.common.util.PropertyUtil;
import org.support.project.common.util.StringUtils;
import org.support.project.di.DI;
Expand All @@ -19,12 +16,15 @@
import org.support.project.web.bean.ApiParams;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.control.ApiControl;
import org.support.project.web.control.GetApiControl;
import org.support.project.web.control.service.Get;
import org.support.project.web.entity.GroupsEntity;

import java.util.ArrayList;
import java.util.List;

@DI(instance = Instance.Prototype)
public class GroupsControl extends ApiControl {
public class GroupsControl extends GetApiControl {
/**
* List groups
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.control.ApiControl;
import org.support.project.web.control.GetApiControl;
import org.support.project.web.control.service.Delete;
import org.support.project.web.control.service.Get;
import org.support.project.web.control.service.Post;
Expand All @@ -28,7 +29,7 @@
import net.arnx.jsonic.JSONException;

@DI(instance = Instance.Prototype)
public class KnowledgesControl extends ApiControl {
public class KnowledgesControl extends GetApiControl {
/** ログ */
private static Log LOG = LogFactory.getLog(ControlManagerFilter.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.support.project.knowledge.control.api;

import java.util.ArrayList;
import java.util.List;

import org.support.project.common.util.PropertyUtil;
import org.support.project.common.util.StringUtils;
import org.support.project.di.DI;
Expand All @@ -14,13 +11,16 @@
import org.support.project.web.bean.ApiParams;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.control.ApiControl;
import org.support.project.web.control.GetApiControl;
import org.support.project.web.control.service.Get;
import org.support.project.web.dao.UsersDao;
import org.support.project.web.entity.UsersEntity;

import java.util.ArrayList;
import java.util.List;

@DI(instance = Instance.Prototype)
public class UsersControl extends ApiControl {
public class UsersControl extends GetApiControl {
/**
* List users
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.support.project.knowledge.control.open.api;

import org.support.project.web.bean.Msg;
import org.support.project.web.boundary.JsonBoundary;
import org.support.project.web.control.ApiControl;
import org.support.project.web.control.service.Get;

/**
* Knowledgeの一覧を取得するためのAPI
*/
public class KnowledgeSearchControl extends ApiControl {

@Get(path="open.api/knowledges", publishToken="")
public JsonBoundary index() {
return send(new Msg("aaa"));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,17 @@ private void addTarget(Class<?> class1, Method method, String targetPackageName,
}
InvokeTarget invokeTarget = new InvokeTarget(class1, method, targetPackageName, classSuffix, new LinkedHashMap<>());
if (invokeTargets.containsKey(key)) {
InvokeTarget exists = invokeTargets.get(key);
if (exists.getTargetClass().getName().equals(invokeTarget.getTargetClass().getName())
&& exists.getTargetMethod().getName().equals(invokeTarget.getTargetMethod().getName())) {
//なぜか、同じクラスの同じメソッドが二回登録されることがあるのでスキップ
LOG.info("same target duplicate add. [" + key + "]");
return;
}
// 既に指定のパスが使われている
LOG.error("Target duplicated. [" + key + "]");
LOG.error("class:" + invokeTarget.getTargetClass().getName() + " method:" + invokeTarget.getTargetMethod().getName());
LOG.error("class:" + exists.getTargetClass().getName() + " method:" + exists.getTargetMethod().getName());
throw new SystemException("Target duplicated. [" + key + "]");
}
// 大文字/小文字は判定しない
Expand Down
45 changes: 1 addition & 44 deletions src/main/java/org/support/project/web/control/ApiControl.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.support.project.web.control;

import org.support.project.common.config.Resources;
import org.support.project.common.util.StringUtils;
import org.support.project.web.bean.ApiParams;
import org.support.project.web.bean.Msg;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
Expand Down Expand Up @@ -31,47 +29,6 @@ protected Boundary sendError(InvalidParamException e) {
}
}

public abstract Boundary getList(ApiParams params);
public abstract Boundary getSingle(String id);
public int maxLimit() {
return 50;
}

protected ApiParams getApiParams() {
// 一覧取得
int limit = 10;
int offset = 0;
String limitStr = getParam("limit");
if (StringUtils.isInteger(limitStr)) {
limit = Integer.parseInt(limitStr);
}
if (limit > maxLimit()) {
limit = maxLimit();
}
String offsetStr = getParam("offset");
if (StringUtils.isInteger(offsetStr)) {
offset = Integer.parseInt(offsetStr);
}
ApiParams params = new ApiParams();
params.setLimit(limit);
params.setOffset(offset);
return params;
}

/**
* APIの基本的なGetのパターンを処理
* 上の getList or getSingle が呼び出される
* @return
*/
protected Boundary get() {
String id = super.getPathString("");
if (StringUtils.isEmpty(id)) {
ApiParams params = getApiParams();
return getList(params);
} else {
// 1件取得
return getSingle(id);
}
}


}
52 changes: 52 additions & 0 deletions src/main/java/org/support/project/web/control/GetApiControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.support.project.web.control;

import org.support.project.common.util.StringUtils;
import org.support.project.web.bean.ApiParams;
import org.support.project.web.boundary.Boundary;

public abstract class GetApiControl extends ApiControl {

public abstract Boundary getList(ApiParams params);
public abstract Boundary getSingle(String id);
public int maxLimit() {
return 50;
}

protected ApiParams getApiParams() {
// 一覧取得
int limit = 10;
int offset = 0;
String limitStr = getParam("limit");
if (StringUtils.isInteger(limitStr)) {
limit = Integer.parseInt(limitStr);
}
if (limit > maxLimit()) {
limit = maxLimit();
}
String offsetStr = getParam("offset");
if (StringUtils.isInteger(offsetStr)) {
offset = Integer.parseInt(offsetStr);
}
ApiParams params = new ApiParams();
params.setLimit(limit);
params.setOffset(offset);
return params;
}

/**
* APIの基本的なGetのパターンを処理
* 上の getList or getSingle が呼び出される
* @return
*/
protected Boundary get() {
String id = super.getPathString("");
if (StringUtils.isEmpty(id)) {
ApiParams params = getApiParams();
return getList(params);
} else {
// 1件取得
return getSingle(id);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.support.project.knowledge.searcher;

import org.junit.Before;
import org.junit.Test;
import org.support.project.common.config.ConfigLoader;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.DateUtils;
import org.support.project.common.util.FileUtil;
import org.support.project.common.util.PropertyUtil;
import org.support.project.common.util.RandomUtil;
import org.support.project.di.Container;
import org.support.project.knowledge.config.AppConfig;
import org.support.project.knowledge.indexer.Indexer;
import org.support.project.knowledge.indexer.IndexingValue;
import org.support.project.knowledge.logic.TemplateLogic;

import java.io.File;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SearchConditionTest {
/** ログ */
private static final Log LOG = LogFactory.getLog(SearchConditionTest.class);

@Before
public void setUp() throws Exception {
// テスト毎にインデックスを初期化する
AppConfig appConfig = ConfigLoader.load(AppConfig.APP_CONFIG, AppConfig.class);
File f = new File(appConfig.getIndexPath());
FileUtil.delete(f);
LOG.info("インデックスを削除しました");
}

/**
* インデックスにアイテム追加
* @param title タイトル
* @param contents コンテンツ
* @param type 種類(記事、添付ファイル、コメントなど)
* @param template テンプレート
* @param creator 作成者
* @param updateDateTime 更新日時
* @param tagIds タグのID
* @param viewUsers 閲覧可能ユーザ
* @param viewGroups 閲覧可能グループ
* @return 登録した情報のID
*/
private String addItem(String title, String contents, int type, int template, int creator, Date updateDateTime,
int[] tagIds, int[] viewUsers, int[] viewGroups) throws Exception {
String id = String.valueOf(RandomUtil.randamNum(0, 999999));
IndexingValue indexingValue = new IndexingValue();
indexingValue.setType(type);
indexingValue.setId(id);
indexingValue.setTitle(title);
indexingValue.setContents(contents);
indexingValue.setTemplate(template);
indexingValue.setCreator(creator);
indexingValue.setTime(updateDateTime.getTime());

if (tagIds != null) {
for (int tagId : tagIds) {
indexingValue.addTag(tagId);
}
}
if (viewUsers != null) {
for (int user : viewUsers) {
indexingValue.addUser(user);
}
}
if (viewGroups != null) {
for (int group : viewGroups) {
indexingValue.addGroup(group);
}
}
Indexer indexer = Container.getComp(Indexer.class);
indexer.writeIndex(indexingValue);
return id;
}

@Test
public void testTemplateFilter() throws Exception {
String id1 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_KNOWLEDGE,
100, DateUtils.now(), null, null, null);
String id2 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_EVENT,
100, DateUtils.now(), null, null, null);
String id3 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_BOOKMARK,
100, DateUtils.now(), null, null, null);

Searcher searcher = Container.getComp(Searcher.class);

SearchingValue searchingValue = new SearchingValue();
searchingValue.addTemplate(TemplateLogic.TYPE_ID_KNOWLEDGE);
List<SearchResultValue> results = searcher.search(searchingValue, 1);
for (SearchResultValue searchResultValue : results) {
LOG.info(PropertyUtil.reflectionToString(searchResultValue));
}
assertEquals(1, results.size());
SearchResultValue result = results.get(0);
assertEquals(id1, result.getId());

searchingValue.addTemplate(TemplateLogic.TYPE_ID_EVENT);
results = searcher.search(searchingValue, 1);
for (SearchResultValue searchResultValue : results) {
LOG.info(PropertyUtil.reflectionToString(searchResultValue));
}
assertEquals(2, results.size());
}

@Test
public void testSearchCreator() throws Exception {
String id1 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_KNOWLEDGE,
100, DateUtils.now(), null, null, null);
String id2 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_EVENT,
101, DateUtils.now(), null, null, null);
String id3 = addItem("title:testTemplateFilter", "contents:testTemplateFilter", 0, TemplateLogic.TYPE_ID_BOOKMARK,
102, DateUtils.now(), null, null, null);

Searcher searcher = Container.getComp(Searcher.class);

SearchingValue searchingValue = new SearchingValue();
searchingValue.setCreator(100);
List<SearchResultValue> results = searcher.search(searchingValue, 1);
for (SearchResultValue searchResultValue : results) {
LOG.info(PropertyUtil.reflectionToString(searchResultValue));
}
assertEquals(1, results.size());
SearchResultValue result = results.get(0);
assertEquals(id1, result.getId());

}

}

0 comments on commit baabbdf

Please sign in to comment.