Beispiel #1
0
        public MobeelizerJsonEntity GetJsonEntity(MobeelizerModelMetadata metadata, MobeelizerDatabaseContext db)
        {
            MobeelizerJsonEntity entity = new MobeelizerJsonEntity();
            entity.Model = metadata.Model;
            entity.Guid = metadata.Guid;
            entity.Fields = new Dictionary<string, string>();
            var result = from MobeelizerWp7Model model in db.GetTable(this.Type) where model.Guid == metadata.Guid select model;
            MobeelizerWp7Model modelObject = result.Single();
            entity.Owner = modelObject.Owner;
            entity.Fields.Add("s_deleted", modelObject.Deleted.ToString().ToLower());
            foreach (MobeelizerField field in this.Fields)
            {
                object value = null;
                PropertyInfo property = modelObject.GetType().GetProperty(field.Accessor.Name);
                if (property == null)
                {
                    throw new ConfigurationException("There is no property " + field.Accessor.Name + " in " + entity.Model + " class.");
                }

                value = property.GetValue(modelObject, null);
                entity.Fields.Add(field.Name, field.FieldType.SetValueFromDatabaseToMap(value));
            }

            return entity;
        }
Beispiel #2
0
 internal void WriteEntity(MobeelizerJsonEntity next)
 {
     try
     {
         outputStream.Write(next.GetJson());
         outputStream.Write('\n');
     }
     catch (IOException e)
     {
         outputStream.Close();
         throw new InvalidOperationException(e.Message, e);
     }
 }
 internal void WriteEntity(MobeelizerJsonEntity next)
 {
     try
     {
         outputStream.Write(next.GetJson());
         outputStream.Write('\n');
     }
     catch (IOException e)
     {
         outputStream.Close();
         throw new InvalidOperationException(e.Message, e);
     }
 }
Beispiel #4
0
        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;
        }