Commit ef377686 by Kunj Gupta

UOFLMA-37: Integrated submit Incident POST API.

parent ed08d6e6
......@@ -4,7 +4,9 @@ import com.vsoft.uofl_catalogue.utils.Constants;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
......@@ -18,6 +20,10 @@ public interface IncidentApi {
@GET(Constants.URL_GET_INCIDENTS)
Call<ResponseBody> getIncident(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysParmQuery,
@Query(Constants.URL_PARAM_SYSPRM_LIMIT) String sysParmLimits);
// Post Incident API
@POST(Constants.URL_POST_INCIDENT)
Call<ResponseBody> submitIncident(@Body String incidentJson);
}
......@@ -124,4 +124,26 @@ public class IncidentApiManager {
return SyncStatus.FAIL;
}
}
public static SyncStatus submitIncidentForm(String incidentJsonString) {
CatalogueLog.d("submitIncidentForm: incidentJson" + incidentJsonString);
final Retrofit retrofit = RestClient.getInitializedRestAdapter(Constants.API_AUTH_PARAM_USER_NAME, Constants.API_AUTH_PARAM_PASSWORD);
Call<ResponseBody> call = retrofit.create(IncidentApi.class).submitIncident(incidentJsonString);
try {
//Retrofit synchronous call
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
return SyncStatus.SUCCESS;
} else {
return SyncStatus.FAIL;
}
} catch (IOException e) {
CatalogueLog.e("IncidentApiManager: submitIncidentForm: IOException: ", e);
return SyncStatus.FAIL;
} catch (NullPointerException e){
CatalogueLog.e("IncidentApiManager: submitIncidentForm: IOException: ", e);
return SyncStatus.FAIL;
}
}
}
\ No newline at end of file
......@@ -122,6 +122,9 @@ public class Incident {
public static class Json {
public static final String IMPACT = "impact";
public static final String OPENED_AT = "opened_at";
public static final String SHORT_DESCRIPTION = "short_description";
public static final String CALLER_ID = "caller_id";
public static final String OPENED_ID = "opened_by";
}
@Override
......
......@@ -6,25 +6,46 @@ package com.vsoft.uofl_catalogue.enums;
*
*/
public enum Impact {
UNKNOWN (-1),
HIGH (1),
MEDIUM (2),
LOW (3);
UNKNOWN (-1, "unknown", "-1"),
HIGH (1, "High", "1"),
MEDIUM (2, "Medium", "2"),
LOW (3, "Low", "3");
int id;
private int id;
private String displayString;
private String serverString;
Impact(int id) {
Impact(int id, String displayString, String serverString) {
this.id = id;
this.displayString = displayString;
this.serverString = serverString;
}
public static int getSyncStatus(Impact status) {
return status.id;
/* getters */
public static int getId(Impact impact) {
return impact.id;
}
public int getId() {
return this.id;
}
public static String getDisplayString(Impact impact) {
return impact.displayString;
}
public String getDisplayString() {
return this.displayString;
}
public String getServerString() {
return serverString;
}
public void setServerString(String serverString) {
this.serverString = serverString;
}
public static Impact from(int id) {
for(int i = 0; i< Impact.values().length; i++) {
Impact impact = Impact.values()[i];
......@@ -33,4 +54,13 @@ public enum Impact {
}
return UNKNOWN;
}
public static Impact from(String string) {
for(int i = 0; i< Impact.values().length; i++) {
Impact impact = Impact.values()[i];
if(impact.displayString.equals(string))
return Impact.values()[i];
}
return UNKNOWN;
}
}
......@@ -37,16 +37,16 @@ public enum ViewType {
}
/* getters */
public static int getId(ViewType expenseType) {
return expenseType.id;
public static int getId(ViewType viewType) {
return viewType.id;
}
public int getId() {
return this.id;
}
public static String getDisplayString(ViewType expenseType) {
return expenseType.displayString;
public static String getDisplayString(ViewType viewType) {
return viewType.displayString;
}
public String getDisplayString() {
......@@ -55,8 +55,8 @@ public enum ViewType {
public static ViewType from(int id) {
for(int i = 0; i< ViewType.values().length; i++) {
ViewType expenseType = ViewType.values()[i];
if(expenseType.id == id)
ViewType viewType = ViewType.values()[i];
if(viewType.id == id)
return ViewType.values()[i];
}
return UNKNOWN;
......@@ -64,8 +64,8 @@ public enum ViewType {
public static ViewType from(String string) {
for(int i = 0; i< ViewType.values().length; i++) {
ViewType expenseType = ViewType.values()[i];
if(expenseType.displayString.equals(string))
ViewType viewType = ViewType.values()[i];
if(viewType.displayString.equals(string))
return ViewType.values()[i];
}
return UNKNOWN;
......
package com.vsoft.uofl_catalogue.ui;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.vsoft.uofl_catalogue.CatalogueApplication;
import com.vsoft.uofl_catalogue.R;
import com.vsoft.uofl_catalogue.api.managers.IncidentApiManager;
import com.vsoft.uofl_catalogue.db.models.Incident;
import com.vsoft.uofl_catalogue.enums.Impact;
import com.vsoft.uofl_catalogue.enums.SyncStatus;
import com.vsoft.uofl_catalogue.utils.PrefManager;
import com.vsoft.uofl_catalogue.utils.Util;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* Created by kunj on 30/8/16.
......@@ -21,8 +41,13 @@ public class ReportIncidentScreen extends AppCompatActivity {
@BindView(R.id.tool_bar_view) Toolbar mToolbar;
@BindView(R.id.incident_impact_spinner) Spinner mImpactSpinner;
@BindView(R.id.incident_short_description_edit_text) EditText mShortDesEditText;
@BindView(R.id.incident_impact_error_tv) TextView mImpactErrorTextView;
@BindView(R.id.incident_short_des_error_tv) TextView mShortDesErrorTextView;
private CatalogueApplication mApplication;
private Impact mImpact;
private String mShortDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -54,6 +79,66 @@ public class ReportIncidentScreen extends AppCompatActivity {
mImpactSpinner.setAdapter(dataAdapter);
}
@OnClick(R.id.incident_submit_text_view)
void submitOnClicked(View view) {
if(mApplication.isNetConnected()) {
if (!hasValidateForm()) {
SharedPreferences sharedPreferences = getSharedPreferences(PrefManager.PREFERENCES_USER_VALUES_KEY, Context.MODE_PRIVATE);
String userSysId = sharedPreferences.getString(PrefManager.PREFERENCE_SYS_ID, "");
if(!userSysId.isEmpty()) {
Util.hideSoftKeyboard(ReportIncidentScreen.this, view);
new submitIncident().execute(userSysId);
} else {
showErrorDialog(R.string.failed_to_submit_form_string);
}
}
} else {
showErrorDialog(R.string.internet_validation_string);
}
}
class submitIncident extends AsyncTask<String, Void, SyncStatus> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ReportIncidentScreen.this);
progressDialog.setMessage(getString(R.string.loading_string));
progressDialog.show();
progressDialog.setCancelable(false);
}
@Override
protected SyncStatus doInBackground(String... params) {
String userSysId = params[0];
JSONObject incidentJsonObject = new JSONObject();
try {
incidentJsonObject.put(Incident.Json.IMPACT, mImpact.getServerString());
incidentJsonObject.put(Incident.Json.SHORT_DESCRIPTION, mShortDescription);
incidentJsonObject.put(Incident.Json.OPENED_AT, userSysId);
incidentJsonObject.put(Incident.Json.CALLER_ID, userSysId);
} catch (JSONException e) {
e.printStackTrace();
}
SyncStatus syncStatus = IncidentApiManager.submitIncidentForm(incidentJsonObject.toString());
return syncStatus;
}
@Override
protected void onPostExecute(SyncStatus syncStatus) {
super.onPostExecute(syncStatus);
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if(syncStatus == SyncStatus.SUCCESS) {
finish();
} else {
showErrorDialog(R.string.failed_to_submit_form_string);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
......@@ -61,4 +146,44 @@ public class ReportIncidentScreen extends AppCompatActivity {
}
return super.onOptionsItemSelected(menuItem);
}
private boolean hasValidateForm() {
boolean hasError = false;
/*Get all edit text values in member variables*/
getAllFromValues();
if (mImpact != Impact.UNKNOWN) {
mImpactErrorTextView.setVisibility(View.GONE);
} else {
hasError = true;
mImpactErrorTextView.setVisibility(View.VISIBLE);
}
if (!TextUtils.isEmpty(mShortDescription)) {
mShortDesErrorTextView.setVisibility(View.GONE);
} else {
hasError = true;
mShortDesErrorTextView.setVisibility(View.VISIBLE);
}
return hasError;
}
private void getAllFromValues() {
/*Form values*/
mImpact = Impact.from((String) mImpactSpinner.getSelectedItem());
mShortDescription = mShortDesEditText.getText().toString();
}
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) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
......@@ -126,4 +126,5 @@ public class Constants {
public static final String URL_GET_USERDETAILS = API_PATH + "sys_user";
/*Incident API */
public static final String URL_GET_INCIDENTS = API_PATH + "incident";
public static final String URL_POST_INCIDENT = API_PATH + "incident";
}
......@@ -63,6 +63,15 @@
android:paddingBottom="@dimen/small_margin"/>
<TextView
android:id="@+id/incident_impact_error_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error_string"
android:visibility="gone"
android:layout_marginLeft="@dimen/normal_margin"
android:textColor="@color/error_color"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_margin"
......@@ -71,7 +80,7 @@
android:textStyle="bold" />
<EditText
android:id="@+id/incident_describe_issue_edit_text"
android:id="@+id/incident_short_description_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/normal_margin"
......@@ -83,6 +92,15 @@
android:lines="4" />
<TextView
android:id="@+id/incident_short_des_error_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error_string"
android:visibility="gone"
android:layout_marginLeft="@dimen/normal_margin"
android:textColor="@color/error_color"/>
<TextView
android:id="@+id/incident_submit_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
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