internal MobeelizerOperationError UpdateFromSync(MobeelizerJsonEntity entity, MobeelizerDatabaseContext db) { var query = from MobeelizerWp7Model e in db.GetTable(this.Type) join MobeelizerModelMetadata m in db.ModelMetadata on e.Guid equals m.Guid where m.Guid == entity.Guid && m.Model == this.Name select new QueryResult() { Entity = e, Metadata = m }; bool exists = true; QueryResult result = null; if (query.Count() == 0) { exists = false; } else { result = query.Single(); } bool modifiedByUser = exists && result.Metadata.ModificationLock == false && result.Entity.Modified ; if (modifiedByUser || !exists && entity.IsDeleted) { return null; } if (entity.ConflictState == MobeelizerJsonEntity.MobeelizerConflictState.NO_IN_CONFLICT && entity.IsDeleted) { if (exists) { var table = db.GetTable(this.Type); table.DeleteAllOnSubmit(from MobeelizerWp7Model record in table where record.Guid == entity.Guid select record); } return null; } Dictionary<String, object> values = new Dictionary<string, object>(); if (entity.ConflictState == MobeelizerJsonEntity.MobeelizerConflictState.IN_CONFLICT_BECAUSE_OF_YOU || entity.Fields.Count == 0) { PropertyInfo property = this.Type.GetProperty("Conflicted"); PropertyInfo modifiedProperty = this.Type.GetProperty("Modified"); property.SetValue(result.Entity, true, null); modifiedProperty.SetValue(result.Entity, false, null); return null; } else if (entity.ConflictState == MobeelizerJsonEntity.MobeelizerConflictState.IN_CONFLICT) { values.Add("Conflicted", 1); } else { values.Add("Conflicted", 0); } values.Add("Owner", entity.Owner); values.Add("Modified", 0); try { values.Add("Deleted", entity.IsDeleted ? 1 : 0); } catch (KeyNotFoundException) { values.Add("Deleted", false); } MobeelizerErrorsHolder errors = new MobeelizerErrorsHolder(); foreach (MobeelizerField field in this.Fields) { field.SetValueFromMapToDatabase(values, entity.Fields, errors); } if (!errors.IsValid) { return MobeelizerOperationError.UpdateFromSyncError(errors); } if (exists) { UpdateEntity(db, result.Metadata, values, entity.Guid, result.Entity); } else { values.Add("Guid", entity.Guid); InsertEntity(db, values); } return null; }
internal bool Validate(MobeelizerWp7Model insert, MobeelizerErrorsHolder errors) { Dictionary<String, object> values = new Dictionary<string, object>(); MapEntityToDictionary(insert, values); foreach (MobeelizerField field in this.Fields) { field.Validate(values, errors); } if (!errors.IsValid) { return false; } return true; }
internal static MobeelizerOperationError UpdateFromSyncError(MobeelizerErrorsHolder errors) { return new MobeelizerOperationError() { Code = "updateFromSync", Message = "Update entities from sync failiture", Arguments = errors.PrepareErrorArguments() }; }
internal static void Validate(this MobeelizerFieldType fieldType, IDictionary <String, object> values, MobeelizerFieldAccessor field, bool required, IDictionary <String, String> options, MobeelizerErrorsHolder errors) { typesMap[fieldType].Validate(values, field, required, options, errors); }
internal static void SetValueFromMapToDatabase(this MobeelizerFieldType fieldType, IDictionary <String, object> values, IDictionary <String, String> map, MobeelizerFieldAccessor field, bool required, IDictionary <String, String> options, MobeelizerErrorsHolder errors) { typesMap[fieldType].SetValueFromMapToDatabase(values, map, field, required, options, errors); }
internal bool ValidateEntity(MobeelizerWp7Model insert, MobeelizerErrorsHolder errors) { String model = insert.GetType().Name; return models[model].Validate(insert, errors); }