Exemplo n.º 1
0
        private static void UpdateIfExists(Abstract1CEntity target, object source, string propertyName)
        {
            var property = target.GetType().GetProperty(propertyName);

            if (property == null)
            {
                return;
            }
            SetValueWithoutTracking(target, property, ComHelpers.GetProperty(source, propertyName));
        }
Exemplo n.º 2
0
        private static void SetValueWithoutTracking(Abstract1CEntity target, PropertyInfo property, object value)
        {
            var oldTrackChanges = target.Controller.TrackChanges;

            target.Controller.TrackChanges = false;
            try
            {
                property.SetValue(target, value);
            }
            finally
            {
                target.Controller.TrackChanges = oldTrackChanges;
            }
        }
Exemplo n.º 3
0
        private void UpdateId(object source, Abstract1CEntity target, ConfigurationName name)
        {
            var property = target.GetType().GetProperty(EntityHelpers.idPropertyName);

            if (property == null)
            {
                const string messageFormat = "type [{0}] has no id";
                throw new InvalidOperationException(string.Format(messageFormat, name));
            }
            var idValue = ComHelpers.Invoke(source, EntityHelpers.idPropertyName);

            idValue = comObjectMapper.MapFrom1C(idValue, typeof(Guid));
            SetValueWithoutTracking(target, property, idValue);
        }
Exemplo n.º 4
0
        private static void AssignNewGuid(Abstract1CEntity target, Dictionary <string, object> committed, string property)
        {
            if (committed.ContainsKey(property))
            {
                return;
            }
            var codeProperty = target.GetType().GetProperty(property);

            if (codeProperty == null)
            {
                return;
            }
            AssignValue(target, committed, codeProperty, Guid.NewGuid().ToString());
        }
Exemplo n.º 5
0
        private static void AssignValue(Abstract1CEntity target, Dictionary <string, object> committed, PropertyInfo property, object value)
        {
            committed[property.Name] = value;
            var oldTrackChanges = target.Controller.TrackChanges;

            target.Controller.TrackChanges = false;
            try
            {
                property.SetMethod.Invoke(target, new[] { value });
            }
            finally
            {
                target.Controller.TrackChanges = oldTrackChanges;
            }
        }
Exemplo n.º 6
0
        private object GetComObjectForEditing(ConfigurationName name, Abstract1CEntity entity)
        {
            var controller  = entity.Controller;
            var valueSource = controller.ValueSource;

            if (!controller.IsNew && name.HasReference)
            {
                return(ComHelpers.Invoke(valueSource.GetBackingStorage(), "ПолучитьОбъект"));
            }
            switch (name.Scope)
            {
            case ConfigurationScope.Справочники:
                object isGroup;
                var    needCreateFolder = controller.Changed != null &&
                                          controller.Changed.TryGetValue("ЭтоГруппа", out isGroup) &&
                                          (bool)isGroup;
                return(needCreateFolder
                        ? ComHelpers.Invoke(globalContext.GetManager(name), "CreateFolder")
                        : ComHelpers.Invoke(globalContext.GetManager(name), "CreateItem"));

            case ConfigurationScope.Документы:
                return(ComHelpers.Invoke(globalContext.GetManager(name), "CreateDocument"));

            case ConfigurationScope.егистрыСведений:
                return(valueSource != null && valueSource.Writable
                        ? valueSource.GetBackingStorage()
                        : ComHelpers.Invoke(globalContext.GetManager(name), "СоздатьМенеджерЗаписи"));

            case ConfigurationScope.ПланыСчетов:
            case ConfigurationScope.ПланыВидовХарактеристик:
                const string message = "creation of [{0}] is not implemented";
                throw new InvalidOperationException(string.Format(message, name.Scope));

            case ConfigurationScope.Перечисления:
            case ConfigurationScope.Константы:
                throw new InvalidOperationException("assertion failure");

            default:
                const string messageFormat = "invalid scope for [{0}]";
                throw new ArgumentOutOfRangeException(string.Format(messageFormat, name));
            }
        }
