diff --git a/build.gradle b/build.gradle index 3bd6612..1d53e0b 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/services.xml b/services.xml index ff2d004..40e7420 100644 --- a/services.xml +++ b/services.xml @@ -64,11 +64,6 @@ InterfaceAgent mas SACIP - - - novaContaorg.json.JSONObject - - diff --git a/src/main/java/sacip/rest/RestInterface.java b/src/main/java/sacip/rest/RestInterface.java index d2c0401..fc846c8 100644 --- a/src/main/java/sacip/rest/RestInterface.java +++ b/src/main/java/sacip/rest/RestInterface.java @@ -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() { @@ -21,22 +32,17 @@ public String fazLogin() { @PostMapping("/contas") @ResponseBody - public String criaNovaConta(@RequestBody Map 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") diff --git a/src/main/java/sacip/sti/agents/InterfaceAgent.java b/src/main/java/sacip/sti/agents/InterfaceAgent.java index b17c042..f969222 100644 --- a/src/main/java/sacip/sti/agents/InterfaceAgent.java +++ b/src/main/java/sacip/sti/agents/InterfaceAgent.java @@ -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 @@ -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); } } diff --git a/src/main/java/sacip/sti/components/DBConnection.java b/src/main/java/sacip/sti/components/DBConnection.java index 1a98311..c643bea 100644 --- a/src/main/java/sacip/sti/components/DBConnection.java +++ b/src/main/java/sacip/sti/components/DBConnection.java @@ -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(); @@ -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": @@ -70,9 +71,7 @@ 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"), @@ -80,12 +79,10 @@ private Student instanceStudent(Map in) (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) in.get("preferencias")); + } catch (Exception e) { + LOG.error("Não foi possível instanciar o estudante", e); + return null; } } @@ -101,8 +98,8 @@ private Content instanceContent(Map in) (List) 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; } } @@ -116,8 +113,7 @@ private Content instanceContent(Map in) public String createUser(Student student) { try { - System.out.println("INICIOU QUERY " + student.toString()); - List> query = cypher.writequery("CREATE (u:USER {" + List> result = cypher.writequery("CREATE (u:USER {" + "name: $name," + "password: $password," + "avatar: $avatar," @@ -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 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(); } } @@ -178,7 +167,8 @@ private Object getUsers(Map attributes) { } catch (Exception e) { - return "FALHOU busca de estudante"+e; + LOG.error("FALHOU busca de estudante", e); + return "FALHOU busca de estudante "+e.getLocalizedMessage(); } } @@ -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(); } } @@ -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(); } } @@ -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(); } } @@ -273,7 +265,8 @@ private Object getContents(Map 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(); } } @@ -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(); } } @@ -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(); } } @@ -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 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 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); } diff --git a/src/main/java/sacip/sti/dataentities/Student.java b/src/main/java/sacip/sti/dataentities/Student.java index 3e11cfd..d7edaa3 100644 --- a/src/main/java/sacip/sti/dataentities/Student.java +++ b/src/main/java/sacip/sti/dataentities/Student.java @@ -90,11 +90,7 @@ public List getPreferencias() { public String getPreferenciasAsString() { StringBuilder builder = new StringBuilder(); - if(preferencias==null) - { - System.out.println("PREFERENCIAS NULLL"); - return ""; - } + for (String preferencia : preferencias) { builder.append(preferencia+","); } @@ -144,18 +140,18 @@ public void setErrosDoEstudante(List 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 + "" + "}"; } -} +} \ No newline at end of file diff --git a/src/main/java/sacip/sti/utils/BoltCypherExecutor.java b/src/main/java/sacip/sti/utils/BoltCypherExecutor.java index 54428ac..f16a92a 100644 --- a/src/main/java/sacip/sti/utils/BoltCypherExecutor.java +++ b/src/main/java/sacip/sti/utils/BoltCypherExecutor.java @@ -33,9 +33,7 @@ public List> readquery(String query, Map par @Override public List> writequery(String query, Map params) { - System.out.println("ESCREVENDO"); try (Session session = getSession()) { - System.out.println("SESSAO"); return session.writeTransaction( tx -> tx.run(query, params).list( r -> r.asMap(BoltCypherExecutor::convert)) );