private ImportEntity RestoreImportEntity(ImportEntityMemento memento)
        {
            var importEntity = new ImportEntity();

            importEntity.RestoreState(memento);
            return(importEntity);
        }
Esempio n. 2
0
        /// <inheritdoc cref="INonPersistentColumnsAggregator"/>
        public void Add(ImportEntity importEntity, ImportColumn column, ImportColumnValue columnValue,
                        ImportColumnDestination destination)
        {
            IColumnProcessor columnProcessor = GetColumnProcessor(destination);

            columnProcessor.Add(importEntity, column, columnValue, destination);
        }
 private void SetEntityColumnValue(ImportEntity importEntity, ImportColumn column, ImportColumnValue columnValue)
 {
     foreach (ImportColumnDestination destination in column.Destinations)
     {
         Entity entity          = importEntity.GetEntityForSave(UserConnection, destination);
         string columnValueName = destination.ColumnValueName;
         object valueForSave    = ColumnsProcessor.FindValueForSave(destination, columnValue);
         if (valueForSave == null)
         {
             continue;
         }
         if (entity.StoringState != StoringObjectState.New)
         {
             object entityValue = entity.GetColumnValue(columnValueName);
             if (valueForSave.Equals(entityValue) || destination.IsKey)
             {
                 continue;
             }
         }
         if (entity.Schema.Columns.GetByName(destination.ColumnName).DataValueType is TextDataValueType)
         {
             valueForSave = valueForSave.ToString().Trim();
         }
         entity.SetColumnValue(columnValueName, valueForSave);
     }
 }
