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

Added update profile in user profile service #13

Open
wants to merge 2 commits into
base: cbrelease-3.0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public final class JsonKey {
public static final String ORG_IMAGE_URL = "orgImageUrl";
public static final String ORG_JOIN_DATE = "orgJoinDate";
public static final String ORG_NAME = "orgName";
public static final String DEPARTMENT_NAME = "departmentName";
public static final String ORG_TYPE = "organisationType";
public static final String ORGANISATION = "organisation";
public static final String ORGANISATION_ID = "organisationId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.sunbird.services.sso.SSOServiceFactory;
import org.sunbird.telemetry.util.TelemetryUtil;
import org.sunbird.user.service.impl.UserLookUpServiceImpl;
import org.sunbird.user.service.impl.UserProfileService;
import org.sunbird.user.service.impl.UserServiceImpl;
import org.sunbird.user.util.MigrationUtils;
import org.sunbird.user.util.UserActorOperations;
Expand Down Expand Up @@ -77,6 +78,8 @@ public class TenantMigrationActor extends BaseActor {
"");
private DataMaskingService maskingService =
org.sunbird.common.models.util.datasecurity.impl.ServiceFactory.getMaskingServiceInstance("");
private UserProfileService userProfileService = new UserProfileService();


@Override
public void onReceive(Request request) throws Throwable {
Expand Down Expand Up @@ -227,6 +230,25 @@ private void migrateUser(Request request) {
}
userOrgErrMsgList.addAll(userExtIdErrMsgList);
response.getResult().put(JsonKey.ERRORS, userOrgErrMsgList);

//Added code for updating user department for profile details
Map<String, Object> orgDao = new HashMap<>();
Util.DbInfo orgDbInfo = Util.dbInfoMap.get(JsonKey.ORG_DB);
Response orgResult =
cassandraOperation.getRecordById(
orgDbInfo.getKeySpace(),
orgDbInfo.getTableName(),
orgId,
request.getRequestContext());
List<Map<String, Object>> list = (List<Map<String, Object>>) orgResult.get(JsonKey.RESPONSE);
if (!(list.isEmpty())) {
orgDao = list.get(0);
}
String userDepatment = orgDao.get(JsonKey.ORG_NAME).toString();
Map<String, Object> profileFields = new HashMap<>();
profileFields.put(JsonKey.DEPARTMENT_NAME, userDepatment);
userProfileService.updateProfile(request.getRequest().get(JsonKey.USER_ID).toString(),profileFields);

// send the response
sender().tell(response, self());
// save user data to ES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ public interface IUserProfileService {
*/
public void validateProfile(Request userRequest);

/**
*
* @param uuid
* @param fields
*/
public void updateProfile(String uuid, Map<String, Object> fields);



}
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package org.sunbird.user.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections.CollectionUtils;
import org.json.JSONObject;
import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.exception.ProjectCommonException;
import org.sunbird.common.models.response.Response;
import org.sunbird.common.models.util.JsonKey;
import org.sunbird.common.models.util.LoggerUtil;
import org.sunbird.common.request.Request;
import org.sunbird.helper.ServiceFactory;
import org.sunbird.learner.util.Util;
import org.sunbird.models.user.User;
import org.sunbird.user.profile.ProfileUtil;
import org.sunbird.user.service.IUserProfileService;
import org.sunbird.user.service.UserProfileReadService;
import org.sunbird.validator.user.JsonSchemaValidator;


import java.util.List;
import java.util.Map;

import static org.sunbird.common.request.orgvalidator.BaseOrgRequestValidator.ERROR_CODE;
Expand All @@ -20,6 +30,9 @@ public class UserProfileService implements IUserProfileService {
private LoggerUtil logger = new LoggerUtil(UserProfileReadService.class);
private static final String SCHEMA = "profileDetails.json";

private CassandraOperation cassandraOperation = ServiceFactory.getInstance();




@Override
Expand All @@ -43,4 +56,69 @@ public void validateProfile(Request userRequest) {
}
}

@Override
public void updateProfile(String uuid, Map<String, Object> profileFields) {

Util.DbInfo usrDbInfo = Util.dbInfoMap.get(JsonKey.USER_DB);
Response record =
cassandraOperation.getRecordById(
usrDbInfo.getKeySpace(), usrDbInfo.getTableName(), uuid, null);
List<Map<String, Object>> userList = (List<Map<String, Object>>) record.get(JsonKey.RESPONSE);
if (CollectionUtils.isNotEmpty(userList)) {
try{
Map<String, Object> userMap = userList.get(0);
JsonNode profileMap = (JsonNode)userMap.get(JsonKey.PROFILE_DETAILS);
//update the profile object with request fields
for(Map.Entry entry: profileFields.entrySet()){
replaceField((ObjectNode)profileMap, entry.getKey().toString(), entry.getValue().toString());
}
System.out.println("replaced profile ==> "+profileMap);
Map m = ProfileUtil.mapper.convertValue(profileMap, Map.class);
User user = new User();
user.setId(uuid);
user.setProfileDetails(m);

Map requestMap = ProfileUtil.mapper.convertValue(user, Map.class);

Response response =
cassandraOperation.updateRecord(
usrDbInfo.getKeySpace(),
usrDbInfo.getTableName(),
requestMap,
null);

if (((String) response.get(JsonKey.RESPONSE)).equalsIgnoreCase(JsonKey.SUCCESS)) {
logger.info(null, "UserProfile: updateUser: User profile update successfully");
} else {
logger.info(null, "UserProfile: updateUser: User profile update failure");
}
}catch(Exception e){
logger.error("UserProfile: updateUser: User profile update failure", e);

}


}

}


private static void replaceField(ObjectNode parent, String fieldName, String newValue) {
if (parent.has(fieldName)) {
parent.put(fieldName, newValue);
}
parent.fields().forEachRemaining(entry -> {
JsonNode entryValue = entry.getValue();
if (entryValue.isArray()) {
for (int i = 0; i < entryValue.size(); i++) {
if (entry.getValue().get(i).isObject())
replaceField((ObjectNode) entry.getValue().get(i), fieldName, newValue);
}
} else if (entryValue.isObject()) {
replaceField((ObjectNode) entry.getValue(), fieldName, newValue);
}
});
}


}