Commit 3fb83923 by Kunj Gupta

UOFLMA-68: Implement API changed response.

parent 3fe9ddce
package com.vsoft.uoflservicenow.api.listeners.get;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.db.models.CatalogueVariableSet;
import java.util.List;
......@@ -10,5 +11,5 @@ import java.util.List;
*
*/
public interface GetCatalogueVariableApiListener {
void onDoneApiCall(List<CatalogueVariable> catalogueVariableList);
void onDoneApiCall(List<CatalogueVariableSet> variableSetList, List<CatalogueVariable> variableList);
}
......@@ -10,6 +10,7 @@ import com.vsoft.uoflservicenow.api.RestClient;
import com.vsoft.uoflservicenow.api.interfaces.CatalogueVariableApi;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueVariableApiListener;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.db.models.CatalogueVariableSet;
import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.Constants;
......@@ -21,6 +22,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;
......@@ -46,8 +49,8 @@ public class CatalogueVariableApiManager {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject error = jsonObject.optJSONObject(Constants.RESPONSE_ERROR_OBJECT_NAME);
if (error == null) {
JSONArray catalogueVariableJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if(catalogueVariableJsonArray.length() > 0) {
JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if(jsonArray.length() > 0) {
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(long.class, new JsonDeserializer<Long>() {
......@@ -88,16 +91,58 @@ public class CatalogueVariableApiManager {
})
.create();
List<CatalogueVariable> catalogueVariableList = new ArrayList<>(catalogueVariableJsonArray.length());
for (int i = 0; i < catalogueVariableJsonArray.length(); i++) {
JSONObject variableJsonObject = catalogueVariableJsonArray.getJSONObject(i);
JSONObject catalogueJsonObject = jsonArray.getJSONObject(0);
JSONArray variableSetJsonArray = catalogueJsonObject.getJSONArray(Constants.RESPONSE_VARIABLE_SET_OBJECT_NAME);
List<CatalogueVariableSet> variableSetList = new ArrayList<>(variableSetJsonArray.length());
for (int i = 0; i < variableSetJsonArray.length(); i++) {
JSONObject variableSetJsonObject = variableSetJsonArray.getJSONObject(i);
CatalogueVariableSet catalogueVariableSet = gson.fromJson(variableSetJsonObject.toString(), CatalogueVariableSet.class);
JSONArray variableJsonArray = variableSetJsonObject.getJSONArray(Constants.RESPONSE_VARIABLES_OBJECT_NAME);
List<CatalogueVariable> variableList = new ArrayList<>(variableJsonArray.length());
for (int j = 0; j < variableJsonArray.length(); j++) {
JSONObject variableJsonObject = variableJsonArray.getJSONObject(j);
CatalogueVariable catalogueVariable = gson.fromJson(variableJsonObject.toString(), CatalogueVariable.class);
catalogueVariable.parseJson(variableJsonObject);
variableList.add(catalogueVariable);
}
Collections.sort(variableList, new Comparator<CatalogueVariable>() {
@Override
public int compare(CatalogueVariable lhs, CatalogueVariable rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
catalogueVariableSet.setVariables(variableList);
variableSetList.add(catalogueVariableSet);
}
Collections.sort(variableSetList, new Comparator<CatalogueVariableSet>() {
@Override
public int compare(CatalogueVariableSet lhs, CatalogueVariableSet rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
JSONArray variableJsonArray = catalogueJsonObject.getJSONArray(Constants.RESPONSE_VARIABLES_OBJECT_NAME);
List<CatalogueVariable> variableList = new ArrayList<>(variableJsonArray.length());
for (int i = 0; i < variableJsonArray.length(); i++) {
JSONObject variableJsonObject = variableJsonArray.getJSONObject(i);
CatalogueVariable catalogueVariable = gson.fromJson(variableJsonObject.toString(), CatalogueVariable.class);
catalogueVariable.parseJson(variableJsonObject);
catalogueVariableList.add(catalogueVariable);
variableList.add(catalogueVariable);
}
listener.onDoneApiCall(catalogueVariableList);
Collections.sort(variableList, new Comparator<CatalogueVariable>() {
@Override
public int compare(CatalogueVariable lhs, CatalogueVariable rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
listener.onDoneApiCall(variableSetList, variableList);
} else {
listener.onDoneApiCall(new ArrayList<CatalogueVariable>(0));
listener.onDoneApiCall(new ArrayList<CatalogueVariableSet>(0), new ArrayList<CatalogueVariable>(0));
}
return SyncStatus.SUCCESS;
} else
......
package com.vsoft.uoflservicenow.db.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Kunj on 12/8/16.
*/
public class CatalogueVariableSet {
@SerializedName("order")
@Expose
private int order;
@SerializedName("show_on_true")
@Expose
private Object showOnTrue;
@SerializedName("variables")
@Expose
private List<CatalogueVariable> variables = new ArrayList<CatalogueVariable>();
/**
*
* @return
* The order
*/
public int getOrder() {
return order;
}
/**
*
* @param order
* The order
*/
public void setOrder(int order) {
this.order = order;
}
/**
*
* @return
* The showOnTrue
*/
public Object getShowOnTrue() {
return showOnTrue;
}
/**
*
* @param showOnTrue
* The show_on_true
*/
public void setShowOnTrue(Object showOnTrue) {
this.showOnTrue = showOnTrue;
}
/**
*
* @return
* The variables
*/
public List<CatalogueVariable> getVariables() {
return variables;
}
/**
*
* @param variables
* The variables
*/
public void setVariables(List<CatalogueVariable> variables) {
this.variables = variables;
}
}
......@@ -35,6 +35,7 @@ import com.vsoft.uoflservicenow.api.listeners.get.GetVariableChoiceApiListener;
import com.vsoft.uoflservicenow.api.managers.CatalogueVariableApiManager;
import com.vsoft.uoflservicenow.api.managers.VariableChoiceApiManager;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.db.models.CatalogueVariableSet;
import com.vsoft.uoflservicenow.db.models.Reference;
import com.vsoft.uoflservicenow.db.models.VariableChoice;
import com.vsoft.uoflservicenow.dialog.SelectReferenceDialog;
......@@ -53,8 +54,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import butterknife.BindView;
......@@ -72,7 +71,7 @@ public class CatalogueVariableScreen extends AppCompatActivity {
@BindView(R.id.variable_screen_container_layout) LinearLayout mContainerLayout;
@BindView(R.id.variable_screen_bottom_layout) RelativeLayout mBottomLayout;
private List<CatalogueVariable> mCatalogueVariableList;
private List<CatalogueVariable> mCatalogueVariableList = new ArrayList<>();
private JSONArray mJsonArray;
private CatalogueApplication mApplication;
private String mCatalogueItemSysId, mCatalogueItemDescription, mCatalogueItemShortDescription, mCatalogueItemTitle;
......@@ -136,34 +135,17 @@ public class CatalogueVariableScreen extends AppCompatActivity {
protected SyncStatus doInBackground(String... params) {
syncStatus = CatalogueVariableApiManager.getCatalogueVariable(mCatalogueItemSysId, new GetCatalogueVariableApiListener() {
@Override
public void onDoneApiCall(List<CatalogueVariable> catalogueVariableList) {
CatalogueLog.e("Data: catalogueVariableList: " + catalogueVariableList);
Collections.sort(catalogueVariableList, new Comparator<CatalogueVariable>() {
@Override
public int compare(CatalogueVariable lhs, CatalogueVariable rhs) {
return (lhs.getOrder() - rhs.getOrder());
}
});
public void onDoneApiCall(List<CatalogueVariableSet> variableSetList, List<CatalogueVariable> variableList) {
/*For variableset */
for (int i = 0; i <variableSetList.size(); i++) {
CatalogueVariableSet catalogueVariableSet = variableSetList.get(i);
setVariableChoiceData(catalogueVariableSet.getVariables());
}
mCatalogueVariableList = catalogueVariableList;
/*For variable list*/
setVariableChoiceData(variableList);
if(!catalogueVariableList.isEmpty()) {
for (int i = 0; i < catalogueVariableList.size(); i++) {
final CatalogueVariable catalogueVariable = catalogueVariableList.get(i);
if (catalogueVariable.getType() == ViewType.MULTIPLE_CHOICE
|| catalogueVariable.getType() == ViewType.SELECT_BOX) {
/*Fetch multi choice variable values*/
syncStatus = VariableChoiceApiManager.getVariableChoice(catalogueVariable.getSysId(), new GetVariableChoiceApiListener() {
@Override
public void onDoneApiCall(List<VariableChoice> variableChoiceList) {
CatalogueLog.e("Data: variableChoiceList: " + variableChoiceList);
catalogueVariable.setVariableChoiceList(variableChoiceList);
}
});
}
}
}
}
});
return syncStatus;
......@@ -185,11 +167,31 @@ public class CatalogueVariableScreen extends AppCompatActivity {
}
}
private void createView() {
getCustomLayout();
private void setVariableChoiceData(List<CatalogueVariable> variableList) {
if(!variableList.isEmpty()) {
for (int i = 0; i < variableList.size(); i++) {
final CatalogueVariable catalogueVariable = variableList.get(i);
if (catalogueVariable.getType() == ViewType.MULTIPLE_CHOICE
|| catalogueVariable.getType() == ViewType.SELECT_BOX) {
/*Fetch multi choice variable values*/
VariableChoiceApiManager.getVariableChoice(catalogueVariable.getSysId(), new GetVariableChoiceApiListener() {
@Override
public void onDoneApiCall(List<VariableChoice> variableChoiceList) {
CatalogueLog.e("Data: variableChoiceList: " + variableChoiceList);
catalogueVariable.setVariableChoiceList(variableChoiceList);
}
});
}
}
mCatalogueVariableList.addAll(variableList);
}
}
private void createView() {
if(!mCatalogueVariableList.isEmpty()) {
mBottomLayout.setVisibility(View.VISIBLE);
/*For variable list */
getCustomLayout();
} else {
mBottomLayout.setVisibility(View.GONE);
}
......@@ -211,7 +213,7 @@ public class CatalogueVariableScreen extends AppCompatActivity {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
shortDescriptionView.setText(Html.fromHtml(mCatalogueItemShortDescription, Html.FROM_HTML_MODE_LEGACY));
} else {
shortDescriptionView.setText(Html.fromHtml(mCatalogueItemShortDescription));
shortDescriptionView.setText(Html.fromHtml(mCatalogueItemShortDescription));
}
mContainerLayout.addView(shortDescriptionView, childControlViewLayoutParams);
......
......@@ -8,13 +8,10 @@ import com.vsoft.uoflservicenow.BuildConfig;
*/
public class Constants {
public static final String GRANT_TYPE = "password";
public static final String CLIENT_ID = "ac0dd3408c1031006907010c2cc6ef6d";
public static final String CLIENT_SECRET = "oklj6znxv3o9jmyn2mlp";
public static final String TAG = "UofLCatalogue";
public static final String[] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
......@@ -114,6 +111,8 @@ public class Constants {
**/
public static final String RESPONSE_RESULT_OBJECT_NAME = "result";
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";
/**
* Catalogue web services urls
......
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