Ejemplo n.º 1
0
        public List <Session> GetSession(string subscriptionId, string usrlData)
        {
            var bytes  = System.Convert.FromBase64String(usrlData);
            var data   = System.Text.Encoding.ASCII.GetString(bytes);
            var inData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(data);

            top        = (long)inData["top"];
            skip       = (long)inData["localSkip"];
            searchText = inData["searchText"] as string;
            columnMap  = inData["columnMap"] as string;
            var            checkQuery = (bool)inData["checkQuery"];
            List <Session> list;

            if (testMode)
            {
                list = testList;
            }
            else
            {
                PowerShellExecutor <Session> executor;
                string key = inData["deploymentInfo"] + ":" + inData["collectionName"];
                if (!SessionController.executors.TryGetValue(key, out executor))
                {
                    executor           = new PowerShellExecutor <Session>();
                    executor.CmdName   = "";
                    executor.NewFunc   = this.NewFunc;
                    executor.Configure = (engine) =>
                    {
                        PowerShellJob.AddVariable(engine, "ConnectionBroker", inData["deploymentInfo"] as string);
                        PowerShellJob.AddVariable(engine, "CollectionName", inData["collectionName"] as string);
                        engine.AddScript("Get-RDUserSession -ConnectionBroker $ConnectionBroker -CollectionName $CollectionName");
                    };
                    executor = SessionController.executors.GetOrAdd(key, executor);
                }

                list = executor.GetList();
            }
            if (list != null && top > 0 && skip >= 0)
            {
                if (!string.IsNullOrWhiteSpace(searchText))
                {
                    try
                    {
                        var colsMap = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, object> >(columnMap.ToLower());
                        list = SessionController.DoQuery <Session>(searchText, list, colsMap, checkQuery);
                    }
                    catch (Exception ex)
                    {
                        ErrorHelper.SendExcepToDB(ex, " GetSession", subscriptionId);
                        throw ex;
                    }
                }
            }

            return(list);
        }
Ejemplo n.º 2
0
        private static List <T> DoQuery <T>(string query, List <T> data, Dictionary <string, object> colsMap, bool checkQuery)
        {
            var queryParts = GetQueryParts(query);

            if (checkQuery)
            {
                if (queryParts.Count != 3)
                {
                    throw new Exception("Query syntax is incorrect.");
                }

                data = new List <T>();
            }

            if (queryParts.Count == 3)
            {
                try
                {
                    return(SessionController.SearchData <T>(data,
                                                            colsMap[queryParts[0]] as string,
                                                            queryParts[1],
                                                            queryParts[2]));
                }
                catch (Exception ex)
                {
                    if (checkQuery)
                    {
                        throw new Exception("Query error");
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }

            return(data);
        }
Ejemplo n.º 3
0
        private static List <T> SearchData <T>(List <T> data, string propName, string op, string searchVal)
        {
            var outputList = new List <T>();
            var prop       = SessionController.GetProperty <T>(propName);

            if (prop != null)
            {
                var query = data.Where((item) =>
                {
                    Type t  = prop.PropertyType;
                    var val = prop.GetValue(item);
                    if (val == null)
                    {
                        return(false);
                    }

                    if (propName.Equals("LogonTime", StringComparison.OrdinalIgnoreCase))
                    {
                        DateTime dateVal    = DateTime.Parse(val.ToString());
                        DateTime searchDate = DateTime.Parse(searchVal.ToString());
                        if (op == "==")
                        {
                            return(dateVal == searchDate);
                        }
                        if (op == "!=")
                        {
                            return(dateVal != searchDate);
                        }
                        else if (op == ">")
                        {
                            return(dateVal > searchDate);
                        }
                        else if (op == "<")
                        {
                            return(dateVal < searchDate);
                        }
                        else if (op == ">=")
                        {
                            return(dateVal >= searchDate);
                        }
                        else if (op == "<=")
                        {
                            return(dateVal <= searchDate);
                        }
                    }
                    else if (t == typeof(int) ||
                             t == typeof(float) ||
                             t == typeof(long) ||
                             t == typeof(double))
                    {
                        var numVal   = double.Parse(searchVal);
                        var checkVal = double.Parse(val.ToString());
                        if (op == "==")
                        {
                            return(numVal == checkVal);
                        }
                        if (op == "!=")
                        {
                            return(numVal != checkVal);
                        }
                        else if (op == ">")
                        {
                            return(checkVal > numVal);
                        }
                        else if (op == "<")
                        {
                            return(checkVal < numVal);
                        }
                        else if (op == ">=")
                        {
                            return(checkVal >= numVal);
                        }
                        else if (op == "<=")
                        {
                            return(checkVal <= numVal);
                        }
                    }
                    else if (propName.Equals("SessionState", StringComparison.OrdinalIgnoreCase))
                    {
                        var valStr = val.ToString();
                        if (op == "==")
                        {
                            return(valStr.Equals("STATE_" + searchVal.ToString().ToUpper()));
                        }
                        if (op == "!=")
                        {
                            return(!valStr.Equals("STATE_" + searchVal.ToString().ToUpper()));
                        }
                    }
                    else
                    {
                        var valStr = val.ToString();
                        if (op == "==")
                        {
                            return(valStr.Equals(searchVal));
                        }
                        else if (op == "!=")
                        {
                            return(!valStr.Equals(searchVal));
                        }
                    }

                    return(false);
                });

                return(query.ToList());
            }

            return(outputList);
        }