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 { ...@@ -28,6 +28,7 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
createCatalogueTable(db); createCatalogueTable(db);
createCatalogueItemsTable(db);
} }
@Override @Override
...@@ -50,4 +51,17 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants { ...@@ -50,4 +51,17 @@ public class DBManager extends SQLiteOpenHelper implements DBConstants {
+ CATALOGUE_SYNC_DIRTY + " integer default " + SYNC_FLAG_NONE + 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
+ ");");
}
} }
...@@ -8,6 +8,9 @@ import com.google.gson.annotations.SerializedName; ...@@ -8,6 +8,9 @@ import com.google.gson.annotations.SerializedName;
*/ */
public class CatalogueItem { public class CatalogueItem {
private long id = -1;
private long catalogue_id = -1;
@SerializedName("short_description") @SerializedName("short_description")
@Expose @Expose
private String shortDescription; private String shortDescription;
...@@ -24,6 +27,24 @@ public class CatalogueItem { ...@@ -24,6 +27,24 @@ public class CatalogueItem {
@Expose @Expose
private String icon; 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 * @return
...@@ -114,6 +135,89 @@ public class CatalogueItem { ...@@ -114,6 +135,89 @@ public class CatalogueItem {
this.icon = icon; 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 class Json {
public static final String URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE = "category"; public static final String URL_PARAM_CATALOGUE_SYSPRM_QUERY_VALUE = "category";
public static final String SYS_ID = "sys_id"; public static final String SYS_ID = "sys_id";
...@@ -122,11 +226,14 @@ public class CatalogueItem { ...@@ -122,11 +226,14 @@ public class CatalogueItem {
@Override @Override
public String toString() { public String toString() {
return "CatalogueItem{" + return "CatalogueItem{" +
"shortDescription='" + shortDescription + '\'' + "id=" + id +
", catalogue_id=" + catalogue_id +
", shortDescription='" + shortDescription + '\'' +
", description='" + description + '\'' + ", description='" + description + '\'' +
", name='" + name + '\'' + ", name='" + name + '\'' +
", sysId='" + sysId + '\'' + ", sysId='" + sysId + '\'' +
", icon='" + icon + '\'' + ", icon='" + icon + '\'' +
", syncDirty=" + syncDirty +
'}'; '}';
} }
} }
...@@ -21,6 +21,9 @@ import com.vsoft.uoflservicenow.R; ...@@ -21,6 +21,9 @@ import com.vsoft.uoflservicenow.R;
import com.vsoft.uoflservicenow.adapters.CatalogueCategoryItemAdapter; import com.vsoft.uoflservicenow.adapters.CatalogueCategoryItemAdapter;
import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueItemApiListener; import com.vsoft.uoflservicenow.api.listeners.get.GetCatalogueItemApiListener;
import com.vsoft.uoflservicenow.api.managers.CatalogueItemApiManager; 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.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.enums.SyncStatus; import com.vsoft.uoflservicenow.enums.SyncStatus;
import com.vsoft.uoflservicenow.utils.CatalogueLog; import com.vsoft.uoflservicenow.utils.CatalogueLog;
...@@ -42,8 +45,7 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -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_list_view) ListView mListView;
@BindView(R.id.catalogue_item_screen_empty_text_view) TextView mEmptyTextView; @BindView(R.id.catalogue_item_screen_empty_text_view) TextView mEmptyTextView;
private String mCatalogueSysId; private Catalogue mCatalogue;
private List<CatalogueItem> mCatalogueItemList;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -54,11 +56,17 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -54,11 +56,17 @@ public class CatalogueItemScreen extends AppCompatActivity {
ButterKnife.bind(this); ButterKnife.bind(this);
CatalogueApplication application = (CatalogueApplication) getApplication(); CatalogueApplication application = (CatalogueApplication) getApplication();
mCatalogueSysId = getIntent().getExtras().getString(Constants.DATA_KEY_SYS_ID); Bundle extras = getIntent().getExtras();
String catalogueTitle = getIntent().getExtras().getString(Constants.DATA_KEY_CATALOGUE_TITLE); 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) { mCatalogue = CatalogueManager.getCatalogueFromSysId(catalogueSysId);
CatalogueLog.e("CatalogueItemScreen: mCatalogueSysId is null"); if(mCatalogue == null) {
CatalogueLog.e("CatalogueItemScreen: onCreate: mCatalogue is null");
return;
} }
setSupportActionBar(mToolbar); setSupportActionBar(mToolbar);
...@@ -66,7 +74,7 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -66,7 +74,7 @@ public class CatalogueItemScreen extends AppCompatActivity {
if(actionBar != null) { if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setElevation(0); actionBar.setElevation(0);
actionBar.setTitle(catalogueTitle); actionBar.setTitle(mCatalogue.getTitle());
actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true); actionBar.setDisplayShowTitleEnabled(true);
} }
...@@ -75,11 +83,16 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -75,11 +83,16 @@ public class CatalogueItemScreen extends AppCompatActivity {
// Send initial screen view hit. // Send initial screen view hit.
Util.sendScreenName(tracker, actionBar.getTitle().toString()); Util.sendScreenName(tracker, actionBar.getTitle().toString());
List<CatalogueItem> catalogueItemList = CatalogueItemManager.getAllCatalogueItems(mCatalogue.getId());
if(catalogueItemList.isEmpty()) {
if(application.isNetConnected()) { if(application.isNetConnected()) {
new FetchCatalogueItem().execute(); new FetchCatalogueItem().execute();
} else { } else {
DialogUtils.showNoConnectionDialogWithCloseActivity(CatalogueItemScreen.this); DialogUtils.showNoConnectionDialogWithCloseActivity(CatalogueItemScreen.this);
} }
} else {
setData(catalogueItemList);
}
} }
class FetchCatalogueItem extends AsyncTask<String, Void, SyncStatus> { class FetchCatalogueItem extends AsyncTask<String, Void, SyncStatus> {
...@@ -96,11 +109,11 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -96,11 +109,11 @@ public class CatalogueItemScreen extends AppCompatActivity {
@Override @Override
protected SyncStatus doInBackground(String... params) { protected SyncStatus doInBackground(String... params) {
return CatalogueItemApiManager.getCatalogueItems(mCatalogueSysId, new GetCatalogueItemApiListener() { return CatalogueItemApiManager.getCatalogueItems(mCatalogue.getSysId(), new GetCatalogueItemApiListener() {
@Override @Override
public void onDoneApiCall(List<CatalogueItem> catalogueItemList) { public void onDoneApiCall(List<CatalogueItem> catalogueItemList) {
CatalogueLog.e("Data: catalogueItemList: "+catalogueItemList); CatalogueLog.e("Data: catalogueItemList: "+catalogueItemList);
mCatalogueItemList = catalogueItemList; CatalogueItemManager.handleGetCatalogueItem(mCatalogue.getId(), catalogueItemList);
} }
}); });
} }
...@@ -112,21 +125,19 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -112,21 +125,19 @@ public class CatalogueItemScreen extends AppCompatActivity {
progressDialog.dismiss(); progressDialog.dismiss();
} }
if(syncStatus == SyncStatus.SUCCESS) { if(syncStatus == SyncStatus.SUCCESS) {
if (mCatalogueItemList != null) { setData(CatalogueItemManager.getAllCatalogueItems(mCatalogue.getId()));
setData();
}
} else { } else {
showErrorDialog(R.string.failed_to_fetch_catalogue_category_items_string); showErrorDialog(R.string.failed_to_fetch_catalogue_category_items_string);
} }
} }
} }
private void setData() { private void setData(final List<CatalogueItem> catalogueItemList) {
if(!mCatalogueItemList.isEmpty()) { if(!catalogueItemList.isEmpty()) {
mListView.setVisibility(View.VISIBLE); mListView.setVisibility(View.VISIBLE);
mEmptyTextView.setVisibility(View.GONE); mEmptyTextView.setVisibility(View.GONE);
CatalogueCategoryItemAdapter adapter = new CatalogueCategoryItemAdapter(CatalogueItemScreen.this); CatalogueCategoryItemAdapter adapter = new CatalogueCategoryItemAdapter(CatalogueItemScreen.this);
adapter.setCatalogueItemList(mCatalogueItemList); adapter.setCatalogueItemList(catalogueItemList);
mListView.setAdapter(adapter); mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...@@ -134,12 +145,12 @@ public class CatalogueItemScreen extends AppCompatActivity { ...@@ -134,12 +145,12 @@ public class CatalogueItemScreen extends AppCompatActivity {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { 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 intent = new Intent(CatalogueItemScreen.this, CatalogueVariableScreen.class);
intent.putExtra(Constants.DATA_KEY_SYS_ID, mCatalogueItemList.get(position).getSysId()); intent.putExtra(Constants.DATA_KEY_SYS_ID, catalogueItemList.get(position).getSysId());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_DESCRIPTION, mCatalogueItemList.get(position).getDescription()); intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_DESCRIPTION, catalogueItemList.get(position).getDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_SHORT_DESCRIPTION, mCatalogueItemList.get(position).getShortDescription()); intent.putExtra(Constants.DATA_KEY_CATALOGUE_ITEM_SHORT_DESCRIPTION, catalogueItemList.get(position).getShortDescription());
intent.putExtra(Constants.DATA_KEY_CATALOGUE_TITLE, mCatalogueItemList.get(position).getName()); intent.putExtra(Constants.DATA_KEY_CATALOGUE_TITLE, catalogueItemList.get(position).getName());
startActivity(intent); startActivity(intent);
} }
}); });
......
...@@ -3,6 +3,7 @@ package com.vsoft.uoflservicenow.utils; ...@@ -3,6 +3,7 @@ package com.vsoft.uoflservicenow.utils;
public interface DBConstants { public interface DBConstants {
//Tables //Tables
String TABLE_CATALOGUE = "catalogue_category"; String TABLE_CATALOGUE = "catalogue_category";
String TABLE_CATALOGUE_ITEM = "catalogue_category_item";
String ID = "_id"; String ID = "_id";
String SYS_ID = "sys_id"; String SYS_ID = "sys_id";
...@@ -34,4 +35,31 @@ public interface DBConstants { ...@@ -34,4 +35,31 @@ public interface DBConstants {
int INDEX_CATALOGUE_SYNC_DIRTY = 5; int INDEX_CATALOGUE_SYNC_DIRTY = 5;
int CATALOGUE_COLUMN_COUNT = 6; 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