Commit 7b78b272 by Kunj Gupta

Added Local DB table for storing Catalogue Items.

parent 1be58602
......@@ -28,6 +28,7 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
@Override
public void onCreate(SQLiteDatabase db) {
createCatalogueTable(db);
createCatalogueItemsTable(db);
}
@Override
......@@ -50,4 +51,17 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
+ CATALOGUE_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE
+ ");");
}
private void createCatalogueItemsTable(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_CATALOGUE_ITEM + "("
+ CATALOGUE_ITEM_ID + " integer primary key autoincrement, "
+ CATALOGUE_ITEM_CATALOGUE_ID + " integer default -1, "
+ CATALOGUE_ITEM_SYS_ID + " text, "
+ CATALOGUE_ITEM_NAME + " integer, "
+ CATALOGUE_ITEM_SHORT_DESCRIPTION + " text, "
+ CATALOGUE_ITEM_DESCRIPTION + " text, "
+ CATALOGUE_ITEM_ICON + " text, "
+ CATALOGUE_ITEM_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.CatalogueItem;
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 CatalogueItemManager implements DBConstants {
public static long save(CatalogueItem catalogueItem, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
catalogueItem.setSyncDirty(syncDirty);
long id = db.insert(TABLE_CATALOGUE_ITEM, null, getContentValues(catalogueItem));
catalogueItem.setId(id);
return id;
} else {
return -1;
}
}
public static int delete(CatalogueItem catalogueItem) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
if (catalogueItem.getSysId() == null || catalogueItem.getSysId().isEmpty()) {
return db.delete(TABLE_CATALOGUE_ITEM, CATALOGUE_ITEM_ID + "=" + catalogueItem.getId(), null);
} else {
return update(catalogueItem, SYNC_FLAG_DELETE);
}
}
return -1;
}
public static int update(CatalogueItem catalogueItem, int syncDirty) {
return update(catalogueItem, null, syncDirty);
}
public static int update(CatalogueItem catalogueItem, List<String> column, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
catalogueItem.setSyncDirty(syncDirty);
if (column == null || column.size() == 0) {
return db.update(TABLE_CATALOGUE_ITEM, getContentValues(catalogueItem), CATALOGUE_ITEM_ID + "=" + catalogueItem.getId(), null);
} else {
ContentValues contentValues = new ContentValues(column.size());
contentValues.put(CATALOGUE_SYNC_DIRTY, catalogueItem.getSyncDirty());
for (int i = 0; i < column.size(); i++) {
String columnName = column.get(i);
if (CATALOGUE_ITEM_SYS_ID.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_SYS_ID, catalogueItem.getSysId());
} else if (CATALOGUE_ITEM_NAME.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_NAME, catalogueItem.getName());
} else if (CATALOGUE_ITEM_CATALOGUE_ID.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_CATALOGUE_ID, catalogueItem.getCatalogueId());
} else if (CATALOGUE_ITEM_SHORT_DESCRIPTION.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_SHORT_DESCRIPTION, catalogueItem.getShortDescription());
} else if (CATALOGUE_ITEM_DESCRIPTION.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_DESCRIPTION, catalogueItem.getDescription());
} else if (CATALOGUE_ITEM_ICON.equals(columnName)) {
contentValues.put(CATALOGUE_ITEM_ICON, catalogueItem.getIcon());
}
}
return db.update(TABLE_CATALOGUE_ITEM, contentValues, CATALOGUE_ITEM_ID + "=" + catalogueItem.getId(), null);
}
} else {
return -1;
}
}
public static void handleGetCatalogueItem(long catalogueId, List<CatalogueItem> serverCatalogueItemList) {
if(serverCatalogueItemList != null && !serverCatalogueItemList.isEmpty()) {
/*catalogueItemSysIdMap contain all server response catalogueItem Sys Id*/
HashMap<String, Integer> catalogueItemSysIdMap = new HashMap<>(0);
Integer intObj = Integer.valueOf(1);
for (int i = 0; i < serverCatalogueItemList.size(); i++) {
String sysId = serverCatalogueItemList.get(i).getSysId();
catalogueItemSysIdMap.put(sysId, intObj);
}
/*localCatalogueItemList is contain all local Catalogues */
List<CatalogueItem> localCatalogueItemList = getAllCatalogueItems(catalogueId);
if (localCatalogueItemList != null && !localCatalogueItemList.isEmpty()) {
for (int i = 0; i < localCatalogueItemList.size(); i++) {
CatalogueItem localCatalogueItem = localCatalogueItemList.get(i);
String localCatalogueItemSysId = localCatalogueItem.getSysId();
if (localCatalogueItemSysId != null
&& !localCatalogueItemSysId.isEmpty()
&& !catalogueItemSysIdMap.containsKey(localCatalogueItemSysId)) {
//Update sys_id with empty string because required to delete locally
localCatalogueItem.setSysId("");
delete(localCatalogueItem);
}
}
}
/*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 < serverCatalogueItemList.size(); i++) {
CatalogueItem catalogueItem = serverCatalogueItemList.get(i);
CatalogueItem localCatalogueItem = getCatalogueItemFromSysId(catalogueItem.getSysId());
if (localCatalogueItem == null) {
catalogueItem.setCatalogueId(catalogueId);
save(catalogueItem, DBConstants.SYNC_FLAG_NONE);
} else {
/*Update complete local Expense object with response Expense object*/
catalogueItem.setCatalogueId(catalogueId);
catalogueItem.setId(localCatalogueItem.getId());
update(catalogueItem, DBConstants.SYNC_FLAG_NONE);
}
}
} else {
/*That means there is no CatalogueItem category in server response, then all local items should be deleted those are contain sys_id*/
/*localCatalogueItemList is contain all local Catalogues */
List<CatalogueItem> localCatalogueItemList = getAllCatalogueItems(catalogueId);
if (localCatalogueItemList != null && !localCatalogueItemList.isEmpty()) {
for (int i = 0; i < localCatalogueItemList.size(); i++) {
CatalogueItem localCatalogueItem = localCatalogueItemList.get(i);
String localCatalogueSysId = localCatalogueItem.getSysId();
if (localCatalogueSysId != null
&& !localCatalogueSysId.isEmpty()) {
//Update sys_id with empty string because required to delete locally
localCatalogueItem.setSysId("");
delete(localCatalogueItem);
}
}
}
}
}
public static List<CatalogueItem> getAllCatalogueItems(long catalogueId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_ITEM
+ " where " + CATALOGUE_ITEM_CATALOGUE_ID + "=" + catalogueId
+ " and " + CATALOGUE_ITEM_SYNC_DIRTY + "!=" + DBConstants.SYNC_FLAG_DELETE, null);
ArrayList<CatalogueItem> catalogueItemList;
if (c.getCount() > 0) {
catalogueItemList = new ArrayList<>(c.getCount());
while (c.moveToNext()) {
CatalogueItem.CatalogueItemBuilder builder = CatalogueItem.CatalogueItemBuilder.aCatalogueItem();
fillAllCatalogueDetails(c, builder);
catalogueItemList.add(builder.build());
}
} else {
catalogueItemList = new ArrayList<>(0);
}
c.close();
return catalogueItemList;
} else {
return new ArrayList<>(0);
}
}
public static CatalogueItem get(long catalogueItemId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
CatalogueItem catalogueItem = null;
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_ITEM + " where " + CATALOGUE_ITEM_ID + "=" + catalogueItemId, null);
if (c.moveToFirst()) {
CatalogueItem.CatalogueItemBuilder builder = CatalogueItem.CatalogueItemBuilder.aCatalogueItem();
fillAllCatalogueDetails(c, builder);
catalogueItem = builder.build();
}
c.close();
}
return catalogueItem;
}
public static CatalogueItem getCatalogueItemFromSysId(String sysId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
CatalogueItem catalogueItem = null;
if(db!=null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_ITEM + " where " + CATALOGUE_SYS_ID + "='" + sysId + "'", null);
if (c.moveToFirst()) {
CatalogueItem.CatalogueItemBuilder builder = CatalogueItem.CatalogueItemBuilder.aCatalogueItem();
fillAllCatalogueDetails(c, builder);
catalogueItem = builder.build();
}
c.close();
}
return catalogueItem;
}
private static void fillAllCatalogueDetails(Cursor c, CatalogueItem.CatalogueItemBuilder builder) {
builder.setId(c.getLong(INDEX_CATALOGUE_ITEM_ID));
builder.setCatalogueId(c.getLong(INDEX_CATALOGUE_ITEM_CATALOGUE_ID));
builder.setSysId(c.getString(INDEX_CATALOGUE_ITEM_SYS_ID));
builder.setName(c.getString(INDEX_CATALOGUE_ITEM_NAME));
builder.setShortDescription(c.getString(INDEX_CATALOGUE_ITEM_SHORT_DESCRIPTION));
builder.setDescription(c.getString(INDEX_CATALOGUE_ITEM_DESCRIPTION));
builder.setIcon(c.getString(INDEX_CATALOGUE_ITEM_ICON));
builder.setSyncDirty(c.getInt(INDEX_CATALOGUE_ITEM_SYNC_DIRTY));
}
private static ContentValues getContentValues(CatalogueItem catalogueItem) {
ContentValues cv = new ContentValues(CATALOGUE_ITEM_COLUMN_COUNT - 1);
cv.put(CATALOGUE_ITEM_CATALOGUE_ID, catalogueItem.getCatalogueId());
cv.put(CATALOGUE_ITEM_SYS_ID, catalogueItem.getSysId());
cv.put(CATALOGUE_ITEM_NAME, catalogueItem.getName());
cv.put(CATALOGUE_ITEM_SHORT_DESCRIPTION, catalogueItem.getShortDescription());
cv.put(CATALOGUE_ITEM_DESCRIPTION, catalogueItem.getDescription());
cv.put(CATALOGUE_ITEM_ICON, catalogueItem.getIcon());
cv.put(CATALOGUE_ITEM_SYNC_DIRTY, catalogueItem.getSyncDirty());
return cv;
}
}
\ No newline at end of file
......@@ -8,6 +8,9 @@ import com.google.gson.annotations.SerializedName;
*/
public class CatalogueItem {
private long id = -1;
private long catalogue_id = -1;
@SerializedName("short_description")
@Expose
private String shortDescription;
......@@ -24,6 +27,24 @@ public class CatalogueItem {
@Expose
private String icon;
private int syncDirty;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getCatalogueId() {
return catalogue_id;
}
public void setCatalogueId(long catalogueId) {
this.catalogue_id = catalogueId;
}
/**
*
* @return
......@@ -114,6 +135,89 @@ public class CatalogueItem {
this.icon = icon;
}
public int getSyncDirty() {
return syncDirty;
}
public void setSyncDirty(int syncDirty) {
this.syncDirty = syncDirty;
}
public static final class CatalogueItemBuilder {
private long id = -1;
private long catalogue_id = -1;
private String shortDescription;
private String description;
private String name;
private String sysId;
private String icon;
private int syncDirty;
private CatalogueItemBuilder() {
}
public static CatalogueItemBuilder aCatalogueItem() {
return new CatalogueItemBuilder();
}
public CatalogueItemBuilder setId(long id) {
this.id = id;
return this;
}
public CatalogueItemBuilder setCatalogueId(long catalogueId) {
this.catalogue_id = catalogueId;
return this;
}
public CatalogueItemBuilder setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
return this;
}
public CatalogueItemBuilder setDescription(String description) {
this.description = description;
return this;
}
public CatalogueItemBuilder setName(String name) {
this.name = name;
return this;
}
public CatalogueItemBuilder setSysId(String sysId) {
this.sysId = sysId;
return this;
}
public CatalogueItemBuilder setIcon(String icon) {
this.icon = icon;
return this;
}
public CatalogueItemBuilder setSyncDirty(int syncDirty) {
this.syncDirty = syncDirty;
return this;
}
public CatalogueItemBuilder but() {
return aCatalogueItem().setId(id).setCatalogueId(catalogue_id).setShortDescription(shortDescription).setDescription(description).setName(name).setSysId(sysId).setIcon(icon).setSyncDirty(syncDirty);
}
public CatalogueItem build() {
CatalogueItem catalogueItem = new CatalogueItem();
catalogueItem.setId(id);
catalogueItem.setCatalogueId(catalogue_id);
catalogueItem.setShortDescription(shortDescription);
catalogueItem.setDescription(description);
catalogueItem.setName(name);
catalogueItem.setSysId(sysId);
catalogueItem.setIcon(icon);
catalogueItem.setSyncDirty(syncDirty);
return catalogueItem;
}
}
public static class Json {
public static final String URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE = "category";
public static final String SYS_ID = "sys_id";
......@@ -122,11 +226,14 @@ public class CatalogueItem {
@Override
public String toString() {
return "CatalogueItem{" +
"shortDescription='" + shortDescription + '\'' +
"id=" + id +
", catalogue_id=" + catalogue_id +
", shortDescription='" + shortDescription + '\'' +
", description='" + description + '\'' +
", name='" + name + '\'' +
", sysId='" + sysId + '\'' +
", icon='" + icon + '\'' +
", syncDirty=" + syncDirty +
'}';
}
}
......@@ -21,6 +21,9 @@ import com.vsoft.uoflservicenow.R;
import com.vsoft.uoflservicenow.adapters.CatalogueCategoryItemAdapter;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueItemApiListener;
import com.vsoft.uoflservicenow.api.managers.CatalogueItemApiManager;
import com.vsoft.uoflservicenow.db.managers.CatalogueItemManager;
import com.vsoft.uoflservicenow.db.managers.CatalogueManager;
import com.vsoft.uoflservicenow.db.models.Catalogue;
import com.vsoft.uoflservicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
......@@ -42,8 +45,7 @@ public class CatalogueItemScreen extends AppCompatActivity {
@BindView(R.id.catalogue_item_screen_list_view) ListView mListView;
@BindView(R.id.catalogue_item_screen_empty_text_view) TextView mEmptyTextView;
private String mCatalogueSysId;
private List<CatalogueItem> mCatalogueItemList;
private Catalogue mCatalogue;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -54,11 +56,17 @@ public class CatalogueItemScreen extends AppCompatActivity {
ButterKnife.bind(this);
CatalogueApplication application = (CatalogueApplication) getApplication();
mCatalogueSysId = getIntent().getExtras().getString(Constants.DATA_KEY_SYS_ID);
String catalogueTitle = getIntent().getExtras().getString(Constants.DATA_KEY_CATALOGUE_TITLE);
Bundle extras = getIntent().getExtras();
String catalogueSysId = null;
if (extras != null) {
catalogueSysId = extras.getString(Constants.DATA_KEY_SYS_ID);
//The key argument here must match that used in the other activity
}
if (mCatalogueSysId == null) {
CatalogueLog.e("CatalogueItemScreen: mCatalogueSysId is null");
mCatalogue = CatalogueManager.getCatalogueFromSysId(catalogueSysId);
if(mCatalogue == null) {
CatalogueLog.e("CatalogueItemScreen: onCreate: mCatalogue is null");
return;
}
setSupportActionBar(mToolbar);
......@@ -66,7 +74,7 @@ public class CatalogueItemScreen extends AppCompatActivity {
if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setElevation(0);
actionBar.setTitle(catalogueTitle);
actionBar.setTitle(mCatalogue.getTitle());
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
}
......@@ -75,11 +83,16 @@ public class CatalogueItemScreen extends AppCompatActivity {
// Send initial screen view hit.
Util.sendScreenName(tracker, actionBar.getTitle().toString());
List<CatalogueItem> catalogueItemList = CatalogueItemManager.getAllCatalogueItems(mCatalogue.getId());
if(catalogueItemList.isEmpty()) {
if(application.isNetConnected()) {
new FetchCatalogueItem().execute();
} else {
DialogUtils.showNoConnectionDialogWithCloseActivity(CatalogueItemScreen.this);
}
} else {
setData(catalogueItemList);
}
}
class FetchCatalogueItem extends AsyncTask<String, Void, SyncStatus> {
......@@ -96,11 +109,11 @@ public class CatalogueItemScreen extends AppCompatActivity {
@Override
protected SyncStatus doInBackground(String... params) {
return CatalogueItemApiManager.getCatalogueItems(mCatalogueSysId, new GetCatalogueItemApiListener() {
return CatalogueItemApiManager.getCatalogueItems(mCatalogue.getSysId(), new GetCatalogueItemApiListener() {
@Override
public void onDoneApiCall(List<CatalogueItem> catalogueItemList) {
CatalogueLog.e("Data: catalogueItemList: "+catalogueItemList);
mCatalogueItemList = catalogueItemList;
CatalogueItemManager.handleGetCatalogueItem(mCatalogue.getId(), catalogueItemList);
}
});
}
......@@ -112,21 +125,19 @@ public class CatalogueItemScreen extends AppCompatActivity {
progressDialog.dismiss();
}
if(syncStatus == SyncStatus.SUCCESS) {
if (mCatalogueItemList != null) {
setData();
}
setData(CatalogueItemManager.getAllCatalogueItems(mCatalogue.getId()));
} else {
showErrorDialog(R.string.failed_to_fetch_catalogue_category_items_string);
}
}
}
private void setData() {
if(!mCatalogueItemList.isEmpty()) {
private void setData(final List<CatalogueItem> catalogueItemList) {
if(!catalogueItemList.isEmpty()) {
mListView.setVisibility(View.VISIBLE);
mEmptyTextView.setVisibility(View.GONE);
CatalogueCategoryItemAdapter adapter = new CatalogueCategoryItemAdapter(CatalogueItemScreen.this);
adapter.setCatalogueItemList(mCatalogueItemList);
adapter.setCatalogueItemList(catalogueItemList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
......@@ -134,12 +145,12 @@ public class CatalogueItemScreen extends AppCompatActivity {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CatalogueLog.e("OnItemClickListener: position: " + position + ", Catalogue: " + mCatalogueItemList.get(position));
CatalogueLog.e("OnItemClickListener: position: " + position + ", Catalogue: " + catalogueItemList.get(position));
Intent intent = new Intent(CatalogueItemScreen.this, CatalogueVariableScreen.class);
intent.putExtra(Constants.DATA_KEY_SYS_ID, mCatalogueItemList.get(position).getSysId());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_DESCRIPTION, mCatalogueItemList.get(position).getDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_SHORT_DESCRIPTION, mCatalogueItemList.get(position).getShortDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_TITLE, mCatalogueItemList.get(position).getName());
intent.putExtra(Constants.DATA_KEY_SYS_ID, catalogueItemList.get(position).getSysId());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_DESCRIPTION, catalogueItemList.get(position).getDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_SHORT_DESCRIPTION, catalogueItemList.get(position).getShortDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_TITLE, catalogueItemList.get(position).getName());
startActivity(intent);
}
});
......
......@@ -3,6 +3,7 @@ package com.vsoft.uoflservicenow.utils;
public interface DBConstants {
//Tables
String TABLE_CATALOGUE = "catalogue_category";
String TABLE_CATALOGUE_ITEM = "catalogue_category_item";
String ID = "_id";
String SYS_ID = "sys_id";
......@@ -34,4 +35,31 @@ public interface DBConstants {
int INDEX_CATALOGUE_SYNC_DIRTY = 5;
int CATALOGUE_COLUMN_COUNT = 6;
/**
* Catalogue_item table
*/
String CATALOGUE_ITEM_ID = ID;
String CATALOGUE_ITEM_CATALOGUE_ID = "catalogue_id";
String CATALOGUE_ITEM_SYS_ID = SYS_ID;
String CATALOGUE_ITEM_NAME = "name";
String CATALOGUE_ITEM_SHORT_DESCRIPTION = "short_description";
String CATALOGUE_ITEM_DESCRIPTION = "item_description";
String CATALOGUE_ITEM_ICON = "icon";
String CATALOGUE_ITEM_SYNC_DIRTY = SYNC_DIRTY;
/**
* Indices for Catalogue_item table. *Use these only if you fetch all columns*
*/
int INDEX_CATALOGUE_ITEM_ID = 0;
int INDEX_CATALOGUE_ITEM_CATALOGUE_ID = 1;
int INDEX_CATALOGUE_ITEM_SYS_ID = 2;
int INDEX_CATALOGUE_ITEM_NAME = 3;
int INDEX_CATALOGUE_ITEM_SHORT_DESCRIPTION = 4;
int INDEX_CATALOGUE_ITEM_DESCRIPTION = 5;
int INDEX_CATALOGUE_ITEM_ICON = 6;
int INDEX_CATALOGUE_ITEM_SYNC_DIRTY = 7;
int CATALOGUE_ITEM_COLUMN_COUNT = 8;
}
\ 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