Skip to content

Commit

Permalink
Issue # SB-308 Developement : Display T&C For TEI Org admin sign-off …
Browse files Browse the repository at this point in the history
…on first time login to NTP via web.
  • Loading branch information
arvindyadav108 committed Oct 31, 2017
1 parent 5756293 commit 442bb86
Show file tree
Hide file tree
Showing 2 changed files with 658 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
package org.sunbird.learner.actors.tenantpreference;

import akka.actor.UntypedAbstractActor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.sunbird.cassandra.CassandraOperation;
import org.sunbird.common.ElasticSearchUtil;
import org.sunbird.common.exception.ProjectCommonException;
import org.sunbird.common.models.response.Response;
import org.sunbird.common.models.util.ActorOperations;
import org.sunbird.common.models.util.JsonKey;
import org.sunbird.common.models.util.LoggerEnum;
import org.sunbird.common.models.util.ProjectLogger;
import org.sunbird.common.models.util.ProjectUtil;
import org.sunbird.common.models.util.ProjectUtil.EsType;
import org.sunbird.common.request.Request;
import org.sunbird.common.responsecode.ResponseCode;
import org.sunbird.helper.ServiceFactory;
import org.sunbird.learner.util.Util;

/**
* Class for T&C .
* Created by arvind on 27/10/17.
*/
public class TenantPreferenceManagementActor extends UntypedAbstractActor {

private CassandraOperation cassandraOperation = ServiceFactory.getInstance();
private Util.DbInfo tenantPreferenceDbInfo = Util.dbInfoMap.get(JsonKey.TENANT_PREFERENCE_DB);
Util.DbInfo userDbInfo = Util.dbInfoMap.get(JsonKey.USER_DB);
Util.DbInfo orgDbInfo = Util.dbInfoMap.get(JsonKey.ORG_DB);
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

@Override
public void onReceive(Object message) throws Throwable {
if (message instanceof Request) {
try {
ProjectLogger.log("TenantPreferenceManagementActor-onReceive called");
Request actorMessage = (Request) message;
if (actorMessage.getOperation().equalsIgnoreCase(ActorOperations.CREATE_TENANT_PREFERENCE.getValue())) {
createTenantPreference(actorMessage);
} else if (actorMessage.getOperation()
.equalsIgnoreCase(ActorOperations.UPDATE_TENANT_PREFERENCE.getValue())) {
updateTenantPreference(actorMessage);
}else if (actorMessage.getOperation()
.equalsIgnoreCase(ActorOperations.GET_TENANT_PREFERENCE.getValue())) {
getTenantPreference(actorMessage);
}else if (actorMessage.getOperation()
.equalsIgnoreCase(ActorOperations.UPDATE_TC_STATUS_OF_USER.getValue())) {
updateTcStatusOfUser(actorMessage);
} else {
ProjectLogger.log("UNSUPPORTED OPERATION", LoggerEnum.INFO.name());
ProjectCommonException exception =
new ProjectCommonException(ResponseCode.invalidOperationName.getErrorCode(),
ResponseCode.invalidOperationName.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
sender().tell(exception, self());
}
} catch (Exception ex) {
ProjectLogger.log(ex.getMessage(), ex);
sender().tell(ex, self());
}
} else {
ProjectLogger.log("UNSUPPORTED MESSAGE");
ProjectCommonException exception =
new ProjectCommonException(ResponseCode.invalidRequestData.getErrorCode(),
ResponseCode.invalidRequestData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
sender().tell(exception, self());
}
}

/**
* Method to get the all Tenal preference of the given root org id .
* @param actorMessage
*/
private void getTenantPreference(Request actorMessage) {

ProjectLogger.log("TenantPreferenceManagementActor-createTenantPreference called");

String requestedBy = (String) actorMessage.getRequest().get(JsonKey.REQUESTED_BY);
String orgId = (String) actorMessage.getRequest().get(JsonKey.ORG_ID);

if(ProjectUtil.isStringNullOREmpty(orgId)){
// throw invalid ord id ,org id should not be null or empty .
throw new ProjectCommonException(ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}

Response finalResponse = new Response();

Response response1 = cassandraOperation.getRecordsByProperty(tenantPreferenceDbInfo.getKeySpace(), tenantPreferenceDbInfo.getTableName(), JsonKey.ORG_ID, orgId);
List<Map<String, Object>> list = (List<Map<String, Object>>) response1.get(JsonKey.RESPONSE);

if(list.isEmpty()){
// throw exception org does not exist...
throw new ProjectCommonException(ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}

finalResponse.getResult().put(JsonKey.TENANT_PREFERENCE , list);
sender().tell(finalResponse , self());

}

/**
* Method to update the Tenant preference on basis of id or (role and org id).
* @param actorMessage
*/
private void updateTenantPreference(Request actorMessage) {

ProjectLogger.log("TenantPreferenceManagementActor-createTenantPreference called");
List<Map<String, Object>> reqList = (List<Map<String, Object>>) actorMessage.getRequest().get(JsonKey.TENANT_PREFERENCE);

Response finalResponse = new Response();
String requestedBy = (String) actorMessage.getRequest().get(JsonKey.REQUESTED_BY);
String orgId = (String) actorMessage.getRequest().get(JsonKey.ORG_ID);

if(ProjectUtil.isStringNullOREmpty(orgId)){
// throw invalid ord id ,org id should not be null or empty .
throw new ProjectCommonException(ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
if(reqList.isEmpty()){
// no need to do anything throw exception invalid request data as list is empty
throw new ProjectCommonException(ResponseCode.invalidRequestData.getErrorCode(),
ResponseCode.invalidRequestData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
Response response1 = cassandraOperation.getRecordsByProperty(tenantPreferenceDbInfo.getKeySpace(), tenantPreferenceDbInfo.getTableName(), JsonKey.ORG_ID, orgId);
List<Map<String, Object>> list = (List<Map<String, Object>>) response1.get(JsonKey.RESPONSE);

for(Map<String , Object> map : reqList){

Map<String, Object> dbMap = null;
String id = (String) map.get(JsonKey.ID);

if(ProjectUtil.isStringNullOREmpty(id)){
String requestedRole =(String)map.get(JsonKey.ROLE);
if(ProjectUtil.isStringNullOREmpty(requestedRole)){
//throw exception either Id or role is mandatory .
throw new ProjectCommonException(ResponseCode.invalidRequestData.getErrorCode(),
ResponseCode.invalidRequestData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}

boolean flag = false;
for(Map<String , Object> m : list){
if(requestedRole.equals((String)m.get(JsonKey.ROLE)) && orgId.equals((String)m.get(JsonKey.ORG_ID))){
dbMap = m;
flag = true;
break;
}
}
if(!flag){
// means data not found
finalResponse.getResult().put(requestedRole , " This role does not exist");
}
}else{
boolean flag = false;
for(Map<String , Object> m : list){
if(id.equals((String)m.get(JsonKey.ID))){
dbMap = m;
flag = true;
break;
}
}
if(!flag){
// means data not found
finalResponse.getResult().put(id , " This id does not exist");
}
}

if(null != dbMap){
if(map.containsKey(JsonKey.ROLE)){
dbMap.put(JsonKey.ROLE , map.get(JsonKey.ROLE));
}
if(map.containsKey(JsonKey.DATA)){
dbMap.put(JsonKey.DATA , map.get(JsonKey.DATA));
}
cassandraOperation.updateRecord(tenantPreferenceDbInfo.getKeySpace(), tenantPreferenceDbInfo.getTableName(), dbMap);
finalResponse.put((String) dbMap.get(JsonKey.ID), JsonKey.SUCCESS);
}

}
sender().tell(finalResponse , self());

}

/**
* Method to create tenant preference on basis of role and org , if already exist it will not create new one .
* @param actorMessage
*/
private void createTenantPreference(Request actorMessage) {

ProjectLogger.log("TenantPreferenceManagementActor-createTenantPreference called");
List<Map<String, Object>> req = (List<Map<String, Object>>) actorMessage.getRequest().get(JsonKey.TENANT_PREFERENCE);

Response finalResponse = new Response();
String requestedBy = (String) actorMessage.getRequest().get(JsonKey.REQUESTED_BY);
String orgId = (String) actorMessage.getRequest().get(JsonKey.ORG_ID);

if(ProjectUtil.isStringNullOREmpty(orgId)){
// throw invalid ord id ,org id should not be null or empty .
throw new ProjectCommonException(ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
//check whether org exist or not
Response result = cassandraOperation.getRecordById(orgDbInfo.getKeySpace(),
orgDbInfo.getTableName(), orgId);
List<Map<String, Object>> orglist = (List<Map<String, Object>>) result.get(JsonKey.RESPONSE);
if(orglist.isEmpty()){
throw new ProjectCommonException(ResponseCode.invalidOrgId.getErrorCode(),
ResponseCode.invalidOrgId.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
if(req.isEmpty()){
// no need to do anything throw exception invalid request data as list is empty
throw new ProjectCommonException(ResponseCode.invalidRequestData.getErrorCode(),
ResponseCode.invalidRequestData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}

Response response1 = cassandraOperation.getRecordsByProperty(tenantPreferenceDbInfo.getKeySpace(), tenantPreferenceDbInfo.getTableName(), JsonKey.ORG_ID, orgId);
List<Map<String, Object>> list = (List<Map<String, Object>>) response1.get(JsonKey.RESPONSE);

for(Map<String , Object> map : req){
String role = (String) map.get(JsonKey.ROLE);

if(ProjectUtil.isStringNullOREmpty(orgId)||ProjectUtil.isStringNullOREmpty(role)){
finalResponse.getResult().put(role , "OrgId , role can not be null .");
}
// check whether already tenant pref has created for the given org and role if yes show error
boolean flag = true;
for(Map<String,Object> m : list){
if(role.equalsIgnoreCase((String) m.get(JsonKey.ROLE))){
finalResponse.getResult().put(role , "Tenant preference already exist for role "+role);
flag = false;
break;
}
}

if(flag) {
Map<String, Object> dbMap = new HashMap<String, Object>();
String id = ProjectUtil.getUniqueIdFromTimestamp(actorMessage.getEnv());
dbMap.put(JsonKey.ID, id);
dbMap.put(JsonKey.ORG_ID, orgId);
dbMap.put(JsonKey.ROLE, role);
dbMap.put(JsonKey.DATA, (String) map.get(JsonKey.DATA));
cassandraOperation.insertRecord(tenantPreferenceDbInfo.getKeySpace(),
tenantPreferenceDbInfo.getTableName(), dbMap);
finalResponse.getResult().put(role, JsonKey.SUCCESS);
}
}
sender().tell(finalResponse , self());
}

/**
* Methos to update the Terms and condition status of user , the status may be ACCEPTED or REJECTED .
* @param actorMessage
*/
private void updateTcStatusOfUser(Request actorMessage) {

ProjectLogger.log("TenantPreferenceManagementActor-updateTcStatusOfUser called");
Map<String, Object> reqBody = (Map<String, Object>) actorMessage.getRequest().get(JsonKey.TENANT_PREFERENCE);
String requestedBy = (String) actorMessage.getRequest().get(JsonKey.REQUESTED_BY);

Response response1=cassandraOperation.getRecordById(userDbInfo.getKeySpace(), userDbInfo.getTableName(), requestedBy);

String tcStatus = (String) reqBody.get(JsonKey.TERM_AND_CONDITION_STATUS);
Map<String , Object> userMap = ((List<Map<String, Object>>) response1.get(JsonKey.RESPONSE)).get(0);

Map<String , Object> dbMap = new HashMap<>();
dbMap.put(JsonKey.TERM_AND_CONDITION_STATUS , tcStatus);
dbMap.put(JsonKey.TC_UPDATED_DATE , format.format(new Date()));
dbMap.put(JsonKey.ID , userMap.get(JsonKey.ID));

//update cassandra
cassandraOperation.updateRecord(userDbInfo.getKeySpace(), userDbInfo.getTableName(), dbMap);
// update elastic search
dbMap.remove(JsonKey.ID);
ElasticSearchUtil
.updateData(ProjectUtil.EsIndex.sunbird.getIndexName(), EsType.user.getTypeName(), requestedBy , dbMap);
Response finalResponse = new Response();
finalResponse.getResult().put(JsonKey.RESPONSE, JsonKey.SUCCESS);
sender().tell(finalResponse , self());

}

}
Loading

0 comments on commit 442bb86

Please sign in to comment.