Skip to content

Commit

Permalink
Arrumando Excecoes e Mappeando JSONs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Oct 14, 2020
1 parent 87bcb75 commit 14f3171
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 117 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ dependencies {
implementation 'br.com.smartechnology.midas:agent-server:1.2.0'

// https://mvnrepository.com/artifact/org.json/json
implementation 'org.json:json:20200518'
// implementation 'org.json:json:20200518'


// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'

// https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver
implementation 'org.neo4j.driver:neo4j-java-driver:4.1.1'
Expand Down
5 changes: 0 additions & 5 deletions services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@
<entity>InterfaceAgent</entity>
<scope>mas</scope>
<organization>SACIP</organization>
<parameters>
<parameter>
<name>novaConta</name><class>org.json.JSONObject</class>
</parameter>
</parameters>
</service>

<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/sacip/rest/RestInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.midas.as.AgentServer;
import org.midas.as.manager.execution.ServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import sacip.sti.dataentities.Student;

@RestController
@RequestMapping("/interface")
public class RestInterface {

private String test = "HELLO WORLD";
private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

public RestInterface() {
super();
}

@GetMapping("/contas")
public String fazLogin() {
Expand All @@ -21,22 +32,17 @@ public String fazLogin() {

@PostMapping("/contas")
@ResponseBody
public String criaNovaConta(@RequestBody Map<String, Object> conta) {
public String criaNovaConta(@RequestBody Student conta) {

try {
//TODO Tentar fazer pegar JSON

System.out.println("REQUEST JSON" + conta.toString());
ServiceWrapper wrapper = AgentServer.require("SACIP", "createAccount");
wrapper.addParameter("novaConta", new JSONObject(conta));
wrapper.addParameter("novaConta", conta);
List run = wrapper.run();
return run.toString();

} catch (Exception e) {
e.printStackTrace();
return "Falhou criar conta: \n"+e;
LOG.error("Falhou criar conta em REST Interface", e);
return "Falhou criar conta: \n"+e.getLocalizedMessage();
}

}

@PutMapping("/contas")
Expand Down
27 changes: 10 additions & 17 deletions src/main/java/sacip/sti/agents/InterfaceAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.midas.as.AgentServer;
import org.midas.as.agent.board.Message;
import org.midas.as.agent.board.MessageListener;
import org.midas.as.agent.templates.Agent;
import org.midas.as.agent.templates.LifeCycleException;
import org.midas.as.agent.templates.ServiceException;
import org.midas.as.manager.execution.ServiceWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InterfaceAgent extends Agent implements MessageListener{

private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

@Override
public void boardChanged(Message msg) {
// TODO Auto-generated method stub
Expand All @@ -24,25 +28,14 @@ public void provide(String service, Map in, List out) throws ServiceException {

if(service.equals("createAccount"))
{
try {
System.out.println("CRIANDO CONTA");
JSONObject conta = (JSONObject) in.get("novaConta");
System.out.println("GERANDO JSON" + conta.toString());
try {
ServiceWrapper wrapper = require("SACIP", "createStudent");
// wrapper.setParameters(in);
wrapper.addParameter("name", conta.get("name"));
wrapper.addParameter("password", conta.get("password"));
wrapper.addParameter("avatar", conta.get("avatar"));
wrapper.addParameter("genero", conta.get("genero"));
wrapper.addParameter("idade", conta.get("idade"));
wrapper.addParameter("nivelEdu", conta.get("nivelEdu"));
wrapper.addParameter("preferencias", conta.get("preferencias"));
System.out.println("GERANDO PARAMETROS");
out = wrapper.run();
wrapper.addParameter("conta", in.get("novaConta"));
List response = wrapper.run();
out.add(response.get(0));

} catch (Exception e) {
e.printStackTrace();
System.out.println("FALHOU INTERFACE AGENT"+e);
LOG.error("Falhou criar conta", e);
out.add("FALHOU INTERFACE AGENT"+e);
}
}
Expand Down
95 changes: 31 additions & 64 deletions src/main/java/sacip/sti/components/DBConnection.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package sacip.sti.components;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.midas.as.AgentServer;
import org.midas.as.agent.templates.Component;
import org.midas.as.agent.templates.ServiceException;
import org.neo4j.driver.Result;

import sacip.sti.dataentities.Content;
import sacip.sti.dataentities.Student;
import sacip.sti.utils.BoltCypherExecutor;
import sacip.sti.utils.CypherExecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("unchecked")
public class DBConnection extends Component {

private final CypherExecutor cypher;
private static Logger LOG = LoggerFactory.getLogger(AgentServer.class);

public DBConnection() {
super();
Expand All @@ -34,7 +35,7 @@ public void provide(String service, Map in, List out) throws ServiceException {
switch (service)
{
case "createStudent":
out.add(createUser(instanceStudent(in)));
out.add(createUser((Student)in.get("conta")));
break;

case "findStudents":
Expand Down Expand Up @@ -70,22 +71,18 @@ public void provide(String service, Map in, List out) throws ServiceException {

private Student instanceStudent(Map in)
{
System.out.println("INSTANCIA ESTUDANTE " + in.get("preferencias"));
ObjectMapper mapper = new ObjectMapper();

ObjectMapper mapper = new ObjectMapper();
try {
return new Student((String)in.get("name"),
(String)in.get("password"),
(String)in.get("avatar"),
(String)in.get("genero"),
(Integer)in.get("idade"),
(String)in.get("nivelEdu"),
mapper.readValue(((JSONArray) in.get("preferencias")).toString(), List.class));
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage() +"\n "+Arrays.asList(e.getStackTrace()));
//TODO REMOVER ISSOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
return new Student();
(List<String>) in.get("preferencias"));
} catch (Exception e) {
LOG.error("Não foi possível instanciar o estudante", e);
return null;
}
}

Expand All @@ -101,8 +98,8 @@ private Content instanceContent(Map in)
(List<String>) in.get("tags"),
(String)in.get("link"));
} catch (Exception e) {
e.printStackTrace();
return new Content();
LOG.error("Não foi possível instanciar o conteúdo", e);
return null;
}
}

Expand All @@ -116,8 +113,7 @@ private Content instanceContent(Map in)
public String createUser(Student student) {
try
{
System.out.println("INICIOU QUERY " + student.toString());
List<Map<String, Object>> query = cypher.writequery("CREATE (u:USER {"
List<Map<String, Object>> result = cypher.writequery("CREATE (u:USER {"
+ "name: $name,"
+ "password: $password,"
+ "avatar: $avatar,"
Expand All @@ -133,19 +129,12 @@ public String createUser(Student student) {
"nivelEdu", student.getNivelEducacional(),
"idade", student.getIdade(),
"preferencias", student.getPreferenciasAsString()));
System.out.println("MANDOU QUERY "+query.toString());
for (Map<String,Object> map : query) {
for (String key: map.keySet()) {
System.out.println("key : " + key + ", value : " + map.get(key));
}
}
return query.toString();
return result.toString();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e.getLocalizedMessage() +"\n "+Arrays.asList(e.getStackTrace()));
return "FALHOU criação de estudante "+e.getStackTrace();
LOG.error("Não foi possível criar o estudante no banco", e);
return "FALHOU criação de estudante "+e.getLocalizedMessage();
}
}

Expand Down Expand Up @@ -178,7 +167,8 @@ private Object getUsers(Map<String, Object> attributes) {
}
catch (Exception e)
{
return "FALHOU busca de estudante"+e;
LOG.error("FALHOU busca de estudante", e);
return "FALHOU busca de estudante "+e.getLocalizedMessage();
}
}

Expand All @@ -194,7 +184,8 @@ private String editUser(String name, String atributeName, String newValue)
}
catch (Exception e)
{
return "FALHOU edicao de estudante"+e;
LOG.error("FALHOU edicao de estudante", e);
return "FALHOU edicao de estudante "+e.getLocalizedMessage();
}
}

Expand All @@ -209,7 +200,8 @@ private String deleteUser(String name)
}
catch (Exception e)
{
return "FALHOU remoção de estudante"+e;
LOG.error("FALHOU remoção de estudante", e);
return "FALHOU remoção de estudante "+e.getLocalizedMessage();
}
}

Expand Down Expand Up @@ -241,8 +233,8 @@ private String createContent(Content content)
}
catch (Exception e)
{
e.printStackTrace();
return "FALHOU criação de conteudo"+e;
LOG.error("FALHOU criação de conteudo", e);
return "FALHOU criação de conteudo "+ e.getLocalizedMessage();
}
}

Expand Down Expand Up @@ -273,7 +265,8 @@ private Object getContents(Map<String, Object> attributes)
}
return content;
} catch (Exception e) {
return "FALHOU busca de conteudo"+e;
LOG.error("FALHOU busca de conteudo", e);
return "FALHOU busca de conteudo "+ e.getLocalizedMessage();
}
}

Expand All @@ -289,7 +282,8 @@ private String editContent(String name, String atributeName, String newValue)
}
catch (Exception e)
{
return "FALHOU edicao de conteudo"+e;
LOG.error("FALHOU edicao de conteudo", e);
return "FALHOU edicao de conteudo "+ e.getLocalizedMessage();
}
}

Expand All @@ -304,7 +298,8 @@ private String deleteContent(String name)
}
catch (Exception e)
{
return "FALHOU remoção de conteudo"+e;
LOG.error("FALHOU remoção de conteudo", e);
return "FALHOU remoção de conteudo " + e.getLocalizedMessage();
}
}

Expand All @@ -324,35 +319,7 @@ private void resetDB()
public static void main(String[] args) {

DBConnection conect = new DBConnection();
// // Student student1 = new Student("Adson", "", "animegirl", "homem", 24, "graduação", new ArrayList<>());
// Student student1 = new Student("Wayne", "ICE", "YGOPRO", "homem", 21, "ensino médio", new ArrayList<>());
// Student student2 = new Student("Saber", "", "bruhh", "homem", 24, "graduação", new ArrayList<>());
// Student student3 = new Student("Aluizio", "", "xx", "homem", 24, "graduação", new ArrayList<>());
// Student student4 = new Student("Rodrigo", "", "aa", "homem", 24, "graduação", new ArrayList<>());
// Student student5 = new Student("Andre", "", "dd", "homem", 24, "graduação", new ArrayList<>());
// Student student6 = new Student("Alisson", "", "ff", "homem", 24, "graduação", new ArrayList<>());
// List<String> preferencias = new ArrayList<>();
// preferencias.add("filmes");
// preferencias.add("musica");
// Student student7 = new Student("Yukino", "3333", "oregairu", "mulher", 24, "mestrado", preferencias);

// conect.createUser(student1);
// conect.createUser(student2);
// conect.createUser(student3);
// conect.createUser(student4);
// conect.createUser(student5);
// conect.createUser(student6);
// conect.createUser(student7);

conect.showNodes();
// List<Student> found = conect.getUsers(Map.of("idade", 24, "nivelEdu", "graduação"));
// if(found!=null)
// System.out.println("encontrou "+found.toString());
// else
// System.out.println("naoencontrou");
// conect.resetDB();

// conect.printPeople("A");
System.exit(0);
}

