public override int Update(List <T> clients) { foreach (var client in clients) { var ge = new GeneralEntityDataset() { EntityId = client.EntityId, FormName = _kindName, Id = client.Id, FieldValues = client.ToValuesList(), KindMetaData = client.KindMetaData }; var saveable = new DbSaveableEntity(ge); saveable.Save(); } return(0); }
private async void SaveUserOptions_Click(object sender, EventArgs e) { var userAuthenticator = new UserAuthenticator(); var userCreds = userAuthenticator.LoadCredentials(); var tNames = FindViewById <EditText>(Resource.Id.tUserNames); var tusername = FindViewById <EditText>(Resource.Id.tUserName); var tpasscode = FindViewById <EditText>(Resource.Id.tUserPassCode); var tpasscodAgain = FindViewById <EditText>(Resource.Id.tUserPassCodeAgain); if (string.IsNullOrWhiteSpace(tNames.Text) || string.IsNullOrWhiteSpace(tusername.Text) || string.IsNullOrWhiteSpace(tpasscode.Text) || (tpasscode.Text != tpasscodAgain.Text)) { showDialog("Alert", "UserName and Passcode are both required, and Passcodes should match"); return; } var uname = tusername.Text.ToLowerInvariant(); var passcode = Convert.ToInt32(tpasscode.Text); var hash = userAuthenticator.computeHash(uname, passcode); var matchingCred = (from cred in userCreds where cred.UserId == uname select cred).FirstOrDefault(); AppUser user = null; if (matchingCred == null) { //means we're ading a new user Toast.MakeText(this, "Creating new user", ToastLength.Long); var id = AppInstance.Instance.LocalEntityStoreInstance.InstanceLocalDb.newId(); user = new AppUser() { Id = new KindKey(id), EntityId = new KindKey(id), UserId = uname, Names = tNames.Text.ToUpperInvariant(), KnownBolg = hash, KindMetaData = (new KindMetaData() { chksum = 1, devid = AppInstance.Instance.Configuration.Serial, facidx = 0 } ).getJson() }; } else { //confirm with the user Toast.MakeText(this, "User found. Updating record", ToastLength.Long); user = matchingCred; user.KnownBolg = hash; user.Names = tNames.Text.ToUpperInvariant(); if (user.EntityId == null) { user.EntityId = user.Id; } if (string.IsNullOrWhiteSpace(user.KindMetaData)) { user.KindMetaData = (new KindMetaData() { chksum = 1, devid = AppInstance.Instance.Configuration.Serial, facidx = 0 } ).getJson(); } else { var metadata = new KindMetaData().fromJson(new KindItem(user.KindMetaData)); metadata.chksum += 1; user.KindMetaData = metadata.getJson(); } } //we save to the database Toast.MakeText(this, "Saving to database", ToastLength.Long); var saveableEntity = new DbSaveableEntity(user) { kindName = UserAuthenticator.KindName }; saveableEntity.Save(); Toast.MakeText(this, "Changes saved", ToastLength.Long); //we reset the form tNames.Text = ""; tusername.Text = ""; tpasscode.Text = ""; tpasscodAgain.Text = ""; //save to cloud var dbentity = new DbSaveableEntity(user.asGeneralEntity(UserAuthenticator.KindName)) { kindName = UserAuthenticator.KindName }; AppInstance.Instance.CloudDbInstance.AddToOutQueue(dbentity); await AppInstance.Instance.CloudDbInstance.EnsureServerSync(new WaitDialogHelper(this, sendToast)); }
private async Task <int> doFinalise() { //we get the data var data = getFormData(); if (data.Count == 0) { return(0); } //if (isInEditMode()) //{ // //means we have data to edit // var jsonRecord = this.Intent.GetStringExtra(Constants.BUNDLE_DATATOEDIT); // var oldRec = DbSaveableEntity // .fromJson<GeneralEntityDataset>(new KindItem(jsonRecord)); // if (!string.IsNullOrWhiteSpace(oldRec.KindMetaData)) // { // var oldMetaData = new KindMetaData().fromJson(new KindItem(oldRec.KindMetaData)); // metaData.chksum = oldMetaData.chksum + 1; // } //} var dateEdited = DateTime.Now; var saveable = getEntityDataset(data, dateEdited); //we start the saving business //:) // //1. Save to module specific lookup tables, if on registration page if (IsRegistrationEndPage()) { saveClientSummary(saveable.FieldValues, saveable.EntityId); } //2. Save the whole record to the local blob db var saveableEntity = new DbSaveableEntity(saveable) { kindName = _kindName }; saveableEntity.Save(); //3. Extract record header and save to Record Summary new LocalDB3().DB.InsertOrReplace(new RecordSummary() { Id = saveable.Id.Value, EntityId = saveable.EntityId.Value, VisitDate = dateEdited, KindName = saveable.FormName }); //4. Save to OutDb, which is used for web uploading //fire and forget AppInstance.Instance.CloudDbInstance.AddToOutQueue(saveableEntity); AppInstance.Instance.TemporalViewData.Clear(); //5. Initiate Server Sync. Ideally, this should be non-blocking //we show the splash screen and await results of the operation //todo: show dialog when beginning server sync and await excution var syncRes = await AppInstance.Instance. CloudDbInstance.EnsureServerSync(new WaitDialogHelper(this, sendToast)); return(syncRes); }