Commit 3082f65a by Kunj Gupta

Added Local DB table for storing My Incidents.

parent 52366a4b
......@@ -31,6 +31,7 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
createCatalogueItemsTable(db);
createVariableTable(db);
createVariableChoiceTable(db);
createMyIncidentTable(db);
}
@Override
......@@ -93,4 +94,14 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
+ VARIABLE_CHOICE_ORDER + " integer, "
+ VARIABLE_CHOICE_MISC + " real);");
}
private void createMyIncidentTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_MY_INCIDENT + "("
+ INCIDENT_ID + " integer primary key autoincrement, "
+ INCIDENT_NUMBER + " text, "
+ INCIDENT_SHORT_DESCRIPTION + " text, "
+ INCIDENT_OPENED_AT + " real, "
+ INCIDENT_IMPACT + " integer, "
+ INCIDENT_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + ");");
}
}
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;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author Kunj on 11-08-2016.
*/
public class MyIncidentsManager 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_MY_INCIDENT, null, getContentValues(incident));
incident.setId(id);
return id;
} else {
return -1;
}
}
public static int delete(Incident incident) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
if (incident.getNumber() == null || incident.getNumber().isEmpty()) {
return db.delete(TABLE_MY_INCIDENT, INCIDENT_ID + "=" + incident.getId(), null);
} else {
return update(incident, SYNC_FLAG_DELETE);
}
}
return -1;
}
public static int update(Incident incident, int syncDirty) {
return update(incident, null, syncDirty);
}
public static int update(Incident incident, List<String> column, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
incident.setSyncDirty(syncDirty);
if (column == null || column.size() == 0) {
return db.update(TABLE_MY_INCIDENT, getContentValues(incident), INCIDENT_ID + "=" + incident.getId(), null);
} else {
ContentValues contentValues = new ContentValues(column.size());
contentValues.put(INCIDENT_SYNC_DIRTY, incident.getSyncDirty());
for (int i = 0; i < column.size(); i++) {
String columnName = column.get(i);
if (INCIDENT_NUMBER.equals(columnName)) {
contentValues.put(INCIDENT_NUMBER, incident.getNumber());
} else if (INCIDENT_SHORT_DESCRIPTION.equals(columnName)) {
contentValues.put(INCIDENT_SHORT_DESCRIPTION, incident.getShortDescription());
} else if (INCIDENT_OPENED_AT.equals(columnName)) {
contentValues.put(INCIDENT_OPENED_AT, incident.getOpenedAt());
} else if (INCIDENT_IMPACT.equals(columnName)) {
contentValues.put(INCIDENT_IMPACT, Impact.getId(incident.getImpact()));
}
}
return db.update(TABLE_MY_INCIDENT, contentValues, INCIDENT_ID + "=" + incident.getId(), null);
}
} else {
return -1;
}
}
public static void handleGetIncident(List<Incident> serverIncidentList) {
if(serverIncidentList != null && !serverIncidentList.isEmpty()) {
/*incidentSysIdMap contain all server response catalogues Sys Id*/
HashMap<String, Integer> incidentSysIdMap = new HashMap<>(0);
Integer intObj = Integer.valueOf(1);
for (int i = 0; i < serverIncidentList.size(); i++) {
String sysId = serverIncidentList.get(i).getNumber();
incidentSysIdMap.put(sysId, intObj);
}
/*localIncidentList is contain all local Incidents */
List<Incident> localIncidentList = getAllIncidents();
if (localIncidentList != null && !localIncidentList.isEmpty()) {
for (int i = 0; i < localIncidentList.size(); i++) {
Incident localIncident = localIncidentList.get(i);
String localIncidentNumber = localIncident.getNumber();
if (localIncidentNumber != null
&& !localIncidentNumber.isEmpty()
&& !incidentSysIdMap.containsKey(localIncidentNumber)) {
//Update sys_id with empty string because required to delete locally
localIncident.setNumber("");
delete(localIncident);
}
}
}
/*Check this catalogue is exist in local DB or not
* If doesn't exist in local, save it
* If exist in local, update the local item with data from server item.
* */
for (int i = 0; i < serverIncidentList.size(); i++) {
Incident incident = serverIncidentList.get(i);
Incident localIncident = getIncidentFromNuber(incident.getNumber());
if (localIncident == null) {
save(incident, DBConstants.SYNC_FLAG_NONE);
} else {
/*Update complete local Catalogue object with response Catalogue object*/
incident.setId(localIncident.getId());
update(incident, DBConstants.SYNC_FLAG_NONE);
}
}
} else {
/*That means there is no Incident in server response, then all local items should be deleted those are contain sys_id*/
/*localIncidentList is contain all local Catalogues */
List<Incident> localIncidentList = getAllIncidents();
if (localIncidentList != null && !localIncidentList.isEmpty()) {
for (int i = 0; i < localIncidentList.size(); i++) {
Incident localIncident = localIncidentList.get(i);
String localIncidentNumber = localIncident.getNumber();
if (localIncidentNumber != null
&& !localIncidentNumber.isEmpty()) {
//Update sys_id with empty string because required to delete locally
localIncident.setNumber("");
delete(localIncident);
}
}
}
}
}
public static List<Incident> getAllIncidents() {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_MY_INCIDENT
+ " where " + INCIDENT_SYNC_DIRTY
+ "!=" + DBConstants.SYNC_FLAG_DELETE, null);
ArrayList<Incident> incidentList;
if (c.getCount() > 0) {
incidentList = new ArrayList<>(c.getCount());
while (c.moveToNext()) {
Incident.IncidentBuilder builder = Incident.IncidentBuilder.anIncident();
fillAllIncidentDetails(c, builder);
incidentList.add(builder.build());
}
} else {
incidentList = new ArrayList<>(0);
}
c.close();
return incidentList;
} else {
return new ArrayList<>(0);
}
}
public static Incident get(long incidentId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
Incident incident = null;
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_MY_INCIDENT + " where " + INCIDENT_ID + "=" + incidentId, null);
if (c.moveToFirst()) {
Incident.IncidentBuilder builder = Incident.IncidentBuilder.anIncident();
fillAllIncidentDetails(c, builder);
incident = builder.build();
}
c.close();
}
return incident;
}
public static Incident getIncidentFromNuber(String number) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
Incident incident = null;
if(db!=null) {
Cursor c = db.rawQuery("select * from " + TABLE_MY_INCIDENT + " where " + INCIDENT_NUMBER + "='" + number + "'", null);
if (c.moveToFirst()) {
Incident.IncidentBuilder builder = Incident.IncidentBuilder.anIncident();
fillAllIncidentDetails(c, builder);
incident = builder.build();
}
c.close();
}
return incident;
}
private static void fillAllIncidentDetails(Cursor c, Incident.IncidentBuilder builder) {
builder.setId(c.getLong(INDEX_INCIDENT_ID));
builder.setNumber(c.getString(INDEX_INCIDENT_NUMBER));
builder.setShortDescription(c.getString(INDEX_INCIDENT_SHORT_DESCRIPTION));
builder.setOpenedAt(c.getLong(INDEX_INCIDENT_OPENED_AT));
builder.setImpact(Impact.from(c.getInt(INDEX_INCIDENT_IMPACT)));
builder.setSyncDirty(c.getInt(INDEX_INCIDENT_SYNC_DIRTY));
}
private static ContentValues getContentValues(Incident incident) {
ContentValues cv = new ContentValues(INCIDENT_COLUMN_COUNT - 1);
cv.put(INCIDENT_NUMBER, incident.getNumber());
cv.put(INCIDENT_SHORT_DESCRIPTION, incident.getShortDescription());
cv.put(INCIDENT_OPENED_AT, incident.getOpenedAt());
cv.put(INCIDENT_IMPACT, Impact.getId(incident.getImpact()));
cv.put(INCIDENT_SYNC_DIRTY, incident.getSyncDirty());
return cv;
}
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ import org.json.JSONObject;
* Created by Kunj on 11/8/16.
*/
public class Incident {
private long id = -1;
@SerializedName("number")
@Expose
private String number;
......@@ -26,6 +26,16 @@ public class Incident {
// @Expose
private Impact impact;
private int syncDirty;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Impact getImpact() {
return impact;
}
......@@ -58,6 +68,14 @@ public class Incident {
this.number = number;
}
public int getSyncDirty() {
return syncDirty;
}
public void setSyncDirty(int syncDirty) {
this.syncDirty = syncDirty;
}
public void parseJson(JSONObject jsonObject) {
int impact = -1;
String openedAt = null;
......@@ -72,6 +90,69 @@ public class Incident {
this.setImpact(Impact.from(impact));
}
public static final class IncidentBuilder {
private long id = -1;
private String number;
private String shortDescription;
// @Expose
private long openedAt;
// @Expose
private Impact impact;
private int syncDirty;
private IncidentBuilder() {
}
public static IncidentBuilder anIncident() {
return new IncidentBuilder();
}
public IncidentBuilder setId(long id) {
this.id = id;
return this;
}
public IncidentBuilder setNumber(String number) {
this.number = number;
return this;
}
public IncidentBuilder setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
return this;
}
public IncidentBuilder setOpenedAt(long openedAt) {
this.openedAt = openedAt;
return this;
}
public IncidentBuilder setImpact(Impact impact) {
this.impact = impact;
return this;
}
public IncidentBuilder setSyncDirty(int syncDirty) {
this.syncDirty = syncDirty;
return this;
}
public IncidentBuilder but() {
return anIncident().setId(id).setNumber(number).setShortDescription(shortDescription).setOpenedAt(openedAt).setImpact(impact).setSyncDirty(syncDirty);
}
public Incident build() {
Incident incident = new Incident();
incident.setId(id);
incident.setNumber(number);
incident.setShortDescription(shortDescription);
incident.setOpenedAt(openedAt);
incident.setImpact(impact);
incident.setSyncDirty(syncDirty);
return incident;
}
}
public static class Json {
public static final String IMPACT = "impact";
public static final String OPENED_AT = "opened_at";
......@@ -83,10 +164,12 @@ public class Incident {
@Override
public String toString() {
return "Incident{" +
"number='" + number + '\'' +
"id=" + id +
", number='" + number + '\'' +
", shortDescription='" + shortDescription + '\'' +
", openedAt='" + openedAt + '\'' +
", openedAt=" + openedAt +
", impact=" + impact +
", syncDirty=" + syncDirty +
'}';
}
}
......@@ -17,6 +17,7 @@ import com.vsoft.uoflservicenow.R;
import com.vsoft.uoflservicenow.adapters.MyIncidentsAdapter;
import com.vsoft.uoflservicenow.api.listeners.get.GetIncidentApiListener;
import com.vsoft.uoflservicenow.api.managers.IncidentApiManager;
import com.vsoft.uoflservicenow.db.managers.MyIncidentsManager;
import com.vsoft.uoflservicenow.db.models.Incident;
import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
......@@ -38,6 +39,7 @@ public class MyIncidentScreen extends AppCompatActivity {
@BindView(R.id.tool_bar_view) Toolbar mToolbar;
@BindView(R.id.my_incidents_screen_list_view) ListView mListView;
private List<Incident> mIncidentList;
@Override
......@@ -64,10 +66,15 @@ public class MyIncidentScreen extends AppCompatActivity {
// Send initial screen view hit.
Util.sendScreenName(tracker, actionBar.getTitle().toString());
if(application.isNetConnected()) {
new FetchIncident().execute();
mIncidentList = MyIncidentsManager.getAllIncidents();
if(mIncidentList.isEmpty()) {
if (application.isNetConnected()) {
new FetchIncident().execute();
} else {
DialogUtils.showNoConnectionDialogWithCloseActivity(MyIncidentScreen.this);
}
} else {
DialogUtils.showNoConnectionDialogWithCloseActivity(MyIncidentScreen.this);
setData(mIncidentList);
}
}
......@@ -114,7 +121,7 @@ public class MyIncidentScreen extends AppCompatActivity {
@Override
public void onDoneApiCall(List<Incident> incidentList) {
CatalogueLog.e("Data: incidentList: "+incidentList);
mIncidentList = incidentList;
MyIncidentsManager.handleGetIncident(incidentList);
}
});
}
......@@ -126,8 +133,8 @@ public class MyIncidentScreen extends AppCompatActivity {
progressDialog.dismiss();
}
if(syncStatus == SyncStatus.SUCCESS) {
if(mIncidentList!=null)
setData(mIncidentList);
mIncidentList = MyIncidentsManager.getAllIncidents();
setData(mIncidentList);
} else {
showErrorDialog(R.string.failed_to_fetch_incident_string);
}
......
......@@ -6,6 +6,7 @@ public interface DBConstants {
String TABLE_CATALOGUE_ITEM = "catalogue_category_item";
String TABLE_CATALOGUE_VARIABLES = "catalogue_variable";
String TABLE_VARIABLE_CHOICES = "variable_choices";
String TABLE_MY_INCIDENT = "my_incidents";
String ID = "_id";
String SYS_ID = "sys_id";
......@@ -119,4 +120,26 @@ public interface DBConstants {
int INDEX_VARIABLE_CHOICE_MISC = 5;
int VARIABLE_CHOICE_COLUMN_COUNT = 6;
/**
* MyIncidents table
*/
String INCIDENT_ID = ID;
String INCIDENT_NUMBER = "number";
String INCIDENT_SHORT_DESCRIPTION = "short_description";
String INCIDENT_OPENED_AT = "opened_at";
String INCIDENT_IMPACT = "impact";
String INCIDENT_SYNC_DIRTY = SYNC_DIRTY;
/**
* Indices for My Incidents table. *Use these only if you fetch all columns*
*/
int INDEX_INCIDENT_ID = 0;
int INDEX_INCIDENT_NUMBER = 1;
int INDEX_INCIDENT_SHORT_DESCRIPTION = 2;
int INDEX_INCIDENT_OPENED_AT = 3;
int INDEX_INCIDENT_IMPACT = 4;
int INDEX_INCIDENT_SYNC_DIRTY = 5;
int INCIDENT_COLUMN_COUNT = 6;
}
\ No newline at end of file
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