データベースへの JSON スキーマの格納 storing-json-schema-in-database
送信されたデータに対してクエリを実行するには、送信されたフォームに関連付けられた JSON スキーマを保存する必要があります。 QueryBuilder では、この JSON スキーマを使用してクエリを作成します。
アダプティブフォームが送信されると、関連する JSON スキーマがデータベース内にあるかどうかを確認します。 JSON スキーマが存在しない場合は、JSON スキーマを取得し、適切なテーブルにスキーマを保存します。 また、フォーム名を JSON スキーマに関連付けます。 次のスクリーンショットは、JSON スキーマが格納されているテーブルを示しています。
public String getJSONSchema(String afPath) {
// TODO Auto-generated method stub
afPath = afPath.replaceAll("/content/dam/formsanddocuments/", "/content/forms/af/");
Resource afResource = getResolver.getServiceResolver().getResource(afPath + "/jcr:content/guideContainer");
javax.jcr.Node resNode = afResource.adaptTo(Node.class);
String schemaNode = null;
try {
schemaNode = resNode.getProperty("schemaRef").getString();
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (schemaNode.startsWith("/content/dam")) {
log.debug("The schema is in the dam");
afResource = getResolver.getServiceResolver()
.getResource(schemaNode + "/jcr:content/renditions/original/jcr:content");
resNode = afResource.adaptTo(Node.class);
InputStream jsonSchemaStream = null;
try {
jsonSchemaStream = resNode.getProperty("jcr:data").getBinary().getStream();
Charset charset = StandardCharsets.UTF_8;
String jasonSchemaString = IOUtils.toString(jsonSchemaStream, charset);
log.debug("The Schema is " + jasonSchemaString);
return jasonSchemaString;
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (schemaNode.startsWith("/assets")) {
afResource = getResolver.getServiceResolver()
.getResource(afPath + "/jcr:content/guideContainer/" + schemaNode + "/jcr:content");
resNode = afResource.adaptTo(Node.class);
InputStream jsonSchemaStream = null;
try {
jsonSchemaStream = resNode.getProperty("jcr:data").getBinary().getStream();
Charset charset = StandardCharsets.UTF_8;
String jasonSchemaString = IOUtils.toString(jsonSchemaStream, charset);
log.debug("The Schema is " + jasonSchemaString);
return jasonSchemaString;
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
NOTE
アダプティブフォームの作成時に、リポジトリ内の JSON スキーマを使用するか、JSON スキーマをアップロードできます。 上記のコードは、両方のケースで機能します。
取得したスキーマは、標準の JDBC 操作を使用してデータベースに保存されます。 次のコードは、データベースにスキーマを挿入します
public void insertJsonSchema(JSONObject jsonSchema, String afForm) {
log.debug("$$$$ in insert Schema" + afForm);
log.debug("$$$$$ The jsonSchema is " + jsonSchema);
Connection con = getConnection();
log.debug("$$$$ got connection is insertJsonSchema");
String insertTableSQL = "INSERT INTO leads.jsonschemas(jsonschema,formname) VALUES(?,?)";
PreparedStatement pstmt = null;
try {
// org.json.JSONObject jsonSchemaObj = new
// org.json.JSONObject(jsonSchema);
pstmt = con.prepareStatement(insertTableSQL);
pstmt.setString(1, jsonSchema.toString());
pstmt.setString(2, afForm);
log.debug("Executing the insert json schema statment " + pstmt.executeUpdate());
con.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
要約すると、これまでに次のことを行いました。
- JSON スキーマに基づいてアダプティブフォームを作成する
- フォームが初めて送信されている場合は、フォームに関連付けられた JSON スキーマをデータベースに保存する
- アダプティブフォームの連結データをデータベースに保存する
次の手順では、 QueryBuilder を使用して、JSON スキーマに基づいて検索するフィールドを表示します
recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e