Esempio n. 4
0
 private void InitEntityForSave(ImportParameters parameters, ImportEntity importEntity)
 {
     importEntity.InitPrimaryEntity(UserConnection, parameters);
     foreach (var column in parameters.Columns)
     {
         var columnValue = importEntity.FindColumnValue(column);
         if (columnValue == null)
         {
             continue;
         }
         foreach (var destination in column.Destinations)
         {
             var entity          = importEntity.GetEntityForSave(UserConnection, destination);
             var columnValueName = destination.ColumnValueName;
             var valueForSave    = ColumnsProcessor.FindValueForSave(destination, columnValue);
             if (valueForSave == null)
             {
                 continue;
             }
             if (entity.StoringState != StoringObjectState.New)
             {
                 var entityValue = entity.GetColumnValue(columnValueName);
                 if (valueForSave.Equals(entityValue) || destination.IsKey)
                 {
                     continue;
                 }
             }
             if (entity.Schema.Columns.GetByName(destination.ColumnName).DataValueType is TextDataValueType)
             {
                 valueForSave = valueForSave.ToString().Trim();
             }
             entity.SetColumnValue(columnValueName, valueForSave);
         }
     }
 }
        /// <summary>
        /// Gets schemas key columns values.
        /// </summary>
        /// <param name="parameters">Import parameters.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <returns>Schemas key columns values.</returns>
        private SchemasKeyColumnsValues GetSchemasKeyColumnsValues(
            ImportParameters parameters, ImportEntity importEntity)
        {
            var schemasKeyColumnsValues = new SchemasKeyColumnsValues();

            foreach (ImportColumn column in parameters.Columns)
            {
                ImportColumnValue columnValue = importEntity.FindColumnValue(column);
                if (columnValue == null)
                {
                    continue;
                }
                foreach (ImportColumnDestination destination in column.Destinations)
                {
                    Guid schemaUId = destination.SchemaUId;
                    if (schemaUId.Equals(parameters.RootSchemaUId))
                    {
                        continue;
                    }
                    object valueForSave = ColumnsProcessor.FindValueForSave(destination, columnValue);
                    if (valueForSave == null)
                    {
                        continue;
                    }
                    EntitySchema             schema = UserConnection.EntitySchemaManager.GetInstanceByUId(schemaUId);
                    EntitiesKeyColumnsValues entitiesKeyColumnsValues;
                    if (!schemasKeyColumnsValues.TryGetValue(schema, out entitiesKeyColumnsValues))
                    {
                        entitiesKeyColumnsValues = new EntitiesKeyColumnsValues();
                        schemasKeyColumnsValues.Add(schema, entitiesKeyColumnsValues);
                    }
                    int              destinationIndex     = destination.GetIndex();
                    object           attributeColumnValue = destination.FindAttributeColumnValue();
                    string           destinationKey       = string.Concat(destinationIndex, attributeColumnValue);
                    KeyColumnsValues keyColumnsValues;
                    if (!entitiesKeyColumnsValues.TryGetValue(destinationKey, out keyColumnsValues))
                    {
                        keyColumnsValues = new KeyColumnsValues();
                        entitiesKeyColumnsValues.Add(destinationKey, keyColumnsValues);
                    }
                    keyColumnsValues.Add(destination.ColumnName, valueForSave);
                    string attributeColumnName = GetDestinationAttributeColumnName(destination, schema);
                    if (!keyColumnsValues.ContainsKey(attributeColumnName))
                    {
                        keyColumnsValues.Add(attributeColumnName, attributeColumnValue);
                    }
                    EntitySchemaColumn connectionColumn     = importEntity.GetReferenceColumn(schema);
                    string             connectionColumnName = connectionColumn.Name;
                    if (!keyColumnsValues.ContainsKey(connectionColumnName))
                    {
                        Guid primaryColumnValue = importEntity.PrimaryEntity.PrimaryColumnValue;
                        keyColumnsValues.Add(connectionColumnName, primaryColumnValue);
                    }
                }
            }
            return(schemasKeyColumnsValues);
        }
        private void OnImportEntitySaveError(Exception e, ImportEntity importEntity, Guid importSessionId)
        {
            var eventArgs = new ImportEntitySaveErrorEventArgs {
                Exception       = e,
                ImportEntity    = importEntity,
                ImportSessionId = importSessionId
            };

            ImportEntitySaveError?.Invoke(this, eventArgs);
        }
        private void OnImportEntityError(Exception e, ImportEntity importEntity, Guid importSessionId)
        {
            var eventArgs = new ImportEntitySaveErrorEventArgs {
                Exception       = e,
                ImportEntity    = importEntity,
                ImportSessionId = importSessionId
            };

            Logger.HandleException(this, eventArgs);
        }
        private Dictionary <string, object> GetFiltersParameters(ImportEntity importEntity, IEnumerable <ImportColumn> keysImportColumns, EntitySchema entitySchema)
        {
            var filtersParameters = new Dictionary <string, object>();

            foreach (var importColumn in keysImportColumns)
            {
                var columnValue          = importEntity.FindColumnValue(importColumn);
                var rootSchemaKeyColumns = importColumn.GetDestinationRootSchemaKeyColumns(entitySchema.UId);
                var filterItemParameter  = rootSchemaKeyColumns.Select(d => GetFilterItemParameter(d, columnValue, entitySchema));
                filtersParameters.AddRange(filterItemParameter);
            }
            return(filtersParameters);
        }
 private void InitEntityForSave(ImportParameters parameters, ImportEntity importEntity)
 {
     importEntity.InitPrimaryEntity(UserConnection, parameters);
     foreach (ImportColumn column in parameters.Columns)
     {
         ImportColumnValue columnValue = importEntity.FindColumnValue(column);
         if (columnValue == null)
         {
             continue;
         }
         SetEntityColumnValue(importEntity, column, columnValue);
     }
 }
        /// <summary>
        /// Creates child entity filters.
        /// </summary>
        /// <param name="esq">Entity schema query.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <param name="destination">Import column destination.</param>
        /// <param name="valueForSave">Value for save.</param>
        /// <returns>Child entity filters.</returns>
        private EntitySchemaQueryFilterCollection CreateChildEntityFilters(EntitySchemaQuery esq,
                                                                           ImportEntity importEntity, Dictionary <string, object> keyColumnsValues)
        {
            EntitySchemaQueryFilterCollection filters = new EntitySchemaQueryFilterCollection(esq);

            foreach (KeyValuePair <string, object> keyColumnValue in keyColumnsValues)
            {
                IEntitySchemaQueryFilterItem filterItem = esq.CreateFilterWithParameters(FilterComparisonType.Equal,
                                                                                         keyColumnValue.Key, keyColumnValue.Value);
                filters.Add(filterItem);
            }
            return(filters);
        }
            public ImportDataChunk <LookupChunkData> CreateChunk(ImportDataChunk <EntityChunkData> chunk)
            {
                var processor = new LookupValuesToProcess(_userConnection);

                foreach (var entityMemento in chunk.Data.Entities)
                {
                    var entity = ImportEntity.CreateFromMemento(entityMemento);
                    processor.Add(entity, _lookupColumns);
                }
                var newLookupChunk = CreateChunk(processor.SaveState());

                return(newLookupChunk);
            }
        /// <summary>
        /// Gets entity key columns names.
        /// </summary>
        /// <param name="destination">Import column destination.</param>
        /// <param name="schema">Entity schema.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <param name="destinationValues">Destination values.</param>
        /// <returns>Entity key columns names.</returns>
        private IEnumerable <string> GetEntityKeyColumnsNames(ImportColumnDestination destination, EntitySchema schema,
                                                              ImportEntity importEntity, Dictionary <ImportColumnDestination, object> destinationValues)
        {
            List <string> entityKeyColumnsNames = new List <string> {
                GetDestinationAttributeColumnName(destination, schema)
            };
            EntitySchemaColumn connectionColumn = importEntity.GetReferenceColumn(schema);

            entityKeyColumnsNames.Add(connectionColumn.Name);
            IEnumerable <string> destinationColumnsNames = GetDestinationColumnsNames(destinationValues, destination);

            entityKeyColumnsNames.AddRange(destinationColumnsNames);
            return(entityKeyColumnsNames);
        }
        /// <summary>
        /// Get import entity from row.
        /// </summary>
        /// <param name="row">Source entity.</param>
        /// <param name="entity">Out Entity.</param>
        /// <returns>New import entity from row.</returns>
        private bool GetEntity(Row row, out ImportEntity entity)
        {
            entity = null;
            IEnumerable <ImportColumnValue> columnValues = ProcessRow(row);
            bool isColumnsValuesEmpty = IsColumnsValuesEmpty(columnValues);

            if (isColumnsValuesEmpty)
            {
                return(false);
            }
            entity = new ImportEntity()
            {
                ColumnValues = columnValues,
                RowIndex     = row.RowIndex.Value
            };
            return(true);
        }
        /// <summary>
        /// Gets import entity key.
        /// </summary>
        /// <param name="destination">Import column destination.</param>
        /// <param name="schema">Entity schema.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <param name="destinationValues">Destination values.</param>
        /// <returns>Import entity key.</returns>
        private string GetImportEntityKey(ImportColumnDestination destination, EntitySchema schema,
                                          ImportEntity importEntity, Dictionary <ImportColumnDestination, object> destinationValues)
        {
            StringBuilder sb = new StringBuilder();
            object        attributeColumnValue = destination.FindAttributeColumnValue();

            sb.Append(attributeColumnValue);
            Guid connectionColumnValue = importEntity.PrimaryEntity.PrimaryColumnValue;

            sb.Append(connectionColumnValue);
            IEnumerable <object> destinationValuesForSave = GetDestinationValuesForSave(destinationValues, destination);

            foreach (object destinationValueForSave in destinationValuesForSave)
            {
                sb.Append(destinationValueForSave);
            }
            return(sb.ToString());
        }
 /// <summary>
 /// Adds child entities.
 /// </summary>
 /// <param name="entities">Entities.</param>
 /// <param name="importEntity">Import entity.</param>
 /// <param name="schemasKeyDestinations">Schemas key destinations.</param>
 private void AddChildEntities(IEnumerable <Entity> entities, ImportEntity importEntity,
                               Dictionary <EntitySchema, Dictionary <ImportColumnDestination, object> > schemasKeyDestinations)
 {
     foreach (var schemaKeyDestinations in schemasKeyDestinations)
     {
         EntitySchema schema = schemaKeyDestinations.Key;
         Dictionary <ImportColumnDestination, object> destinationValues = schemaKeyDestinations.Value;
         foreach (var destinationValue in destinationValues)
         {
             ImportColumnDestination destination = destinationValue.Key;
             Entity childEntity = importEntity.FindChildEntity(destination);
             if (childEntity != null)
             {
                 continue;
             }
             AddChildEntity(destination, schema, importEntity, destinationValues, entities);
         }
     }
 }
 /// <summary>
 /// Adds child entities query filters.
 /// </summary>
 /// <param name="parameters">Import parameters.</param>
 /// <param name="importEntity">Import entity.</param>
 /// <param name="schemasKeyColumnsValues">Schemas key columns values.</param>
 private void AddChildEntitiesQueryFilters(ImportParameters parameters, ImportEntity importEntity,
                                           SchemasKeyColumnsValues schemasKeyColumnsValues)
 {
     foreach (var schemaKeyColumnsValues in schemasKeyColumnsValues)
     {
         EntitySchema      schema = schemaKeyColumnsValues.Key;
         EntitySchemaQuery esq;
         if (!ChildEntitiesQueries.TryGetValue(schema, out esq))
         {
             esq = CreateEntitiesSearchQuery(schema);
             ChildEntitiesQueries.Add(schema, esq);
         }
         foreach (var entitiesKeyColumnsValues in schemaKeyColumnsValues.Value)
         {
             Dictionary <string, object>       keyColumnsValues = entitiesKeyColumnsValues.Value;
             EntitySchemaQueryFilterCollection filters          = CreateChildEntityFilters(esq, importEntity,
                                                                                           keyColumnsValues);
             esq.Filters.Add(filters);
         }
     }
 }
        /// <summary>
        /// Adds child entity.
        /// </summary>
        /// <param name="destination">Import column destination.</param>
        /// <param name="schema">Entity schema.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <param name="destinationValues">Destination values.</param>
        /// <param name="entities">Entities.</param>
        private void AddChildEntity(ImportColumnDestination destination, EntitySchema schema,
                                    ImportEntity importEntity, Dictionary <ImportColumnDestination, object> destinationValues,
                                    IEnumerable <Entity> entities)
        {
            string importEntityKey = GetImportEntityKey(destination, schema, importEntity, destinationValues);
            IEnumerable <string> entityKeyColumnsNames = GetEntityKeyColumnsNames(destination, schema, importEntity,
                                                                                  destinationValues);

            foreach (Entity entity in entities)
            {
                if (!entity.Schema.UId.Equals(schema.UId))
                {
                    continue;
                }
                string entityKey = GetEntityKey(entity, entityKeyColumnsNames);
                if (importEntityKey.Equals(entityKey))
                {
                    importEntity.AddChildEntity(destination, entity);
                    break;
                }
            }
        }
        private void SetBufferedImportEntityInsertColumnValues(ImportEntity importEntity, Insert insertQuery,
                                                               IEnumerable <ImportColumn> keyColumns)
        {
            var schemaColumns = _schema.Columns;

            foreach (var c in keyColumns.Select((c, i) => (Column: c, Index: i + 1)))
            {
                var column      = c.Column;
                var index       = c.Index;
                var columnName  = $"Column{index}";
                var destination = column.Destinations.FirstOrDefault();
                if (destination == null)
                {
                    throw new ArgumentNullOrEmptyException(nameof(destination));
                }
                var dataValueType = schemaColumns.GetByName(destination.ColumnName).DataValueType;
                var columnValue   = importEntity.FindColumnValue(column);
                var valueForSave  = columnValue == null
                                                ? dataValueType.DefValue
                                                : ColumnsProcessor.FindValueForSave(destination, columnValue);
                insertQuery.Set(columnName, Column.Parameter(valueForSave, dataValueType));
            }
        }
        /// <summary>
        /// Gets schemas key columns values.
        /// </summary>
        /// <param name="parameters">Import parameters.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <returns>Schemas key columns values.</returns>
        private Dictionary <EntitySchema, Dictionary <ImportColumnDestination, object> > GetSchemasKeyDestinations(
            ImportParameters parameters, ImportEntity importEntity)
        {
            var schemasKeyDestinations = new Dictionary <EntitySchema, Dictionary <ImportColumnDestination, object> >();

            foreach (ImportColumn column in parameters.Columns)
            {
                ImportColumnValue columnValue = importEntity.FindColumnValue(column);
                if (columnValue == null)
                {
                    continue;
                }
                foreach (ImportColumnDestination destination in column.Destinations)
                {
                    Guid schemaUId = destination.SchemaUId;
                    if (schemaUId.Equals(parameters.RootSchemaUId))
                    {
                        continue;
                    }
                    object valueForSave = ColumnsProcessor.FindValueForSave(destination, columnValue);
                    if (valueForSave == null)
                    {
                        continue;
                    }
                    EntitySchema schema = UserConnection.EntitySchemaManager.GetInstanceByUId(schemaUId);
                    Dictionary <ImportColumnDestination, object> keyDestinations;
                    if (!schemasKeyDestinations.TryGetValue(schema, out keyDestinations))
                    {
                        keyDestinations = new Dictionary <ImportColumnDestination, object>();
                        schemasKeyDestinations.Add(schema, keyDestinations);
                    }
                    keyDestinations.Add(destination, valueForSave);
                }
            }
            return(schemasKeyDestinations);
        }
 /// <summary>
 /// Creates instance of type <see cref="LookupCell" />.
 /// </summary>
 public LookupCell(ImportEntity importEntity, ImportColumn importColumn)
 {
     _rowIndex    = importEntity == null ? default(uint) : importEntity.RowIndex;
     _columnIndex = importColumn?.Index;
 }
 /// <summary>
 /// Adds raw value that will be processed.
 /// </summary>
 /// <param name="importEntity">Import entity.</param>
 /// <param name="column">Import column.</param>
 /// <param name="columnValue">Import column value.</param>
 /// <param name="destination">Import column destination.</param>
 public void Add(ImportEntity importEntity, ImportColumn column, ImportColumnValue columnValue,
                 ImportColumnDestination destination)
 {
     LookupValuesToProcess.Add(importEntity, column, columnValue, destination);
 }
 /// <inheritdoc cref="IColumnProcessor"/>
 public void Add(ImportEntity importEntity, ImportColumn column, ImportColumnValue columnValue, ImportColumnDestination destination)
 {
 }
        /// <summary>
        /// Gets key columns values.
        /// </summary>
        /// <param name="parameters">Import parameters.</param>
        /// <param name="importEntity">Import entity.</param>
        /// <param name="keyColumns">Key columns.</param>
        /// <returns>Key columns values.</returns>
        private List <ColumnExpression> GetKeyColumnsValues(ImportParameters parameters, ImportEntity importEntity,
                                                            IEnumerable <ImportColumn> keyColumns)
        {
            var columnsExpressions = new List <ColumnExpression>();

            foreach (var column in keyColumns)
            {
                var columnValue = importEntity.FindColumnValue(column);
                if (columnValue == null)
                {
                    continue;
                }
                foreach (var destination in column.Destinations)
                {
                    if (!destination.SchemaUId.Equals(parameters.RootSchemaUId) || !destination.IsKey)
                    {
                        continue;
                    }
                    var valueForSave = ColumnsProcessor.FindValueForSave(destination, columnValue);
                    if (valueForSave == null)
                    {
                        continue;
                    }
                    columnsExpressions.Add(new ColumnExpression {
                        ColumnValueName  = destination.ColumnName,
                        ComparisonType   = FilterComparisonType.Equal,
                        ColumnValue      = valueForSave,
                        DataValueTypeUId = Guid.Parse(destination.Properties[_typeUIdPropertyName].ToString())
                    });
                }
            }
            return(columnsExpressions);
        }