public DataTable GetCustomField(Guid tenantId, string entityName, DataTable source, QueryContext queryModel)
        {
            if (source == null || source.Rows.Count == 0)
            {
                return(source);
            }
            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var subTypes = iMetadataManager.GetSubTypesDetails(entityName);
            IPicklistManager iPicklistManager = new PicklistManager();
            var entity            = iMetadataManager.GetEntitityByName(entityName);
            List <MappedItem> val = new List <MappedItem> ();
            var customFields      = new List <FieldModel> ();

            // string[] userQuery = null;
            if (queryModel.Fields != null && queryModel.Fields.Any())
            {
                var entityDetails = iMetadataManager.GetEntitityByName(entityName);
                if (entityDetails?.Fields != null && entityDetails.Fields.Any())
                {
                    var userQuery = queryModel?.Fields?.Split(',');
                    foreach (var clientField in userQuery)
                    {
                        var match = entityDetails.Fields.FirstOrDefault(t =>
                                                                        t.Name.ToLower().Equals(clientField.ToString().ToLower()) && !string.IsNullOrEmpty(t.Context) && !string.IsNullOrEmpty(t.ContextType));
                        if (match != null)
                        {
                            if (match.ContextType.Equals("CustomServerFieldBase"))
                            {
                                source.Columns.Add(match.Name, typeof(string));
                                customFields.Add(match);
                            }
                            else if (match.ContextType.Equals("CustomClientFieldBase"))
                            {
                                DataColumn client = new DataColumn(match.Name, typeof(string));
                                // client.DefaultValue = match.Context;
                                source.Columns.Add(client);
                            }
                        }
                    }
                }
            }

            foreach (DataRow item in source.Rows)
            {
                try {
                    if (customFields.Any())
                    {
                        foreach (var cf in customFields)
                        {
                            if (item.Table.Columns.Contains(cf.Name))
                            {
                                var conditions = new Dictionary <string, string> ();
                                var id         = Guid.Parse(item["InternalId"].ToString());
                                item[cf.Name] = GetCustomFieldValue(id, cf.Context, conditions);
                            }
                        }
                    }
                } catch { //digest exceptions }
                }
            }
            return(source);
        }
        public DataTable MapResult(Guid tenantId, string entityName, DataTable source, QueryContext queryModel)
        {
            if (source == null || source.Rows.Count == 0)
            {
                return(source);
            }

            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var subTypes = iMetadataManager.GetSubTypesDetails(entityName);
            IPicklistManager iPicklistManager = new PicklistManager();
            var picklists             = iPicklistManager.GetAllPicklists(tenantId);
            var simpleNonCustomizable = picklists.Where(t => !t.CustomizeValue).ToList();

            if (!simpleNonCustomizable.Any())
            {
                return(source);
            }
            var entity            = iMetadataManager.GetEntitityByName(entityName);
            List <MappedItem> val = new List <MappedItem>();

            if (entity.Fields != null && entity.Fields.Any())
            {
                GetUsedPicklistInEntity(tenantId, simpleNonCustomizable, entity.Fields, val);
            }
            if (entity.VersionControl != null && entity.VersionControl.Fields != null && entity.VersionControl.Fields.Any())
            {
                GetUsedPicklistInEntity(tenantId, simpleNonCustomizable, entity.VersionControl.Fields, val);
            }

            DataTable dtClone = source.Clone(); //just copy structure, no data

            for (int i = 0; i < dtClone.Columns.Count; i++)
            {
                if (dtClone.Columns[i].DataType != typeof(string))
                {
                    dtClone.Columns[i].DataType = typeof(string);
                }
            }

            foreach (DataRow dr in source.Rows)
            {
                dtClone.ImportRow(dr);
            }

            var customFields = new List <FieldModel>();

            // string[] userQuery = null;
            if (queryModel.Fields != null && queryModel.Fields.Any())
            {
                var entityDetails = iMetadataManager.GetEntitityByName(entityName);

                if (entityDetails?.Fields != null && entityDetails.Fields.Any())
                {
                    var userQuery = queryModel?.Fields?.Split(',');
                    foreach (var clientField in userQuery)
                    {
                        var match = entityDetails.Fields.FirstOrDefault(t =>
                                                                        t.Name.ToLower().Equals(clientField.ToString().ToLower()) && !string.IsNullOrEmpty(t.Context) && !string.IsNullOrEmpty(t.ContextType));
                        if (match != null)
                        {
                            if (match.ContextType.Equals("CustomServerFieldBase"))
                            {
                                dtClone.Columns.Add(match.Name, typeof(string));
                                customFields.Add(match);
                            }
                            else if (match.ContextType.Equals("CustomClientFieldBase"))
                            {
                                DataColumn client = new DataColumn(match.Name, typeof(string));
                                //  client.DefaultValue = match.Context;
                                dtClone.Columns.Add(client);
                            }
                        }
                    }
                }
            }

            foreach (DataRow item in dtClone.Rows)
            {
                try
                {
                    var subType = item["SubType"];
                    var mapped  = subTypes.FirstOrDefault(t => t.Key.Equals(subType));
                    if (mapped.Key != null)
                    {
                        item["SubType"] = mapped.Value.ToString();
                    }

                    foreach (var data in val)
                    {
                        if (item.Table.Columns.Contains(data.Name.ToLower()))
                        {
                            if (data.Result != null && data.Result.Rows.Count > 0)
                            {
                                var       actualValue = item[data.Name].ToString();
                                DataRow[] rowsData    = data.Result.Select();
                                for (int i = 0; i < rowsData.Length; i++)
                                {
                                    var existenceValue = rowsData[i]["InternalId"];
                                    if (actualValue.ToString().ToLower().Equals(existenceValue.ToString().ToLower()))
                                    {
                                        var text = rowsData[i]["Text"].ToString();
                                        item[data.Name] = text.ToString();
                                    }
                                }
                            }
                        }
                    }
                    //------------------------- custom client field
                    if (customFields.Any())
                    {
                        foreach (var cf in customFields)
                        {
                            if (item.Table.Columns.Contains(cf.Name))
                            {
                                var conditions = new Dictionary <string, string>();
                                //conditions.Add ("InternalId", item["InternalId"].ToString ());
                                var id = Guid.Parse(item["InternalId"].ToString());
                                item[cf.Name] = GetCustomFieldValue(id, cf.Context, conditions);
                            }
                        }
                    }
                }
                catch
                { //digest exceptions }
                }
            }
            return(dtClone);
        }