Exemplo n.º 7
0
 internal void PrepareToSave(Abstract1CEntity owner, List <Abstract1CEntity> entitiesToSave)
 {
     if (observedValues != null)
     {
         foreach (var item in observedValues)
         {
             if (Changed != null && Changed.ContainsKey(item.Key))
             {
                 continue;
             }
             var value  = item.Value;
             var entity = value.value as Abstract1CEntity;
             if (entity != null)
             {
                 entity.Controller.PrepareToSave(entity, entitiesToSave);
                 continue;
             }
             var list = value.value as IList;
             if (list != null)
             {
                 foreach (Abstract1CEntity e in list)
                 {
                     e.Controller.PrepareToSave(e, entitiesToSave);
                 }
                 var syncList = SyncList.Compare(value.originalList, list);
                 if (syncList.Commands.Count > 0)
                 {
                     MarkAsChanged(item.Key, syncList);
                 }
             }
         }
         observedValues = null;
     }
     if (!EntityHelpers.IsTableSection(owner.GetType()) && IsDirty)
     {
         entitiesToSave.Add(owner);
     }
 }
Exemplo n.º 8
0
        private void Save(Abstract1CEntity source, object comObject, Stack <object> pending)
        {
            if (!source.Controller.IsDirty)
            {
                return;
            }
            if (pending.Contains(source))
            {
                const string messageFormat = "cycle detected for entity type [{0}]: [{1}]";
                throw new InvalidOperationException(string.Format(messageFormat, source.GetType().FormatName(),
                                                                  pending
                                                                  .Reverse()
                                                                  .Select(x => x is Abstract1CEntity ? x.GetType().FormatName() : x)
                                                                  .JoinStrings("->")));
            }
            pending.Push(source);
            Metadata          metadata;
            ConfigurationName?configurationName;

            if (comObject == null)
            {
                configurationName = ConfigurationName.Get(source.GetType());
                metadata          = metadataAccessor.GetMetadata(configurationName.Value);
                comObject         = GetComObjectForEditing(configurationName.Value, source);
            }
            else
            {
                metadata          = null;
                configurationName = null;
            }
            bool?newPostingValue = null;
            var  changeLog       = source.Controller.Changed;

            if (changeLog != null)
            {
                foreach (var p in changeLog)
                {
                    var value = p.Value;
                    if (p.Key == "Проведен" && configurationName.HasValue &&
                        configurationName.Value.Scope == ConfigurationScope.Документы)
                    {
                        newPostingValue = (bool?)value;
                        continue;
                    }
                    pending.Push(p.Key);
                    SaveProperty(p.Key, p.Value, comObject, pending, metadata);
                    pending.Pop();
                }
            }
            var needPatchWithOriginalValues = configurationName.HasValue &&
                                              configurationName.Value.Scope == ConfigurationScope.егистрыСведений &&
                                              source.Controller.ValueSource != null &&
                                              !source.Controller.ValueSource.Writable &&
                                              changeLog != null;

            if (needPatchWithOriginalValues)
            {
                var backingStorage = source.Controller.ValueSource.GetBackingStorage();
                foreach (var r in metadata.Requisites)
                {
                    if (!changeLog.ContainsKey(r.Name))
                    {
                        pending.Push(r);
                        var value = ComHelpers.GetProperty(backingStorage, r.Name);
                        SaveProperty(r.Name, value, comObject, pending, metadata);
                        pending.Pop();
                    }
                }
            }
            object valueSourceComObject;

            if (configurationName.HasValue)
            {
                if (!newPostingValue.HasValue && configurationName.Value.Scope == ConfigurationScope.Документы)
                {
                    var oldPostingValue = Convert.ToBoolean(ComHelpers.GetProperty(comObject, "Проведен"));
                    if (oldPostingValue)
                    {
                        Write(comObject, configurationName.Value, false);
                        newPostingValue = true;
                    }
                }
                Write(comObject, configurationName.Value, newPostingValue);
                switch (configurationName.Value.Scope)
                {
                case ConfigurationScope.Справочники:
                    UpdateIfExists(source, comObject, "Код");
                    break;

                case ConfigurationScope.Документы:
                    UpdateIfExists(source, comObject, "Номер");
                    break;
                }
                if (configurationName.Value.HasReference)
                {
                    valueSourceComObject = ComHelpers.GetProperty(comObject, "Ссылка");
                    UpdateId(valueSourceComObject, source, configurationName.Value);
                }
                else
                {
                    valueSourceComObject = comObject;
                }
            }
            else
            {
                UpdateIfExists(source, comObject, "НомерСтроки");
                valueSourceComObject = comObject;
            }
            var valueSourceIsWriteable = configurationName.HasValue &&
                                         configurationName.Value.Scope == ConfigurationScope.егистрыСведений;

            source.Controller.ResetDirty(new ComValueSource(valueSourceComObject, comObjectMapper,
                                                            valueSourceIsWriteable));
            pending.Pop();
        }
Exemplo n.º 9
0
        private InMemoryEntity Save(Abstract1CEntity entity, bool isTableSection)
        {
            if (entity == null)
            {
                return(null);
            }
            var changed = entity.Controller.Changed;

            if (changed != null)
            {
                var keys = changed.Keys.ToArray();
                foreach (var k in keys)
                {
                    var abstract1CEntity = changed[k] as Abstract1CEntity;
                    if (abstract1CEntity != null)
                    {
                        changed[k] = Save(abstract1CEntity, false);
                        continue;
                    }
                    var list = changed[k] as IList;
                    if (list != null)
                    {
                        changed[k] = ConvertList(list);
                        continue;
                    }
                    var syncList = changed[k] as SyncList;
                    if (syncList != null)
                    {
                        changed[k] = ConvertList(syncList.Current);
                    }
                }
            }
            InMemoryEntity inMemoryEntity;

            if (!entity.Controller.IsNew)
            {
                var inmemoryEntityRevision = (InMemoryEntityRevision)entity.Controller.ValueSource;
                inMemoryEntity = inmemoryEntityRevision.inMemoryEntity;
                if (changed != null)
                {
                    inMemoryEntity.revision = new InMemoryEntityRevision(inMemoryEntity, inmemoryEntityRevision, changed);
                    Collection(entity.GetType()).revision++;
                }
            }
            else
            {
                if (changed == null)
                {
                    changed = new Dictionary <string, object>();
                }
                inMemoryEntity = new InMemoryEntity();
                var revision = new InMemoryEntityRevision(inMemoryEntity, null, changed);
                inMemoryEntity.entityType = entity.GetType();
                inMemoryEntity.revision   = revision;
                if (!isTableSection)
                {
                    var configurationName = ConfigurationName.Get(entity.GetType());
                    if (configurationName.Scope == ConfigurationScope.Справочники)
                    {
                        AssignNewGuid(entity, changed, "Код");
                    }
                    else if (configurationName.Scope == ConfigurationScope.Документы)
                    {
                        AssignNewGuid(entity, changed, "Номер");
                    }
                    if (entity.Controller.IsNew && configurationName.HasReference)
                    {
                        var idProperty = entity.GetType().GetProperty(EntityHelpers.idPropertyName);
                        if (idProperty == null)
                        {
                            throw new InvalidOperationException("assertion failure");
                        }
                        AssignValue(entity, changed, idProperty, Guid.NewGuid());
                    }
                    var inMemoryEntityCollection = Collection(entity.GetType());
                    inMemoryEntityCollection.revision++;
                    inMemoryEntityCollection.list.Add(inMemoryEntity);
                }
            }
            entity.Controller.ResetDirty(inMemoryEntity.revision);
            return(inMemoryEntity);
        }