Commit 5e6f6060 by Kunj Gupta

UOFLMA-39: Incident GET API is integrated.

parent d46d1d42
package com.vsoft.uofl_catalogue.api.interfaces;
import com.vsoft.uofl_catalogue.utils.Constants;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* @since 1.0
* @author Kunj on 31/8/16.
*
*/
public interface IncidentApi {
// Get Incident API
@GET(Constants.URL_GET_INCIDENTS)
Call<ResponseBody> getIncident(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysParmQuery,
@Query(Constants.URL_PARAM_SYSPRM_LIMIT) String sysParmLimits);
}
package com.vsoft.uofl_catalogue.api.listeners.get;
import com.vsoft.uofl_catalogue.db.models.Incident;
import java.util.List;
/**
* @since 1.0
* @author Kunj on 11/8/16
*
*/
public interface GetIncidentApiListener {
void onDoneApiCall(List<Incident> incidentList);
}
package com.vsoft.uofl_catalogue.api.managers;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.vsoft.uofl_catalogue.api.RestClient;
import com.vsoft.uofl_catalogue.api.interfaces.IncidentApi;
import com.vsoft.uofl_catalogue.api.listeners.get.GetIncidentApiListener;
import com.vsoft.uofl_catalogue.db.models.Incident;
import com.vsoft.uofl_catalogue.enums.SyncStatus;
import com.vsoft.uofl_catalogue.utils.CatalogueLog;
import com.vsoft.uofl_catalogue.utils.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
/**
* @author Kunj on 31/8/16.
*
*/
public class IncidentApiManager {
public static SyncStatus getIncident(GetIncidentApiListener listener) {
CatalogueLog.d("IncidentApiManager: getIncident: ");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("caller_id=javascript:gs.getUserID()");
CatalogueLog.d("IncidentApiManager: getIncident: request parameter: "+stringBuilder.toString());
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD);
Call<ResponseBody> call = retrofit.create(IncidentApi.class).getIncident(stringBuilder.toString(), "");
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 incidentJsonArray = jsonObject.getJSONArray(Constants.RESPONSE_RESULT_OBJECT_NAME);
if(incidentJsonArray.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("IncidentApiManager: getIncident: 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("IncidentApiManager: getIncident: 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("IncidentApiManager: getIncident: deserialize: float.class: NumberFormatException: ", e);
}
return value;
}
})
.create();
List<Incident> incidentList = new ArrayList<>(incidentJsonArray.length());
for (int i = 0; i < incidentJsonArray.length(); i++) {
JSONObject expenseJsonObject = incidentJsonArray.getJSONObject(i);
Incident incident = gson.fromJson(expenseJsonObject.toString(), Incident.class);
incident.parseJson(expenseJsonObject);
incidentList.add(incident);
}
listener.onDoneApiCall(incidentList);
} else {
listener.onDoneApiCall(new ArrayList<Incident>(0));
}
return SyncStatus.SUCCESS;
} else
return SyncStatus.FAIL;
} catch (JSONException e) {
CatalogueLog.e("IncidentApiManager: getIncident: onResponse: ", e);
return SyncStatus.FAIL;
} catch (IOException e) {
CatalogueLog.e("IncidentApiManager: getIncident: onResponse: ", e);
return SyncStatus.FAIL;
}
} else {
return SyncStatus.FAIL;
}
} catch (IOException e) {
CatalogueLog.e("IncidentApiManager: getIncident: IOException: ", e);
return SyncStatus.FAIL;
} catch (NullPointerException e) {
CatalogueLog.e("IncidentApiManager: getIncident: NullPointerException: ", e);
return SyncStatus.FAIL;
}
}
}
\ No newline at end of file
......@@ -3,6 +3,10 @@ package com.vsoft.uofl_catalogue.db.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.vsoft.uofl_catalogue.enums.Impact;
import com.vsoft.uofl_catalogue.utils.Util;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by Kunj on 11/8/16.
......@@ -54,6 +58,19 @@ public class Incident {
this.number = number;
}
public void parseJson(JSONObject jsonObject) {
int impact = -1;
String openedAt = null;
try {
openedAt = jsonObject.getString(Json.OPENED_AT);
impact = jsonObject.getInt(Json.IMPACT);
} catch (JSONException e) {
e.printStackTrace();
}
this.setOpenedAt(Util.getDateTime(openedAt));
this.setImpact(Impact.from(impact));
}
public static final class IncidentBuilder {
private String number;
......@@ -102,6 +119,11 @@ public class Incident {
}
}
public static class Json {
public static final String IMPACT = "impact";
public static final String OPENED_AT = "opened_at";
}
@Override
public String toString() {
return "Incident{" +
......
......@@ -6,6 +6,7 @@ package com.vsoft.uofl_catalogue.enums;
*
*/
public enum Impact {
UNKNOWN (-1),
HIGH (1),
MEDIUM (2),
LOW (3);
......@@ -23,4 +24,13 @@ public enum Impact {
public int getId() {
return this.id;
}
public static Impact from(int id) {
for(int i = 0; i< Impact.values().length; i++) {
Impact impact = Impact.values()[i];
if(impact.id == id)
return Impact.values()[i];
}
return UNKNOWN;
}
}
package com.vsoft.uofl_catalogue.ui;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
......@@ -12,9 +14,12 @@ import android.widget.ListView;
import com.vsoft.uofl_catalogue.CatalogueApplication;
import com.vsoft.uofl_catalogue.R;
import com.vsoft.uofl_catalogue.adapters.MyIncidentsAdapter;
import com.vsoft.uofl_catalogue.api.listeners.get.GetIncidentApiListener;
import com.vsoft.uofl_catalogue.api.managers.IncidentApiManager;
import com.vsoft.uofl_catalogue.db.models.Incident;
import com.vsoft.uofl_catalogue.enums.SyncStatus;
import com.vsoft.uofl_catalogue.utils.CatalogueLog;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
......@@ -27,7 +32,7 @@ import butterknife.OnItemClick;
public class MyIncidentScreen extends AppCompatActivity {
@BindView(R.id.tool_bar_view) Toolbar mToolbar;
@BindView(R.id.my_incidents_screen_list_view) ListView mIncidentListView;
@BindView(R.id.my_incidents_screen_list_view) ListView mListView;
private CatalogueApplication mApplication;
private List<Incident> mIncidentList;
......@@ -52,17 +57,11 @@ public class MyIncidentScreen extends AppCompatActivity {
actionBar.setDisplayShowTitleEnabled(true);
}
MyIncidentsAdapter adapter = new MyIncidentsAdapter(MyIncidentScreen.this);
mIncidentList = new ArrayList<>(5);
for (int i = 0; i < 5 ; i++) {
Incident incident = new Incident();
incident.setNumber("INC000010");
incident.setShortDescription("short description");
incident.setOpenedAt("23 Aug 2016, 15.30PM");
mIncidentList.add(incident);
if(mApplication.isNetConnected()) {
new FetchIncident().execute();
} else {
showErrorDialog(R.string.internet_validation_string);
}
adapter.setIncidentList(mIncidentList);
mIncidentListView.setAdapter(adapter);
}
@OnItemClick(R.id.my_incidents_screen_list_view)
......@@ -90,6 +89,52 @@ public class MyIncidentScreen extends AppCompatActivity {
alert.show();
}
class FetchIncident extends AsyncTask<String, Void, SyncStatus> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MyIncidentScreen.this);
progressDialog.setMessage(getString(R.string.loading_string));
progressDialog.show();
progressDialog.setCancelable(false);
}
@Override
protected SyncStatus doInBackground(String... params) {
SyncStatus syncStatus = IncidentApiManager.getIncident(new GetIncidentApiListener() {
@Override
public void onDoneApiCall(List<Incident> incidentList) {
CatalogueLog.e("Data: incidentList: "+incidentList);
mIncidentList = incidentList;
}
});
return syncStatus;
}
@Override
protected void onPostExecute(SyncStatus syncStatus) {
super.onPostExecute(syncStatus);
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if(syncStatus == SyncStatus.SUCCESS) {
if(mIncidentList!=null)
setData(mIncidentList);
} else {
showErrorDialog(R.string.failed_to_fetch_incident_string);
}
}
}
private void setData(final List<Incident> catalogueList) {
MyIncidentsAdapter adapter = new MyIncidentsAdapter(MyIncidentScreen.this);
adapter.setIncidentList(catalogueList);
mListView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
......@@ -97,4 +142,17 @@ public class MyIncidentScreen extends AppCompatActivity {
}
return super.onOptionsItemSelected(menuItem);
}
private void showErrorDialog(int message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
......@@ -25,6 +25,7 @@ public class Constants {
*/
public static final String URL_PARAM_SYSPRM_QUERY = "sysparm_query";
public static final String URL_PARAM_SYSPRM_FIELDS = "sysparm_fields";
public static final String URL_PARAM_SYSPRM_LIMIT = "sysparm_limit";
/**
* Debug logs
......@@ -105,11 +106,21 @@ public class Constants {
/**
* Catalogue web services urls
*/
/*Login API */
public static final String URL_POST_LOGIN_ITEM = "/oauth_token.do";
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = API_PATH + "sc_category";
/*Catalogue Category Items API */
public static final String URL_GET_CATALOGUE_ITEM = API_PATH + "sc_cat_item";
/*Variable form API */
public static final String URL_GET_VARIABLE = "/api/uno33/uofl_mobile/variables";
public static final String URL_GET_VARIABLE_CHOICE = API_PATH + "question_choice";
public static final String URL_POST_CATALOGUE_ITEM = "api/uno33/uofl_mobile";
public static final String URL_GET_REFERENCE = API_PATH;
public static final String URL_POST_LOGIN_ITEM = "/oauth_token.do";
/*Incident API */
public static final String URL_GET_INCIDENTS = API_PATH + "incident";
}
......@@ -224,7 +224,7 @@ public class Util {
public static String getDefaultDate() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy", Locale.US);
String formattedDate = df.format(c.getTime());
return formattedDate;
}
......@@ -238,7 +238,7 @@ public class Util {
}
public static long getDateFromString(String strDate) {
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy");
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy", Locale.US);
Date date = null;
try {
date = df.parse(strDate);
......@@ -250,12 +250,12 @@ public class Util {
public static String getDateFromLong(long timeStamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat
("yyyy-MM-dd", Locale.getDefault());
("yyyy-MM-dd", Locale.US);
return dateFormat.format(timeStamp);
}
public static long getDateTimeFromString(String strDate) {
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy HH:mm");
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy HH:mm", Locale.US);
Date date = null;
try {
date = df.parse(strDate);
......@@ -267,10 +267,24 @@ public class Util {
public static String getDateTimeFromLong(long timeStamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat
("dd MMM, yyyy HH:mm", Locale.getDefault());
("dd MMM, yyyy HH:mm", Locale.US);
return dateFormat.format(timeStamp);
}
public static String getDateTime(String strDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormat = new SimpleDateFormat
("dd MMM, yyyy HH:mm", Locale.US);
return dateFormat.format(date.getTime());
}
public static void hideSoftKeyboard(Context context, View view) {
InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
......
......@@ -27,6 +27,7 @@
<string name="failed_to_fetch_variable_choice_string">Failed to fetch Choice Items.</string>
<string name="failed_to_fetch_reference_string">Failed to fetch References.</string>
<string name="failed_to_submit_form_string">Failed to submit form.</string>
<string name="failed_to_fetch_incident_string">Failed to fetch Incident.</string>
<!--Login Screen-->
<string name="login_screen_user_name_string">Username</string>
......
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