Example #1
0
        private List <ColumnAndField> GetMatchingFields(string className, List <ColumnAndField> columns, JObject clientPayload, MatchingOptions options)
        {
            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var tableName    = iMetadataManager.GetTableNameByEntityname(className);
            var primaryKey   = iMetadataManager.GetPrimaryKeyByEntityname(className);
            var classColumns = MatchingColumnsFromUserQuery(className, columns, clientPayload, options, tableName, primaryKey);

            //add internalid and tenant id from rest table

            var consolidatedChildren =
                from c in classColumns
                group c by new {
                c.EntityFullName,
                c.ClientName
            }
            into gcs
                select new GroupedColumns()
            {
                EntityFullName = gcs.Key.EntityFullName,
                ClientName     = gcs.Key.ClientName,
                Columns        = gcs.ToList(),
            };

            //add primary column and tenant columns
            foreach (var item in consolidatedChildren)
            {
                var tableName1  = item.Columns[0].TableName;
                var primaryKey1 = item.Columns[0].PrimaryKey;
                var tablePrefix = item.Columns[0].EntityPrefix;
                var itemId      = (item.EntityFullName.ToLower().Equals(className.ToLower()))?options.PrimaryId:Guid.NewGuid();
                AddBusinessColumn(columns, classColumns, primaryKey1, tableName1, tablePrefix, itemId);
                AddBusinessColumn(columns, classColumns, BusinessConstant.TenantId, tableName1, tablePrefix, options.TenantId);

                //------------------- new logic for product
                var exceptionalColumns = columns.Where(t => t.ReferenceColumnName.ToLower().Equals(primaryKey1.ToLower()) && t.ReferenceTableName.ToLower().Equals(tableName1.ToLower())).ToList();
                if (exceptionalColumns.Any())
                {
                    foreach (var col in exceptionalColumns)
                    {
                        col.Value = itemId;
                        classColumns.Add(col);
                    }
                }
                //-------------------
            }
            //
            var entityIsAnItem = iMetadataManager.EntityIsAnItem(className, false);

            if (!entityIsAnItem)
            {
                return(classColumns);
            }

            var itemFields         = ItemHelper.GetItemSelectDetails(Guid.Empty, "not-required", 0);
            var itemMatchingFields = MatchingColumnsFromUserQuery(itemFields[0].EntityFullName, itemFields, clientPayload, options, ItemHelper.ItemTableName, ItemHelper.ItemTablePrimarykey);

            var mergeColumns = classColumns.Concat(itemMatchingFields).ToList();

            //add rest item if table is item
            AddRestItemColumns(itemFields, mergeColumns, options, clientPayload);
            return(mergeColumns);
        }
        //tested on
        //batchtype (26th July 2019),

        public dynamic BuildDeleteQuery(Guid tenantId, Guid userId, Guid itemId, string entityName)
        {
            IMetadataManager entityManager = new MetadataManager.Contracts.MetadataManager();
            var columns = entityManager.GetColumnNameByEntityName(entityName, null);

            //@todo need to add options based column methods.
            if (!columns.Any())
            {
                return(string.Empty);
            }

            var query              = "";
            var tableName          = entityManager.GetTableNameByEntityname(entityName);
            var primaryKey         = entityManager.GetPrimaryKeyByEntityname(entityName);
            var selectQueryBuilder = new SelectQueryBuilder();

            selectQueryBuilder.SelectFromTable(tableName, string.Empty);
            selectQueryBuilder.AddWhere(primaryKey, Comparison.Equals, itemId.ToString(), 1);
            var          selectQuery  = selectQueryBuilder.BuildQuery();
            IQueryReview review       = new QueryReview();
            var          targetResult = review.GetResult(tenantId, entityName, selectQuery);

            if (targetResult == null || targetResult.Rows.Count == 0)
            {
                return("");
            }


            //delete entity
            query += GetDeleteQuery(tableName, primaryKey, itemId.ToString());

            //delete item
            //@todo need to change business constat isPicklist..
            var isItem = entityManager.EntityIsAnItem(entityName, BusinessConstant.IsPickList);

            if (isItem)
            {
                query += GetDeleteQuery(ItemHelper.ItemTableName, ItemHelper.ItemTablePrimarykey, itemId.ToString());
            }

            foreach (var col in columns)
            {
                if (col.DataType.ToString().ToLower().Equals("picklist") || col.DataType.ToString().ToLower().Equals("lookup"))
                {
                    continue;
                }

                if (!col.EntityFullName.ToLower().Equals(entityName.ToLower()))
                {
                    continue;
                }
                var targetColum = col.ColumnName.TrimStart('[').TrimEnd(']');
                var targetValue = targetResult.Rows[0][targetColum].ToString();

                //inverse matching column
                if (!string.IsNullOrEmpty(col.InverseColumnName) && !string.IsNullOrEmpty(col.InverseTableName))
                {
                    query += GetDeleteQuery(col.InverseTableName, col.InverseColumnName, targetValue);
                }

                //foreign key matching column
                if (!string.IsNullOrEmpty(col.ReferenceColumnName) && !string.IsNullOrEmpty(col.ReferenceTableName))
                {
                    query += GetDeleteQuery(col.InverseTableName, col.InverseColumnName, targetValue);
                }
            }


            return(TransactionHelper.BuildQuery(query));
        }