Commit 52366a4b by Kunj Gupta

Added Local DB table for storing Variable form input values.

parent b1352af9
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.CatalogueVariable;
import com.vsoft.uoflservicenow.enums.ViewType;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.DBConstants;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Kunj on 11-08-2016.
*/
public class CatalogueVariableValueManager implements DBConstants {
public static long save(String tableName, List<String> columnNameList, List<String> valueList, int syncDirty) {
CatalogueLog.e("CatalogueVariableValueManager: save");
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
ContentValues contentValues = new ContentValues(columnNameList.size());
contentValues.put(CATALOGUE_VARIABLE_SYNC_DIRTY, syncDirty);
for (int i = 0; i < columnNameList.size(); i++) {
String columnName = columnNameList.get(i);
String value = valueList.get(i);
if(columnName!=null && value!=null)
contentValues.put(columnName, value);
}
long id = db.insert(tableName, null, contentValues);
return id;
} else {
return -1;
}
}
public static int delete(CatalogueVariable catalogueVariable) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
if (catalogueVariable.getSysId() == null || catalogueVariable.getSysId().isEmpty()) {
return db.delete(TABLE_CATALOGUE_VARIABLES, CATALOGUE_VARIABLE_ID + "=" + catalogueVariable.getId(), null);
} else {
return update(catalogueVariable, SYNC_FLAG_DELETE);
}
}
return -1;
}
public static int update(CatalogueVariable catalogueVariable, int syncDirty) {
return update(catalogueVariable, null, syncDirty);
}
public static int update(CatalogueVariable catalogueVariable, List<String> column, int syncDirty) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
catalogueVariable.setSyncDirty(syncDirty);
if (column == null || column.size() == 0) {
return db.update(TABLE_CATALOGUE_VARIABLES, getContentValues(catalogueVariable), CATALOGUE_VARIABLE_ID + "=" + catalogueVariable.getId(), null);
} else {
ContentValues contentValues = new ContentValues(column.size());
contentValues.put(CATALOGUE_VARIABLE_SYNC_DIRTY, catalogueVariable.getSyncDirty());
for (int i = 0; i < column.size(); i++) {
String columnName = column.get(i);
if (CATALOGUE_VARIABLE_CATALOGUE_ITEM_ID.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_CATALOGUE_ITEM_ID, catalogueVariable.getCatalogueItemId());
} else if (CATALOGUE_VARIABLE_NAME.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_NAME, catalogueVariable.getName());
} else if (CATALOGUE_VARIABLE_QUESTION_TEXT.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_QUESTION_TEXT, catalogueVariable.getQuestionText());
} else if (CATALOGUE_VARIABLE_TYPE.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_TYPE, ViewType.getId(catalogueVariable.getType()));
} else if (CATALOGUE_VARIABLE_SYS_ID.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_SYS_ID, catalogueVariable.getSysId());
} else if (CATALOGUE_VARIABLE_SYNC_DIRTY.equals(columnName)) {
contentValues.put(CATALOGUE_VARIABLE_SYNC_DIRTY, catalogueVariable.getSyncDirty());
}
}
return db.update(TABLE_CATALOGUE_VARIABLES, contentValues, CATALOGUE_VARIABLE_ID + "=" + catalogueVariable.getId(), null);
}
} else {
return -1;
}
}
public static List<CatalogueVariable> getAllVariable(long catalogueItemId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_VARIABLES
+ " where " + CATALOGUE_VARIABLE_CATALOGUE_ITEM_ID + "=" + catalogueItemId
+ " and " + CATALOGUE_VARIABLE_SYNC_DIRTY + "!=" + DBConstants.SYNC_FLAG_DELETE, null);
ArrayList<CatalogueVariable> variableList;
if (c.getCount() > 0) {
variableList = new ArrayList<>(c.getCount());
while (c.moveToNext()) {
CatalogueVariable.CatalogueVariableBuilder builder = CatalogueVariable.CatalogueVariableBuilder.aCatalogueVariable();
fillAllVariableDetails(c, builder);
variableList.add(builder.build());
}
} else {
variableList = new ArrayList<>(0);
}
c.close();
return variableList;
} else {
return new ArrayList<>(0);
}
}
public static CatalogueVariable get(long catalogueId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
CatalogueVariable catalogueVariable = null;
if (db != null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_VARIABLES + " where " + CATALOGUE_VARIABLE_ID + "=" + catalogueId, null);
if (c.moveToFirst()) {
CatalogueVariable.CatalogueVariableBuilder builder = CatalogueVariable.CatalogueVariableBuilder.aCatalogueVariable();
fillAllVariableDetails(c, builder);
catalogueVariable = builder.build();
}
c.close();
}
return catalogueVariable;
}
public static CatalogueVariable getVariableFromSysId(String sysId) {
SQLiteDatabase db = CatalogueApplication.getDatabase();
CatalogueVariable catalogueVariable = null;
if(db!=null) {
Cursor c = db.rawQuery("select * from " + TABLE_CATALOGUE_VARIABLES + " where " + CATALOGUE_VARIABLE_SYS_ID + "='" + sysId + "'", null);
if (c.moveToFirst()) {
CatalogueVariable.CatalogueVariableBuilder builder = CatalogueVariable.CatalogueVariableBuilder.aCatalogueVariable();
fillAllVariableDetails(c, builder);
catalogueVariable = builder.build();
}
c.close();
}
return catalogueVariable;
}
private static void fillAllVariableDetails(Cursor c, CatalogueVariable.CatalogueVariableBuilder builder) {
builder.setId(c.getLong(INDEX_CATALOGUE_VARIABLE_ID));
builder.setCatalogueItemId(c.getLong(INDEX_CATALOGUE_VARIABLE_CATALOGUE_ITEM_ID));
builder.setSysId(c.getString(INDEX_CATALOGUE_VARIABLE_SYS_ID));
builder.setName(c.getString(INDEX_CATALOGUE_VARIABLE_NAME));
builder.setQuestionText(c.getString(INDEX_CATALOGUE_VARIABLE_QUESTION_TEXT));
builder.setType(ViewType.from(c.getInt(INDEX_CATALOGUE_VARIABLE_TYPE)));
builder.setSyncDirty(c.getInt(INDEX_CATALOGUE_VARIABLE_SYNC_DIRTY));
}
private static ContentValues getContentValues(CatalogueVariable catalogueVariable) {
ContentValues cv = new ContentValues(CATALOGUE_VARIABLE_COLUMN_COUNT - 1);
cv.put(CATALOGUE_VARIABLE_CATALOGUE_ITEM_ID, catalogueVariable.getCatalogueItemId());
cv.put(CATALOGUE_VARIABLE_SYS_ID, catalogueVariable.getSysId());
cv.put(CATALOGUE_VARIABLE_NAME, catalogueVariable.getName());
cv.put(CATALOGUE_VARIABLE_QUESTION_TEXT, catalogueVariable.getQuestionText());
cv.put(CATALOGUE_VARIABLE_TYPE, catalogueVariable.getType().getId());
cv.put(CATALOGUE_VARIABLE_SYNC_DIRTY, catalogueVariable.getSyncDirty());
return cv;
}
}
\ No newline at end of file
......@@ -26,6 +26,7 @@ import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.google.android.gms.analytics.Tracker;
import com.vsoft.uoflservicenow.CatalogueApplication;
......@@ -37,6 +38,7 @@ import com.vsoft.uoflservicenow.api.managers.VariableChoiceApiManager;
import com.vsoft.uoflservicenow.api.managers.VariableChoiceManager;
import com.vsoft.uoflservicenow.db.managers.CatalogueItemManager;
import com.vsoft.uoflservicenow.db.managers.CatalogueVariableManager;
import com.vsoft.uoflservicenow.db.managers.CatalogueVariableValueManager;
import com.vsoft.uoflservicenow.db.models.CatalogueItem;
import com.vsoft.uoflservicenow.db.models.CatalogueVariable;
import com.vsoft.uoflservicenow.db.models.Reference;
......@@ -48,6 +50,7 @@ import com.vsoft.uoflservicenow.listeners.ReferenceListener;
import com.vsoft.uoflservicenow.ui.supportviews.DateAndTimePickerFragment;
import com.vsoft.uoflservicenow.utils.CatalogueLog;
import com.vsoft.uoflservicenow.utils.Constants;
import com.vsoft.uoflservicenow.utils.DBConstants;
import com.vsoft.uoflservicenow.utils.DialogUtils;
import com.vsoft.uoflservicenow.utils.TagObject;
import com.vsoft.uoflservicenow.utils.Util;
......@@ -166,8 +169,6 @@ public class CatalogueVariableScreen extends AppCompatActivity {
});
}
}
/*Create Dynamic table for CatalogueVariable*/
Util.createDynamicTable(mCatalogueItem.getSysId(), catalogueVariableList);
}
}
});
......@@ -404,10 +405,14 @@ public class CatalogueVariableScreen extends AppCompatActivity {
if(hasErrors)
return;
if(mApplication.isNetConnected()) {
new SubmitForm().execute();
/*Create Dynamic table for CatalogueVariable Values*/
Util.createDynamicTable(mCatalogueItem.getSysId(), mCatalogueVariableList);
/*Save data in local DB*/
long id = CatalogueVariableValueManager.save("t_" + mCatalogueItem.getSysId(), columnList, valueList, DBConstants.SYNC_FLAG_CREATE);
if(id == -1) {
Toast.makeText(CatalogueVariableScreen.this, "Not saved: id: "+id, Toast.LENGTH_LONG).show();
} else {
DialogUtils.showNoConnectionDialogWithCloseActivity(CatalogueVariableScreen.this);
Toast.makeText(CatalogueVariableScreen.this, "Saved: id: "+id, Toast.LENGTH_LONG).show();
}
}
......
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