Skip to content

Commit

Permalink
Refactoring. 一旦ここで足を止めてテスト。 (#245)
Browse files Browse the repository at this point in the history
* refactoring.
* 一旦ここで足を止めてテスト。
  • Loading branch information
igapyon authored Apr 17, 2021
1 parent 6382331 commit 1795444
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 198 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>jp.oiyokan</groupId>
<artifactId>oiyokan</artifactId>
<version>1.4.20210417</version>
<version>1.4.20210418</version>
<name>oiyokan</name>
<description>Oiyokan is a simple OData v4 Server. (based on Apache
Olingo / Spring Boot / h2 database)</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jp/oiyokan/OiyokanConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class OiyokanConstants {
/**
* Oiyokan のバージョン番号
*/
public static final String VERSION = "1.4.20210417c";
public static final String VERSION = "1.4.20210418a";

/**
* OData のルートパス.
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/jp/oiyokan/OiyokanCsdlEntityContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ public void ensureBuild() throws ODataApplicationException {
// Oiyokan が動作する際に必要になる内部データベースのバージョン情報および Oiyo info をセットアップ.
OiyokanKanDatabase.setupKanDatabase(oiyoInfo);

for (OiyoSettingsEntitySet entitySetCnof : getOiyoInfoInstance().getSettings().getEntitySet()) {
// System.err.println("TRACE: entitySet: " + entitySetCnof.getEntitySetName() +
// ", dbname: "
// + entitySetCnof.getDatabaseName());

// TODO FIXME ここが設定としての EntitySet をロードして設定しているところ。ここを見直したい。
for (OiyoSettingsEntitySet entitySet : getOiyoInfoInstance().getSettings().getEntitySet()) {
// EntitySet の初期セットを実施。
getEntitySets().add(new OiyokanCsdlEntitySet(oiyoInfo, this, entitySetCnof));
getEntitySets().add(new OiyokanCsdlEntitySet(oiyoInfo, this, entitySet));
}
}
}
Expand Down
31 changes: 12 additions & 19 deletions src/main/java/jp/oiyokan/OiyokanEntityProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import jp.oiyokan.basic.OiyoBasicJdbcUtil;
import jp.oiyokan.common.OiyoInfo;
import jp.oiyokan.common.OiyoInfoUtil;
import jp.oiyokan.dto.OiyoSettingsDatabase;
import jp.oiyokan.dto.OiyoSettingsEntitySet;

/**
Expand Down Expand Up @@ -84,24 +85,19 @@ public void readEntity(ODataRequest request, ODataResponse response, UriInfo uri
List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();

// データベースに接続.
final OiyokanCsdlEntitySet entitySet = OiyoBasicJdbcEntityOneBuilder.findEntitySet(edmEntitySet);
if (entitySet == null) {
// [M211] No such EntitySet found (createEntity)
System.err.println(OiyokanMessages.M211);
throw new ODataApplicationException(OiyokanMessages.M211, 500, Locale.ENGLISH);
}

final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo,
final OiyoSettingsDatabase database = OiyoInfoUtil.getOiyoDatabaseByEntitySetName(oiyoInfo,
edmEntitySet.getName());
if (oiyoEntitySet.getCanRead() != null && oiyoEntitySet.getCanRead() == false) {

final OiyoSettingsEntitySet entitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, edmEntitySet.getName());
if (entitySet.getCanRead() != null && entitySet.getCanRead() == false) {
// [M052] WARN: No Read access by canRead==false.
System.err.println(OiyokanMessages.M052 + ": Entity:" + edmEntitySet.getName());
throw new ODataApplicationException(OiyokanMessages.M052 + ": Entity:" + edmEntitySet.getName(), //
OiyokanMessages.M052_CODE, Locale.ENGLISH);
}

Entity entity = null;
try (Connection connTargetDb = OiyoBasicJdbcUtil.getConnection(entitySet.getSettingsDatabase(oiyoInfo))) {
try (Connection connTargetDb = OiyoBasicJdbcUtil.getConnection(database)) {
entity = new OiyoBasicJdbcEntityOneBuilder(oiyoInfo).readEntityData(connTargetDb, uriInfo, edmEntitySet,
keyPredicates);
} catch (SQLException ex) {
Expand Down Expand Up @@ -153,9 +149,8 @@ public void createEntity(ODataRequest request, ODataResponse response, UriInfo u
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();
EdmEntityType edmEntityType = edmEntitySet.getEntityType();

final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo,
edmEntitySet.getName());
if (oiyoEntitySet.getCanCreate() != null && oiyoEntitySet.getCanCreate() == false) {
final OiyoSettingsEntitySet entitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, edmEntitySet.getName());
if (entitySet.getCanCreate() != null && entitySet.getCanCreate() == false) {
// [M051] WARN: No Create access by canCreate==false.
System.err.println(OiyokanMessages.M051 + ": Entity:" + edmEntitySet.getName());
throw new ODataApplicationException(OiyokanMessages.M051 + ": Entity:" + edmEntitySet.getName(), //
Expand Down Expand Up @@ -208,9 +203,8 @@ public void updateEntity(ODataRequest request, ODataResponse response, UriInfo u
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo,
edmEntitySet.getName());
if (oiyoEntitySet.getCanUpdate() != null && oiyoEntitySet.getCanUpdate() == false) {
final OiyoSettingsEntitySet entitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, edmEntitySet.getName());
if (entitySet.getCanUpdate() != null && entitySet.getCanUpdate() == false) {
// [M053] WARN: No Update access by canUpdate==false.
System.err.println(OiyokanMessages.M053 + ": Entity:" + edmEntitySet.getName());
throw new ODataApplicationException(OiyokanMessages.M053 + ": Entity:" + edmEntitySet.getName(), //
Expand Down Expand Up @@ -275,9 +269,8 @@ public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo u
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo,
edmEntitySet.getName());
if (oiyoEntitySet.getCanDelete() != null && oiyoEntitySet.getCanDelete() == false) {
final OiyoSettingsEntitySet entitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, edmEntitySet.getName());
if (entitySet.getCanDelete() != null && entitySet.getCanDelete() == false) {
// [M054] WARN: No Delete access by canDelete==false.
System.err.println(OiyokanMessages.M054 + ": Entity:" + edmEntitySet.getName());
throw new ODataApplicationException(OiyokanMessages.M054 + ": Entity:" + edmEntitySet.getName(), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet;
import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef;
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.uri.UriInfo;
Expand All @@ -41,6 +40,7 @@
import jp.oiyokan.basic.sql.OiyoSqlQueryListBuilder;
import jp.oiyokan.common.OiyoInfo;
import jp.oiyokan.common.OiyoInfoUtil;
import jp.oiyokan.dto.OiyoSettingsDatabase;
import jp.oiyokan.dto.OiyoSettingsEntitySet;
import jp.oiyokan.h2.data.OiyoExperimentalH2FullTextSearch;

Expand Down Expand Up @@ -76,15 +76,15 @@ public EntityCollection build(EdmEntitySet edmEntitySet, UriInfo uriInfo) throws
return entityCollection;
}

OiyokanCsdlEntitySet entitySet = null;
OiyokanCsdlEntitySet csdlEntitySet = null;
for (CsdlEntitySet look : provider.getEntityContainer().getEntitySets()) {
if (edmEntitySet.getName().equals(look.getName())) {
entitySet = (OiyokanCsdlEntitySet) look;
csdlEntitySet = (OiyokanCsdlEntitySet) look;
break;
}
}

if (entitySet == null) {
if (csdlEntitySet == null) {
// 処理対象外の要素セットです. 処理せずに戻します.
return entityCollection;
}
Expand Down Expand Up @@ -121,15 +121,16 @@ public EntityCollection build(EdmEntitySet edmEntitySet, UriInfo uriInfo) throws
}

// データベースに接続.
try (Connection connTargetDb = OiyoBasicJdbcUtil.getConnection(entitySet.getSettingsDatabase(oiyoInfo))) {
final OiyoSettingsDatabase database = OiyoInfoUtil.getOiyoDatabaseByEntitySetName(oiyoInfo,
edmEntitySet.getName());
try (Connection connTargetDb = OiyoBasicJdbcUtil.getConnection(database)) {
if (uriInfo.getSearchOption() != null) {
// $search.
new OiyoExperimentalH2FullTextSearch().process(connTargetDb, edmEntitySet, uriInfo, entityCollection);
return entityCollection;
}

final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo,
edmEntitySet.getName());
final OiyoSettingsEntitySet oiyoEntitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, edmEntitySet.getName());

if (uriInfo.getCountOption() != null && uriInfo.getCountOption().getValue()) {
// 件数カウントがONの場合は基本的にカウント処理を実行。
Expand All @@ -139,12 +140,12 @@ public EntityCollection build(EdmEntitySet edmEntitySet, UriInfo uriInfo) throws
System.err.println(OiyokanMessages.M042);
} else {
// $count.
processCountQuery(entitySet, uriInfo, connTargetDb, entityCollection);
processCountQuery(csdlEntitySet, uriInfo, connTargetDb, entityCollection);
}
}

// 実際のデータ取得処理を実行。
processCollectionQuery(entitySet, uriInfo, connTargetDb, entityCollection);
processCollectionQuery(csdlEntitySet, uriInfo, connTargetDb, entityCollection);

return entityCollection;
} catch (
Expand Down Expand Up @@ -208,16 +209,17 @@ private void processCountQuery(OiyokanCsdlEntitySet entitySet, UriInfo uriInfo,
/**
* クエリを実行してエンティティの一覧を取得。直接は利用しないでください。
*
* @param entitySet instance of OiyokanCsdlEntitySet.
* @param csdlEntitySet instance of OiyokanCsdlEntitySet.
* @param uriInfo instance of
* org.apache.olingo.server.core.uri.UriInfoImpl.
* @param connTargetDb Connection of db.
* @param entityCollection result of search.
* @throws ODataApplicationException OData App Exception occured.
*/
public void processCollectionQuery(OiyokanCsdlEntitySet entitySet, UriInfo uriInfo, Connection connTargetDb,
public void processCollectionQuery(OiyokanCsdlEntitySet csdlEntitySet, UriInfo uriInfo, Connection connTargetDb,
EntityCollection entityCollection) throws ODataApplicationException {
OiyoSqlQueryListBuilder basicSqlBuilder = new OiyoSqlQueryListBuilder(oiyoInfo, entitySet.getName(), entitySet);
OiyoSqlQueryListBuilder basicSqlBuilder = new OiyoSqlQueryListBuilder(oiyoInfo, csdlEntitySet.getName(),
csdlEntitySet);

// UriInfo 情報を元に SQL文を組み立て.
basicSqlBuilder.buildSelectQuery(uriInfo);
Expand All @@ -226,6 +228,8 @@ public void processCollectionQuery(OiyokanCsdlEntitySet entitySet, UriInfo uriIn
if (OiyokanConstants.IS_TRACE_ODATA_V4)
System.err.println("OData v4: TRACE: SQL: " + sql);

final OiyoSettingsEntitySet entitySet = OiyoInfoUtil.getOiyoEntitySet(oiyoInfo, csdlEntitySet.getName());

final long startMillisec = System.currentTimeMillis();
try (var stmt = connTargetDb.prepareStatement(sql)) {
// set query timeout
Expand Down Expand Up @@ -254,32 +258,31 @@ public void processCollectionQuery(OiyokanCsdlEntitySet entitySet, UriInfo uriIn
ent.addProperty(prop);
}

if (entitySet.getEntityType().getKey().size() == 0) {
if (entitySet.getEntityType().getKeyName().size() == 0) {
// キーが存在しないのは OData としてはまずい。
// 別の箇所にて標準エラー出力にて報告。
} else {
// キーが存在する場合は、キーの値を元にIDとして設定。
OiyokanCsdlEntitySet iyoEntitySet = (OiyokanCsdlEntitySet) entitySet;
if (iyoEntitySet.getEntityType().getKey().size() == 1) {
if (entitySet.getEntityType().getKeyName().size() == 1) {
// 単一項目によるキー
final Property prop = ent.getProperty(iyoEntitySet.getEntityType().getKey().get(0).getName());
final Property prop = ent.getProperty(entitySet.getEntityType().getKeyName().get(0));
String idVal = prop.getValue().toString();
if ("Edm.String".equals(prop.getType())) {
// TODO FIXME Property の値を文字列に変換する共通関数を期待したい.
idVal = "'" + OiyoBasicUrlUtil.encodeUrl4Key(idVal) + "'";
}
ent.setId(createId(entitySet.getName(), idVal));
ent.setId(createId(csdlEntitySet.getName(), idVal));
} else {
// 複数項目によるキー
String keyString = "";
boolean isFirst = true;
for (CsdlPropertyRef propRef : iyoEntitySet.getEntityType().getKey()) {
for (String keyName : entitySet.getEntityType().getKeyName()) {
if (isFirst) {
isFirst = false;
} else {
keyString += ",";
}
final Property prop = ent.getProperty(propRef.getName());
final Property prop = ent.getProperty(keyName);
keyString += prop.getName() + "=";
String idVal = prop.getValue().toString();
if ("Edm.String".equals(prop.getType())) {
Expand All @@ -288,7 +291,7 @@ public void processCollectionQuery(OiyokanCsdlEntitySet entitySet, UriInfo uriIn
}
keyString += idVal;
}
ent.setId(createId(entitySet.getName(), keyString));
ent.setId(createId(csdlEntitySet.getName(), keyString));
}
}

Expand Down
Loading

0 comments on commit 1795444

Please sign in to comment.