Exemple #1
0
        /// <summary>Сохранить изменения в объекте</summary>
        /// <param name="sync">Синхронизация?</param>
        /// <param name="updId">Нужно обновить ID</param>
        /// <returns>Id</returns>
        protected virtual object SaveChanges <T>(bool sync, bool updId) where T : dbObject
        {
            object idValue;

            LastModified = DateTime.Now;
            ISynced syncObj = this as ISynced;

            if (syncObj != null)
            {
                syncObj.IsSynced = sync;
            }

            if (IsNew)
            {
                if (string.IsNullOrEmpty(SyncRef))
                {
                    SyncRef = GenerateSyncRef();
                }

                DocumentObject document = this as DocumentObject;

                if (document != null && document.Date == SqlDateTime.MinValue.Value)
                {
                    document.Date = DateTime.Now;
                }

                idValue = CreateNew <T>(updId);
            }
            else
            {
                idValue = Update <T>();
            }

            return(idValue);
        }
Exemple #2
0
        /// <summary>Копировать объект</summary>
        /// <param name="source">Объект-исходник</param>
        /// <returns>Скопированный объект</returns>
        public static dbObject Copy(dbObject source)
        {
            if (source != null)
            {
                Type           type          = source.GetType();
                dbObject       copy          = (dbObject)Activator.CreateInstance(type);
                PropertyInfo[] propertyInfos = type.GetProperties();

                foreach (PropertyInfo property in propertyInfos)
                {
                    dbFieldAtt attributes = Attribute.GetCustomAttribute(property, typeof(dbFieldAtt)) as dbFieldAtt;

                    if (attributes != null)
                    {
                        object value = property.GetValue(source, null);
                        property.SetValue(copy, value, null);
                    }
                }

                ISynced synced = copy as ISynced;
                if (synced != null)
                {
                    synced.IsSynced = false;
                }

                IBarcodeOwner barcode = copy as IBarcodeOwner;
                if (barcode != null)
                {
                    barcode.BarCode = string.Empty;
                }

                copy.SyncRef = string.Empty;
                copy.Id      = 0;

                return(copy);
            }

            return(null);
        }
Exemple #3
0
        /// <summary>Создание объектов синхронизации</summary>
        /// <param name="table">Данные о объектах</param>
        /// <param name="skipExists">Пропустить существующие</param>
        /// <param name="deferredProperty">Список отложеных свойств</param>
        /// <param name="updId">Нужно обновить ID</param>
        private static void CreateSyncObject <T>(CatalogSynchronizer synchronizer, DataTable table, bool skipExists, ref List <DataAboutDeferredProperty> deferredProperty, bool updId) where T : dbObject
        {
            if (synchronizer != null)
            {
                int rowsCount = table.Rows.Count;
                for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++)
                {
                    DataRow row = table.Rows[rowIndex];
                    synchronizer.Merge(row);

                    //if (rowIndex % 10 == 0)
                    {
                        Trace.WriteLine(string.Format("{1} %, rowIndex = {0} from {2}", rowIndex, (int)(100 * rowIndex / rowsCount), rowsCount));
                    }

                    //if (rowIndex % 500 == 0)
                    //    {
                    //    GC.Collect();
                    //    GC.WaitForPendingFinalizers();
                    //    }
                }
                return;
            }

            Type type = typeof(T);

            PropertyInfo[] properties        = type.GetProperties();
            string         syncRef           = string.Empty;
            string         syncRefName       = dbObject.SYNCREF_NAME.ToLower();
            int            lastDeferredIndex = deferredProperty.Count;

            foreach (DataRow row in table.Rows)
            {
                object newObj       = Activator.CreateInstance(typeof(T));
                T      newObject    = (T)newObj;
                bool   needDeferred = false;

                foreach (PropertyInfo property in properties)
                {
                    dbFieldAtt attribute = Attribute.GetCustomAttribute(property, typeof(dbFieldAtt)) as dbFieldAtt;

                    if (attribute != null && table.Columns.Contains(property.Name))
                    {
                        object value = row[property.Name];

                        //Не существует ли елемент с такой ссылкой?
                        if (property.Name.ToLower().Equals(syncRefName))
                        {
                            if (BarcodeWorker.IsRefExist(type, value.ToString()))
                            {
                                if (skipExists)
                                {
                                    break;
                                }

                                newObject.SetNotNew();
                            }

                            syncRef = value.ToString();
                        }
                        else if (updId && property.Name.ToLower().Equals(dbObject.IDENTIFIER_NAME))
                        {
                            continue;
                        }

                        if (property.PropertyType == typeof(int))
                        {
                            value = string.IsNullOrEmpty(value.ToString()) ? 0 : Convert.ToInt32(value);
                        }
                        else if (property.PropertyType == typeof(double))
                        {
                            value = string.IsNullOrEmpty(value.ToString()) ? 0D : Convert.ToDouble(value);
                        }
                        else if (property.PropertyType == typeof(long))
                        {
                            if (attribute.dbObjectType == null)
                            {
                                value = string.IsNullOrEmpty(value.ToString()) ? 0L : Convert.ToInt64(value);
                            }
                            else
                            {
                                if (value != null && !string.IsNullOrEmpty(value.ToString()))
                                {
                                    DataAboutDeferredProperty data = new DataAboutDeferredProperty(type, attribute.dbObjectType, property.Name, value);
                                    deferredProperty.Add(data);
                                    needDeferred = true;
                                }

                                value = 0L;
                            }
                        }
                        else if (property.PropertyType == typeof(bool))
                        {
                            value = Convert.ToBoolean(value);
                        }
                        else if (property.PropertyType == typeof(DateTime))
                        {
                            value = StringParser.ParseDateTime(value);
                        }
                        else if (property.PropertyType.IsEnum)
                        {
                            value = Enum.Parse(property.PropertyType, value.ToString(), false);
                        }
                        else if (property.PropertyType == typeof(string))
                        {
                            string fullValue = value.ToString();
                            value = value.ToString();

                            if (property.Name != CatalogObject.DESCRIPTION)
                            {
                                int length = attribute.StrLength == 0
                                                 ? dbFieldAtt.DEFAULT_STR_LENGTH
                                                 : attribute.StrLength;

                                if (fullValue.Length > length)
                                {
                                    value = fullValue.Substring(0, length);
                                }
                            }
                        }

                        property.SetValue(newObject, value, null);
                    }
                }

                ISynced syncObject = newObject as ISynced;

                if (syncObject != null)
                {
                    syncObject.IsSynced = true;
                }

                if (updId && !skipExists && !string.IsNullOrEmpty(syncRef))
                {
                    newObject.Id = Convert.ToInt64(BarcodeWorker.GetIdByRef(type, syncRef));
                }

                CatalogObject catalog = newObject as CatalogObject;

                if (catalog != null)
                {
                    dbElementAtt attribute = Attribute.GetCustomAttribute(newObject.GetType(), typeof(dbElementAtt)) as dbElementAtt;
                    int          length    = attribute == null || attribute.DescriptionLength == 0
                                     ? dbElementAtt.DEFAULT_DES_LENGTH
                                     : attribute.DescriptionLength;

                    if (catalog.Description.Length > length)
                    {
                        catalog.Description = catalog.Description.Substring(0, length);
                    }
                }

                newObject.Sync <T>(updId);

                if (needDeferred)
                {
                    for (int i = lastDeferredIndex; i < deferredProperty.Count; i++)
                    {
                        deferredProperty[i].Id = newObject.Id;
                    }

                    lastDeferredIndex = deferredProperty.Count;
                }
            }
        }