Commit 9085c295 by Kunj

Combined two APIs in one in Catalogue Categories Screen.

parent 6aab7e0c
......@@ -21,7 +21,6 @@ public class AppConfig {
*/
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = "api/vsng2/app_apis/category";
public static final String URL_GET_CATALOGUE_ORDER = "api/vsng2/app_apis/catalohhome";
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = "api/vsng2/app_apis/catalog_item";
......
......@@ -17,10 +17,6 @@ public interface CatalogueCategoryApi {
// Get Catalogue API
@GET(Constants.URL_GET_CATALOGUE)
Call<ResponseBody> getCatalogue(@Query(Constants.URL_PARAM_SYS_ID) String sysId);
// Get Catalogue Order API
@GET(Constants.URL_GET_CATALOGUE_ORDER)
Call<ResponseBody> getCatalogueOrder();
}
......@@ -10,14 +10,12 @@ 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.CatalogueCategoryApi;
import com.vsoft.servicenow.api.listeners.get.GetCatalogueApiListener;
import com.vsoft.servicenow.api.listeners.get.GetCatalogueOrderApiListener;
import com.vsoft.servicenow.db.models.Catalogue;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.api.interfaces.CatalogueCategoryApi;
import com.vsoft.servicenow.db.models.CatalogueOrder;
import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONArray;
......@@ -38,250 +36,120 @@ import retrofit2.Retrofit;
/**
* @author Kunj on 11/8/16.
*
*/
public class CatalogueApiManager {
public static void getCatalogues(Context context, final GetCatalogueApiListener listener) {
CatalogueLog.d("CatalogueApiManager: getCatalogues: ");
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueCategoryApi.class).getCatalogue("e0d08b13c3330100c8b837659bba8fb4");
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) {
JSONArray catalogueJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if(catalogueJsonArray.length() > 0) {
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("CatalogueApiManager: getCatalogues: 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("CatalogueApiManager: getCatalogues: 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("CatalogueApiManager: getCatalogues: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
final List<Catalogue> catalogueList = new ArrayList<>(catalogueJsonArray.length());
final List<Catalogue> finalCatalogueList = new ArrayList<>(catalogueJsonArray.length());
for (int i = 0; i < catalogueJsonArray.length(); i++) {
JSONObject catalogueJsonObject = catalogueJsonArray.getJSONObject(i);
Catalogue catalogue = gson.fromJson(catalogueJsonObject.toString(), Catalogue.class);
catalogueList.add(catalogue);
}
if(!catalogueList.isEmpty()) {
getCatalogueOrder(context, new GetCatalogueOrderApiListener() {
@Override
public void onDoneApiCall(List<CatalogueOrder> catalogueOrderList) {
for (int i = 0; i<catalogueOrderList.size(); i++) {
CatalogueOrder catalogueOrder = catalogueOrderList.get(i);
for(int j = 0; j<catalogueList.size(); j++) {
Catalogue catalogue = catalogueList.get(j);
if (catalogue.getSysId().equals(catalogueOrder.getId())) {
finalCatalogueList.add(catalogue);
break;
}
}
}
listener.onDoneApiCall(finalCatalogueList);
}
@Override
public void onFailApiCall() {
listener.onFailApiCall();
}
});
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
} else
listener.onFailApiCall();
} catch (JSONException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: onResponse: ", e);
listener.onFailApiCall();
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: onResponse: ", e);
listener.onFailApiCall();
}
} else {
CatalogueLog.d("CatalogueApiManager: getCatalogues: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: "+PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getCatalogues(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
}
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: IOException: ", e);
listener.onFailApiCall();
} catch (NullPointerException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: NullPointerException: ", e);
listener.onFailApiCall();
}
}
public static void getCatalogueOrder(Context context, GetCatalogueOrderApiListener listener) {
CatalogueLog.d("CatalogueApiManager: getCatalogueOrder: ");
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueCategoryApi.class).getCatalogueOrder();
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) {
JSONObject resultJsonObject = jsonObject.getJSONObject(Constants.RESPONSE_RESULT_OBJECT_NAME);
JSONArray catalogueOrderJsonArray = resultJsonObject.getJSONArray(Constants.RESPONSE_CATEGORY_OBJECT_NAME);
if(catalogueOrderJsonArray.length() > 0) {
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("CatalogueApiManager: getCatalogueOrder: 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("CatalogueApiManager: getCatalogueOrder: 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("CatalogueApiManager: getCatalogueOrder: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
List<CatalogueOrder> catalogueOrderList = new ArrayList<>(catalogueOrderJsonArray.length());
for (int i = 0; i < catalogueOrderJsonArray.length(); i++) {
JSONObject orderJsonObject = catalogueOrderJsonArray.getJSONObject(i);
CatalogueOrder catalogueOrder = gson.fromJson(orderJsonObject.toString(), CatalogueOrder.class);
catalogueOrderList.add(catalogueOrder);
}
public static void getCatalogues(Context context, final GetCatalogueApiListener listener) {
CatalogueLog.d("CatalogueApiManager: getCatalogues: ");
/*Sort List of CatalogueOrder*/
Collections.sort(catalogueOrderList, new Comparator<CatalogueOrder>() {
@Override
public int compare(CatalogueOrder lhs, CatalogueOrder rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if (accessToken.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(CatalogueCategoryApi.class).getCatalogue("e0d08b13c3330100c8b837659bba8fb4");
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) {
JSONArray catalogueJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if (catalogueJsonArray.length() > 0) {
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("CatalogueApiManager: getCatalogues: 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("CatalogueApiManager: getCatalogues: 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("CatalogueApiManager: getCatalogues: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
listener.onDoneApiCall(catalogueOrderList);
} else {
listener.onDoneApiCall(new ArrayList<CatalogueOrder>(0));
}
} else
listener.onFailApiCall();
} catch (JSONException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: onResponse: ", e);
listener.onFailApiCall();
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: onResponse: ", e);
listener.onFailApiCall();
}
} else {
CatalogueLog.d("CatalogueApiManager: getCatalogueOrder: response is not success");
if(response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: "+PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getCatalogueOrder(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
}
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: IOException: ", e);
listener.onFailApiCall();
} catch (NullPointerException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: NullPointerException: ", e);
listener.onFailApiCall();
}
}
final List<Catalogue> catalogueList = new ArrayList<>(catalogueJsonArray.length());
for (int i = 0; i < catalogueJsonArray.length(); i++) {
JSONObject catalogueJsonObject = catalogueJsonArray.getJSONObject(i);
Catalogue catalogue = gson.fromJson(catalogueJsonObject.toString(), Catalogue.class);
catalogueList.add(catalogue);
}
if (!catalogueList.isEmpty()) {
Collections.sort(catalogueList, new Comparator<Catalogue>() {
@Override
public int compare(Catalogue lhs, Catalogue rhs) {
return (int) (lhs.getOrder() - rhs.getOrder());
}
});
listener.onDoneApiCall(catalogueList);
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
} else
listener.onFailApiCall();
} catch (JSONException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: onResponse: ", e);
listener.onFailApiCall();
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: onResponse: ", e);
listener.onFailApiCall();
}
} else {
CatalogueLog.d("CatalogueApiManager: getCatalogues: response is not success");
if (response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: " + PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
getCatalogues(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
}
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: IOException: ", e);
listener.onFailApiCall();
} catch (NullPointerException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogues: NullPointerException: ", e);
listener.onFailApiCall();
}
}
}
......@@ -20,6 +20,9 @@ public class Catalogue {
@SerializedName("homepage_image")
@Expose
private String icon;
@SerializedName("order")
@Expose
private long order;
/**
*
......@@ -93,17 +96,36 @@ public class Catalogue {
this.icon = icon;
}
/**
*
* @param order
* The order
*/
public void setOrder(long order) {
this.order = order;
}
/**
*
* @return
* The order
*/
public long getOrder() {
return order;
}
public static class Json {
public static final String URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE = "sc_catalog";
}
@Override
public String toString() {
return "Catalogue{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", sysId='" + sysId + '\'' +
", icon='" + icon + '\'' +
'}';
return "Catalogue{"
+ "title='" + title + '\''
+ ", description='" + description + '\''
+ ", sysId='" + sysId + '\''
+ ", icon='" + icon + '\''
+ ", order=" + order
+ '}';
}
}
......@@ -132,7 +132,6 @@ public class Constants {
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = DOMAIN + AppConfig.URL_GET_CATALOGUE;
public static final String URL_GET_CATALOGUE_ORDER = DOMAIN + AppConfig.URL_GET_CATALOGUE_ORDER;
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = DOMAIN + AppConfig.URL_GET_CATALOGUE_ITEM;
......
......@@ -20,8 +20,7 @@ public class AppConfig {
* Web services urls
*/
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = "api/uno33/uofl_mobile/category";
public static final String URL_GET_CATALOGUE_ORDER = "api/uno33/uofl_mobile/catalohhome";
public static final String URL_GET_CATALOGUE = "api/uno33/uofl_mobile/catalogue_screen";
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = "api/uno33/uofl_mobile/catalog_item";
......
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