Expand Down
30 changes: 13 additions & 17 deletions src/main/java/sacip/sti/dataentities/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ public List<String> getPreferencias() {
public String getPreferenciasAsString() {

StringBuilder builder = new StringBuilder();
if(preferencias==null)
{
System.out.println("PREFERENCIAS NULLL");
return "";
}

for (String preferencia : preferencias) {
builder.append(preferencia+",");
}
Expand Down Expand Up @@ -144,18 +140,18 @@ public void setErrosDoEstudante(List<String> errosDoEstudante) {
@Override
public String toString() {
return "{" +
" name='" + name + "'" +
", password='" + password + "'" +
", avatar='" + avatar + "'" +
", genero='" + genero + "'" +
", idade='" + idade + "'" +
", nivelEducacional='" + nivelEducacional + "'" +
", preferencias='" + preferencias + "'" +
", trilha='" + trilha + "'" +
", exerciciosResolvidos='" + exerciciosResolvidos + "'" +
", tempoResolucao='" + tempoResolucao + "'" +
", errosDoEstudante='" + errosDoEstudante + "'" +
" name:'" + name + "'" +
", password:'" + password + "'" +
", avatar:'" + avatar + "'" +
", genero:'" + genero + "'" +
", idade:" + idade + "" +
", nivelEducacional:'" + nivelEducacional + "'" +
", preferencias:" + preferencias + "" +
", trilha:" + trilha + "" +
", exerciciosResolvidos:" + exerciciosResolvidos + "" +
", tempoResolucao:" + tempoResolucao + "" +
", errosDoEstudante:" + errosDoEstudante + "" +
"}";
}

}
}
Loading

0 comments on commit 14f3171

Please sign in to comment.