Commit 1aaa0bc6 authored by Roberto Loayza's avatar Roberto Loayza

Mantenimiento GoalForAction:

 - Get
 - GetAll
parent 09ba7810
package com.bytesw.bytebot.controller;
import com.bytesw.bytebot.bean.AgentBean;
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.bytesw.xdf.sql.beans.Pagination;
import com.google.gson.GsonBuilder;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.exception.ExceptionUtils;
......@@ -21,10 +20,13 @@ import java.util.Map;
@RequestMapping("/service/settings/goal")
@ProgramSecurity("GOAL")
@Log4j2
public class GoalController extends XDFController<GoalBean, String> {
public class GoalController extends XDFController<GoalBean, Long> {
@Autowired
private GoalService goalService;
@Autowired
private GsonBuilder gsonBuilder;
public GoalController(GoalService service) {
super(service);
}
......@@ -32,7 +34,7 @@ public class GoalController extends XDFController<GoalBean, String> {
@PostMapping(value = "/goalForAction")
@PreAuthorize("hasPermission(this, 'view')")
public ResponseEntity<String> createGoalForAction(@RequestBody Map<String, Object> body) {
GoalBean info = goalService.save(body);
GoalBean info = goalService.saveGoalForAction(body);
return new ResponseEntity(gson.toJson(info), HttpStatus.OK);
}
......@@ -41,7 +43,7 @@ public class GoalController extends XDFController<GoalBean, String> {
public ResponseEntity<String> GoalForAction(@ApiParam(value = "id", required = true) @PathVariable("id") Long id) {
String info = "";
try{
goalService.delete(id);
goalService.deleteGoalForAction(id);
info = id.toString();
return new ResponseEntity(gson.toJson(info), HttpStatus.OK);
} catch (Exception e) {
......@@ -63,4 +65,30 @@ public class GoalController extends XDFController<GoalBean, String> {
}
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);
}
}
}
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;
import com.bytesw.bytebot.etl.model.GoalForActions;
import com.bytesw.bytebot.repository.GoalForActionsRepository;
import com.bytesw.bytebot.repository.GoalRepository;
......@@ -51,7 +53,7 @@ public class GoalForActionService extends XDFService<GoalForActions, GoalForActi
}
@Transactional(propagation = Propagation.REQUIRED)
public void save(Long idGoal, List<Object> actions) {
public void saveGoalForAction(Long idGoal, List<Object> actions) {
if (!goalRepository.existsById(idGoal)) {
throw new NotFoundException("Goal no registrado.");
}
......@@ -69,7 +71,7 @@ public class GoalForActionService extends XDFService<GoalForActions, GoalForActi
}
@Transactional(propagation = Propagation.REQUIRED)
public void delete(Long goalId) {
public void deleteGoalForAction(Long goalId) {
Optional<List<GoalForActions>> listGoalForActions = goalForActionsRepository.findGoalForActionsByGoalId(goalId);
if (!listGoalForActions.isPresent()) {
......@@ -82,7 +84,7 @@ public class GoalForActionService extends XDFService<GoalForActions, GoalForActi
}
@Transactional(propagation = Propagation.REQUIRED)
public void update(Long idGoal, List<Object> actions) {
public void updateGoalForAction(Long idGoal, List<Object> actions) {
Optional<List<GoalForActions>> listActual = goalForActionsRepository.findGoalForActionsByGoalId(idGoal);
if (!listActual.isPresent()) {
throw new NotFoundException("GoalForActions no encontrados.");
......@@ -109,7 +111,27 @@ public class GoalForActionService extends XDFService<GoalForActions, GoalForActi
newGoalForActions.setActionId(id);
goalForActionsRepository.save(newGoalForActions);
}
}
public List<Map> getGoalForAction(Long id) {
List<Map> list = new ArrayList<>();
Optional<List<GoalForActions>> goalForActions = goalForActionsRepository.findGoalForActionsByGoalId(id);
if (!goalForActions.isPresent()) {
throw new NotFoundException("No se encontraron actions.");
}
for (GoalForActions actions : goalForActions.get()) {
Action action = actionRepository.findById(actions.getActionId()).get();
if (action == null) {
log.debug("No se encontró la accion.");
continue;
}
Map<String, String> map = new HashMap<>();
map.put("IdAction", String.valueOf(action.getId()));
map.put("desc", action.getIdentifier());
list.add(map);
}
return list;
}
}
......@@ -2,6 +2,7 @@ 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;
......@@ -13,9 +14,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
@Service
@Transactional
......@@ -55,7 +54,7 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
}
@Transactional(propagation = Propagation.REQUIRED)
public GoalBean save(Map<String, Object> body) {
public GoalBean saveGoalForAction(Map<String, Object> body) {
Goal goal = new Goal();
......@@ -64,12 +63,12 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
}
Long agenId = Long.valueOf(body.get("AgentId").toString());
String ident = body.get("Ident").toString();
if (!agentRepository.existsById(agenId)) {
throw new NotFoundException("Agente no registrado.");
}
String ident = body.get("Ident").toString();
Optional<Goal> goalOptional= goalRepository.findByAgentAndIdent(agenId, ident);
if (goalOptional.isPresent()) {
......@@ -79,20 +78,19 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
goal.setAgenId(agenId);
goal.setIdentifier(ident);
goal = goalRepository.save(goal);
goalForActionService.save(goal.getId(), (List) body.get("actionList"));
goalForActionService.saveGoalForAction(goal.getId(), (List) body.get("actionList"));
return toBean(goal);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void delete(Long goalId) {
public void deleteGoalForAction(Long goalId) {
if (!goalRepository.existsById(goalId)){
throw new NotFoundException("Goal no registrado.");
}
goalForActionService.delete(goalId);
goalForActionService.deleteGoalForAction(goalId);
goalRepository.deleteById(goalId);
}
......@@ -120,6 +118,34 @@ public class GoalService extends XDFService<Goal, GoalBean, Long> {
goal.setIdentifier(ident);
goal = goalRepository.save(goal);
goalForActionService.update(goal.getId(), (List) body.get("actionList"));
goalForActionService.updateGoalForAction(goal.getId(), (List) body.get("actionList"));
}
public Map<String, Object> getGoalForActions(Long id){
Map<String, Object> result = new HashMap<>();
Optional<Goal> goal = goalRepository.findById(id);
if (!goal.isPresent()) {
throw new NotFoundException("Goal no se encuenta registrado");
}
List<Map> listAction = goalForActionService.getGoalForAction(id);
if (listAction.isEmpty()) {
throw new NotFoundException("Actions no encontrados");
}
result.put("AgentId", goal.get().getAgenId());
result.put("Ident", goal.get().getIdentifier());
result.put("actionList", listAction);
return result;
}
public List<Map> getGoalForActionsAll(){
List<Map> result = new ArrayList<>();
List<Goal> list = (List<Goal>) goalRepository.findAll();
for (Goal goal: list) {
result.add(getGoalForActions(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