示例#1
0
        public async Task Import(IOspSession session, string filePath, CancellationToken?cancellationToken = null)
        {
            Logger.Info("Importing RT entities using file started.");

            using (var stream = File.OpenText(filePath))
            {
                await RtSerializer.DeserializeAsync(stream, x => ImportEntity(session, x), cancellationToken);
            }

            // Finish the last entities
            await ImportToDatabase(session);

            Logger.Info($"{_entityImportIds.Count} entities, {_associationsCount} associations imported.");
        }
        public async Task Export(IOspSession session, OspObjectId queryId, string filePath, CancellationToken?cancellationToken)
        {
            var query = await _tenantContext.Repository.GetRtEntityAsync(session, new RtEntityId(Constants.SystemQueryCkId, queryId));

            if (CheckCancellation(cancellationToken))
            {
                throw new OperationCanceledException();
            }

            if (query == null)
            {
                throw new ModelExportException($"Query '{queryId}‘ does not exist.");
            }

            DataQueryOperation dataQueryOperation = new DataQueryOperation();

            var sortingDtoList =
                JsonConvert.DeserializeObject <ICollection <SortDto> >(query.GetAttributeStringValueOrDefault("Sorting"));

            dataQueryOperation.SortOrders = sortingDtoList.Select(dto =>
                                                                  new SortOrderItem(dto.AttributeName.ToPascalCase(), (SortOrders)dto.SortOrder));
            var fieldFilterDtoList =
                JsonConvert.DeserializeObject <ICollection <FieldFilterDto> >(
                    query.GetAttributeStringValueOrDefault("FieldFilter"));

            dataQueryOperation.FieldFilters = fieldFilterDtoList.Select(dto =>
                                                                        new FieldFilter(TransformAttributeName(dto.AttributeName), (FieldFilterOperator)dto.Operator,
                                                                                        dto.ComparisonValue));

            var ckId = query.GetAttributeStringValueOrDefault("QueryCkId");

            var resultSet = await _tenantContext.Repository.GetRtEntitiesByTypeAsync(session, ckId, dataQueryOperation);

            var entityCacheItem = _tenantContext.CkCache.GetEntityCacheItem(ckId);

            RtModelRoot model = new RtModelRoot();

            model.RtEntities.AddRange(resultSet.Result.Select(entity =>
            {
                var exEntity = new RtEntity
                {
                    Id            = entity.RtId.ToOspObjectId(),
                    WellKnownName = entity.WellKnownName,
                    CkId          = entity.CkId,
                };

                exEntity.Attributes.AddRange(entity.Attributes.Select(pair =>
                {
                    var attributeCacheItem = entityCacheItem.Attributes[pair.Key];
                    return(new RtAttribute
                    {
                        Id = attributeCacheItem.AttributeId,
                        Value = pair.Value
                    });
                }));

                return(exEntity);
            }));

            await using StreamWriter streamWriter = new StreamWriter(filePath);
            RtSerializer.Serialize(streamWriter, model);

            await session.CommitTransactionAsync();
        }