From 87bcb7519993a486c250c636b2bdaaeb5599bd9e Mon Sep 17 00:00:00 2001 From: Adsu Date: Tue, 13 Oct 2020 23:25:55 -0300 Subject: [PATCH] conectando cliente e servidor --- build.gradle | 4 ++ services.xml | 14 ++++++ src/main/java/sacip/rest/RestInterface.java | 36 ++++++++++---- .../java/sacip/sti/agents/InterfaceAgent.java | 26 +++++++++- .../sacip/sti/components/DBConnection.java | 48 +++++++++++++++---- .../java/sacip/sti/dataentities/Student.java | 6 ++- .../sacip/sti/utils/BoltCypherExecutor.java | 1 - 7 files changed, 115 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 8fcaced..3bd6612 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,10 @@ dependencies { implementation 'com.google.guava:guava:29.0-jre' implementation 'br.com.smartechnology.midas:agent-server:1.2.0' + + // https://mvnrepository.com/artifact/org.json/json + implementation 'org.json:json:20200518' + // https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver diff --git a/services.xml b/services.xml index b8f59b0..ff2d004 100644 --- a/services.xml +++ b/services.xml @@ -49,6 +49,7 @@ mas SACIP + getInterface retorna informações do aluno usuário @@ -57,6 +58,19 @@ SACIP + + createAccount + cria nova conta de usuário + 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 06e94b5..d2c0401 100644 --- a/src/main/java/sacip/rest/RestInterface.java +++ b/src/main/java/sacip/rest/RestInterface.java @@ -1,22 +1,42 @@ package sacip.rest; +import java.util.List; +import java.util.Map; + +import org.json.JSONObject; +import org.midas.as.AgentServer; +import org.midas.as.manager.execution.ServiceWrapper; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/interface") public class RestInterface { - - private String test = "HELLO WORLD"; + + private String test = "HELLO WORLD"; @GetMapping("/contas") - public String fazLogin() - { - return ""; + public String fazLogin() { + return ""; } - @PostMapping("/contas") - public String criaNovaConta(String name) { - return test; + @PostMapping("/contas") + @ResponseBody + public String criaNovaConta(@RequestBody Map 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)); + List run = wrapper.run(); + return run.toString(); + + } catch (Exception e) { + e.printStackTrace(); + return "Falhou criar conta: \n"+e; + } + } @PutMapping("/contas") diff --git a/src/main/java/sacip/sti/agents/InterfaceAgent.java b/src/main/java/sacip/sti/agents/InterfaceAgent.java index b122230..b17c042 100644 --- a/src/main/java/sacip/sti/agents/InterfaceAgent.java +++ b/src/main/java/sacip/sti/agents/InterfaceAgent.java @@ -3,11 +3,13 @@ import java.util.List; import java.util.Map; +import org.json.JSONObject; 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; public class InterfaceAgent extends Agent implements MessageListener{ @@ -20,9 +22,29 @@ public void boardChanged(Message msg) { @Override public void provide(String service, Map in, List out) throws ServiceException { - if(service.equals("getAluno")) + if(service.equals("createAccount")) { - out.add("Adson"); + try { + System.out.println("CRIANDO CONTA"); + JSONObject conta = (JSONObject) in.get("novaConta"); + System.out.println("GERANDO JSON" + conta.toString()); + 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(); + + } catch (Exception e) { + e.printStackTrace(); + System.out.println("FALHOU INTERFACE AGENT"+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 6d93505..1a98311 100644 --- a/src/main/java/sacip/sti/components/DBConnection.java +++ b/src/main/java/sacip/sti/components/DBConnection.java @@ -1,11 +1,17 @@ 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.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; @@ -29,42 +35,56 @@ public void provide(String service, Map in, List out) throws ServiceException { { case "createStudent": out.add(createUser(instanceStudent(in))); + break; case "findStudents": out.add(getUsers(in)); + break; case "editStudent": out.add(editUser((String)in.get("name"), (String)in.get("attrName"), (String)in.get("newValue"))); + break; case "deleteStudent": out.add(deleteUser((String) in.get("name"))); + break; case "createContent": out.add(createContent(instanceContent(in))); + break; case "findContent": out.add(getContents(in)); + break; case "editContent": out.add(editContent((String)in.get("name"), (String)in.get("attrName"), (String)in.get("newValue"))); + break; case "deleteContent": out.add(deleteContent((String) in.get("name"))); + break; } } private Student instanceStudent(Map in) { + System.out.println("INSTANCIA ESTUDANTE " + in.get("preferencias")); + ObjectMapper mapper = new ObjectMapper(); + try { return new Student((String)in.get("name"), (String)in.get("password"), (String)in.get("avatar"), (String)in.get("genero"), - (int)in.get("idade"), + (Integer)in.get("idade"), (String)in.get("nivelEdu"), - (List) in.get("preferencias")); + 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(); } } @@ -81,6 +101,7 @@ private Content instanceContent(Map in) (List) in.get("tags"), (String)in.get("link")); } catch (Exception e) { + e.printStackTrace(); return new Content(); } } @@ -95,7 +116,8 @@ private Content instanceContent(Map in) public String createUser(Student student) { try { - var result = cypher.writequery("CREATE (u:USER {" + System.out.println("INICIOU QUERY " + student.toString()); + List> query = cypher.writequery("CREATE (u:USER {" + "name: $name," + "password: $password," + "avatar: $avatar," @@ -111,11 +133,19 @@ public String createUser(Student student) { "nivelEdu", student.getNivelEducacional(), "idade", student.getIdade(), "preferencias", student.getPreferenciasAsString())); - return result.toString(); + 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(); } catch (Exception e) { - return "FALHOU criação de estudante"+e; + e.printStackTrace(); + System.out.println(e.getLocalizedMessage() +"\n "+Arrays.asList(e.getStackTrace())); + return "FALHOU criação de estudante "+e.getStackTrace(); } } @@ -132,7 +162,7 @@ private Object getUsers(Map attributes) { query.append("}) RETURN n"); //realisa a busca - var result = cypher.writequery(query.toString(), attributes); + var result = cypher.readquery(query.toString(), attributes); if(result.isEmpty()) { return null; @@ -211,6 +241,7 @@ private String createContent(Content content) } catch (Exception e) { + e.printStackTrace(); return "FALHOU criação de conteudo"+e; } } @@ -228,7 +259,7 @@ private Object getContents(Map attributes) query.append("}) RETURN n"); //realisa a busca - var result = cypher.writequery(query.toString(), attributes); + var result = cypher.readquery(query.toString(), attributes); if(result.isEmpty()) { return null; @@ -293,7 +324,8 @@ 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("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<>()); diff --git a/src/main/java/sacip/sti/dataentities/Student.java b/src/main/java/sacip/sti/dataentities/Student.java index 13a1ac0..3e11cfd 100644 --- a/src/main/java/sacip/sti/dataentities/Student.java +++ b/src/main/java/sacip/sti/dataentities/Student.java @@ -90,7 +90,11 @@ 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+","); } diff --git a/src/main/java/sacip/sti/utils/BoltCypherExecutor.java b/src/main/java/sacip/sti/utils/BoltCypherExecutor.java index f761d1e..54428ac 100644 --- a/src/main/java/sacip/sti/utils/BoltCypherExecutor.java +++ b/src/main/java/sacip/sti/utils/BoltCypherExecutor.java @@ -55,7 +55,6 @@ static Object convert(Value value) { case "NODE": case "RELATIONSHIP": return value.asMap(); - default: } return value.asObject(); }