Commit 93aa5d9e by Kunj Gupta

Worked on the code qualitiy for ChatServer API call.

parent b3fc35a5
package com.vsoft.servicenow; package com.vsoft.servicenow;
import android.app.Application;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
...@@ -10,11 +9,10 @@ import android.net.ConnectivityManager; ...@@ -10,11 +9,10 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.support.multidex.MultiDexApplication; import android.support.multidex.MultiDexApplication;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.crashlytics.android.Crashlytics; import com.crashlytics.android.Crashlytics;
import com.github.nkzawa.socketio.client.IO; import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket; import com.github.nkzawa.socketio.client.Socket;
...@@ -26,11 +24,6 @@ import com.vsoft.servicenow.service.SyncService; ...@@ -26,11 +24,6 @@ import com.vsoft.servicenow.service.SyncService;
import com.vsoft.servicenow.ui.LoginScreen; import com.vsoft.servicenow.ui.LoginScreen;
import com.vsoft.servicenow.utils.Constants; import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager; import com.vsoft.servicenow.utils.PrefManager;
import com.vsoft.servicenow.utils.Util;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import io.fabric.sdk.android.Fabric; import io.fabric.sdk.android.Fabric;
...@@ -93,8 +86,18 @@ public class CatalogueApplication extends MultiDexApplication { ...@@ -93,8 +86,18 @@ public class CatalogueApplication extends MultiDexApplication {
/*Database is created*/ /*Database is created*/
initializeDatabase(); initializeDatabase();
configureURL();
if(mSocket == null) {
Constants.CHAT_SERVER_URL = PrefManager.getSharedPref(mContext, PrefManager.PREFERENCE_CHAT_SERVER_URL);
if(!TextUtils.isEmpty(Constants.CHAT_SERVER_URL)) {
initializeSocket();
}
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(mNetworkChangeReceiver, intentFilter);
} }
@Override @Override
...@@ -140,38 +143,15 @@ public class CatalogueApplication extends MultiDexApplication { ...@@ -140,38 +143,15 @@ public class CatalogueApplication extends MultiDexApplication {
return netInfo != null && netInfo.isConnected(); return netInfo != null && netInfo.isConnected();
} }
private void initializeSocket() { public void initializeSocket() {
try { try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL); mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public Socket getSocket() { public Socket getSocket() {
return mSocket; return mSocket;
} }
private void configureURL(){
AQuery aq;
aq = new AQuery(this);
aq.ajax("http://ai.test.vsoftconsulting.com:3000/?env=test", JSONObject.class, this, "jsonCallback");
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status) {
//When JSON is not null
try {
if (json != null) {
AppConfig.CHAT_SERVER_URL_STAGING=json.getString("chatbot_url");
if(Util.isChatItemEnabled()) {
initializeSocket();
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mNetworkChangeReceiver = new NetworkChangeReceiver();
registerReceiver(mNetworkChangeReceiver, intentFilter);
}
} catch (JSONException e) {
Log.i("Exception e", e + "");
}
}
} }
package com.vsoft.servicenow.api.interfaces;
import com.vsoft.servicenow.utils.Constants;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* @since 1.0
* @author Kunj on 04/05/18.
*
*/
public interface ChatServerApi {
// Get chat server detail
@GET(Constants.URL_GET_API_CHAT_SERVER)
Call<ResponseBody> getChatServerUrl();
}
package com.vsoft.servicenow.api.listeners.get;
import com.vsoft.servicenow.api.pojos.ChatServerApiResponse;
/**
* @since 1.0
* @author Kunj on 04/05/2018
*
*/
public interface GetChatServerApiListener {
void onDoneApiCall(ChatServerApiResponse chatServerApiResponse);
void onFailApiCall();
}
package com.vsoft.servicenow.api.managers;
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.vsoft.servicenow.api.RestClient;
import com.vsoft.servicenow.api.interfaces.ChatServerApi;
import com.vsoft.servicenow.api.listeners.get.GetChatServerApiListener;
import com.vsoft.servicenow.api.pojos.ChatServerApiResponse;
import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.utils.CatalogueLog;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
/**
* Created by Kunj on 04/05/2018.
*/
public class ChatServerApiManager {
public static SyncStatus getChatServerUrl(GetChatServerApiListener listener) {
final Retrofit retrofit = RestClient.getInitializedRestAdapterWithOutAuthorizationHeader();
Call<ResponseBody> call = retrofit.create(ChatServerApi.class).getChatServerUrl();
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(long.class, new JsonDeserializer<Long>() {
@Override
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
long value = 0;
try {
value = json.getAsLong();
} catch (NumberFormatException e) {
CatalogueLog.d("ChatServerApiManager: getChatServerUrl: deserialize: long.class: NumberFormatException: ");
}
return value;
}
})
.registerTypeAdapter(int.class, new JsonDeserializer<Integer>() {
@Override
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
int value = 0;
try {
value = json.getAsInt();
} catch (NumberFormatException e) {
CatalogueLog.d("ChatServerApiManager: getChatServerUrl: deserialize: int.class: NumberFormatException: ");
}
return value;
}
})
.registerTypeAdapter(float.class, new JsonDeserializer<Float>() {
@Override
public Float deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
float value = 0;
try {
value = json.getAsFloat();
} catch (NumberFormatException e) {
CatalogueLog.e("ChatServerApiManager: getChatServerUrl: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
ChatServerApiResponse chatServerApiResponse = gson.fromJson(jsonObject.toString(), ChatServerApiResponse.class);
listener.onDoneApiCall(chatServerApiResponse);
return SyncStatus.SUCCESS;
} catch (JSONException e) {
CatalogueLog.e("ChatServerApiManager: getChatServerUrl: onResponse: ", e);
return SyncStatus.FAIL;
} catch (IOException e) {
CatalogueLog.e("ChatServerApiManager: getChatServerUrl: onResponse: ", e);
return SyncStatus.FAIL;
}
} else {
CatalogueLog.d("getChatServerUrl: getChatServerUrl: response is not success");
listener.onFailApiCall();
return SyncStatus.FAIL;
}
} catch (IOException e) {
CatalogueLog.e("ChatServerApiManager: getChatServerUrl: IOException: ", e);
return SyncStatus.FAIL;
} catch (NullPointerException e) {
CatalogueLog.e("ChatServerApiManager: getChatServerUrl: NullPointerException: ", e);
return SyncStatus.FAIL;
}
}
}
package com.vsoft.servicenow.api.pojos;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Kunj on 05/04/18.
*/
public class ChatServerApiResponse {
@SerializedName("sn_base_url")
@Expose
private String snBaseUrl;
@SerializedName("chatbot_url")
@Expose
private String chatbotUrl;
@SerializedName("env")
@Expose
private String env;
public String getSnBaseUrl() {
return snBaseUrl;
}
public void setSnBaseUrl(String snBaseUrl) {
this.snBaseUrl = snBaseUrl;
}
public String getChatbotUrl() {
return chatbotUrl;
}
public void setChatbotUrl(String chatbotUrl) {
this.chatbotUrl = chatbotUrl;
}
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
}
...@@ -4,14 +4,17 @@ import android.app.IntentService; ...@@ -4,14 +4,17 @@ import android.app.IntentService;
import android.content.Intent; import android.content.Intent;
import com.vsoft.servicenow.CatalogueApplication; import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.api.listeners.get.GetChatServerApiListener;
import com.vsoft.servicenow.api.listeners.post.PostAttachmentApiListener; import com.vsoft.servicenow.api.listeners.post.PostAttachmentApiListener;
import com.vsoft.servicenow.api.listeners.post.PostHRCaseAttachmentApiListener; import com.vsoft.servicenow.api.listeners.post.PostHRCaseAttachmentApiListener;
import com.vsoft.servicenow.api.listeners.post.PostHRCaseVariableFormApiListener; import com.vsoft.servicenow.api.listeners.post.PostHRCaseVariableFormApiListener;
import com.vsoft.servicenow.api.listeners.post.PostIncidentApiListener; import com.vsoft.servicenow.api.listeners.post.PostIncidentApiListener;
import com.vsoft.servicenow.api.listeners.post.PostVariableFormApiListener; import com.vsoft.servicenow.api.listeners.post.PostVariableFormApiListener;
import com.vsoft.servicenow.api.managers.CatalogueVariableApiManager; import com.vsoft.servicenow.api.managers.CatalogueVariableApiManager;
import com.vsoft.servicenow.api.managers.ChatServerApiManager;
import com.vsoft.servicenow.api.managers.HRCaseVariableApiManager; import com.vsoft.servicenow.api.managers.HRCaseVariableApiManager;
import com.vsoft.servicenow.api.managers.IncidentApiManager; import com.vsoft.servicenow.api.managers.IncidentApiManager;
import com.vsoft.servicenow.api.pojos.ChatServerApiResponse;
import com.vsoft.servicenow.api.pojos.HRCaseVariableSubmitApiResponse; import com.vsoft.servicenow.api.pojos.HRCaseVariableSubmitApiResponse;
import com.vsoft.servicenow.db.DBConstants; import com.vsoft.servicenow.db.DBConstants;
import com.vsoft.servicenow.db.managers.AttachmentManager; import com.vsoft.servicenow.db.managers.AttachmentManager;
...@@ -69,14 +72,47 @@ public class SyncService extends IntentService { ...@@ -69,14 +72,47 @@ public class SyncService extends IntentService {
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
CatalogueApplication mApplication = (CatalogueApplication) getApplication(); CatalogueApplication application = (CatalogueApplication) getApplication();
if (!mApplication.isNetConnected()) { if (!application.isNetConnected()) {
CatalogueLog.e("SyncService: onHandleIntent(): Not connected to net. Exit."); CatalogueLog.e("SyncService: onHandleIntent(): Not connected to net. Exit.");
return; return;
} }
if(Util.isChatItemEnabled()) {
if (application.getSocket() == null) {
getChatServerUrl();
}
}
startSync(); startSync();
} }
/**
* Get Chatserver Url.
**/
private SyncStatus getChatServerUrl() {
CatalogueLog.d("getChatServerUrl");
ChatServerApiManager.getChatServerUrl(new GetChatServerApiListener() {
@Override
public void onDoneApiCall(ChatServerApiResponse chatServerApiResponse) {
CatalogueLog.e("getChatServerUrl: result is SUCCESS");
syncStatus = SyncStatus.SUCCESS;
PrefManager.setSharedPref(SyncService.this, PrefManager.PREFERENCE_CHAT_SERVER_URL, chatServerApiResponse.getChatbotUrl());
Constants.CHAT_SERVER_URL = chatServerApiResponse.getChatbotUrl();
CatalogueApplication application = (CatalogueApplication) getApplication();
application.initializeSocket();
}
@Override
public void onFailApiCall() {
syncStatus = SyncStatus.FAIL;
CatalogueLog.e("getChatServerUrl: result is FAIL");
}
});
return syncStatus;
}
private void startSync() { private void startSync() {
CatalogueLog.d("---- START SyncService START ----"); CatalogueLog.d("---- START SyncService START ----");
SyncStatus result; SyncStatus result;
......
...@@ -150,6 +150,11 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -150,6 +150,11 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
CatalogueApplication app = (CatalogueApplication) getApplication(); CatalogueApplication app = (CatalogueApplication) getApplication();
mSocket = app.getSocket(); mSocket = app.getSocket();
if(mSocket == null) {
CatalogueLog.e("mSocket is null");
finish();
return;
}
mSocket.on(Socket.EVENT_CONNECT, onConnect); mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
......
...@@ -221,23 +221,25 @@ public class LoginScreen extends Activity { ...@@ -221,23 +221,25 @@ public class LoginScreen extends Activity {
String userId = mUserDetails.get(0).getUserId(); String userId = mUserDetails.get(0).getUserId();
String userEmailId = mUserDetails.get(0).getUserEmailId(); String userEmailId = mUserDetails.get(0).getUserEmailId();
/*Start Chat Local DB Part*/ if(Util.isChatItemEnabled()) {
//Here we'll save logged in user detail in local DB for chat history. /*Start Chat Local DB Part*/
ChatBotUser localChatBotUser = ChatBotUserManager.getChatBotUsersByUserSysId(sysid); //Here we'll save logged in user detail in local DB for chat history.
if(localChatBotUser == null) { ChatBotUser localChatBotUser = ChatBotUserManager.getChatBotUsersByUserSysId(sysid);
/*Clears all data from CHAT_BOT_HISTORY and CHAT_BOT_USER tables*/ if (localChatBotUser == null) {
ChatBotHistoryManager.deleteAllRows(); /*Clears all data from CHAT_BOT_HISTORY and CHAT_BOT_USER tables*/
ChatBotUserManager.deleteAllRows(); ChatBotHistoryManager.deleteAllRows();
ChatBotUserManager.deleteAllRows();
/*Save Logged in user in local db for chat screen*/
ChatBotUser chatBotUser = ChatBotUser.ChatBotUserBuilder.aChatBotUser() /*Save Logged in user in local db for chat screen*/
.setUserSysId(sysid) ChatBotUser chatBotUser = ChatBotUser.ChatBotUserBuilder.aChatBotUser()
.setName(firstName) .setUserSysId(sysid)
.build(); .setName(firstName)
ChatBotUserManager.save(chatBotUser); .build();
} else {//Update the name of user ChatBotUserManager.save(chatBotUser);
localChatBotUser.setName(firstName); } else {//Update the name of user
ChatBotUserManager.update(localChatBotUser); localChatBotUser.setName(firstName);
ChatBotUserManager.update(localChatBotUser);
}
} }
/*End Chat Local DB Part*/ /*End Chat Local DB Part*/
......
...@@ -199,21 +199,12 @@ public class Constants { ...@@ -199,21 +199,12 @@ public class Constants {
public static final String URL_GET_ALL_PENDING_APPROVALS = DOMAIN + "api/vsng2/uofl_mobile/manager_approval"; public static final String URL_GET_ALL_PENDING_APPROVALS = DOMAIN + "api/vsng2/uofl_mobile/manager_approval";
public static final String URL_PUT_PENDING_APPROVAL_STATE = API_PATH + "sysapproval_approver/{" + PendingApprovalsApi.SYS_ID_OF_APPROVAL_RECORD + "}"; public static final String URL_PUT_PENDING_APPROVAL_STATE = API_PATH + "sysapproval_approver/{" + PendingApprovalsApi.SYS_ID_OF_APPROVAL_RECORD + "}";
public static final String URL_GET_API_CHAT_SERVER = (BUILD_TYPE_RELEASE == BuildConfig.BUILD_TYPE_INT
? AppConfig.CHAT_SERVER_API_URL_RELEASE
: (BUILD_TYPE_DEBUG == BuildConfig.BUILD_TYPE_INT ? AppConfig.CHAT_SERVER_API_URL_DEBUG : AppConfig.CHAT_SERVER_API_URL_STAGING));
/** /**
* Chat Server URL * Chat Server URL
* */ * */
public static final String CHAT_SERVER_URL = (BUILD_TYPE_RELEASE == BuildConfig.BUILD_TYPE_INT public static String CHAT_SERVER_URL;
? AppConfig.CHAT_SERVER_URL_RELEASE
: (BUILD_TYPE_DEBUG == BuildConfig.BUILD_TYPE_INT ? AppConfig.CHAT_SERVER_URL_DEBUG : AppConfig.CHAT_SERVER_URL_STAGING));
/**
* Category API - Hardcode value.
* We are using category api for getting catalogue category and HR case category.
* For both, We're passing hardcode sysId.
*
* For both part, We're using same screens and same API calls also, But we need to make only one change.
* We need to pass different-2 sysid for getting different category.
**/
public static final String CATELOGUE_CATEGORY_SYS_ID = "e0d08b13c3330100c8b837659bba8fb4";
public static final String HR_CASE_CATEGORY_SYS_ID = "9a6a1d924f8286005e1a3d728110c75b";
} }
...@@ -15,6 +15,9 @@ public class PrefManager { ...@@ -15,6 +15,9 @@ public class PrefManager {
public static final String PREFERENCE_USER_ID = "user_id"; public static final String PREFERENCE_USER_ID = "user_id";
public static final String PREFERENCE_USER_EMAIL_ID = "user_email_id"; public static final String PREFERENCE_USER_EMAIL_ID = "user_email_id";
//Chat Server Url
public static final String PREFERENCE_CHAT_SERVER_URL = "chat_server_url";
/*Access Token */ /*Access Token */
public static final String PREFERENCE_ACCESS_TOKEN = "access_token"; public static final String PREFERENCE_ACCESS_TOKEN = "access_token";
public static final String PREFERENCE_REFRESH_TOKEN = "refresh_token"; public static final String PREFERENCE_REFRESH_TOKEN = "refresh_token";
......
...@@ -53,14 +53,10 @@ public class AppConfig { ...@@ -53,14 +53,10 @@ public class AppConfig {
public static final String URL_POST_HR_CASE_ITEM = "/api/sn_sc/servicecatalog/items/{sys_id}/submit_producer"; public static final String URL_POST_HR_CASE_ITEM = "/api/sn_sc/servicecatalog/items/{sys_id}/submit_producer";
/** /**
* Socket Chat server URLs - Urls given by Ravi * We will hit the below url to get the chat server url.
* develop - http://111.93.6.218:12911/
* test - http://111.93.6.218:12912/
* demo - http://111.93.6.218:12913/
* release - http://111.93.6.218:12914/
* */ * */
public static final String CHAT_SERVER_URL_RELEASE = "http://111.93.6.218:12914/"; public static final String CHAT_SERVER_API_URL_RELEASE = "http://ai.test.vsoftconsulting.com:3000/?env=release";
public static final String CHAT_SERVER_URL_DEBUG = "http://111.93.6.218:12911/"; public static final String CHAT_SERVER_API_URL_DEBUG = "http://ai.test.vsoftconsulting.com:3000/?env=dev";
public static String CHAT_SERVER_URL_STAGING = ""; public static final String CHAT_SERVER_API_URL_STAGING = "http://ai.test.vsoftconsulting.com:3000/?env=test";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment