為了能夠查詢提交的資料,我們需要儲存與提交的表單相關聯的JSON結構描述。 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;
}
建立最適化表單時,您可以使用存放庫中的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();
}
}
}
}
總結一下,我們目前已完成下列工作
後續步驟會是使用QueryBuilder顯示根據JSON結構描述搜尋的欄位