Commit 9085c295 by Kunj

Combined two APIs in one in Catalogue Categories Screen.

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