private static bool AreSelectQueryColumnEqual(SelectQueryColumn expected, SelectQueryColumn actual)
        {
            var areEqual = expected.Expression.ColumnPath == actual.Expression.ColumnPath &&
                           expected.Expression.ExpressionType == actual.Expression.ExpressionType &&
                           expected.OrderDirection == actual.OrderDirection &&
                           expected.OrderPosition == actual.OrderPosition;

            return(areEqual);
        }
        private void ApplyOrderDirectionAndOrderPosition(SelectQueryColumn orderedColumn,
                                                         MethodCallExpression expression, ModelQueryBuildConfig config)
        {
            var position  = GetOrderedColumnsCount(config) + 1;
            var direction = GetOrderDirection(expression);

            orderedColumn.OrderPosition  = position;
            orderedColumn.OrderDirection = direction;
        }
Ejemplo n.º 3
0
        public static void AddColumn(SelectQuery query, string columnPath, string alias = "", OrderDirection orderDirection = OrderDirection.None, int orderPosition = -1)
        {
            var key    = string.IsNullOrEmpty(alias) ? columnPath : alias;
            var column = new SelectQueryColumn()
            {
                Expression = new ColumnExpression()
                {
                    ColumnPath     = columnPath,
                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn
                },
                OrderDirection = orderDirection,
                OrderPosition  = orderPosition
            };

            query.Columns.Items.Add(key, column);
        }
        // Main method of the application.
        static void Main(string[] args)
        {
            // Calling authentication method. If the user is not authenticated, the application exits.
            if (!TryLogin("Supervisor", "Supervisor"))
            {
                Console.WriteLine("Authentication error!");
                return;
            }

            // Instance of the SelectQuery class.
            var selectQuery = new SelectQuery()
            {
                // Root schema name.
                RootSchemaName = "Contact",
                // Adding column values collection.
                Columns = new SelectQueryColumns()
            };

            // Expression class instance of the entity schema query.
            // Used to configure the [Full name] column.
            var columnExpressionName = new ColumnExpression()
            {
                // Entity schema query expression type is a schema column.
                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                // Path to column.
                ColumnPath = "Name"
            };

            // Configuring the [Name] column.
            var selectQueryColumnName = new SelectQueryColumn()
            {
                //Title.
                Caption = "Name",
                // Sorting order — ascending.
                OrderDirection = OrderDirection.Ascending,
                // Sorting order position.
                OrderPosition = 0,
                // Expression that specifies column type.
                Expression = columnExpressionName
            };

            // Expression that specifies [Number of activities] column type.
            var columnExpressionActivitiesCount = new ColumnExpression()
            {
                // Expression type is subquery.
                ExpressionType = EntitySchemaQueryExpressionType.SubQuery,
                // Path to column in relation to root schema.
                ColumnPath = "[Activity:Contact].Id",
                // Function type is aggregation.
                FunctionType = FunctionType.Aggregation,
                // Aggregation type is quantity.
                AggregationType = AggregationType.Count
            };

            // Configuring the [Number of activities] column.
            var selectQueryColumnActivitiesCount = new SelectQueryColumn()
            {
                //Title.
                Caption = "Activities count",
                // Ascending sorting direction.
                OrderDirection = OrderDirection.Ascending,
                // Sorting order position.
                OrderPosition = 1,
                // Expression, which specifies column type.
                Expression = columnExpressionActivitiesCount
            };

            // Adding columns to query.
            selectQuery.Columns.Items = new Dictionary <string, SelectQueryColumn>()
            {
                {
                    "Name",
                    selectQueryColumnName
                },
                {
                    "ActivitiesCount",
                    selectQueryColumnActivitiesCount
                }
            };


            // Serializing the SelectQuery instance to a JSON string.
            var json = new JavaScriptSerializer().Serialize(selectQuery);

            // You can log the JSON string to the console.
            Console.WriteLine(json);
            Console.WriteLine();

            // Converting JSON string to a byte array.
            byte[] jsonArray = Encoding.UTF8.GetBytes(json);
            // Creating HTTP request instance.
            var selectRequest = HttpWebRequest.Create(selectQueryUri) as HttpWebRequest;

            // Defining HTTP method of the request.
            selectRequest.Method = "POST";
            // Defining request content type.
            selectRequest.ContentType = "application/json";
            // Adding the previously received authentication cookies.
            selectRequest.CookieContainer = AuthCookie;
            // Setting the request content length.
            selectRequest.ContentLength = jsonArray.Length;

            // Putting BPMCSRF token to the request header.
            CookieCollection cookieCollection = AuthCookie.GetCookies(new Uri(authServiceUri));
            string           csrfToken        = cookieCollection["BPMCSRF"].Value;

            selectRequest.Headers.Add("BPMCSRF", csrfToken);

            // Adding a JSON string to the request body.
            using (var requestStream = selectRequest.GetRequestStream())
            {
                requestStream.Write(jsonArray, 0, jsonArray.Length);
            }

            // Executing the HTTP request and receiving response from the server.
            using (var response = (HttpWebResponse)selectRequest.GetResponse())
            {
                // Displaying respose in console.
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    Console.WriteLine(reader.ReadToEnd());
                    // Response is just a JSON string.
                    // The main prooperties of such JSON are:
                    // "rowConfig" - contains the structure of response records.
                    // "rows" - contains the collection of response records.
                    // "success" - indicates whether the record was added successfully.
                    // You can convert this JSON to a plain old CLR object in same way as it is done in TryLogin() method above.
                    // But you have to define corresponding class before doing this.
                }
            }

            // Pause.
            Console.ReadKey();
        }
        public static void ReadEntity(string probableName, Guid accountId, bool isByGuid)
        {
            var selectQuery = new SelectQuery()
            {
                RootSchemaName = "Account",
                Columns        = new SelectQueryColumns()
            };

            #region nameColumn
            var columnExpressionName = new ColumnExpression()
            {
                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                ColumnPath     = "Name"
            };
            var selectQueryColumnName = new SelectQueryColumn()
            {
                Expression = columnExpressionName
            };
            #endregion

            #region createdOnColumn
            var columnExpressionCreatedOn = new ColumnExpression()
            {
                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                ColumnPath     = "CreatedOn"
            };
            var selectQueryColumnCreatedOn = new SelectQueryColumn()
            {
                Expression = columnExpressionCreatedOn
            };
            #endregion

            #region comletenessColumn
            var columnExpressionCompleteness = new ColumnExpression()
            {
                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                ColumnPath     = "Completeness"
            };
            var selectQueryColumnCompleteness = new SelectQueryColumn()
            {
                Expression = columnExpressionCompleteness
            };
            #endregion

            #region zipColumn
            var columnExpressionZip = new ColumnExpression()
            {
                ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                ColumnPath     = "Zip"
            };
            var selectQueryColumnZip = new SelectQueryColumn()
            {
                Expression = columnExpressionZip
            };
            #endregion

            #region accountTypeColumn
            var columnExpressionAccountType = new ColumnExpression()
            {
                ExpressionType = EntitySchemaQueryExpressionType.SubQuery,
                ColumnPath     = "[AccountType:Id:TypeId].Name",
            };
            var selectQueryColumnAccountType = new SelectQueryColumn()
            {
                Expression = columnExpressionAccountType
            };
            #endregion

            selectQuery.Columns.Items = new Dictionary <string, SelectQueryColumn>()
            {
                {
                    "Name",
                    selectQueryColumnName
                },
                {
                    "CreatedOn",
                    selectQueryColumnCreatedOn
                },
                {
                    "Completeness",
                    selectQueryColumnCompleteness
                },
                {
                    "Zip",
                    selectQueryColumnZip
                },
                {
                    "AccountType",
                    selectQueryColumnAccountType
                },
            };
            var byNameFilter = new Filters()
            {
                FilterType     = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,
                ComparisonType = FilterComparisonType.Contain,
                LeftExpression = new BaseExpression()
                {
                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                    ColumnPath     = "Name"
                },
                RightExpression = new BaseExpression()
                {
                    ExpressionType = EntitySchemaQueryExpressionType.Parameter,
                    Parameter      = new Parameter()
                    {
                        DataValueType = DataValueType.Text,
                        Value         = probableName
                    }
                },
            };

            var byIdFilter = new Filters()
            {
                FilterType     = Terrasoft.Nui.ServiceModel.DataContract.FilterType.CompareFilter,
                ComparisonType = FilterComparisonType.Equal,
                LeftExpression = new BaseExpression()
                {
                    ExpressionType = EntitySchemaQueryExpressionType.SchemaColumn,
                    ColumnPath     = "Id"
                },
                RightExpression = new BaseExpression()
                {
                    ExpressionType = EntitySchemaQueryExpressionType.Parameter,
                    Parameter      = new Parameter()
                    {
                        DataValueType = DataValueType.Guid,
                        Value         = accountId
                    }
                }
            };
            if (isByGuid)
            {
                selectQuery.Filters = byIdFilter;
            }
            else
            {
                selectQuery.Filters = byNameFilter;
            }


            var    json      = new JavaScriptSerializer().Serialize(selectQuery);
            byte[] jsonArray = Encoding.UTF8.GetBytes(json);
            LoginClass.CreateAndSendHttpRequestDataService(out HttpWebRequest selectRequest, Configuration.RequestType.Select);
            using (var requestStream = selectRequest.GetRequestStream())
            {
                requestStream.Write(jsonArray, 0, jsonArray.Length);
            }
            string jsonanswer;
            using (var response = (HttpWebResponse)selectRequest.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    jsonanswer = reader.ReadToEnd();
                }
            }

            RootObject jsonObject = JsonConvert.DeserializeObject <RootObject>(jsonanswer);
            foreach (var item in jsonObject.rows)
            {
                Console.WriteLine(String.Format(Localize.AccountOutput, item.GetPropertyValue("Name"), item.GetPropertyValue("AccountType"), item.GetPropertyValue("Zip"), item.GetPropertyValue("Completeness"), item.GetPropertyValue("CreatedOn")));
            }
        }