Armazenamento do esquema JSON no banco de dados storing-json-schema-in-database
Para poder consultar os dados enviados, precisaremos armazenar o esquema JSON associado ao formulário enviado. O esquema JSON é usado no construtor de consultas para criar a consulta.
Quando um Formulário adaptável é enviado, verificamos se o esquema JSON associado está no banco de dados. Se o esquema JSON não existir, buscamos o esquema JSON e armazenamos o esquema na tabela apropriada. Também associamos o nome do formulário ao esquema JSON. A captura de tela a seguir mostra a tabela em que os esquemas JSON são armazenados.
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;
}
O esquema buscado é armazenado no banco de dados usando as operações JDBC padrão. O código a seguir insere o esquema no banco de dados
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();
}
}
}
}
Resumindo, fizemos o seguinte até agora
- Criar formulário adaptável com base no esquema JSON
- Se o formulário estiver sendo enviado na primeira vez que armazenarmos o esquema JSON associado ao formulário no banco de dados.
- Armazenamos os dados vinculados do Formulário adaptável no banco de dados.
As próximas etapas seriam usar o QueryBuilder para exibir os campos a serem pesquisados com base no Esquema JSON