Commit 3141cb84 by Kunj Gupta

Added Local DB table for storing Report Incident form values.

parent 8cfa8b19
package com.vsoft.uoflservicenow.db.managers;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.vsoft.uoflservicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.db.models.Incident;
import com.vsoft.uoflservicenow.enums.Impact;
import com.vsoft.uoflservicenow.utils.DBConstants;
/**
*
* @author Kunj on 16-09-2016.
*/
public class ReportIncidentValueManager implements DBConstants {
public static long save(Incident incident, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
incident.setSyncDirty(syncDirty);
long id = db.insert(TABLE_INCIDENT_FORM, null, getContentValues(incident));
return id;
} else {
return -1;
}
}
public static Incident get(long incidentId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
Incident incident = null;
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_INCIDENT_FORM + " where " + INCIDENT_FORM_ID + "=" + incidentId, null);
if (c.moveToFirst()) {
Incident.IncidentBuilder builder = Incident.IncidentBuilder.anIncident();
fillAllIncidentFormDetails(c, builder);
incident = builder.build();
}
c.close();
}
return incident;
}
public static Incident getIncidentFromSysId(String sysId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
Incident incident = null;
if(db!=null) {
Cursor c = db.rawQuery("select * from " + TABLE_INCIDENT_FORM + " where " + CATALOGUE_VARIABLE_SYS_ID + "='" + sysId + "'", null);
if (c.moveToFirst()) {
Incident.IncidentBuilder builder = Incident.IncidentBuilder.anIncident();
fillAllIncidentFormDetails(c, builder);
incident = builder.build();
}
c.close();
}
return incident;
}
private static void fillAllIncidentFormDetails(Cursor c, Incident.IncidentBuilder builder) {
builder.setId(c.getLong(INDEX_INCIDENT_FORM_ID));
builder.setImpact(Impact.from(c.getInt(INDEX_INCIDENT_FORM_IMPACT)));
builder.setShortDescription(c.getString(INDEX_INCIDENT_FORM_SHORT_DESCRIPTION));
builder.setSyncDirty(c.getInt(INDEX_INCIDENT_FORM_SYNC_DIRTY));
}
private static ContentValues getContentValues(Incident incident) {
ContentValues cv = new ContentValues(INCIDENT_FORM_COLUMN_COUNT - 1);
cv.put(INCIDENT_FORM_IMPACT, Impact.getId(incident.getImpact()));
cv.put(INCIDENT_FORM_SHORT_DESCRIPTION, incident.getShortDescription());
cv.put(INCIDENT_FORM_SYNC_DIRTY, incident.getSyncDirty());
return cv;
}
}
\ No newline at end of file
...@@ -406,13 +406,12 @@ public class CatalogueVariableScreen extends AppCompatActivity { ...@@ -406,13 +406,12 @@ public class CatalogueVariableScreen extends AppCompatActivity {
return; return;
/*Create Dynamic table for CatalogueVariable Values*/ /*Create Dynamic table for CatalogueVariable Values*/
Util.createDynamicTable(mCatalogueItem.getSysId(), mCatalogueVariableList); Util.createDynamicTableForVariableFormValues(mCatalogueItem.getSysId(), mCatalogueVariableList);
/*Save data in local DB*/ /*Save data in local DB*/
long id = CatalogueVariableValueManager.save("t_" + mCatalogueItem.getSysId(), columnList, valueList, DBConstants.SYNC_FLAG_CREATE); long id = CatalogueVariableValueManager.save("t_" + mCatalogueItem.getSysId(), columnList, valueList, DBConstants.SYNC_FLAG_CREATE);
if(id == -1) { if(id != -1) {
Toast.makeText(CatalogueVariableScreen.this, "Not saved: id: "+id, Toast.LENGTH_LONG).show(); Toast.makeText(CatalogueVariableScreen.this, "Data Saved in Local DB", Toast.LENGTH_LONG).show();
} else { finish();
Toast.makeText(CatalogueVariableScreen.this, "Saved: id: "+id, Toast.LENGTH_LONG).show();
} }
} }
......
...@@ -22,9 +22,11 @@ import com.google.android.gms.analytics.Tracker; ...@@ -22,9 +22,11 @@ import com.google.android.gms.analytics.Tracker;
import com.vsoft.uoflservicenow.CatalogueApplication; import com.vsoft.uoflservicenow.CatalogueApplication;
import com.vsoft.uoflservicenow.R; import com.vsoft.uoflservicenow.R;
import com.vsoft.uoflservicenow.api.managers.IncidentApiManager; import com.vsoft.uoflservicenow.api.managers.IncidentApiManager;
import com.vsoft.uoflservicenow.db.managers.ReportIncidentValueManager;
import com.vsoft.uoflservicenow.db.models.Incident; import com.vsoft.uoflservicenow.db.models.Incident;
import com.vsoft.uoflservicenow.enums.Impact; import com.vsoft.uoflservicenow.enums.Impact;
import com.vsoft.uoflservicenow.enums.SyncStatus; import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.DBConstants;
import com.vsoft.uoflservicenow.utils.DialogUtils; import com.vsoft.uoflservicenow.utils.DialogUtils;
import com.vsoft.uoflservicenow.utils.PrefManager; import com.vsoft.uoflservicenow.utils.PrefManager;
import com.vsoft.uoflservicenow.utils.Util; import com.vsoft.uoflservicenow.utils.Util;
...@@ -96,7 +98,16 @@ public class ReportIncidentScreen extends AppCompatActivity { ...@@ -96,7 +98,16 @@ public class ReportIncidentScreen extends AppCompatActivity {
String userSysId = sharedPreferences.getString(PrefManager.PREFERENCE_SYS_ID, ""); String userSysId = sharedPreferences.getString(PrefManager.PREFERENCE_SYS_ID, "");
if(!userSysId.isEmpty()) { if(!userSysId.isEmpty()) {
Util.hideSoftKeyboard(ReportIncidentScreen.this, view); Util.hideSoftKeyboard(ReportIncidentScreen.this, view);
new submitIncident().execute(userSysId); Util.createDynamicTableForIncidentFormValues();
Incident incident = Incident.IncidentBuilder.anIncident()
.setImpact(mImpact)
.setShortDescription(mShortDescription)
.build();
long id = ReportIncidentValueManager.save(incident, DBConstants.SYNC_FLAG_CREATE);
if(id != -1) {
showSuccessDialog();
}
// new submitIncident().execute(userSysId);
} else { } else {
showErrorDialog(R.string.failed_to_submit_form_string); showErrorDialog(R.string.failed_to_submit_form_string);
} }
......
...@@ -8,6 +8,7 @@ public interface DBConstants { ...@@ -8,6 +8,7 @@ public interface DBConstants {
String TABLE_VARIABLE_CHOICES = "variable_choices"; String TABLE_VARIABLE_CHOICES = "variable_choices";
String TABLE_MY_INCIDENT = "my_incidents"; String TABLE_MY_INCIDENT = "my_incidents";
String TABLE_MY_REQUEST = "my_requests"; String TABLE_MY_REQUEST = "my_requests";
String TABLE_INCIDENT_FORM = "incident_form";
String ID = "_id"; String ID = "_id";
String SYS_ID = "sys_id"; String SYS_ID = "sys_id";
...@@ -165,4 +166,22 @@ public interface DBConstants { ...@@ -165,4 +166,22 @@ public interface DBConstants {
int INDEX_REQUEST_SYNC_DIRTY = 5; int INDEX_REQUEST_SYNC_DIRTY = 5;
int REQUEST_COLUMN_COUNT = 6; int REQUEST_COLUMN_COUNT = 6;
/**
* Incident Form table
*/
String INCIDENT_FORM_ID = ID;
String INCIDENT_FORM_IMPACT = "number";
String INCIDENT_FORM_SHORT_DESCRIPTION = "short_description";
String INCIDENT_FORM_SYNC_DIRTY = SYNC_DIRTY;
/**
* Request for Incident Form table. *Use these only if you fetch all columns*
*/
int INDEX_INCIDENT_FORM_ID = 0;
int INDEX_INCIDENT_FORM_IMPACT = 1;
int INDEX_INCIDENT_FORM_SHORT_DESCRIPTION = 2;
int INDEX_INCIDENT_FORM_SYNC_DIRTY = 3;
int INCIDENT_FORM_COLUMN_COUNT = 6;
} }
\ No newline at end of file
...@@ -314,7 +314,7 @@ public class Util { ...@@ -314,7 +314,7 @@ public class Util {
return date.getTime(); return date.getTime();
} }
public static void createDynamicTable(String catalogueItemSysId, List<CatalogueVariable> catalogueVariableList) { public static void createDynamicTableForVariableFormValues(String catalogueItemSysId, List<CatalogueVariable> catalogueVariableList) {
StringBuilder queryString = new StringBuilder(); StringBuilder queryString = new StringBuilder();
//Opens database in writable mode. //Opens database in writable mode.
...@@ -345,7 +345,35 @@ public class Util { ...@@ -345,7 +345,35 @@ public class Util {
queryString.append(DBConstants.SYNC_FLAG_NONE); queryString.append(DBConstants.SYNC_FLAG_NONE);
queryString.append(");"); queryString.append(");");
System.out.println("Create Table Stmt : "+ queryString); System.out.println("createDynamicTableForVariableFormValues: Create Table Stmt : "+ queryString);
database.execSQL(queryString.toString());
}
public static void createDynamicTableForIncidentFormValues() {
StringBuilder queryString = new StringBuilder();
//Opens database in writable mode.
SQLiteDatabase database = CatalogueApplication.getDatabase();
queryString.append("CREATE TABLE IF NOT EXISTS ");
queryString.append(DBConstants.TABLE_INCIDENT_FORM);
queryString.append(" (");
queryString.append(DBConstants.ID);
queryString.append(" integer primary key autoincrement, ");
queryString.append(DBConstants.INCIDENT_FORM_IMPACT);
queryString.append(" integer, ");
queryString.append(DBConstants.INCIDENT_FORM_SHORT_DESCRIPTION);
queryString.append(" text, ");
queryString.append(DBConstants.INCIDENT_FORM_SYNC_DIRTY);
queryString.append(" integer default ");
queryString.append(DBConstants.SYNC_FLAG_NONE);
queryString.append(");");
System.out.println("createDynamicTableForIncidentFormValues: Create Table Stmt : "+ queryString);
database.execSQL(queryString.toString()); database.execSQL(queryString.toString());
} }
...@@ -359,6 +387,4 @@ public class Util { ...@@ -359,6 +387,4 @@ public class Util {
InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} }
} }
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