Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.8.16 dev #4

Open
wants to merge 11 commits into
base: cbrelease-4.8.16
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@ private static String formEventData(Map<String, Object> data) {
}
return jsonMessage;
}

public static void mentorshipUserUpdateEvent(String key, String topic, Map<String, String> data) throws Exception {
String jsonMessage = null;
try {
jsonMessage = mapper.writeValueAsString(data);
} catch (Exception e) {
logger.error("Error creating JSON message: " + e.getMessage(), e);
}
if (StringUtils.isBlank(jsonMessage)) {
throw new ProjectCommonException(ResponseCode.valueOf("BE_JOB_REQUEST_EXCEPTION"), "mentorship user update Event is not generated properly.", ResponseCode.CLIENT_ERROR.getResponseCode());
}
if (StringUtils.isNotBlank(topic)) {
if (StringUtils.isNotBlank(key)) KafkaClient.send(key, jsonMessage, topic);
else KafkaClient.send(jsonMessage, topic);
} else {
throw new ProjectCommonException(ResponseCode.valueOf("BE_JOB_REQUEST_EXCEPTION"), "Invalid topic id.", ResponseCode.CLIENT_ERROR.getResponseCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,9 @@ public final class JsonKey {
public static final String PHONE_CAPS = "Phone";
public static final String EMAIL_CAPS = "Email";
public static final String GCP = "GCP";
public static final String MENTORING_ROLES = "mentoring.roles";
public static final String MENTORING = "mentoring";
public static final String USERID= "userid";

private JsonKey() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,6 @@ user_read_api_v2_non_mandatory_fields= profileDetails.employmentDetails.employee
kafka_user_first_login_event_topic= dev.user.first.login
otpValidationSecretKey=secretKey
otpExpirationTimeToken=300000
kafka_mentorship_user_update_topic=dev.mentorship.user.update
mentoring.roles= MENTOR

10 changes: 10 additions & 0 deletions service/src/main/java/org/sunbird/actor/user/UserUpdateActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.sunbird.dao.user.impl.UserSelfDeclarationDaoImpl;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.ResponseCode;
import org.sunbird.kafka.InstructionEventGenerator;
import org.sunbird.keys.JsonKey;
import org.sunbird.model.location.Location;
import org.sunbird.model.user.User;
Expand Down Expand Up @@ -293,6 +294,15 @@ private void updateUser(Request actorMessage) {
Map<String, Object> completeUserDetails = new HashMap<>(userDbRecord);
completeUserDetails.putAll(requestMap);
saveUserDetailsToEs(completeUserDetails, actorMessage.getRequestContext());
String topic = ProjectUtil.getConfigValue("kafka_mentorship_user_update_topic");
try {
HashMap<String,String> userDetails = new HashMap<>();
userDetails.put(JsonKey.USER_ID,user.getUserId());
InstructionEventGenerator.mentorshipUserUpdateEvent("", topic, userDetails);
logger.info("kafka_mentorship_user_update_topic event pushed");
}catch (Exception e){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do formatting

logger.info("error while generating mentorship event");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error should give proper msg and it should be error

}
}
generateUserTelemetry(
userMap, actorMessage, (String) userMap.get(JsonKey.USER_ID), JsonKey.UPDATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,37 @@ public Response getUserProfileData(Request actorMessage) {
result.put(JsonKey.IDENTIFIER, userId);

mapUserRoles(result);
if(result.containsKey(JsonKey.ROLES) && CollectionUtils.isNotEmpty((Collection) result.get(JsonKey.ROLES))){
List<String> mentoringRoles = new ArrayList<>();
List<String> roles = new ArrayList<>();
if (result.get(JsonKey.ROLES) instanceof List) {
List<?> list = (List<?>) result.get(JsonKey.ROLES);

if (!list.isEmpty() && list.get(0) instanceof HashMap) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle null pointer and try to use collectionUtils as it handle the proper null pointer

for (Object element : list) {
HashMap<?, ?> hashmap = (HashMap<?, ?>) element;
if (hashmap.containsKey(JsonKey.ROLE) && hashmap.get(JsonKey.ROLE) instanceof String) {
roles.add((String) hashmap.get(JsonKey.ROLE));
}
}
} else if (!list.isEmpty() && list.get(0) instanceof String) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle null pointer and try to use collectionUtils as it handle the proper null pointer

for (Object element : list) {
roles.add((String) element);
}
}
}
List<String> mentorRoles = List.of(ProjectUtil.getConfigValue(JsonKey.MENTORING_ROLES).split(","));
for (String element : roles) {
if (mentorRoles.contains(element)) {
mentoringRoles.add(element);
}
}
if (!mentoringRoles.isEmpty()){
Map<String, Object> mentorObj = new HashMap<>();
mentorObj.put(JsonKey.ROLES,mentoringRoles);
result.put(JsonKey.MENTORING,mentorObj);
}
}

// Record the start time for measuring the execution time.
long startTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.ElasticSearchHelper;
import org.sunbird.dao.user.UserDao;
import org.sunbird.dao.user.UserLookupDao;
Expand All @@ -22,6 +23,7 @@
import org.sunbird.dto.SearchDTO;
import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.ResponseCode;
import org.sunbird.helper.ServiceFactory;
import org.sunbird.keys.JsonKey;
import org.sunbird.logging.LoggerUtil;
import org.sunbird.model.adminutil.AdminUtilRequestData;
Expand Down Expand Up @@ -55,6 +57,7 @@ public class UserServiceImpl implements UserService {
private final OrgService orgService = OrgServiceImpl.getInstance();
private final UserRoleService userRoleService = UserRoleServiceImpl.getInstance();
private final ObjectMapper mapper = new ObjectMapper();
private final CassandraOperation cassandraOperation = ServiceFactory.getInstance();

public static UserService getInstance() {
if (userService == null) {
Expand Down Expand Up @@ -211,6 +214,7 @@ public List<Map<String, Object>> searchUserNameInUserLookup(
public Response userLookUpByKey(
String key, String value, List<String> fields, RequestContext context) {
Response response;
Map<String, List<String>> userRoleMap = new HashMap<>();
if (JsonKey.ID.equalsIgnoreCase(key)) {
List<String> ids = new ArrayList<>(2);
ids.add(value);
Expand All @@ -226,6 +230,28 @@ record -> {
ids.add((String) record.get(JsonKey.USER_ID));
});
response = userDao.getUserPropertiesById(ids, fields, context);
if (fields.contains(JsonKey.ROLES)) {
Response cassandraResponse = cassandraOperation.getRecordsByProperties(
JsonKey.SUNBIRD, JsonKey.USER_ROLES,
Collections.singletonMap(JsonKey.USERID, ids),
Arrays.asList(JsonKey.ROLE, JsonKey.USERID), context);

((List<Map<String, Object>>) cassandraResponse.getResult().get(JsonKey.RESPONSE)).stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plead do formatting

.filter(userRole -> userRole.get(JsonKey.ROLE) != null && !((String) userRole.get(JsonKey.ROLE)).isEmpty())
.forEach(userRole ->
userRoleMap.computeIfAbsent((String) userRole.get(JsonKey.USER_ID), k -> new ArrayList<>())
.add((String) userRole.get(JsonKey.ROLE))
);

((List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE)).stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you do typecast of not assign to any variable

.forEach(user -> {
if (userRoleMap.containsKey((String) user.get(JsonKey.ID))) {
user.put(JsonKey.ROLES, userRoleMap.get((String) user.get(JsonKey.ID)));
}
UserUtility.decryptUserDataFrmES(user);
});
}
return response;
}
for (Map<String, Object> userMap :
(List<Map<String, Object>>) response.getResult().get(JsonKey.RESPONSE)) {
Expand Down