public static void ImportForm(int siteId, string directoryPath, bool overwrite) { if (!Directory.Exists(directoryPath)) { return; } var isHistoric = IsHistoric(directoryPath); var filePaths = Directory.GetFiles(directoryPath); foreach (var filePath in filePaths) { var feed = AtomFeed.Load(new FileStream(filePath, FileMode.Open)); var formInfo = new FormInfo(); foreach (var tableColumn in FormManager.Repository.TableColumns) { var value = GetValue(feed.AdditionalElements, tableColumn); formInfo.Set(tableColumn.AttributeName, value); } formInfo.SiteId = siteId; formInfo.AddDate = DateTime.Now; if (isHistoric) { formInfo.Title = GetDcElementContent(feed.AdditionalElements, "InputName"); } var srcFormInfo = FormManager.GetFormInfoByTitle(siteId, formInfo.Title); if (srcFormInfo != null) { if (overwrite) { FormManager.Repository.Delete(siteId, srcFormInfo.Id); } else { formInfo.Title = FormManager.Repository.GetImportTitle(siteId, formInfo.Title); } } formInfo.Id = FormManager.Repository.Insert(formInfo); var directoryName = GetDcElementContent(feed.AdditionalElements, "Id"); if (isHistoric) { directoryName = GetDcElementContent(feed.AdditionalElements, "InputID"); } var titleAttributeNameDict = new NameValueCollection(); if (!string.IsNullOrEmpty(directoryName)) { var fieldDirectoryPath = FormUtils.PathCombine(directoryPath, directoryName); titleAttributeNameDict = ImportFields(siteId, formInfo.Id, fieldDirectoryPath, isHistoric); } var entryList = new List <AtomEntry>(); foreach (AtomEntry entry in feed.Entries) { entryList.Add(entry); } entryList.Reverse(); foreach (var entry in entryList) { var logInfo = new LogInfo(); foreach (var tableColumn in LogManager.Repository.TableColumns) { var value = GetValue(entry.AdditionalElements, tableColumn); logInfo.Set(tableColumn.AttributeName, value); } var attributes = GetDcElementNameValueCollection(entry.AdditionalElements); foreach (string entryName in attributes.Keys) { logInfo.Set(entryName, attributes[entryName]); } if (isHistoric) { foreach (var title in titleAttributeNameDict.AllKeys) { logInfo.Set(title, logInfo.Get(titleAttributeNameDict[title])); } logInfo.ReplyContent = GetDcElementContent(entry.AdditionalElements, "Reply"); if (!string.IsNullOrEmpty(logInfo.ReplyContent)) { logInfo.IsReplied = true; } logInfo.AddDate = FormUtils.ToDateTime(GetDcElementContent(entry.AdditionalElements, "adddate")); } LogManager.Repository.Insert(formInfo, logInfo); } } }
public async Task ImportFormAsync(int siteId, string directoryPath, bool overwrite) { if (!Directory.Exists(directoryPath)) { return; } var isHistoric = IsHistoric(directoryPath); var filePaths = Directory.GetFiles(directoryPath); foreach (var filePath in filePaths) { if (!StringUtils.EndsWithIgnoreCase(filePath, ".xml")) { continue; } var feed = AtomFeed.Load(new FileStream(filePath, FileMode.Open)); var formInfo = new FormInfo(); foreach (var tableColumn in _formRepository.TableColumns) { var value = GetValue(feed.AdditionalElements, tableColumn); formInfo.Set(tableColumn.AttributeName, value); } formInfo.SiteId = siteId; var srcFormInfo = await _formRepository.GetFormInfoByTitleAsync(siteId, formInfo.Title); if (srcFormInfo != null) { if (overwrite) { await DeleteAsync(siteId, srcFormInfo.Id); } else { formInfo.Title = await _formRepository.GetImportTitleAsync(siteId, formInfo.Title); } } formInfo.Id = await _formRepository.InsertAsync(formInfo); var directoryName = GetDcElementContent(feed.AdditionalElements, "Id"); var titleAttributeNameDict = new NameValueCollection(); if (!string.IsNullOrEmpty(directoryName)) { var fieldDirectoryPath = PathUtils.Combine(directoryPath, directoryName); titleAttributeNameDict = await ImportFieldsAsync(siteId, formInfo.Id, fieldDirectoryPath, isHistoric); } var entryList = new List <AtomEntry>(); foreach (AtomEntry entry in feed.Entries) { entryList.Add(entry); } entryList.Reverse(); foreach (var entry in entryList) { var dataInfo = new DataInfo(); foreach (var tableColumn in _dataRepository.TableColumns) { var value = GetValue(entry.AdditionalElements, tableColumn); dataInfo.Set(tableColumn.AttributeName, value); } var attributes = GetDcElementNameValueCollection(entry.AdditionalElements); foreach (string entryName in attributes.Keys) { dataInfo.Set(entryName, attributes[entryName]); } if (isHistoric) { foreach (var title in titleAttributeNameDict.AllKeys) { dataInfo.Set(title, dataInfo.Get(titleAttributeNameDict[title])); } dataInfo.ReplyContent = GetDcElementContent(entry.AdditionalElements, "Reply"); if (!string.IsNullOrEmpty(dataInfo.ReplyContent)) { dataInfo.IsReplied = true; } dataInfo.CreatedDate = FormUtils.ToDateTime(GetDcElementContent(entry.AdditionalElements, "adddate")); } await _dataRepository.InsertAsync(formInfo, dataInfo); } } }