forked from shyamprasadkr/Eagle
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from igot-gov/feature-amit-51983-auth-tool
Task #51983 webhook API event
- Loading branch information
Showing
11 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...vices/src/main/java/com/infosys/lexauthoringservices/consumer/CompetencyDataConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...vices/src/main/java/com/infosys/lexauthoringservices/controller/CompetencyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
apps/authoring-services/src/main/java/com/infosys/lexauthoringservices/model/Competency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...vices/src/main/java/com/infosys/lexauthoringservices/model/CompetencyWebHookResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../authoring-services/src/main/java/com/infosys/lexauthoringservices/producer/Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ng-services/src/main/java/com/infosys/lexauthoringservices/service/CompetencyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
6 changes: 6 additions & 0 deletions
6
...rvices/src/main/java/com/infosys/lexauthoringservices/service/OutBoundRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
111 changes: 111 additions & 0 deletions
111
...ices/src/main/java/com/infosys/lexauthoringservices/serviceimpl/CompetencyServiceImp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...rc/main/java/com/infosys/lexauthoringservices/serviceimpl/OutBoundRequestServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.