diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java index 41e4c1ba640ccfb371b3f0acb33f09285ddc86c5..f92ca1751930d3460606384e24199f030c4c1d6f 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/LocationBulkUploadBackGroundJobActor.java @@ -1,6 +1,8 @@ package org.sunbird.actor.bulkupload; import akka.actor.ActorRef; +import akka.pattern.Patterns; +import akka.util.Timeout; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @@ -8,28 +10,34 @@ import java.sql.Timestamp; import java.text.MessageFormat; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import javax.inject.Inject; import javax.inject.Named; + +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.sunbird.client.location.LocationClient; -import org.sunbird.client.location.impl.LocationClientImpl; import org.sunbird.exception.ProjectCommonException; import org.sunbird.exception.ResponseCode; import org.sunbird.keys.JsonKey; import org.sunbird.model.bulkupload.BulkUploadProcess; import org.sunbird.model.bulkupload.BulkUploadProcessTask; import org.sunbird.model.location.Location; -import org.sunbird.model.location.UpsertLocationRequest; +import org.sunbird.operations.LocationActorOperation; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.response.Response; +import org.sunbird.service.location.LocationService; +import org.sunbird.service.location.LocationServiceImpl; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.ProjectUtil; import org.sunbird.util.Util; +import scala.concurrent.Await; +import scala.concurrent.Future; +import scala.concurrent.duration.Duration; public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgroundJobActor { - private LocationClient locationClient = new LocationClientImpl(); - + private final LocationService locationService = new LocationServiceImpl(); @Inject @Named("location_actor") private ActorRef locationActor; @@ -71,9 +79,10 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou if (checkMandatoryFields(row, JsonKey.CODE)) { Location location = null; try { - location = - locationClient.getLocationByCode( - locationActor, (String) row.get(JsonKey.CODE), context); + List<Location> locationList = locationService.locationSearch(JsonKey.CODE, (String) row.get(JsonKey.CODE), context); + if (CollectionUtils.isNotEmpty(locationList)) { + location = locationList.get(0); + } } catch (Exception ex) { setTaskStatus(task, ProjectUtil.BulkProcessStatus.FAILED, ex.getMessage(), row, null); } @@ -135,8 +144,12 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou } ObjectMapper mapper = new ObjectMapper(); try { - locationClient.updateLocation( - locationActor, mapper.convertValue(row, UpsertLocationRequest.class), context); + Request request = new Request(); + request.getRequest().putAll(row); + request.setOperation(LocationActorOperation.UPDATE_LOCATION.getValue()); + logger.info(context, "callUpdateLocation "); + locationActor.tell(request, self()); + } catch (Exception ex) { logger.error( context, @@ -167,9 +180,13 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou ObjectMapper mapper = new ObjectMapper(); String locationId = ""; try { - locationId = - locationClient.createLocation( - locationActor, mapper.convertValue(row, UpsertLocationRequest.class), context); + request.setOperation(LocationActorOperation.CREATE_LOCATION.getValue()); + logger.info(context, "callCreateLocation "); + Object obj = actorCall(locationActor, request, context); + if (obj instanceof Response) { + Response response = (Response) obj; + locationId = (String) response.get(JsonKey.ID); + } } catch (Exception ex) { logger.error( context, @@ -208,6 +225,39 @@ public class LocationBulkUploadBackGroundJobActor extends BaseBulkUploadBackgrou } } + private Object actorCall(ActorRef actorRef, Request request, RequestContext context) { + Object obj = null; + try { + Timeout t = new Timeout(Duration.create(10, TimeUnit.SECONDS)); + Future<Object> future = Patterns.ask(actorRef, request, t); + obj = Await.result(future, t.duration()); + } catch (ProjectCommonException pce) { + throw pce; + } catch (Exception e) { + logger.error( + context, + "Unable to communicate with actor: Exception occurred with error message = " + + e.getMessage(), + e); + ProjectCommonException.throwServerErrorException( + ResponseCode.unableToCommunicateWithActor, + ResponseCode.unableToCommunicateWithActor.getErrorMessage()); + } + checkLocationResponseForException(obj); + return obj; + } + + private void checkLocationResponseForException(Object obj) { + if (obj instanceof ProjectCommonException) { + throw (ProjectCommonException) obj; + } else if (obj instanceof Exception) { + throw new ProjectCommonException( + ResponseCode.SERVER_ERROR.getErrorCode(), + ResponseCode.SERVER_ERROR.getErrorMessage(), + ResponseCode.SERVER_ERROR.getResponseCode()); + } + } + @Override public void preProcessResult(Map<String, Object> result) { // Do nothing diff --git a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java index 9975ecf0a01699b9e8bde9ddccd322f581b1d2e8..af30c7ebf15c0799f53ec50240575e30d3b5f461 100644 --- a/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java +++ b/service/src/main/java/org/sunbird/actor/bulkupload/OrgBulkUploadBackgroundJobActor.java @@ -12,9 +12,9 @@ import java.util.List; import java.util.Map; import javax.inject.Inject; import javax.inject.Named; + +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.sunbird.client.location.LocationClient; -import org.sunbird.client.location.impl.LocationClientImpl; import org.sunbird.client.org.OrganisationClient; import org.sunbird.client.org.impl.OrganisationClientImpl; import org.sunbird.client.systemsettings.SystemSettingClient; @@ -28,6 +28,8 @@ import org.sunbird.model.organisation.OrgTypeEnum; import org.sunbird.model.organisation.Organisation; import org.sunbird.request.Request; import org.sunbird.request.RequestContext; +import org.sunbird.service.location.LocationService; +import org.sunbird.service.location.LocationServiceImpl; import org.sunbird.telemetry.dto.TelemetryEnvKey; import org.sunbird.util.ProjectUtil; import org.sunbird.util.Util; @@ -36,6 +38,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob private OrganisationClient orgClient = new OrganisationClientImpl(); private SystemSettingClient systemSettingClient = new SystemSettingClientImpl(); + private final LocationService locationService = new LocationServiceImpl(); @Inject @Named("system_settings_actor") @@ -74,11 +77,10 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob private void processTasks( List<BulkUploadProcessTask> bulkUploadProcessTasks, RequestContext context) { Map<String, Location> locationCache = new HashMap<>(); - LocationClient locationClient = new LocationClientImpl(); for (BulkUploadProcessTask task : bulkUploadProcessTasks) { if (task.getStatus() != null && task.getStatus() != ProjectUtil.BulkProcessStatus.COMPLETED.getValue()) { - processOrg(task, locationClient, locationCache, locationActor, context); + processOrg(task, locationCache, context); task.setLastUpdatedOn(new Timestamp(System.currentTimeMillis())); task.setIterationId(task.getIterationId() + 1); } @@ -87,9 +89,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob private void processOrg( BulkUploadProcessTask task, - LocationClient locationClient, Map<String, Location> locationCache, - ActorRef locationActor, RequestContext context) { logger.info(context, "OrgBulkUploadBackgroundJobActor: processOrg called"); String data = task.getData(); @@ -135,7 +135,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob callUpdateOrg(organisation, task, locationCodes, context); } setLocationInformation( - task, locationClient, locationCache, locationActor, locationCodes, context); + task, locationCache, locationCodes, context); } catch (Exception e) { logger.error( context, @@ -147,9 +147,7 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob private void setLocationInformation( BulkUploadProcessTask task, - LocationClient locationClient, Map<String, Location> locationCache, - ActorRef locationActor, List<String> locationCodes, RequestContext context) throws IOException { @@ -160,9 +158,11 @@ public class OrgBulkUploadBackgroundJobActor extends BaseBulkUploadBackgroundJob if (locationCache.containsKey(locationCode)) { locationNames.add(locationCache.get(locationCode).getName()); } else { - Location location = - locationClient.getLocationByCode(locationActor, locationCode, context); - locationNames.add(location.getName()); + List<Location> locationList = locationService.locationSearch(JsonKey.CODE, locationCode, context); + if (CollectionUtils.isNotEmpty(locationList)) { + Location location = locationList.get(0); + locationNames.add(location.getName()); + } } } Map<String, Object> row = mapper.readValue(task.getSuccessResult(), Map.class); diff --git a/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java b/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java index 1736b10331b9976e60f8b26346086584bc31d47d..5810f333c23cee6764c91a721252b412b8ccc0f3 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java +++ b/service/src/test/java/org/sunbird/actor/user/UserManagementActorTestBase.java @@ -22,8 +22,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.cassandraimpl.CassandraOperationImpl; -import org.sunbird.client.location.LocationClient; -import org.sunbird.client.location.impl.LocationClientImpl; import org.sunbird.client.org.OrganisationClient; import org.sunbird.client.org.impl.OrganisationClientImpl; import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; @@ -71,7 +69,6 @@ import scala.concurrent.Promise; OrgService.class, UserUtil.class, Patterns.class, - LocationClientImpl.class, DataCacheHandler.class, ElasticSearchRestHighImpl.class, PipeToSupport.PipeableFuture.class, @@ -105,7 +102,6 @@ public abstract class UserManagementActorTestBase { public static CassandraOperationImpl cassandraOperation; public static ElasticSearchService esService; protected static OrganisationClient organisationClient; - public LocationClient locationClient; public static UserLookUpServiceImpl userLookupService; public static UserRoleServiceImpl userRoleService; @@ -147,15 +143,6 @@ public abstract class UserManagementActorTestBase { Mockito.any())) .thenReturn(new HashMap<>()); - PowerMockito.mockStatic(LocationClientImpl.class); - locationClient = mock(LocationClientImpl.class); - when(LocationClientImpl.getInstance()).thenReturn(locationClient); - when(locationClient.getLocationsByCodes(Mockito.any(), Mockito.anyList(), Mockito.any())) - .thenReturn(getLocationLists()); - when(locationClient.getRelatedLocationIds(Mockito.any(), Mockito.anyList(), Mockito.any())) - .thenReturn(getLocationIdLists()); - when(locationClient.getLocationByIds(Mockito.any(), Mockito.anyList(), Mockito.any())) - .thenReturn(getLocationLists()); PowerMockito.mockStatic(FormApiUtilHandler.class); PowerMockito.when(FormApiUtilHandler.getFormApiConfig(eq("locationCode1"), Mockito.any())) .thenReturn(getFormApiConfig()); diff --git a/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java index 91fd44e335672ef106d252aec11748f3db05558c..71c938e6590a40e88707349b19463a89c01244c9 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserProfileReadActorTest.java @@ -23,7 +23,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.sunbird.cassandraimpl.CassandraOperationImpl; -import org.sunbird.client.location.impl.LocationClientImpl; import org.sunbird.client.systemsettings.impl.SystemSettingClientImpl; import org.sunbird.common.ElasticSearchHelper; import org.sunbird.common.ElasticSearchRestHighImpl; @@ -69,7 +68,6 @@ import scala.concurrent.Promise; UserServiceImpl.class, OrgServiceImpl.class, UserUtil.class, - LocationClientImpl.class, DataCacheHandler.class, UserExternalIdentityService.class, UserExternalIdentityServiceImpl.class, diff --git a/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java b/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java index 9c105b41603df26c0d3061bff43894e4da66cb9b..9be5eea45552246228bee4a6e0fc03ce257ab4bc 100644 --- a/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java +++ b/service/src/test/java/org/sunbird/actor/user/UserUpdateActorTest.java @@ -320,8 +320,6 @@ public class UserUpdateActorTest extends UserManagementActorTestBase { when(Patterns.ask( Mockito.any(ActorRef.class), Mockito.any(Request.class), Mockito.any(Timeout.class))) .thenReturn(future); - when(locationClient.getLocationsByCodes(Mockito.any(), Mockito.anyList(), Mockito.any())) - .thenReturn(getLocationLists2()); boolean result = testScenario( getRequest(