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

Issue #SC-1193 read data from registry if es fails #45

Open
wants to merge 10 commits into
base: release-2.2.0-prime
Choose a base branch
from
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "sunbird-user-registry"]
path = sunbird-user-registry
url = https://github.com/project-sunbird/sunbird-user-registry
branch = release-2.2.0-prime
url = https://github.com/project-sunbird/sunbird-user-registry.git
branch = release-2.2.0-prime
1 change: 1 addition & 0 deletions sunbird-user-registry
Submodule sunbird-user-registry added at 924dc4
1 change: 0 additions & 1 deletion sunbird-user-registry/java
Submodule java deleted from 8e89e0
16 changes: 16 additions & 0 deletions user-org-actor/src/main/java/org/sunbird/ServiceImplType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sunbird;

public enum ServiceImplType {

USER("userService");

private String serviceType;

ServiceImplType(String serviceType) {
this.serviceType = serviceType;
}

public String getServiceType() {
return this.serviceType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sunbird.factory;

import org.sunbird.ServiceImplType;
import org.sunbird.user.service.UserServiceImpl;

/**
* This class is created for keeping all service's object creation.
*/
public class ServiceFactory {

public static Object getService(String serviceName) {
Object serviceObj = null;
if(serviceName.equals(ServiceImplType.USER.getServiceType())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

DO reverse check to avoid NPE.
serviceName.equals(ServiceImplType.USER.getServiceType()) to ServiceImplType.USER.getServiceType().equals(serviceName)

serviceObj = UserServiceImpl.getInstance();
}
return serviceObj;
}
}
65 changes: 42 additions & 23 deletions user-org-actor/src/main/java/org/sunbird/user/UserReadActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import org.sunbird.BaseActor;
import org.sunbird.DaoImplType;
import org.sunbird.ServiceImplType;
import org.sunbird.actor.core.ActorConfig;
import org.sunbird.actorOperation.UserActorOperations;
import org.sunbird.exception.BaseException;
import org.sunbird.factory.ServiceFactory;
import org.sunbird.request.Request;
import org.sunbird.response.Response;
import org.sunbird.user.dao.IUserDao;
import org.sunbird.user.dao.UserDaoFactory;
import org.sunbird.user.service.IUserService;
import org.sunbird.util.LoggerEnum;
import org.sunbird.util.ProjectLogger;
import org.sunbird.util.jsonkey.JsonKey;

/**
Expand All @@ -23,29 +28,43 @@
)
public class UserReadActor extends BaseActor {

IUserDao userESDao = (IUserDao) UserDaoFactory.getDaoImpl(DaoImplType.ES.getType());
private IUserService userService = null;
IUserDao userESDao = (IUserDao) UserDaoFactory.getDaoImpl(DaoImplType.ES.getType());

@Override
public void onReceive(Request request) throws Throwable {
if (UserActorOperations.READ_USER_BY_ID
.getOperation()
.equalsIgnoreCase(request.getOperation())) {
readUserById(request);
} else {
onReceiveUnsupportedMessage(this.getClass().getName());
}
}

@Override
public void onReceive(Request request) throws Throwable {
if (UserActorOperations.READ_USER_BY_ID
.getOperation()
.equalsIgnoreCase(request.getOperation())) {
readUserById(request);
} else {
onReceiveUnsupportedMessage(this.getClass().getName());
}
}

/**
* this method is used to read user from elastic search.
*
* @param request
* @throws BaseException
*/
public void readUserById(Request request) throws BaseException {
startTrace("readUserById");
Response response = userESDao.getUserById((String) request.getRequest().get(JsonKey.USER_ID));
endTrace("readUserById");
sender().tell(response, self());
}
/**
* this method is used to read user from elastic search.
*
* @param request
* @throws BaseException
*/
public void readUserById(Request request) throws BaseException {
startTrace("readUserById");
Response response = null;
try {
response = userESDao.getUserById((String) request.getRequest().get(JsonKey.USER_ID));
} catch (Exception e) {

Choose a reason for hiding this comment

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

Add logger in catch block to print sacktrace, to know what went wrong.

Copy link
Author

Choose a reason for hiding this comment

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

done

ProjectLogger.log(
"UserReadActor:readUserById: "
+ "Exception in getting the record from ES : "
+ e.getMessage(),
LoggerEnum.ERROR.name());
ProjectLogger.log("Exception occurred while reading user ES.", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Logger parretn need to be same for all places, we are following "className:methodName message,LoggerEum.Value.name"

Copy link
Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

remove ProjectLogger.log("Exception occurred while reading user ES.", e); because we already have logger above.

userService = (IUserService) ServiceFactory.getService(ServiceImplType.USER.getServiceType());
response = userService.readUser((String) request.getRequest().get(JsonKey.USER_ID));
}
endTrace("readUserById");
sender().tell(response, self());
}
}
94 changes: 47 additions & 47 deletions user-org-actor/src/main/java/org/sunbird/user/dao/IOSDao.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
package org.sunbird.user.dao;

import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import org.sunbird.response.Response;

/** This class contains method to interact with open saber */
import java.util.Map;

/**
* This class contains method to interact with open saber
*/
public interface IOSDao {

/**
* This method will add entity to open saber
*
* @param entity
* @return Response
* @throws Exception
*/
Response addEntity(Map<String, Object> entity) throws Exception;

/**
* This method will read data from open saber and return response in JsonLD
*
* @param entity
* @return Response
* @throws Exception
*/
Response readJSONLDEntity(Map<String, Object> entity) throws Exception;

/**
* This method will read data from open saber
*
* @param entity
* @return response
* @throws Exception
*/
Response readEntity(Map<String, Object> entity) throws Exception;

/**
* This method will search entity from open saber
*
* @param entity
* @return response
* @throws Exception
*/
Response searchEntity(Map<String, Object> entity) throws Exception;

/**
* This method will update the existing entity in open saber.
*
* @param entity
* @param entityId
* @return Response
* @throws Exception
*/
Response updateEntity(Map<String, Object> entity, String entityId) throws Exception;
/**
* This method will add entity to open saber
* @param entity
* @return Response
* @throws Exception
*/
Response addEntity(Map<String,Object> entity) throws Exception;

/**
* This method will read data from open saber and return response in JsonLD
* @param entity
* @return Response
* @throws Exception
*/
Response readJSONLDEntity(Map<String,Object> entity) throws Exception;

/**
* This method will read data from open saber
* @param entity
* @return response
* @throws Exception
*/
Response readEntity(JsonNode entity) throws Exception;

/**
* This method will search entity from open saber
* @param entity
* @return response
* @throws Exception
*/
Response searchEntity(Map<String,Object> entity) throws Exception;

/**
* This method will update the existing entity in open saber.
* @param entity
* @param entityId
* @return Response
* @throws Exception
*/
Response updateEntity(Map<String,Object> entity, String entityId) throws Exception;

}
107 changes: 56 additions & 51 deletions user-org-actor/src/main/java/org/sunbird/user/dao/OSDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,60 @@
import org.sunbird.response.Response;
import org.sunbird.util.jsonkey.JsonKey;

/** This class will contains method to interact with open saber. */
public class OSDaoImpl implements IOSDao {

private Localizer localizer = Localizer.getInstance();
private RegistryHelper registryHelper;
private ObjectMapper objectMapper;

private OSDaoImpl() {
objectMapper = new ObjectMapper();
registryHelper = Application.applicationContext.getBean(RegistryHelper.class);
}

/**
* this method should be used to get the instance of the class
*
* @return class object
*/
public static OSDaoImpl getInstance() {
return new OSDaoImpl();
}

@Override
public Response addEntity(Map<String, Object> entity) throws Exception {
Response response = new Response();
JsonNode userJsonNode = objectMapper.convertValue(entity, JsonNode.class);
String entityId = registryHelper.addEntity(userJsonNode, "");
Map<String, Object> entityMap = objectMapper.convertValue(userJsonNode, Map.class);
entityMap.put(JsonKey.ID, entityId);
response.putAll(entityMap);
return response;
}

@Override
public Response readJSONLDEntity(Map<String, Object> entity) throws Exception {
return null;
}

@Override
public Response readEntity(Map<String, Object> entity) throws Exception {
return null;
}

@Override
public Response searchEntity(Map<String, Object> entity) throws Exception {
return null;
}

@Override
public Response updateEntity(Map<String, Object> entity, String entityId) throws Exception {
return null;
}
/**
* This class will contains method to interact with open saber.
*/
public class OSDaoImpl implements IOSDao{

private Localizer localizer = Localizer.getInstance();
private RegistryHelper registryHelper;
private ObjectMapper objectMapper;

private OSDaoImpl() {
objectMapper = new ObjectMapper();
registryHelper = Application.applicationContext.getBean(RegistryHelper.class);
}

/**
* this method should be used to get the instance of the class
* @return class object
*/
public static OSDaoImpl getInstance() {
return new OSDaoImpl();
}

@Override
public Response addEntity(Map<String, Object> entity) throws Exception {
Response response = new Response();
JsonNode userJsonNode = objectMapper.convertValue(entity, JsonNode.class);
String entityId = registryHelper.addEntity(userJsonNode, "");
Map<String,Object> entityMap = objectMapper.convertValue(userJsonNode, Map.class);
entityMap.put(JsonKey.ID,entityId);
response.putAll(entityMap);
return response;
}

@Override
public Response readEntity(JsonNode inputNode) throws Exception {
Response response = new Response();
JsonNode responseNode = registryHelper.readEntity(inputNode,"");
response.putAll(objectMapper.convertValue(responseNode,Map.class));
return response;
}

@Override
public Response readJSONLDEntity(Map<String, Object> entity) throws Exception {
return null;
}

@Override
public Response searchEntity(Map<String, Object> entity) throws Exception {
return null;
}

@Override
public Response updateEntity(Map<String, Object> entity, String entityId) throws Exception {
return null;
}

}
Loading