Skip to content

Commit

Permalink
coursera progress implementation added. (#58) (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Arpitha <arpitha.sureshappa@tarento.com>
  • Loading branch information
SaipradeepR and ArpithaSureshappa authored Dec 9, 2024
1 parent b719180 commit 12bad78
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class CiosContentServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ public void processReceivedData(String partnerCode, List<Map<String, String>> pr
JsonNode partnerInfo = dataTransformUtility.fetchPartnerInfoUsingApi(partnerCode);
// Extract and cache contentJson
cachedContentJson = objectMapper.convertValue(
partnerInfo.path(Constants.RESULT).path("trasformContentJson"),
partnerInfo.path("trasformContentJson"),
new TypeReference<List<Object>>() {
}
);
if (cachedContentJson == null || cachedContentJson.isEmpty()) {
log.error("trasformContentJson is missing, please update in contentPartner");
log.error("trasformContentJson is missing, please update in contentPartner for partner {}",partnerCode);
throw new CiosContentException(
"ERROR",
"trasformContentJson is missing, please update in contentPartner",
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/igot/cios/dto/RequestBodyDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
public class RequestBodyDTO {
private String serviceCode;
private Map<String, String> urlMap;
private String partnerCode;
private Map<String, String> headerMap;
private Map<String, String> requestBody;
}
4 changes: 2 additions & 2 deletions src/main/java/com/igot/cios/kafka/KafkaProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class KafkaProducer {
public void push(String topic, Object data) {
try {
String message = objectMapper.writeValueAsString(data);
log.info("KafkaProducer::sendCornellData: topic: {}", topic);
log.info("KafkaProducer::sendData: topic: {}", topic);
this.kafkaTemplate.send(topic, message);
log.info("Data sent to kafka topic {} and message is {}", topic);
log.debug("Data sent to kafka topic {} and message is {}", topic,message);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/igot/cios/plugins/DataTransformUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ public String updatingPartnerInfo(JsonNode jsonNode) {
String url = cbServerProperties.getCbPoresbaseUrl() + cbServerProperties.getPartnerCreateEndPoint();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Create the request entity with body and headers
HttpEntity<Object> entity = new HttpEntity<>(jsonNode, headers);

// Make the POST request
ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
return response.getBody();
Expand All @@ -229,7 +225,7 @@ public String updatingPartnerInfo(JsonNode jsonNode) {


public JsonNode fetchPartnerInfoUsingApi(String partnerCode) {
log.info("CiosContentServiceImpl::fetchPartnerInfoUsingApi:fetching partner data by partnerCode");
log.info("CiosContentServiceImpl::fetchPartnerInfoUsingApi:fetching partner data by partnerCode {}",partnerCode);
String getApiUrl = cbServerProperties.getCbPoresbaseUrl() + cbServerProperties.getPartnerReadEndPoint() + partnerCode;
Map<String, String> headers = new HashMap<>();
Map<String, Object> readData = (Map<String, Object>) fetchResultUsingGet(getApiUrl, headers);
Expand All @@ -238,7 +234,7 @@ public JsonNode fetchPartnerInfoUsingApi(String partnerCode) {
throw new RuntimeException("Failed to get data from API: Response is null");
}
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(readData, JsonNode.class);
return objectMapper.convertValue(readData.get("result"), JsonNode.class);
}

public Object fetchResultUsingGet(String uri, Map<String, String> headersValues) {
Expand Down Expand Up @@ -461,4 +457,28 @@ public void flattenContentData(Map<String, Object> entityMap) {
}
}
}

public JsonNode callCiosReadApi(String extCourseId,String partnerId) {
log.info("CourseScheduler :: callCiosReadApi");
try {
String url = cbServerProperties.getCbPoresbaseUrl() + cbServerProperties.getFixedUrl() + extCourseId + "/" + partnerId;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<Object> response = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
Object.class
);
if (response.getStatusCode().is2xxSuccessful()) {
JsonNode jsonNode = objectMapper.valueToTree(response.getBody());
return jsonNode;
} else {
throw new CiosContentException(Constants.ERROR, "Failed to retrieve externalId. Status code: " + response.getStatusCodeValue());
}
} catch (Exception e) {
throw new CiosContentException(Constants.ERROR, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.type.TypeReference;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Service
public class CourseSchedulerService {
public class CornellSchedulerService implements SchedulerInterface {

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -45,14 +48,15 @@ public class CourseSchedulerService {
@Autowired
private DataTransformUtility dataTransformUtility;

private void callCornellEnrollmentAPI(String partnerCode, JsonNode rawContentData) {
public void callEnrollmentAPI(String partnerCode, JsonNode rawContentData) {
try {
log.info("CourseSchedulerService::callCornellEnrollmentAPI");
log.info("CornellSchedulerService::callEnrollmentAPI");
JsonNode entity = dataTransformUtility.fetchPartnerInfoUsingApi(partnerCode);
List<Object> contentJson = Collections.singletonList(entity.path("result").get("transformProgressJson").toString());
List<Object> contentJson = objectMapper.convertValue(entity.get("transformProgressViaApi"), new TypeReference<List<Object>>() {});
JsonNode transformData = dataTransformUtility.transformData(rawContentData, contentJson);
String extCourseId = transformData.get("courseid").asText();
JsonNode result = callCiosReadApi(extCourseId);
String partnerId = entity.get("id").asText();
JsonNode result = dataTransformUtility.callCiosReadApi(extCourseId,partnerId);
String courseId = result.path("content").get("contentId").asText();
String[] parts = transformData.get("userid").asText().split("@");
((ObjectNode) transformData).put("userid", parts[0]);
Expand Down Expand Up @@ -87,51 +91,17 @@ private String updateDateFormatFromTimestamp(Long completedon) {
return sdf.format(date);
}

private JsonNode callCiosReadApi(String extCourseId) {
log.info("CourseScheduler :: callCiosReadApi");
try {
String url = cbServerProperties.getCbPoresbaseUrl() + cbServerProperties.getFixedUrl() + extCourseId;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<Object> response = restTemplate.exchange(
url,
HttpMethod.GET,
entity,
Object.class
);
if (response.getStatusCode().is2xxSuccessful()) {
JsonNode jsonNode = objectMapper.valueToTree(response.getBody());
return jsonNode;
} else {
throw new CiosContentException(Constants.ERROR, "Failed to retrieve externalId. Status code: " + response.getStatusCodeValue());
}
} catch (Exception e) {
throw new CiosContentException(Constants.ERROR, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

}

private JsonNode transformData(Object sourceObject, List<Object> specJson) {
log.debug("CiosContentServiceImpl::transformData");
try {
String inputJson = objectMapper.writeValueAsString(sourceObject);
Chainr chainr = Chainr.fromSpec(specJson);
Object transformedOutput = chainr.transform(JsonUtils.jsonToObject(inputJson));
return objectMapper.convertValue(transformedOutput, JsonNode.class);
} catch (JsonProcessingException e) {
log.error("Error transforming data", e);
return null;
}
}

public JsonNode loadCornellEnrollment() throws JsonProcessingException {
public JsonNode loadEnrollment() {
RequestBodyDTO requestBodyDTO = new RequestBodyDTO();
requestBodyDTO.setServiceCode(cbServerProperties.getCornellEnrollmentServiceCode());
requestBodyDTO.setUrlMap(formUrlMapForEnrollment());
requestBodyDTO.setPartnerCode("CORNELL");
String requestBody = objectMapper.writeValueAsString(requestBodyDTO);
return performEnrollmentCall(requestBodyDTO.getPartnerCode(), requestBody);
String payload = null;
try {
payload = objectMapper.writeValueAsString(requestBodyDTO);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return performEnrollmentCall(cbServerProperties.cornellPartnerCode,payload);
}

private Map<String, String> formUrlMapForEnrollment() {
Expand All @@ -148,7 +118,7 @@ private Map<String, String> formUrlMapForEnrollment() {
return urlMap;
}

private JsonNode performEnrollmentCall(String partnerCode, String requestBody) {
public JsonNode performEnrollmentCall(String partnerCode, String requestBody) {
log.info("calling service locator for getting {} enrollment list", partnerCode);
String url = cbServerProperties.getServiceLocatorHost() + cbServerProperties.getServiceLocatorFixedUrl();
HttpHeaders headers = new HttpHeaders();
Expand All @@ -165,7 +135,7 @@ private JsonNode performEnrollmentCall(String partnerCode, String requestBody) {
JsonNode jsonData = jsonNode.path("responseData").get("enrollments");
jsonData.forEach(
eachContentData -> {
callCornellEnrollmentAPI(partnerCode, eachContentData);
callEnrollmentAPI(partnerCode, eachContentData);
});
return jsonData;
} else {
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/igot/cios/scheduler/CourseScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@
public class CourseScheduler {

@Autowired
CourseSchedulerService courseSchedulerService;
CornellSchedulerService cornellSchedulerService;

@Autowired
CourseraSchedulerService courseraSchedulerService;

@Value("${scheduler.enabled}")
private boolean schedulerEnabled;

@Value("${coursera.scheduler.enabled}")
private boolean courseraSchedulerEnabled;

@Scheduled(cron = "${scheduler.cron}")
private void callEnrollmentApi() throws JsonProcessingException {
private void callCornellEnrollmentApi() throws JsonProcessingException{
if (schedulerEnabled) {
log.info("CourseScheduler :: callEnrollmentApi");
courseSchedulerService.loadCornellEnrollment();
log.info("CourseScheduler :: callCornellEnrollmentApi");
cornellSchedulerService.loadEnrollment();
}
}

@Scheduled(cron = "${coursera.scheduler.cron}")
private void callCourseraEnrollmentApi() throws JsonProcessingException {
if (courseraSchedulerEnabled) {
log.info("CourseScheduler :: callCourseraEnrollmentApi");
courseraSchedulerService.loadEnrollment();
}
}
}
Loading

0 comments on commit 12bad78

Please sign in to comment.