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; ...@@ -4,7 +4,9 @@ import com.vsoft.uofl_catalogue.utils.Constants;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query; import retrofit2.http.Query;
...@@ -18,6 +20,10 @@ public interface IncidentApi { ...@@ -18,6 +20,10 @@ public interface IncidentApi {
@GET(Constants.URL_GET_INCIDENTS) @GET(Constants.URL_GET_INCIDENTS)
Call<ResponseBody> getIncident(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysParmQuery, Call<ResponseBody> getIncident(@Query(Constants.URL_PARAM_SYSPRM_QUERY) String sysParmQuery,
@Query(Constants.URL_PARAM_SYSPRM_LIMIT) String sysParmLimits); @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 { ...@@ -124,4 +124,26 @@ public class IncidentApiManager {
return SyncStatus.FAIL; 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 { ...@@ -122,6 +122,9 @@ public class Incident {
public static class Json { public static class Json {
public static final String IMPACT = "impact"; public static final String IMPACT = "impact";
public static final String OPENED_AT = "opened_at"; 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 @Override
......
...@@ -6,25 +6,46 @@ package com.vsoft.uofl_catalogue.enums; ...@@ -6,25 +6,46 @@ package com.vsoft.uofl_catalogue.enums;
* *
*/ */
public enum Impact { public enum Impact {
UNKNOWN (-1), UNKNOWN (-1, "unknown", "-1"),
HIGH (1), HIGH (1, "High", "1"),
MEDIUM (2), MEDIUM (2, "Medium", "2"),
LOW (3); 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.id = id;
this.displayString = displayString;
this.serverString = serverString;
} }
public static int getSyncStatus(Impact status) { /* getters */
return status.id; public static int getId(Impact impact) {
return impact.id;
} }
public int getId() { public int getId() {
return this.id; 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) { public static Impact from(int id) {
for(int i = 0; i< Impact.values().length; i++) { for(int i = 0; i< Impact.values().length; i++) {
Impact impact = Impact.values()[i]; Impact impact = Impact.values()[i];
...@@ -33,4 +54,13 @@ public enum Impact { ...@@ -33,4 +54,13 @@ public enum Impact {
} }
return UNKNOWN; 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 { ...@@ -37,16 +37,16 @@ public enum ViewType {
} }
/* getters */ /* getters */
public static int getId(ViewType expenseType) { public static int getId(ViewType viewType) {
return expenseType.id; return viewType.id;
} }
public int getId() { public int getId() {
return this.id; return this.id;
} }
public static String getDisplayString(ViewType expenseType) { public static String getDisplayString(ViewType viewType) {
return expenseType.displayString; return viewType.displayString;
} }
public String getDisplayString() { public String getDisplayString() {
...@@ -55,8 +55,8 @@ public enum ViewType { ...@@ -55,8 +55,8 @@ public enum ViewType {
public static ViewType from(int id) { public static ViewType from(int id) {
for(int i = 0; i< ViewType.values().length; i++) { for(int i = 0; i< ViewType.values().length; i++) {
ViewType expenseType = ViewType.values()[i]; ViewType viewType = ViewType.values()[i];
if(expenseType.id == id) if(viewType.id == id)
return ViewType.values()[i]; return ViewType.values()[i];
} }
return UNKNOWN; return UNKNOWN;
...@@ -64,8 +64,8 @@ public enum ViewType { ...@@ -64,8 +64,8 @@ public enum ViewType {
public static ViewType from(String string) { public static ViewType from(String string) {
for(int i = 0; i< ViewType.values().length; i++) { for(int i = 0; i< ViewType.values().length; i++) {
ViewType expenseType = ViewType.values()[i]; ViewType viewType = ViewType.values()[i];
if(expenseType.displayString.equals(string)) if(viewType.displayString.equals(string))
return ViewType.values()[i]; return ViewType.values()[i];
} }
return UNKNOWN; return UNKNOWN;
......
package com.vsoft.uofl_catalogue.ui; 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.os.Bundle;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView;
import com.vsoft.uofl_catalogue.CatalogueApplication; import com.vsoft.uofl_catalogue.CatalogueApplication;
import com.vsoft.uofl_catalogue.R; 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.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick;
/** /**
* Created by kunj on 30/8/16. * Created by kunj on 30/8/16.
...@@ -21,8 +41,13 @@ public class ReportIncidentScreen extends AppCompatActivity { ...@@ -21,8 +41,13 @@ public class ReportIncidentScreen extends AppCompatActivity {
@BindView(R.id.tool_bar_view) Toolbar mToolbar; @BindView(R.id.tool_bar_view) Toolbar mToolbar;
@BindView(R.id.incident_impact_spinner) Spinner mImpactSpinner; @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 CatalogueApplication mApplication;
private Impact mImpact;
private String mShortDescription;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -54,6 +79,66 @@ public class ReportIncidentScreen extends AppCompatActivity { ...@@ -54,6 +79,66 @@ public class ReportIncidentScreen extends AppCompatActivity {
mImpactSpinner.setAdapter(dataAdapter); 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 @Override
public boolean onOptionsItemSelected(MenuItem menuItem) { public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) { if (menuItem.getItemId() == android.R.id.home) {
...@@ -61,4 +146,44 @@ public class ReportIncidentScreen extends AppCompatActivity { ...@@ -61,4 +146,44 @@ public class ReportIncidentScreen extends AppCompatActivity {
} }
return super.onOptionsItemSelected(menuItem); 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 { ...@@ -126,4 +126,5 @@ public class Constants {
public static final String URL_GET_USERDETAILS = API_PATH + "sys_user"; public static final String URL_GET_USERDETAILS = API_PATH + "sys_user";
/*Incident API */ /*Incident API */
public static final String URL_GET_INCIDENTS = API_PATH + "incident"; public static final String URL_GET_INCIDENTS = API_PATH + "incident";
public static final String URL_POST_INCIDENT = API_PATH + "incident";
} }
...@@ -63,6 +63,15 @@ ...@@ -63,6 +63,15 @@
android:paddingBottom="@dimen/small_margin"/> android:paddingBottom="@dimen/small_margin"/>
<TextView <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_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/normal_margin" android:layout_margin="@dimen/normal_margin"
...@@ -71,7 +80,7 @@ ...@@ -71,7 +80,7 @@
android:textStyle="bold" /> android:textStyle="bold" />
<EditText <EditText
android:id="@+id/incident_describe_issue_edit_text" android:id="@+id/incident_short_description_edit_text"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/normal_margin" android:layout_marginBottom="@dimen/normal_margin"
...@@ -83,6 +92,15 @@ ...@@ -83,6 +92,15 @@
android:lines="4" /> android:lines="4" />
<TextView <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:id="@+id/incident_submit_text_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="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