Commit 4335aee8 by Kunj Gupta

Fix - In Order Service Screen, implement api for showing items in order.

parent 3fb83923
......@@ -19,6 +19,10 @@ public interface CatalogueCategoryApi {
Call<ResponseBody> getCatalogue(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysparmQuery,
@Query(Constants.URL_PARAM_SYSPRM_FIELDS) String sysParmFields,
@Query(Constants.URL_PARAM_SYSPRM_DISPLAY_VALUE) String sysParmDisplayValue);
// Get Catalogue Order API
@GET(Constants.URL_GET_CATALOGUE_ORDER)
Call<ResponseBody> getCatalogueOrder();
}
package com.vsoft.uoflservicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.CatalogueOrder;
import java.util.List;
/**
* @since 1.0
* @author Kunj on 14/9/16
*
*/
public interface GetCatalogueOrderApiListener {
void onDoneApiCall(List<CatalogueOrder> catalogueOrderList);
}
......@@ -8,8 +8,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.vsoft.uoflservicenow.api.RestClient;
import com.vsoft.uoflservicenow.api.interfaces.CatalogueCategoryApi;
import com.vsoft.uoflservicenow.db.models.Catalogue;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueApiListener;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueOrderApiListener;
import com.vsoft.uoflservicenow.db.models.Catalogue;
import com.vsoft.uoflservicenow.db.models.CatalogueOrder;
import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.Constants;
......@@ -21,6 +23,8 @@ import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import okhttp3.ResponseBody;
......@@ -94,13 +98,33 @@ public class CatalogueApiManager {
})
.create();
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++) {
JSONObject expenseJsonObject = catalogueJsonArray.getJSONObject(i);
Catalogue catalogue = gson.fromJson(expenseJsonObject.toString(), Catalogue.class);
catalogueList.add(catalogue);
}
listener.onDoneApiCall(catalogueList);
if(!catalogueList.isEmpty()) {
getCatalogueOrder(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);
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
} else {
listener.onDoneApiCall(new ArrayList<Catalogue>(0));
}
......@@ -125,4 +149,101 @@ public class CatalogueApiManager {
return SyncStatus.FAIL;
}
}
public static SyncStatus getCatalogueOrder(GetCatalogueOrderApiListener listener) {
CatalogueLog.d("CatalogueApiManager: getCatalogueOrder: ");
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD);
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 expenseJsonObject = catalogueOrderJsonArray.getJSONObject(i);
CatalogueOrder catalogueOrder = gson.fromJson(expenseJsonObject.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));
}
return SyncStatus.SUCCESS;
} else
return SyncStatus.FAIL;
} catch (JSONException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: onResponse: ", e);
return SyncStatus.FAIL;
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: onResponse: ", e);
return SyncStatus.FAIL;
}
} else {
return SyncStatus.FAIL;
}
} catch (IOException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: IOException: ", e);
return SyncStatus.FAIL;
} catch (NullPointerException e) {
CatalogueLog.e("CatalogueApiManager: getCatalogueOrder: NullPointerException: ", e);
return SyncStatus.FAIL;
}
}
}
\ No newline at end of file
package com.vsoft.uoflservicenow.db.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Kunj on 14/9/16.
*/
public class CatalogueOrder {
@SerializedName("id")
@Expose
private String id;
@SerializedName("order")
@Expose
private Integer order;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The order
*/
public Integer getOrder() {
return order;
}
/**
*
* @param order
* The order
*/
public void setOrder(Integer order) {
this.order = order;
}
}
......@@ -113,6 +113,7 @@ public class Constants {
public static final String RESPONSE_ERROR_OBJECT_NAME = "error";
public static final String RESPONSE_VARIABLE_SET_OBJECT_NAME = "variablesets";
public static final String RESPONSE_VARIABLES_OBJECT_NAME = "variables";
public static final String RESPONSE_CATEGORY_OBJECT_NAME = "Category";
/**
* Catalogue web services urls
......@@ -122,6 +123,7 @@ public class Constants {
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = API_PATH + "sc_category";
public static final String URL_GET_CATALOGUE_ORDER = DOMAIN + "api/uno33/uofl_mobile/catalohhome";
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = API_PATH + "sc_cat_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