Commit 0683f8b6 authored by Roberto Loayza's avatar Roberto Loayza

Ajustes en Controlador y Servicio de Goal

parent 22163f10
package com.bytesw.bytebot.controller;
import com.bytesw.bytebot.etl.beans.GoalBean;
import com.bytesw.bytebot.service.GoalService;
import com.bytesw.xdf.annotation.ProgramSecurity;
import com.bytesw.xdf.controller.XDFController;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
......@@ -19,7 +14,7 @@ import java.util.Map;
@RequestMapping("/service/settings/goal")
@ProgramSecurity("GOAL")
@Log4j2
public class GoalController extends XDFController<GoalBean, Long> {
public class GoalController extends XDFController<Map, Long> {
@Autowired
private GoalService goalService;
......@@ -30,54 +25,4 @@ public class GoalController extends XDFController<GoalBean, Long> {
super(service);
}
@PostMapping(value = "/goalForAction")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> createGoalForAction(@RequestBody Map<String, Object> body) {
GoalBean info = goalService.saveGoalForAction(body);
return new ResponseEntity(gson.toJson(info), HttpStatus.OK);
}
@PutMapping(value = "/goalForAction/{id}")
@PreAuthorize("hasPermission(this, 'edit')")
public ResponseEntity<String> updateConversationalAgent(@PathVariable("id") Long id,
@RequestBody Map<String, Object> body) {
String info = "";
try{
goalService.updateGoalForActions(body, id);
info = "Ok";
return new ResponseEntity(gson.toJson(info), HttpStatus.OK);
} catch (Exception e) {
info = e.getMessage();
}
return new ResponseEntity(gson.toJson(info), HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping(value = "/goalForAction/{id}")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> getActionByGoalId(@ApiParam(value = "id", required = true) @PathVariable("id") Long id) {
HttpStatus hs = HttpStatus.OK;
try {
return new ResponseEntity<>(gsonBuilder.create().toJson(goalService.getGoalForActions(id)), hs);
} catch (Exception e) {
String info = "Error detectado al obtener informacion";
hs = HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<>(gsonBuilder.create().toJson(info), hs);
}
}
@GetMapping(value = "/goalForAction/")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> getActionByGoalIdAll() {
HttpStatus hs = HttpStatus.OK;
try {
return new ResponseEntity<>(gsonBuilder.create().toJson(goalService.getGoalForActionsAll()), hs);
} catch (Exception e) {
String info = "Error detectado al obtener informacion";
hs = HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<>(gsonBuilder.create().toJson(info), hs);
}
}
}
......@@ -19,7 +19,7 @@ import java.util.*;
@Service
@Transactional
@Log4j2
public class GoalService extends XDFService<Goal, GoalBean, Long> {
public class GoalService extends XDFService<Goal, Map, Long> {
@Autowired
private GoalRepository goalRepository;
......@@ -38,23 +38,28 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
}
@Override
protected Goal toModel(Goal model, GoalBean bean) {
protected Goal toModel(Goal model, Map map) {
if (model == null) {
model = new Goal();
}
BeanUtils.copyProperties(bean, model);
model.setId((Long) map.get("id"));
model.setIdentifier(String.valueOf(map.get("ident")));
model.setAgenId((Long) map.get("agentId"));
return model;
}
@Override
protected GoalBean toBean(Goal model) {
GoalBean bean = new GoalBean();
BeanUtils.copyProperties(model, bean);
return bean;
protected Map toBean(Goal model) {
Map<String, Object> map = new HashMap<>();
map.put("id",model.getId());
map.put("ident", model.getIdentifier());
map.put("agentId", model.getAgenId());
return map;
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public GoalBean saveGoalForAction(Map<String, Object> body) {
public Map create(Map body) {
Goal goal = new Goal();
......@@ -62,6 +67,11 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
throw new NotFoundException("No se encuentran parametros.");
}
List<Object> actions = (List) body.get("actionList");
if (actions.isEmpty()) {
throw new NotFoundException("Debe poseer minimo un action.");
}
Long agenId = Long.valueOf(body.get("AgentId").toString());
if (!agentRepository.existsById(agenId)) {
......@@ -78,7 +88,7 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
goal.setAgenId(agenId);
goal.setIdentifier(ident);
goal = goalRepository.save(goal);
goalForActionService.saveGoalForAction(goal.getId(), (List) body.get("actionList"));
goalForActionService.saveGoalForAction(goal.getId(), actions);
return toBean(goal);
}
......@@ -93,8 +103,9 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
goalRepository.deleteById(goalId);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void updateGoalForActions(Map<String, Object> body, Long goalId) {
public Map update(Map body, Long goalId) {
if (body.isEmpty()) {
throw new NotFoundException("No se encuentran parametros.");
......@@ -104,6 +115,11 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
throw new NotFoundException("Goal no se encuenta registrado");
}
List<Object> actions = (List) body.get("actionList");
if (actions.isEmpty()) {
throw new NotFoundException("Debe poseer minimo un action.");
}
Long agenId = Long.valueOf(body.get("AgentId").toString());
String ident = body.get("Ident").toString();
......@@ -111,16 +127,27 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
throw new NotFoundException("Agente no registrado.");
}
Optional<Goal> goalOptional= goalRepository.findByAgentAndIdent(agenId, ident);
if (goalOptional.isPresent()) {
if (goalOptional.get().getId() != goalId) {
throw new AlreadyExistsException("Goal ya existe en la base de datos para el agente ingresado.");
}
}
Goal goal = new Goal();
goal.setId(goalId);
goal.setAgenId(agenId);
goal.setIdentifier(ident);
goal = goalRepository.save(goal);
goalForActionService.updateGoalForAction(goal.getId(), (List) body.get("actionList"));
goalForActionService.updateGoalForAction(goal.getId(), actions);
return toBean(goal);
}
public Map<String, Object> getGoalForActions(Long id){
@Override
public Map getById(Long id){
Map<String, Object> result = new HashMap<>();
Optional<Goal> goal = goalRepository.findById(id);
......@@ -133,17 +160,19 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
throw new NotFoundException("Actions no encontrados");
}
result.put("id", goal.get().getId());
result.put("AgentId", goal.get().getAgenId());
result.put("Ident", goal.get().getIdentifier());
result.put("actionList", listAction);
return result;
}
public List<Map> getGoalForActionsAll(){
@Override
public List<Map> getAll(){
List<Map> result = new ArrayList<>();
List<Goal> list = (List<Goal>) goalRepository.findAll();
for (Goal goal: list) {
result.add(getGoalForActions(goal.getId()));
result.add(getById(goal.getId()));
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment