Commit 0869aecc authored by Roberto Loayza Miljanovich's avatar Roberto Loayza Miljanovich

Merge branch 'dev_marcos' into 'developer'

Dev marcos

See merge request ByteBot/web/bytebot-service!16
parents 142501b7 2ad3fa3d
package com.bytesw.bytebot.controller;
import com.bytesw.bytebot.etl.beans.ActionBean;
import com.bytesw.bytebot.service.ActionService;
import com.bytesw.xdf.annotation.ProgramSecurity;
import com.bytesw.xdf.controller.XDFController;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController()
@RequestMapping("/service/settings/actions")
@ProgramSecurity("ACTIONS")
@Log4j2
public class ActionController extends XDFController<ActionBean, Long> {
public ActionController(ActionService service) {
super(service);
}
}
......@@ -190,4 +190,17 @@ public class AgentController {
}
}
@GetMapping(value = "/")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> getAgentAll() {
HttpStatus hs = HttpStatus.OK;
try {
return new ResponseEntity<>(gsonBuilder.create().toJson(agentService.getAgentAll()), hs);
} catch (Exception e) {
String info = "Error detectado al obtener informacion";
hs = HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<>(gsonBuilder.create().toJson(info), hs);
}
}
}
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.apache.commons.lang.exception.ExceptionUtils;
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;
......@@ -20,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;
......@@ -31,65 +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);
}
@DeleteMapping(value = "/goalForAction/{id}")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> GoalForAction(@ApiParam(value = "id", required = true) @PathVariable("id") Long id) {
String info = "";
try{
goalService.deleteGoalForAction(id);
info = id.toString();
return new ResponseEntity(gson.toJson(info), HttpStatus.OK);
} catch (Exception e) {
info = e.getMessage();
}
return new ResponseEntity(gson.toJson(info), HttpStatus.INTERNAL_SERVER_ERROR);
}
@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);
}
}
}
......@@ -16,5 +16,4 @@ public class GoalForActionController extends XDFController<GoalForActionsBean, L
public GoalForActionController(GoalForActionService service) {
super(service);
}
}
......@@ -14,6 +14,6 @@ public class ActionBean {
private Long id;
@Expose
private String ident;
private String identifier;
}
package com.bytesw.bytebot.etl.dao;
import com.bytesw.bytebot.etl.model.Action;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.util.Optional;
......
......@@ -21,4 +21,5 @@ import java.util.List;
public interface AgentRepository extends CrudRepository<Agent, Long>, JpaSpecificationExecutor<Agent> {
List<Agent> findByStatus(AgentStatusEnum status);
}
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.etl.beans.ActionBean;
import com.bytesw.bytebot.etl.dao.ActionRepository;
import com.bytesw.bytebot.etl.model.Action;
import com.bytesw.xdf.service.XDFService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
@Log4j2
public class ActionService extends XDFService<Action, ActionBean, Long> {
protected ActionService(ActionRepository repository) {
super(repository);
}
@Override
protected Action toModel(Action model, ActionBean bean) {
if (model == null) {
model = new Action();
}
BeanUtils.copyProperties(bean, model);
return model;
}
@Override
protected ActionBean toBean(Action model) {
ActionBean bean = new ActionBean();
BeanUtils.copyProperties(model, bean);
return bean;
}
}
......@@ -6,6 +6,7 @@ import com.bytesw.bytebot.jdbc.AgentJDBCRepository;
import com.bytesw.bytebot.model.enums.*;
import com.bytesw.bytebot.repository.*;
import com.bytesw.xdf.config.AppContextManager;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.sql.beans.Pagination;
import lombok.extern.log4j.Log4j2;
import org.apache.camel.ProducerTemplate;
......@@ -507,4 +508,25 @@ public class AgentService extends CustomPaginationService<Agent> {
}
}
public List<Map> getAgentAll() {
List<Agent> lista = (List<Agent>) agentRepository.findAll();
if (lista.size() <= 0) {
throw new NotFoundException("Error al listar agentes.");
}
List<Map> result = new ArrayList<>();
for(Agent agent: lista) {
Map<String, Object> map = new HashMap<>();
map.put("id", agent.getId());
map.put("name", agent.getName());
map.put("description", agent.getDescription());
map.put("version", agent.getVersion());
map.put("status", agent.getStatus().getName());
map.put("period", agent.getPeriod());
map.put("timezone", agent.getTimezone());
result.add(map);
}
return result;
}
}
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.etl.beans.ActionBean;
import com.bytesw.bytebot.etl.beans.GoalForActionsBean;
import com.bytesw.bytebot.etl.dao.ActionRepository;
import com.bytesw.bytebot.etl.model.Action;
......
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.etl.beans.GoalBean;
import com.bytesw.bytebot.etl.model.Goal;
import com.bytesw.bytebot.etl.model.GoalForActions;
import com.bytesw.bytebot.repository.*;
import com.bytesw.xdf.exception.AlreadyExistsException;
import com.bytesw.xdf.exception.NotFoundException;
......@@ -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,24 +88,24 @@ 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);
}
@Transactional(propagation = Propagation.REQUIRED)
public void deleteGoalForAction(Long goalId) {
@Override
public void delete(Long goalId) {
if (!goalRepository.existsById(goalId)){
throw new NotFoundException("Goal no registrado.");
}
goalForActionService.deleteGoalForAction(goalId);
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.");
......@@ -105,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();
......@@ -112,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);
......@@ -134,18 +160,21 @@ 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;
}
}
\ No newline at end of file
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