/// <summary>Обновить связанные свойства</summary> private void updateDeferredProperties() { dbObject lastObject = null; foreach (DataAboutDeferredProperty propertyData in deferredProperty) { if (lastObject == null || lastObject.GetType() != propertyData.AccessoryType || lastObject.Id != propertyData.Id) { lastObject = (dbObject)Activator.CreateInstance(propertyData.AccessoryType); lastObject.Read(propertyData.Id); } if (lastObject.Id != 0) { object idValue = BarcodeWorker.GetIdByRef( propertyData.PropertyType, propertyData.Value.ToString()); long id = Convert.ToInt64(idValue); if (!id.Equals(0L)) { lastObject.SetValue(propertyData.PropertyName, idValue); lastObject.Sync(); } } } }
private void addCaseParameters(SqlCeCommand query, DataRow row) { query.AddParameter("ElectronicUnit", BarcodeWorker.GetIdByRef(typeof(ElectronicUnits), row["ElectronicUnit"] as string)); query.AddParameter("Lamp", BarcodeWorker.GetIdByRef(typeof(db.Lamps), row["Lamp"] as string)); query.AddParameter("Map", CatalogHelper.GetModelId <Maps>(row["Map"])); query.AddParameter("Position", Convert.ToInt32(row["Position"])); query.AddParameter("Register", Convert.ToInt32(row["Register"])); }
/// <summary>Синхронизировать изменения по док "Отправить на .." на ТСД</summary> /// <typeparam name="T">Документ</typeparam> /// <typeparam name="S">Таблица</typeparam> private void SyncInSending <T, S>(SyncModes mode) where T : Sending where S : SubSending { string docName = typeof(T).Name; string tableName = typeof(S).Name; PerformQuery("GetSendingDocs", docName, tableName, (int)mode); if (IsExistParameters) { DataTable table = ResultParameters[0] as DataTable; if (table != null) { T newDoc = null; foreach (DataRow row in table.Rows) { int currId = Convert.ToInt32(row["Id"]); if (newDoc == null || newDoc.Id != currId) { newDoc = (T)Activator.CreateInstance(typeof(T)); newDoc.Contractor = Convert.ToInt32(row["Contractor"]); newDoc.Date = Convert.ToDateTime(row["Date"]); newDoc.TypeOfAccessory = (TypeOfAccessories)Convert.ToInt32(row["TypeOfAccessories"]); newDoc.BarCode = currId.ToString(); newDoc.IsSynced = true; newDoc.Sync <T>(); } S newSubDoc = (S)Activator.CreateInstance(typeof(S)); newSubDoc.TypeOfAccessory = newDoc.TypeOfAccessory; newSubDoc.Id = currId; newSubDoc.IsSynced = true; switch (mode) { case SyncModes.StandartToX: newSubDoc.Document = row["Document"].ToString(); break; case SyncModes.SendingToExchange: string syncRef = row["Nomenclature"].ToString(); object modelId = BarcodeWorker.GetIdByRef(typeof(Models), syncRef); newSubDoc.SetValue("Nomenclature", modelId); break; } newSubDoc.Sync <S>(); } } } }
/// <summary>Синхронизировать документы "Приемка нового компл." с сервера</summary> private void SyncAccepmentsDocFromServer() { DataTable acceptedDoc = AcceptanceOfNewComponents.GetAcceptedDocuments(); DataTable notAcceptedDoc = AcceptanceOfNewComponents.GetNotAcceptedDocuments(); PerformQuery("GetAcceptDocs", acceptedDoc, notAcceptedDoc); if (IsExistParameters) { AcceptanceOfNewComponents.ClearAcceptedDocuments(); DataTable table = ResultParameters[0] as DataTable; if (table != null) { foreach (DataRow row in table.Rows) { string contractorBarcode = row["Contractor"].ToString(); string partyRef = row["InvoiceNumber"].ToString(); string caseModelRef = row["CaseModel"].ToString(); string lampModelRef = row["LampModel"].ToString(); string unitModelRef = row["UnitModel"].ToString(); object contractorObj = BarcodeWorker.GetIdByBarcode(typeof(Contractors), contractorBarcode); object partyObj = BarcodeWorker.GetIdByRef(typeof(Party), partyRef); object caseModelObj = BarcodeWorker.GetIdByRef(typeof(Models), caseModelRef); object lampModelObj = BarcodeWorker.GetIdByRef(typeof(Models), lampModelRef); object unitModelObj = BarcodeWorker.GetIdByRef(typeof(Models), unitModelRef); AcceptanceOfNewComponents doc = new AcceptanceOfNewComponents { Id = Convert.ToInt64(row["Id"]), Contractor = Convert.ToInt64(contractorObj), Date = Convert.ToDateTime(row["Date"]), InvoiceDate = Convert.ToDateTime(row["InvoiceDate"]), InvoiceNumber = Convert.ToInt64(partyObj), MarkForDeleting = false, CaseModel = Convert.ToInt64(caseModelObj), LampModel = Convert.ToInt64(lampModelObj), UnitModel = Convert.ToInt64(unitModelObj), TypesOfWarrantly = (TypesOfLampsWarrantly) Convert.ToInt32(row["TypesOfWarrantly"]), TypeOfAccessories = (TypeOfAccessories) Convert.ToInt32(row["TypeOfAccessories"]), WarrantlyHours = Convert.ToInt32(row["WarrantlyHours"]), WarrantlyYears = Convert.ToInt32(row["WarrantlyYears"]), State = (TypesOfLampsStatus)Convert.ToInt32(row["State"]) }; doc.Sync <AcceptanceOfNewComponents>(); } } } }
internal static long GetModelId <T>(object syncRef) where T : CatalogObject { string strSyncRef = ((syncRef ?? string.Empty) as string).TrimEnd(); if (string.IsNullOrEmpty(strSyncRef)) { return(0); } long id; SyncRefsDict itemsCache = getItemsCache <T>(); if (!itemsCache.TryGetValue(strSyncRef, out id)) { id = Convert.ToInt64(BarcodeWorker.GetIdByRef(typeof(T), strSyncRef)); itemsCache.Add(strSyncRef, id); } return(id); }
/// <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; } } }