private string ImportFXRates(DataSet ds) { string res = String.Empty; //курсы пишутся через механизм импорта; //т.к. максимальное количество параметров в SQL запросе 2100, то пишется кусками по 1000 строк const int pageSize = 1000; DataSet tempDataSet; DataTable tempDataTable; FormModel model = FormModelRepository.GetFormModel("FXRate", FormModelType.ImportExport); ds.Tables[0].Columns[CurrencyCodeColumnName].ColumnName = "Currency"; ds.Tables[0].TableName = model.Caption.Replace(" ", "_"); for (int i = 0; i < ds.Tables[0].Rows.Count; i = i + pageSize) { tempDataSet = new DataSet(); tempDataTable = new DataTable(); tempDataTable = ds.Tables[0].AsEnumerable().Skip(i).Take(pageSize).CopyToDataTable(); tempDataTable.TableName = ds.Tables[0].TableName; tempDataSet.Tables.Add(tempDataTable); ImportResult result = ImportExportHelper.Import(model.MainViewName, tempDataSet); res += result.GetReport(new ImportResultTextFormatter()) + Environment.NewLine; } return(res); }
private void SetShowImportExportOptions(MetaFormModel form) { var model = FormModelRepository.GetFormModel(form.Id, FormModelType.All); if (model.IsEmpty || string.IsNullOrEmpty(model.MainViewName)) { return; } var viewNames = model.Blocks.Select(b => b.ViewName).Distinct().ToList(); using (var context = Settings.CreateDataContext()) { var containsForbidden = context.MetaView.Any(mv => !mv.MasterEntity.IsAvailableForImportExport && viewNames.Contains(mv.Name)); form.ShowImportExportOptions = !containsForbidden; } }
public void DeleteEntityByForms(string columnName, object valueId, List <string> formNames) { if (formNames == null) { return; } var replaceIds = new Dictionary <object, object>(); var mdRep = new MetadataRepositoty(); foreach (var formName in formNames) { var form = FormModelRepository.GetFormModel(formName, FormModelType.All); EntityMetadata md = mdRep.GetEntityMetadataByViewName(form.MainViewName); List <dynamic> records = md.Get(FilterCriteriaSet.And.Equal(valueId, columnName), ExternalMethodsCallMode.None); DynamicRepository.DeleteByView(md.Name, records.Select(c => (c as DynamicEntity).GetId()).ToList(), false); } }
public Dictionary <string, Dictionary <object, object> > CopyEntityByForms(string columnName, object fromId, object toId, List <string> formNames, Dictionary <string, FilterCriteriaSet> filters) { var replaceIds = new Dictionary <string, Dictionary <object, object> >(); var formList = new List <FormModel>(); var mdRep = new MetadataRepositoty(); #region Вставка основной записи foreach (var formName in formNames) { var form = FormModelRepository.GetFormModel(formName, FormModelType.All); EntityMetadata md = mdRep.GetEntityMetadataByViewName(form.MainViewName); var filter = FilterCriteriaSet.And.Equal(fromId, columnName); if (filters.ContainsKey(form.MainViewName)) { filter = filter.Merge(filters[form.MainViewName]); } List <dynamic> records = md.Get(filter, ExternalMethodsCallMode.None); foreach (DynamicEntity r in records) { var oldId = r[md.PrimaryKeyAttribute.ColumnName]; md.SetNewPrimaryKey(r); if (!replaceIds.ContainsKey(md.TableName)) { replaceIds.Add(md.TableName, new Dictionary <object, object>()); } replaceIds[md.TableName].Add(oldId, r[md.PrimaryKeyAttribute.ColumnName]); r[columnName] = toId; } md.Insert(records, ExternalMethodsCallMode.None); formList.Add(form); } #endregion #region Вставка дочерних и обновленеи ids foreach (var form in formList) { EntityMetadata md = mdRep.GetEntityMetadataByViewName(form.MainViewName); var replaceAtt = md.Attributes.Where(c => !string.IsNullOrEmpty(c.ReferencedTableName) && replaceIds.ContainsKey(c.ReferencedTableName)).ToList(); if (form.Blocks.Where(c => !string.IsNullOrWhiteSpace(c.BaseEntityIdName)).Count() == 1 && replaceAtt.Count == 0) { continue; } var filter1 = FilterCriteriaSet.And.Equal(toId, columnName); if (filters.ContainsKey(form.MainViewName)) { filter1 = filter1.Merge(filters[form.MainViewName]); } List <dynamic> records = md.Get(filter1, ExternalMethodsCallMode.None); foreach (DynamicEntity r in records) { foreach (AttributeDescription rAtt in replaceAtt) { var replaceIdsForTable = replaceIds[rAtt.ReferencedTableName]; if (r[rAtt.PropertyName] != null && replaceIdsForTable.ContainsKey(r[rAtt.PropertyName])) { r[rAtt.PropertyName] = replaceIdsForTable[r[rAtt.PropertyName]]; } } #region Создаем записи в дочерних таблицах foreach (var block in form.Blocks.Where(c => form.MainViewName != c.ViewName && !string.IsNullOrWhiteSpace(c.BaseEntityIdName))) { EntityMetadata blockMd = mdRep.GetEntityMetadataByViewName(block.ViewName); if (md.TableName == blockMd.TableName) { continue; } object oldParentEntityId = replaceIds[md.TableName].Where(c => c.Value.Equals(r.GetId())).Select(c => c.Key).First(); var filter2 = FilterCriteriaSet.And.Equal(oldParentEntityId, block.BaseEntityIdName); if (filters.ContainsKey(block.ViewName)) { filter2 = filter2.Merge(filters[block.ViewName]); } List <dynamic> blockRecords = blockMd.Get(filter2, ExternalMethodsCallMode.None); foreach (DynamicEntity blockR in blockRecords) { var oldId = blockR[blockMd.PrimaryKeyAttribute.ColumnName]; blockMd.SetNewPrimaryKey(blockR); if (!replaceIds.ContainsKey(blockMd.TableName)) { replaceIds.Add(blockMd.TableName, new Dictionary <object, object>()); } replaceIds[blockMd.TableName].Add(oldId, blockR[blockMd.PrimaryKeyAttribute.ColumnName]); var replaceAttForBlock = blockMd.Attributes.Where(c => !string.IsNullOrEmpty(c.ReferencedTableName) && replaceIds.ContainsKey(c.ReferencedTableName)).ToList(); foreach (AttributeDescription rAtt in replaceAttForBlock) { var replaceIdsForTable = replaceIds[rAtt.ReferencedTableName]; if (replaceIdsForTable.ContainsKey(blockR[rAtt.PropertyName])) { blockR[rAtt.PropertyName] = replaceIdsForTable[blockR[rAtt.PropertyName]]; } } } blockMd.Insert(blockRecords, ExternalMethodsCallMode.None); } #endregion } md.Update(records, ExternalMethodsCallMode.None); } return(replaceIds); #endregion }