forked from Sunbird-Lern/lms-service
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85948a3
commit c0fec6f
Showing
29 changed files
with
1,184 additions
and
17 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
course-mw/course-actors-common/src/main/java/org/sunbird/common/Common.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,37 @@ | ||
package org.sunbird.common; | ||
|
||
import org.sunbird.common.models.util.JsonKey; | ||
import org.sunbird.common.request.Request; | ||
|
||
import java.util.Map; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class Common { | ||
public static Map<String, String[]> getRequestHeadersInArray(Map<String, List<String>> requestHeaders) { | ||
Map<String, String[]> requestHeadersArray = new HashMap(); | ||
requestHeaders.entrySet().forEach(entry -> requestHeadersArray.put(entry.getKey(), entry.getValue().toArray(new String[0]))); | ||
return requestHeadersArray; | ||
} | ||
|
||
public static void handleFixedBatchIdRequest(Request request) { | ||
String courseIdKey = request.getRequest().containsKey(JsonKey.COURSE_ID) | ||
? JsonKey.COURSE_ID | ||
: (request.getRequest().containsKey(JsonKey.ENROLLABLE_ITEM_ID) | ||
? JsonKey.ENROLLABLE_ITEM_ID | ||
: JsonKey.COLLECTION_ID); | ||
String courseId = request.getRequest().getOrDefault(courseIdKey, "").toString(); | ||
request.getRequest().put(JsonKey.COURSE_ID, courseId); | ||
//Till we add a type, we will use fixed batch identifier to prefix to course to get the batchId | ||
String fixedBatchId = request.getRequest().getOrDefault(JsonKey.FIXED_BATCH_ID, "").toString(); | ||
String batchId = request.getRequest().getOrDefault(JsonKey.BATCH_ID, "").toString(); | ||
if (!fixedBatchId.isEmpty()) { | ||
batchId = formBatchIdForFixedBatchId(courseId, fixedBatchId); | ||
} | ||
request.getRequest().put(JsonKey.BATCH_ID, batchId); | ||
} | ||
|
||
public static String formBatchIdForFixedBatchId(String courseId, String fixedBatchId) { | ||
return fixedBatchId + "-" + courseId; | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
...course-actors-common/src/main/java/org/sunbird/learner/actors/event/EventContentUtil.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,90 @@ | ||
package org.sunbird.learner.actors.event; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.mashape.unirest.http.HttpResponse; | ||
import com.mashape.unirest.http.Unirest; | ||
import com.mashape.unirest.http.exceptions.UnirestException; | ||
import org.sunbird.common.models.response.Response; | ||
import org.sunbird.common.models.util.JsonKey; | ||
import org.sunbird.common.request.Request; | ||
import org.sunbird.common.responsecode.ResponseCode; | ||
import org.sunbird.keys.SunbirdKey; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.sunbird.common.models.util.JsonKey.EKSTEP_BASE_URL; | ||
import static org.sunbird.common.models.util.ProjectUtil.getConfigValue; | ||
|
||
public class EventContentUtil { | ||
|
||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
public static List<String> getChildEventIds(Request request, String eventSetId) throws JsonProcessingException, UnirestException { | ||
Response response = getContent(request, JsonKey.IDENTIFIER, eventSetId, new HashMap<>(), "/eventset/v4/hierarchy/{identifier}"); | ||
if (response != null && response.getResponseCode().getResponseCode() == ResponseCode.OK.getResponseCode()) { | ||
Map<String, Object> result = (Map<String, Object>) response.getResult().getOrDefault(SunbirdKey.EVENT_SET, new HashMap<String, Object>()); | ||
return (List<String>) result.getOrDefault("childNodes", new ArrayList<String>()); | ||
} | ||
else | ||
return new ArrayList<>(); | ||
} | ||
|
||
private static Response getContent(Request request, String pathId, String pathValue, Map<String, Object> queryMap, String uri) throws UnirestException, JsonProcessingException { | ||
String requestUrl = getConfigValue(EKSTEP_BASE_URL) + uri; | ||
Map<String, String> headers = new HashMap<>(); | ||
headers.put(SunbirdKey.CONTENT_TYPE_HEADER, SunbirdKey.APPLICATION_JSON); | ||
headers.put(SunbirdKey.X_CHANNEL_ID, request.getContext().getOrDefault(SunbirdKey.CHANNEL, "").toString()); | ||
HttpResponse<String> httpResponse = Unirest.get(requestUrl).headers(headers).routeParam(pathId, pathValue).queryString(queryMap).asString(); | ||
Response response = null; | ||
if (null != httpResponse) { | ||
response = mapper.readValue(httpResponse.getBody(), Response.class); | ||
} | ||
return response; | ||
} | ||
|
||
public static Response postContent(Request request, String contentKey, String uri, Map<String, Object> contentMap, String pathId, String pathVal) throws UnirestException, JsonProcessingException { | ||
String requestUrl = getConfigValue(EKSTEP_BASE_URL) + uri; | ||
Map<String, String> headers = new HashMap<String, String>() {{ | ||
put(SunbirdKey.CONTENT_TYPE_HEADER, SunbirdKey.APPLICATION_JSON); | ||
put(SunbirdKey.X_CHANNEL_ID, (String) request.getContext().get(SunbirdKey.CHANNEL)); | ||
}}; | ||
Map<String, Object> requestMap = new HashMap<String, Object>() {{ | ||
put(SunbirdKey.REQUEST, new HashMap<String, Object>() {{ | ||
put(contentKey, contentMap); | ||
}}); | ||
}}; | ||
|
||
HttpResponse<String> updateResponse = | ||
Unirest.patch(requestUrl) | ||
.headers(headers) | ||
.routeParam(pathId, pathVal) | ||
.body(mapper.writeValueAsString(requestMap)) | ||
.asString(); | ||
|
||
Response response = null; | ||
if (null != updateResponse) response = mapper.readValue(updateResponse.getBody(), Response.class); | ||
return response; | ||
} | ||
|
||
public static Response deleteContent(Request request, String uri, String pathId, String pathVal) throws UnirestException, JsonProcessingException { | ||
String requestUrl = getConfigValue(EKSTEP_BASE_URL) + uri; | ||
Map<String, String> headers = new HashMap<String, String>() {{ | ||
put(SunbirdKey.CONTENT_TYPE_HEADER, SunbirdKey.APPLICATION_JSON); | ||
put(SunbirdKey.X_CHANNEL_ID, (String) request.getContext().get(SunbirdKey.CHANNEL)); | ||
}}; | ||
|
||
HttpResponse<String> updateResponse = | ||
Unirest.delete(requestUrl) | ||
.headers(headers) | ||
.routeParam(pathId, pathVal) | ||
.asString(); | ||
|
||
Response response = null; | ||
if (null != updateResponse) response = mapper.readValue(updateResponse.getBody(), Response.class); | ||
return response; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...se-actors-common/src/main/java/org/sunbird/learner/actors/event/EventManagementActor.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,93 @@ | ||
package org.sunbird.learner.actors.event; | ||
|
||
import org.apache.commons.collections.MapUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.sunbird.actor.base.BaseActor; | ||
import org.sunbird.common.Common; | ||
import org.sunbird.common.exception.ProjectCommonException; | ||
import org.sunbird.common.models.response.Response; | ||
import org.sunbird.common.models.util.JsonKey; | ||
import org.sunbird.common.request.Request; | ||
import org.sunbird.common.responsecode.ResponseCode; | ||
import org.sunbird.keys.SunbirdKey; | ||
import org.sunbird.learner.actors.coursebatch.service.UserCoursesService; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class EventManagementActor extends BaseActor { | ||
|
||
private final UserCoursesService userCoursesService = new UserCoursesService(); | ||
|
||
@Override | ||
public void onReceive(Request request) throws Throwable { | ||
String requestedOperation = request.getOperation(); | ||
switch (requestedOperation) { | ||
case "discardEvent": | ||
discardEvent(request); | ||
break; | ||
default: | ||
onReceiveUnsupportedOperation(requestedOperation); | ||
break; | ||
} | ||
} | ||
|
||
private void discardEvent(Request request) throws Exception { | ||
validateNoEnrollments(request); | ||
String pathId = JsonKey.IDENTIFIER; | ||
String pathVal = request.getRequest().getOrDefault(JsonKey.IDENTIFIER, "").toString(); | ||
Response response = EventContentUtil.deleteContent(request, "/private/event/v4/discard/{identifier}", pathId, pathVal); | ||
try { | ||
if (response != null && response.getResponseCode().getResponseCode() == ResponseCode.OK.getResponseCode()) { | ||
sender().tell(response, self()); | ||
} else if (response != null) { | ||
Map<String, Object> resultMap = | ||
Optional.ofNullable(response.getResult()).orElse(new HashMap<>()); | ||
String message = "Event discard failed "; | ||
if (MapUtils.isNotEmpty(resultMap)) { | ||
Object obj = Optional.ofNullable(resultMap.get(SunbirdKey.TB_MESSAGES)).orElse(""); | ||
if (obj instanceof List) { | ||
message += ((List<String>) obj).stream().collect(Collectors.joining(";")); | ||
} else if (StringUtils.isNotEmpty(response.getParams().getErrmsg())) { | ||
message += response.getParams().getErrmsg(); | ||
} else { | ||
message += String.valueOf(obj); | ||
} | ||
} | ||
ProjectCommonException.throwClientErrorException( | ||
ResponseCode.customServerError, | ||
MessageFormat.format( | ||
ResponseCode.customServerError.getErrorMessage(), message)); | ||
} else { | ||
ProjectCommonException.throwClientErrorException(ResponseCode.CLIENT_ERROR); | ||
} | ||
} catch (Exception ex) { | ||
logger.error(request.getRequestContext(), "EventManagementActor:discardEvent : discard error ", ex); | ||
if (ex instanceof ProjectCommonException) { | ||
throw ex; | ||
} else { | ||
throw new ProjectCommonException( | ||
ResponseCode.SERVER_ERROR.getErrorCode(), | ||
ResponseCode.SERVER_ERROR.getErrorMessage(), | ||
ResponseCode.SERVER_ERROR.getResponseCode()); | ||
} | ||
} | ||
} | ||
|
||
private void validateNoEnrollments(Request request) { | ||
String identifier = request.get(SunbirdKey.IDENTIFIER).toString(); | ||
String fixedBatchId = request.get(JsonKey.FIXED_BATCH_ID).toString(); | ||
String batchId = Common.formBatchIdForFixedBatchId(identifier, fixedBatchId); | ||
List<String> participants = userCoursesService.getParticipantsList(batchId, true, request.getRequestContext()); | ||
if (!participants.isEmpty()) { | ||
ProjectCommonException.throwClientErrorException( | ||
ResponseCode.cannotUpdateEventSetHavingEnrollments, | ||
ResponseCode.cannotUpdateEventSetHavingEnrollments.getErrorMessage()); | ||
} | ||
} | ||
|
||
} |
145 changes: 145 additions & 0 deletions
145
...actors-common/src/main/java/org/sunbird/learner/actors/event/EventSetManagementActor.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,145 @@ | ||
package org.sunbird.learner.actors.event; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.mashape.unirest.http.exceptions.UnirestException; | ||
import org.apache.commons.collections.MapUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.sunbird.actor.base.BaseActor; | ||
import org.sunbird.common.Common; | ||
import org.sunbird.common.exception.ProjectCommonException; | ||
import org.sunbird.common.models.response.Response; | ||
import org.sunbird.common.models.util.JsonKey; | ||
import org.sunbird.common.request.Request; | ||
import org.sunbird.common.responsecode.ResponseCode; | ||
import org.sunbird.keys.SunbirdKey; | ||
import org.sunbird.learner.actors.coursebatch.service.UserCoursesService; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class EventSetManagementActor extends BaseActor { | ||
private final UserCoursesService userCoursesService = new UserCoursesService(); | ||
|
||
@Override | ||
public void onReceive(Request request) throws Throwable { | ||
String requestedOperation = request.getOperation(); | ||
switch (requestedOperation) { | ||
case "updateEventSet": | ||
updateEventSet(request); | ||
break; | ||
case "discardEventSet": | ||
discardEventSet(request); | ||
break; | ||
default: | ||
onReceiveUnsupportedOperation(requestedOperation); | ||
break; | ||
} | ||
} | ||
|
||
private void updateEventSet(Request request) throws Exception { | ||
validateNoEventEnrollments(request); | ||
try { | ||
Map<String, Object> contentMap = new HashMap<>((Map<String, Object>) request.get(SunbirdKey.EVENT_SET)); | ||
String pathId = JsonKey.IDENTIFIER; | ||
String pathVal = request.getRequest().getOrDefault(JsonKey.IDENTIFIER, "").toString(); | ||
Response response = EventContentUtil.postContent(request, SunbirdKey.EVENT_SET, "/private/eventset/v4/update/{identifier}", contentMap, pathId, pathVal); | ||
if (null != response) { | ||
if (response.getResponseCode().getResponseCode() == ResponseCode.OK.getResponseCode()) { | ||
sender().tell(response, self()); | ||
} else { | ||
String message = formErrorDetailsMessage(response, "EventSet updation failed "); | ||
ProjectCommonException.throwClientErrorException( | ||
ResponseCode.customServerError, | ||
MessageFormat.format( | ||
ResponseCode.customServerError.getErrorMessage(), message)); | ||
} | ||
} else { | ||
ProjectCommonException.throwClientErrorException(ResponseCode.CLIENT_ERROR); | ||
} | ||
} catch (Exception ex) { | ||
logger.error(request.getRequestContext(), "EventSetManagementActor:updateEventSet : update error ", ex); | ||
if (ex instanceof ProjectCommonException) { | ||
throw ex; | ||
} else { | ||
throw new ProjectCommonException( | ||
ResponseCode.SERVER_ERROR.getErrorCode(), | ||
ResponseCode.SERVER_ERROR.getErrorMessage(), | ||
ResponseCode.SERVER_ERROR.getResponseCode()); | ||
} | ||
} | ||
} | ||
|
||
private String formErrorDetailsMessage(Response response, String message) { | ||
Map<String, Object> resultMap = | ||
Optional.ofNullable(response.getResult()).orElse(new HashMap<>()); | ||
if (MapUtils.isNotEmpty(resultMap)) { | ||
Object obj = Optional.ofNullable(resultMap.get(SunbirdKey.TB_MESSAGES)).orElse(""); | ||
if (obj instanceof List) { | ||
message += ((List<String>) obj).stream().collect(Collectors.joining(";")); | ||
} else if (StringUtils.isNotEmpty(response.getParams().getErrmsg())) { | ||
message += response.getParams().getErrmsg(); | ||
} else { | ||
message += String.valueOf(obj); | ||
} | ||
} | ||
return message; | ||
} | ||
|
||
private void discardEventSet(Request request) throws Exception { | ||
validateNoEventEnrollments(request); | ||
try { | ||
String pathId = JsonKey.IDENTIFIER; | ||
String pathVal = request.getRequest().getOrDefault(JsonKey.IDENTIFIER, "").toString(); | ||
Response response = EventContentUtil.deleteContent(request, "/private/eventset/v4/discard/{identifier}", pathId, pathVal); | ||
if (null != response) { | ||
if (response.getResponseCode().getResponseCode() == ResponseCode.OK.getResponseCode()) { | ||
sender().tell(response, self()); | ||
} else { | ||
String message = formErrorDetailsMessage(response, "EventSet discard failed "); | ||
ProjectCommonException.throwClientErrorException( | ||
ResponseCode.customServerError, | ||
MessageFormat.format( | ||
ResponseCode.customServerError.getErrorMessage(), message)); | ||
} | ||
} else { | ||
ProjectCommonException.throwClientErrorException(ResponseCode.CLIENT_ERROR); | ||
} | ||
} catch (Exception ex) { | ||
logger.error(request.getRequestContext(), "EventSetManagementActor:discardEventSet : discard error ", ex); | ||
if (ex instanceof ProjectCommonException) { | ||
throw ex; | ||
} else { | ||
throw new ProjectCommonException( | ||
ResponseCode.SERVER_ERROR.getErrorCode(), | ||
ResponseCode.SERVER_ERROR.getErrorMessage(), | ||
ResponseCode.SERVER_ERROR.getResponseCode()); | ||
} | ||
} | ||
} | ||
|
||
private void validateNoEventEnrollments(Request request) { | ||
String identifier = request.get(SunbirdKey.IDENTIFIER).toString(); | ||
String fixedBatchId = request.get(JsonKey.FIXED_BATCH_ID).toString(); | ||
List<String> participants = new ArrayList<>(); | ||
try { | ||
List<String> eventsIds = EventContentUtil.getChildEventIds(request, identifier); | ||
participants = eventsIds.stream().map(childId -> { | ||
String childBatchId = Common.formBatchIdForFixedBatchId(childId, fixedBatchId); | ||
return userCoursesService.getParticipantsList(childBatchId, true, request.getRequestContext()); | ||
}) | ||
.filter(Objects::nonNull) | ||
.flatMap(List::stream) | ||
.collect(Collectors.toList()); | ||
} catch (UnirestException | JsonProcessingException e) { | ||
ProjectCommonException.throwServerErrorException( | ||
ResponseCode.SERVER_ERROR, | ||
e.getMessage()); | ||
} | ||
if (!participants.isEmpty()) { | ||
ProjectCommonException.throwClientErrorException( | ||
ResponseCode.cannotUpdateEventSetHavingEnrollments, | ||
ResponseCode.cannotUpdateEventSetHavingEnrollments.getErrorMessage()); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.