Skip to content

Commit

Permalink
Merge pull request #24 from igot-gov/feature-amit-51983-auth-tool
Browse files Browse the repository at this point in the history
Task #51983 webhook API event
  • Loading branch information
karthik-tarento authored Oct 13, 2020
2 parents fdaa19a + 0ef19b5 commit a9e9abc
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.infosys.lexauthoringservices.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.infosys.lexauthoringservices.model.Competency;
import com.infosys.lexauthoringservices.service.CompetencyService;
import com.infosys.lexauthoringservices.util.LexLogger;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class CompetencyDataConsumer {

@Autowired
private LexLogger logger;

@Autowired
private CompetencyService competencyService;

@KafkaListener(id = "id0", groupId = "competencyUpdateTopic-consumer", topicPartitions = {
@TopicPartition(topic = "${kafka.topics.competency.update}", partitions = { "0", "1", "2", "3" }) })
public void processMessage(ConsumerRecord<String, String> data) throws IOException {
ObjectMapper mapper = new ObjectMapper();
try {
Competency competency = mapper.readValue(String.valueOf(data.value()), Competency.class);
competencyService.processUpdateCompetencyData(competency);
} catch (Exception ex) {
logger.error("Competency Update Process Exception : " + ex.toString());
throw ex;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.infosys.lexauthoringservices.controller;

import com.infosys.lexauthoringservices.model.Competency;
import com.infosys.lexauthoringservices.model.CompetencyWebHookResponse;
import com.infosys.lexauthoringservices.service.CompetencyService;
import com.infosys.lexauthoringservices.util.LexConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping("/action/competency")
public class CompetencyController {

@Autowired
private CompetencyService competencyService;

@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<CompetencyWebHookResponse> updateCompetencyData(@RequestHeader("rootOrg") String rootOrg, @Valid @RequestBody Competency competency) {
competency.setRootOrg(rootOrg);
competencyService.updateCompetencyData(competency);
CompetencyWebHookResponse response = new CompetencyWebHookResponse();
HashMap<String, Object> statusInfo = new HashMap<>();
statusInfo.put(LexConstants.STATUS_CODE, HttpStatus.ACCEPTED.value());
statusInfo.put(LexConstants.STATUS_MESSAGE, LexConstants.STATUS_MESSAGE_VALUE);
statusInfo.put(LexConstants.ERROR_MESSAGE, "");
response.setResponseData(true);
response.setStatusInfo(statusInfo);
return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.infosys.lexauthoringservices.model;

import java.util.HashMap;

public class Competency {

private String type;

private String id;

private String name;

private String description;

private String status;

private String source;

private HashMap<String, Object> additionalProperties;

private String rootOrg;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public HashMap<String, Object> getAdditionalProperties() {
return additionalProperties;
}

public void setAdditionalProperties(HashMap<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}

public String getRootOrg() {
return rootOrg;
}

public void setRootOrg(String rootOrg) {
this.rootOrg = rootOrg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.infosys.lexauthoringservices.model;

import java.util.HashMap;

public class CompetencyWebHookResponse {
Boolean responseData;

HashMap<String, Object> statusInfo;

public Boolean getResponseData() {
return responseData;
}

public void setResponseData(Boolean responseData) {
this.responseData = responseData;
}

public HashMap<String, Object> getStatusInfo() {
return statusInfo;
}

public void setStatusInfo(HashMap<String, Object> statusInfo) {
this.statusInfo = statusInfo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.infosys.lexauthoringservices.producer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.infosys.lexauthoringservices.util.LexLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class Producer {

@Autowired
private LexLogger logger;

@Autowired
KafkaTemplate<String, String> kafkaTemplate;

public void push(String topic, Object value) {
ObjectMapper mapper = new ObjectMapper();
try {
kafkaTemplate.send(topic, mapper.writeValueAsString(value));
} catch (JsonProcessingException e) {
logger.error(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.infosys.lexauthoringservices.service;

import com.infosys.lexauthoringservices.model.Competency;

public interface CompetencyService {

public void updateCompetencyData(Competency competency);

public Competency processUpdateCompetencyData(Competency competency);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.infosys.lexauthoringservices.service;

public interface OutBoundRequestService {

public Object fetchResult(StringBuilder uri);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.infosys.lexauthoringservices.serviceimpl;

import com.infosys.lexauthoringservices.model.Competency;
import com.infosys.lexauthoringservices.producer.Producer;
import com.infosys.lexauthoringservices.service.CompetencyService;
import com.infosys.lexauthoringservices.service.ContentCrudService;
import com.infosys.lexauthoringservices.util.LexLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
public class CompetencyServiceImp implements CompetencyService {

@Autowired
private LexLogger logger;

@Autowired
private Producer producer;

@Autowired
private ContentCrudService contentCrudService;

@Autowired
private GraphServiceImpl graphService;

@Autowired
private OutBoundRequestServiceImpl outBoundRequestService;

@Value("${kafka.topics.competency.update}")
private String competencyUpdateTopic;

@Value("${sbext.service.host}")
private String sbextServiceURL;

@Value("${sbext.service.competencySearchPath}")
private String competencySearchPath;

public final String COMPETENCIES_CONST = "competencies";

public final String ID_CONST = "id";

public final String NAME_CONST = "name";

public final String DESCRIPTION_CONST = "description";

public final String IDENTIFIER_CONST = "identifier";

public final String TYPE_CONST = "type";

@Override
public void updateCompetencyData(Competency competency) {
producer.push(competencyUpdateTopic, competency);
}

/**
* Update the copetency in neo4j & elasticsearch
*
* @param competency
* @return
*/
@Override
public Competency processUpdateCompetencyData(Competency competency) {
StringBuilder builder = new StringBuilder();
String path = competencySearchPath.replace("{org}", competency.getRootOrg()).replace("{id}", competency.getId());
builder.append(sbextServiceURL).append(path);
Object response = outBoundRequestService.fetchResult(builder);
if (ObjectUtils.isEmpty(response))
return competency;
List<HashMap<String, Object>> listOfIds = (List<HashMap<String, Object>>) response;

if (listOfIds.isEmpty())
return competency;
List<String> successRecords = new ArrayList<>();
List<String> failedRecords = new ArrayList<>();
logger.info("Competency update started ..............");
for (HashMap<String, Object> map : listOfIds) {
String lex_id = (String) map.get(IDENTIFIER_CONST);
try {
Map<String, Object> contentData = contentCrudService.getContentNode(competency.getRootOrg(), lex_id);
Object competencyObject = contentData.get(COMPETENCIES_CONST);
if (ObjectUtils.isEmpty(competencyObject)) {
failedRecords.add(lex_id);
continue;
}
List<HashMap<String, String>> competencyData = (List<HashMap<String, String>>) competencyObject;
for (HashMap<String, String> data : competencyData) {
if (data.get(ID_CONST).equals(competency.getId())) {
data.put(NAME_CONST, competency.getName());
data.put(DESCRIPTION_CONST, competency.getDescription());
}
}
contentData.put(COMPETENCIES_CONST, competencyData);
contentCrudService.updateContentMetaNode(competency.getRootOrg(), "", lex_id, contentData);
successRecords.add(lex_id);
} catch (Exception ex) {
failedRecords.add(lex_id);
logger.error(ex.toString());
}
}
if (!successRecords.isEmpty())
logger.info("Success Records : " + successRecords.toString());
if (!failedRecords.isEmpty())
logger.info("Failed Records : " + failedRecords.toString());
logger.info("Competency update finished ..............");
return competency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.infosys.lexauthoringservices.serviceimpl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.infosys.lexauthoringservices.service.OutBoundRequestService;
import com.infosys.lexauthoringservices.util.LexLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OutBoundRequestServiceImpl implements OutBoundRequestService {

@Autowired
private LexLogger logger;

@Autowired
private RestTemplate restTemplate;

/**
* @param uri
* @return
* @throws Exception
*/
public Object fetchResult(StringBuilder uri) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Object response = null;
StringBuilder str = new StringBuilder(this.getClass().getCanonicalName()).append(".fetchResult:")
.append(System.lineSeparator());
str.append("URI: ").append(uri.toString()).append(System.lineSeparator());
try {
String message = str.toString();
logger.debug(message);
response = restTemplate.getForObject(uri.toString(), List.class);
} catch (HttpClientErrorException e) {
logger.error("External Service threw an Exception: " + e.toString());
} catch (Exception e) {
logger.error("Exception while fetching from the external service: " + e.toString());
}
return response;
}
}
Loading

0 comments on commit a9e9abc

Please sign in to comment.