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,7 +36,6 @@ import retrofit2.Retrofit; ...@@ -38,7 +36,6 @@ import retrofit2.Retrofit;
/** /**
* @author Kunj on 11/8/16. * @author Kunj on 11/8/16.
*
*/ */
public class CatalogueApiManager { public class CatalogueApiManager {
...@@ -46,7 +43,7 @@ public class CatalogueApiManager { ...@@ -46,7 +43,7 @@ public class CatalogueApiManager {
CatalogueLog.d("CatalogueApiManager: getCatalogues: "); CatalogueLog.d("CatalogueApiManager: getCatalogues: ");
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN); String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
if(accessToken.isEmpty()) { if (accessToken.isEmpty()) {
listener.onFailApiCall(); listener.onFailApiCall();
return; return;
} }
...@@ -61,7 +58,7 @@ public class CatalogueApiManager { ...@@ -61,7 +58,7 @@ public class CatalogueApiManager {
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME); JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) { if (error == null) {
JSONArray catalogueJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME); JSONArray catalogueJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if(catalogueJsonArray.length() > 0) { if (catalogueJsonArray.length() > 0) {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation() .excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(long.class, new JsonDeserializer<Long>() { .registerTypeAdapter(long.class, new JsonDeserializer<Long>() {
...@@ -103,34 +100,19 @@ public class CatalogueApiManager { ...@@ -103,34 +100,19 @@ public class CatalogueApiManager {
.create(); .create();
final List<Catalogue> catalogueList = new ArrayList<>(catalogueJsonArray.length()); final List<Catalogue> catalogueList = new ArrayList<>(catalogueJsonArray.length());
final List<Catalogue> finalCatalogueList = new ArrayList<>(catalogueJsonArray.length());
for (int i = 0; i < catalogueJsonArray.length(); i++) { for (int i = 0; i < catalogueJsonArray.length(); i++) {
JSONObject catalogueJsonObject = catalogueJsonArray.getJSONObject(i); JSONObject catalogueJsonObject = catalogueJsonArray.getJSONObject(i);
Catalogue catalogue = gson.fromJson(catalogueJsonObject.toString(), Catalogue.class); Catalogue catalogue = gson.fromJson(catalogueJsonObject.toString(), Catalogue.class);
catalogueList.add(catalogue); catalogueList.add(catalogue);
} }
if(!catalogueList.isEmpty()) { if (!catalogueList.isEmpty()) {
getCatalogueOrder(context, new GetCatalogueOrderApiListener() { Collections.sort(catalogueList, new Comparator<Catalogue>() {
@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 @Override
public void onFailApiCall() { public int compare(Catalogue lhs, Catalogue rhs) {
listener.onFailApiCall(); return (int) (lhs.getOrder() - rhs.getOrder());
} }
}); });
listener.onDoneApiCall(catalogueList);
} else { } else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0)); listener.onDoneApiCall(new ArrayList<Catalogue>(0));
} }
...@@ -148,11 +130,11 @@ public class CatalogueApiManager { ...@@ -148,11 +130,11 @@ public class CatalogueApiManager {
} }
} else { } else {
CatalogueLog.d("CatalogueApiManager: getCatalogues: response is not success"); CatalogueLog.d("CatalogueApiManager: getCatalogues: response is not success");
if(response.code() == 401) { if (response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token..."); Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: "+PrefManager.getSharedPref(context, PrefManager.PREFERENCE_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)); SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if(status == SyncStatus.SUCCESS) { if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same..."); CatalogueLog.d("refresh token success, retry same...");
getCatalogues(context, listener); getCatalogues(context, listener);
} else { } else {
...@@ -170,118 +152,4 @@ public class CatalogueApiManager { ...@@ -170,118 +152,4 @@ public class CatalogueApiManager {
listener.onFailApiCall(); 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*/
Collections.sort(catalogueOrderList, new Comparator<CatalogueOrder>() {
@Override
public int compare(CatalogueOrder lhs, CatalogueOrder rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
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();
}
}
} }
...@@ -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