Skip to content

Commit

Permalink
conectando cliente e servidor
Browse files Browse the repository at this point in the history
  • Loading branch information
AdsonEsteves committed Oct 14, 2020
1 parent 0434d18 commit 87bcb75
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 20 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<scope>mas</scope>
<organization>SACIP</organization>
</service>

<service>
<name>getInterface</name>
<description>retorna informações do aluno usuário</description>
Expand All @@ -57,6 +58,19 @@
<organization>SACIP</organization>
</service>

<service>
<name>createAccount</name>
<description>cria nova conta de usuário</description>
<entity>InterfaceAgent</entity>
<scope>mas</scope>
<organization>SACIP</organization>
<parameters>
<parameter>
<name>novaConta</name><class>org.json.JSONObject</class>
</parameter>
</parameters>
</service>

<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!-- SACIP Recommender Agent -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/sacip/rest/RestInterface.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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")
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/sacip/sti/agents/InterfaceAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -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);
}
}

}
Expand Down
48 changes: 40 additions & 8 deletions src/main/java/sacip/sti/components/DBConnection.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String>) 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();
}
}
Expand All @@ -81,6 +101,7 @@ private Content instanceContent(Map in)
(List<String>) in.get("tags"),
(String)in.get("link"));
} catch (Exception e) {
e.printStackTrace();
return new Content();
}
}
Expand All @@ -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<Map<String, Object>> query = cypher.writequery("CREATE (u:USER {"
+ "name: $name,"
+ "password: $password,"
+ "avatar: $avatar,"
Expand All @@ -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<String,Object> 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();
}
}

Expand All @@ -132,7 +162,7 @@ private Object getUsers(Map<String, Object> 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;
Expand Down Expand Up @@ -211,6 +241,7 @@ private String createContent(Content content)
}
catch (Exception e)
{
e.printStackTrace();
return "FALHOU criação de conteudo"+e;
}
}
Expand All @@ -228,7 +259,7 @@ private Object getContents(Map<String, Object> 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;
Expand Down Expand Up @@ -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<>());
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/sacip/sti/dataentities/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ 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
1 change: 0 additions & 1 deletion src/main/java/sacip/sti/utils/BoltCypherExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static Object convert(Value value) {
case "NODE":
case "RELATIONSHIP":
return value.asMap();
default:
}
return value.asObject();
}
Expand Down

0 comments on commit 87bcb75

Please sign in to comment.