Example #3
0
        internal List <ColumnAndField> AddDataToColumns(Guid tenantId, Guid resourceId, string entityName, string tableName, string primaryKey, Dictionary <string, string> payload, List <ColumnAndField> necessaryColumns)
        {
            IMetadataManager entityManager = new MetadataManager.Contracts.MetadataManager();
            //added primary key....
            var matchingColumns         = new List <ColumnAndField> ();
            var entityPrimaryKeyColumns = necessaryColumns.FirstOrDefault(t => t.ColumnName.Equals(primaryKey) && t.TableName.Equals(tableName));

            if (entityPrimaryKeyColumns != null)
            {
                entityPrimaryKeyColumns.Value = resourceId;
                matchingColumns.Add(entityPrimaryKeyColumns);
            }

            //added query matching..
            foreach (var col in necessaryColumns)
            {
                var colname  = !string.IsNullOrEmpty(col.ClientName) ? col.ClientName + "." + col.FieldName : col.FieldName;
                var matching = payload.FirstOrDefault(t => t.Key.ToLower().Equals(colname.ToLower()));
                if (matching.Key == null)
                {
                    continue;
                }

                if (col.Value == "#ENTCAST") //this is for context to get Entitycontext Id by defaultvalueattributes
                {
                    col.Value = entityManager.GetEntityContextByEntityName(matching.Value);
                }
                else
                {
                    col.Value = matching.Value;
                }

                matchingColumns.Add(col);
            }

            //inverser key matching....
            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var columns    = iMetadataManager.GetColumnNameByEntityName(entityName, null);
            var inverseKey = columns.Where(t => !string.IsNullOrEmpty(t.InverseTableName) && !string.IsNullOrEmpty(t.InverseColumnName)).ToList();

            if (inverseKey != null)
            {
                foreach (var item in inverseKey)
                {
                    var isAddedInMatching = matchingColumns.FirstOrDefault(t => t.TableName.Equals(item.InverseTableName) && t.EntityPrefix.Equals(item.InversePrefixName));
                    if (isAddedInMatching != null && !string.IsNullOrEmpty(isAddedInMatching.Value))
                    {
                        var targetColumn = item.ColumnName.TrimStart('[').TrimEnd(']');
                        var resultValue  = GetValueFromInverseColumns(tenantId, item.TableName, item.EntityPrefix, primaryKey, targetColumn, resourceId.ToString());

                        if (!string.IsNullOrEmpty(resultValue))
                        {
                            item.Value = resultValue.ToString();
                            var updateResult = UpdateValueInInverseTable(tenantId, item.InverseTableName, item.InversePrefixName, item.PrimaryKey, necessaryColumns, resultValue.ToString());
                        }
                        else
                        {
                            var addedResult = AddValueFromInverseColumns(tenantId, item.InverseTableName, item.InversePrefixName, item.PrimaryKey, necessaryColumns, columns);
                            item.Value = addedResult.ToString();
                        }
                        matchingColumns.Add(item);
                    }
                }
            }
            List <Entity> intersects = null;
            var           entity     = iMetadataManager.GetEntitityByName(entityName);

            if (entity != null && entity.DetailEntities != null && entity.DetailEntities.Any())
            {
                intersects = entity.DetailEntities.Where(t => t.Type.ToLower().Equals("intersectentity")).ToList();
                if (intersects != null && intersects.Any())
                {
                    foreach (var item in intersects)
                    {
                        var match = payload.FirstOrDefault(t => t.Key.ToLower().Equals(item.Name.ToLower()));
                        if (match.Key != null)
                        {
                            var value = match.Value;
                            if (!string.IsNullOrEmpty(value))
                            {
                                break;
                            }
                        }
                    }
                    // if (isRequiredToAddIntersect) {
                    //     // var intersectColumns = InsertIntersectEntity (tenantId, userId, entityName, resource, entityColumns, itemId);
                    //     // var intersectQuery = BuildInsertQuery (intersectColumns);
                    //     // admin.SaveResult (tenantId, intersectColumns[0].TableName, intersectQuery);
                    // }
                }
            }

            var forenkeysInColumn          = columns.Where(t => !string.IsNullOrEmpty(t.ReferenceTableName) && !string.IsNullOrEmpty(t.ReferenceColumnName)).ToList();
            List <ColumnAndField> forenkey = new List <ColumnAndField> ();

            if (intersects != null && intersects.Any())
            {
                foreach (var fore in forenkeysInColumn)
                {
                    var matchingWithForeignKey = intersects.FirstOrDefault(t => t.Name.Equals(fore.EntityFullName));
                    if (matchingWithForeignKey == null)
                    {
                        forenkey.Add(fore);
                    }
                }
            }

            if (forenkey != null)
            {
                foreach (var item in forenkey)
                {
                    var isAddedInMatching = matchingColumns.FirstOrDefault(t => t.TableName.Equals(item.ReferenceTableName) && t.ColumnName.Equals(item.ReferenceColumnName));
                    if (isAddedInMatching != null && !string.IsNullOrEmpty(isAddedInMatching.Value.ToString()))
                    {
                        var targetColumn = item.ColumnName.TrimStart('[').TrimEnd(']');
                        var resultValue  = GetValueFromForeignKeyColumns(tenantId, item.TableName, item.EntityPrefix, primaryKey, targetColumn, resourceId.ToString());
                        if (!string.IsNullOrEmpty(resultValue))
                        {
                            item.Value = resultValue.ToString();
                            var updateResult = UpdateValueInForeignTable(tenantId, item.TableName, item.EntityPrefix, item.PrimaryKey, necessaryColumns, resultValue.ToString());
                        }
                        else
                        {
                            var addedResult = AddValueFromForeignKeyColumns(tenantId, item.TableName, item.EntityPrefix, item.ColumnName, resourceId, necessaryColumns, columns);
                            item.Value = addedResult.ToString();
                        }
                        // matchingColumns.Add (item);
                    }
                }
            }

            return(matchingColumns);
        }