Example #1
0
        private GeneralEntityDataset getEntityDataset(List <NameValuePair> data, DateTime dateEdited)
        {
            KindKey       kindKey      = null;
            NameValuePair creationDate = null;
            var           metaData     = new KindMetaData()
            {
                chksum = 1,
                devid  = AppInstance.Instance.Configuration.Serial,
                facidx = 0
            };

            //we check if we are in an edit context and read values from there
            if (isInEditMode())
            {
                var jsonOldRecord   = this.Intent.GetStringExtra(Constants.BUNDLE_DATATOEDIT);
                var oldRecordEntity = JsonConvert
                                      .DeserializeObject <GeneralEntityDataset>(jsonOldRecord);
                kindKey      = new KindKey(oldRecordEntity.Id.Value);
                creationDate = oldRecordEntity.GetValue(Constants.SYS_FIELD_DATECREATED);
                if (!string.IsNullOrWhiteSpace(oldRecordEntity.KindMetaData))
                {
                    var oldMetaData = new KindMetaData().fromJson(new KindItem(oldRecordEntity.KindMetaData));
                    metaData.chksum = oldMetaData.chksum + 1;
                }
            }
            else
            {
                kindKey = new KindKey(AppInstance.Instance
                                      .LocalEntityStoreInstance.InstanceLocalDb.newId());
            }

            KindKey entityId = null;

            if (IsRegistrationEndPage())
            {
                //assign a key
                entityId = new KindKey(kindKey.Value);
                //we get the device size
                var moduleClientSummaries = getModuleClientSummaries(data);
                data.AddRange(moduleClientSummaries);
            }
            else
            {
                //also update client details but only if they have changes
                entityId = new KindKey(CurrentClient.EntityId.Value);
            }

            if (creationDate == null)
            {
                data.Add(new NameValuePair()
                {
                    Name  = Constants.SYS_FIELD_DATECREATED,
                    Value = dateEdited.ToString(System.Globalization.CultureInfo.InvariantCulture)
                });
            }

            var editDate = data
                           .Where(t => t.Name.Contains(Constants.SYS_FIELD_DATEEDITED))
                           .FirstOrDefault();

            if (editDate == null)
            {
                data.Add(new NameValuePair()
                {
                    Name  = Constants.SYS_FIELD_DATEEDITED,
                    Value = dateEdited.ToString(System.Globalization.CultureInfo.InvariantCulture)
                });
            }
            data.Add(new NameValuePair()
            {
                Name  = Constants.FIELD_ID,
                Value = kindKey.Value
            });
            data.Add(new NameValuePair()
            {
                Name  = Constants.FIELD_ENTITYID,
                Value = entityId.Value
            });

            var saveable = new GeneralEntityDataset()
            {
                Id           = kindKey,
                EntityId     = entityId,
                FormName     = _kindName.Value,
                FieldValues  = data,
                KindMetaData = metaData.getJson()
            };

            return(saveable);
        }
Example #2
0
        /// <summary>
        /// Returns matching entities by key supplied
        /// </summary>
        /// <param name="entityId">Id to search by</param>
        /// <returns>List of matching entities</returns>
        private List <KindItem> GetBlob(KindKey entityId = null)
        {
            var toReturn = new List <KindItem>();

            using (var conn = new SqlConnection(_db.ConnectionString))
            {
                try
                {
                    conn.Open();
                }
                catch (SqlException ex)
                {
                    if (MainLogger != null)
                    {
                        MainLogger.Log(string.Format(
                                           "Error opening database connection{0}{1}", Environment.NewLine, ex.ToString()));
                    }
                    return(new List <KindItem>()
                    {
                        new KindItem(Constants.DBSAVE_ERROR)
                    });
                }

                try
                {
                    //check if our table tables, create if it doesn't
                    var command = conn.CreateCommand();
                    if (entityId == null)
                    {
                        command.CommandText = string.Format(selectDatablobs, _tableName.Value);
                    }
                    else
                    {
                        command.CommandText = string.Format(selectDatablobsById, _tableName.Value);
                        command.Parameters.AddWithValue("@id", entityId.Value);
                    }
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        toReturn.Add(new KindItem(reader.GetString(0)));
                    }
                }
                catch (SqlException ex)
                {
                    if (MainLogger != null)
                    {
                        MainLogger.Log(string.Format(
                                           "Error reading data from database{0}{1}", Environment.NewLine, ex.ToString()));
                    }
                    return(new List <KindItem>()
                    {
                        new KindItem(Constants.DBSAVE_ERROR)
                    });
                }
                finally
                {
                    conn.Close();
                }
            }
            return(toReturn);
        }