private string FormatLogicalExpression(IFilterTree tree, bool isolate)
        {
            var operatorText = TokenMappings.TokenTypeToDefaultValueMap[tree.Token.Type];

            string resultExpression;

            if (TokenMappings.IsBinaryLogicalOperation(tree.Token))
            {
                var operandTexts = new List <string>();
                foreach (var childItem in tree.GetChildren())
                {
                    operandTexts.Add(FormatExpression(childItem, true));
                }

                resultExpression = string.Join($" {operatorText} ", operandTexts);
            }
            else if (TokenMappings.IsUnaryLogicalOperation(tree.Token))
            {
                var childNode   = tree.GetChild(0);
                var operandText = FormatExpression(childNode, false);

                resultExpression = TokenMappings.IsBinaryLogicalOperation(childNode.Token)
                                        ? $"{operatorText} ({operandText})"
                                        : $"{operatorText} {operandText}";
            }
            else
            {
                throw new FormaterException($"Encountered unexpected node of type {tree.Token.Type}.");
            }

            return(isolate
                                ? $"({resultExpression})"
                                : resultExpression);
        }
示例#2
0
 public StackingViewSpecification(string viewDisplayName, IFilterTree filterTree,
                                  IEnumerable <Guid> necessaryMIATypeIDs, IEnumerable <Guid> optionalMIATypeIDs, bool onlyOnline) :
     base(viewDisplayName, filterTree, necessaryMIATypeIDs, optionalMIATypeIDs, onlyOnline)
 {
     SortedSubViews         = true; // Stacking view has special sorting included.
     CustomItemsListSorting = SortByRecordingDate;
 }
        public void TestFilterTree()
        {
            IFilterTree tree = new RelationshipFilterTree(baseRole);

            tree.AddFilter(new RelationalFilter(MediaAspect.ATTR_TITLE, RelationalOperator.EQ, "Item2"),
                           new FilterTreePath(item1Role, item2Role));
            tree.AddFilter(new RelationalFilter(MediaAspect.ATTR_TITLE, RelationalOperator.EQ, "Item3"),
                           new FilterTreePath(item1Role, item3Role));
            tree.AddFilter(new RelationalFilter(MediaAspect.ATTR_TITLE, RelationalOperator.EQ, "Item1"),
                           new FilterTreePath(item1Role));
            tree.AddFilter(new RelationalFilter(MediaAspect.ATTR_TITLE, RelationalOperator.EQ, "Item0"));

            //No linked ids, should build full filter in both directions
            TestFilterTreeNoLinkedIds(tree);
            //Test copy
            IFilterTree copy = tree.DeepCopy();

            TestFilterTreeNoLinkedIds(copy);

            //Add a linked id in the middle, should optimise by ignoring any filters on the linked media item
            tree.AddLinkedId(item1Id, new FilterTreePath(item1Role));
            TestFilterTreeWithLinkedIds(tree);
            //Test copy
            copy = tree.DeepCopy();
            TestFilterTreeWithLinkedIds(copy);
        }
示例#4
0
 public MediaLibraryQueryViewSpecification(string viewDisplayName, IFilterTree filterTree,
                                           IEnumerable <Guid> necessaryMIATypeIDs, IEnumerable <Guid> optionalMIATypeIDs, bool onlyOnline) :
     base(viewDisplayName, necessaryMIATypeIDs, optionalMIATypeIDs)
 {
     _filterTree = filterTree ?? new SimpleFilterTree();
     _query      = new MediaItemQuery(necessaryMIATypeIDs, optionalMIATypeIDs, _filterTree.BuildFilter());
     _onlyOnline = onlyOnline;
 }
        /// <inheritdoc />
        /// <exception cref="ArgumentNullException"><paramref name="tree"/> is <see langword="null" />.</exception>
        public string FormatString(IFilterTree tree)
        {
            if (tree == null)
            {
                throw new ArgumentNullException(nameof(tree));
            }

            return(FormatExpression(tree));
        }
示例#6
0
        /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is <see langword="null" />.</exception>
        public static bool CompareTrees([NotNull] IFilterTree expected, [NotNull] IFilterTree actual)
        {
            if (expected == null)
            {
                throw new ArgumentNullException(nameof(expected));
            }
            if (actual == null)
            {
                throw new ArgumentNullException(nameof(actual));
            }

            if (expected.Token.Type != actual.Token.Type)
            {
                return(false);
            }

            if (expected.ChildCount != actual.ChildCount)
            {
                return(false);
            }

            if (IsContentToken(expected.Token.Type))
            {
                if (expected.Token.Type == TokenType.Real)
                {
                    var expectedValue = ToDouble(expected.Token.Value);
                    var actualValue   = ToDouble(actual.Token.Value);

                    // a precision of 10^-12 is enough (same as defined in Zeiss.PiWeb.Common.Util.DoubleCompareHelper)
                    return(Math.Abs(expectedValue - actualValue) <= 1e-12);
                }

                if (expected.Token.Type == TokenType.Integer)
                {
                    var expectedValue = int.Parse(expected.Token.Value);
                    var actualValue   = int.Parse(actual.Token.Value);

                    return(expectedValue == actualValue);
                }

                if (!string.Equals(expected.Token.Value, actual.Token.Value))
                {
                    return(false);
                }
            }

            for (var i = 0; i < expected.ChildCount; ++i)
            {
                if (!CompareTrees(expected.GetChild(i), actual.GetChild(i)))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#7
0
        public static IFilterTree MakeLegacyValueList(params string[] values)
        {
            var items = new IFilterTree[values.Length];

            for (var i = 0; i < values.Length; ++i)
            {
                items[i] = MakeLegacyValue(values[i]);
            }

            return(new FilterTree(new Token(TokenType.LegacyValueList), items));
        }
        private string FormatComparisonExpression(IFilterTree tree)
        {
            var attributeText = tree.GetChild(0).Token.Value;
            var operatorText  = TokenMappings.TokenTypeToLegacyValueMap[tree.Token.Type];

            var contentText = TokenMappings.IsListOperation(tree.Token)
                                ? FormatValueList(tree.GetChild(1))
                                : FormatValue(tree.GetChild(1));

            return($"{attributeText} {operatorText} [{contentText}]");
        }
        private static string FormatValue(IFilterTree tree)
        {
            if (tree.Token.Type != TokenType.LegacyValue)
            {
                throw new UnsupportedTokenException(tree.Token);
            }

            if (tree.Token.Value.IndexOf(']') != -1)
            {
                throw new UnsupportedCharacterException(']', tree.Token);
            }

            return(tree.Token.Value);
        }
        private string FormatExpression(IFilterTree tree)
        {
            if (TokenMappings.IsLogicalOperation(tree.Token))
            {
                return(FormatLogicalExpression(tree));
            }

            if (TokenMappings.IsComparisonOperation(tree.Token))
            {
                return(FormatComparisonExpression(tree));
            }

            throw new FormaterException($"Encountered unexpected node of type {tree.Token.Type}.");
        }
示例#11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FilterTree"/> class.
        /// </summary>
        /// <exception cref="ArgumentNullException"><paramref name="token"/> or <paramref name="child"/> is <see langword="null" />.</exception>
        public FilterTree([NotNull] Token token, [NotNull] IFilterTree child)
        {
            if (child == null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            _Children = new ReadOnlyCollection <IFilterTree>(new List <IFilterTree> {
                child
            });
            Token = token ?? throw new ArgumentNullException(nameof(token));

            PositionSetup();
        }
示例#12
0
        private static string FormatValue(IFilterTree tree)
        {
            var text = tree.Token.Value;

            switch (tree.Token.Type)
            {
            case TokenType.String:
                return(EscapeText(text));

            case TokenType.Integer:
            case TokenType.Real:
                return(text);

            default:
                throw new UnsupportedTokenException(tree.Token);
            }
        }
        private static string FormatValueList(IFilterTree tree)
        {
            var elementTexts = new List <string>();

            foreach (var treeItem in tree.GetChildren())
            {
                var text = FormatValue(treeItem);
                if (NeedsEscaping(text))
                {
                    text = EscapeText(text);
                }

                elementTexts.Add(text);
            }

            return(string.Join(", ", elementTexts));
        }
示例#14
0
        void TestFilterTreeWithLinkedIds(IFilterTree tree)
        {
            IFilter item0Filter = tree.BuildFilter();

            Assert.AreEqual(item0Filter.ToString(),
                            "MediaItem.Title EQ Item0 And (LINKED_ID = '11111111-aaaa-aaaa-aaaa-111111111111' AND ROLE = '00000000-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '22222222-aaaa-aaaa-aaaa-111111111111')");

            IFilter item1Filter = tree.BuildFilter(new FilterTreePath(item1Role));

            Assert.AreEqual(item1Filter.ToString(),
                            "MEDIA_ITEM_ID = '11111111-aaaa-aaaa-aaaa-111111111111'");

            IFilter item2Filter = tree.BuildFilter(new FilterTreePath(item1Role, item2Role));

            Assert.AreEqual(item2Filter.ToString(),
                            "MediaItem.Title EQ Item2 And (LINKED_ID = '11111111-aaaa-aaaa-aaaa-111111111111' AND ROLE = '44444444-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '22222222-aaaa-aaaa-aaaa-111111111111')");
        }
示例#15
0
        void TestFilterTreeNoLinkedIds(IFilterTree tree)
        {
            IFilter item0Filter = tree.BuildFilter();

            Assert.AreEqual(item0Filter.ToString(),
                            "MediaItem.Title EQ Item0 And (ROLE = '00000000-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item1 And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '44444444-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item2)) And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '66666666-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item3))))");

            IFilter item1Filter = tree.BuildFilter(new FilterTreePath(item1Role));

            Assert.AreEqual(item1Filter.ToString(),
                            "MediaItem.Title EQ Item1 And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '44444444-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item2)) And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '66666666-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item3)) And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '00000000-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item0))");

            IFilter item2Filter = tree.BuildFilter(new FilterTreePath(item1Role, item2Role));

            Assert.AreEqual(item2Filter.ToString(),
                            "MediaItem.Title EQ Item2 And (ROLE = '44444444-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item1 And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '66666666-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item3)) And (ROLE = '22222222-aaaa-aaaa-aaaa-111111111111' AND LINKED_ROLE = '00000000-aaaa-aaaa-aaaa-111111111111' AND LINKED_ID IN (MediaItem.Title EQ Item0))))");
        }
示例#16
0
        private static string FormatValueList(IFilterTree tree)
        {
            if (tree.Token.Type != TokenType.ValueList)
            {
                throw new UnsupportedTokenException(tree.Token);
            }

            var elementTexts = new List <string>();

            foreach (var treeItem in tree.GetChildren())
            {
                var text = FormatValue(treeItem);
                elementTexts.Add(text);
            }

            var innerList = string.Join(", ", elementTexts);

            return($"({innerList})");
        }
示例#17
0
        public MediaLibraryQueryViewSpecification CreateSubViewSpecification(string viewDisplayName, FilterTreePath filterPath, IFilter filter, Guid?linkedId)
        {
            IFilterTree newFilterTree = _filterTree.DeepCopy();

            if (linkedId.HasValue)
            {
                newFilterTree.AddLinkedId(linkedId.Value, filterPath);
            }
            else if (filter != null)
            {
                newFilterTree.AddFilter(filter, filterPath);
            }

            return(new MediaLibraryQueryViewSpecification(viewDisplayName, newFilterTree, _necessaryMIATypeIds, _optionalMIATypeIds, _onlyOnline)
            {
                MaxNumItems = _maxNumItems,
                FilterPath = filterPath
            });
        }
        private string FormatLogicalExpression(IFilterTree tree)
        {
            if (tree.Token.Type != TokenType.And)
            {
                throw new UnsupportedTokenException(tree.Token);
            }

            var operatorText = TokenMappings.TokenTypeToLegacyValueMap[tree.Token.Type];

            var operandTexts = new List <string>();

            foreach (var childItem in tree.GetChildren())
            {
                operandTexts.Add(FormatExpression(childItem));
            }

            var resultExpression = string.Join($" {operatorText} ", operandTexts);

            return(resultExpression);
        }
示例#19
0
        /// <exception cref="ArgumentNullException"><paramref name="attrName"/> is <see langword="null" />.</exception>
        internal static IFilterTree MakeComparison(TokenType operationToken, [NotNull] string attrName, IFilterTree valueTree)
        {
            if (attrName == null)
            {
                throw new ArgumentNullException(nameof(attrName));
            }

            var attrItem = FilterTree.MakeAttr(attrName);

            return(new FilterTree(new Token(operationToken), new[] { attrItem, valueTree }));
        }
示例#20
0
        public virtual void InitMediaNavigation(out string mediaNavigationMode, out NavigationData navigationData)
        {
            Prepare();

            string             nextScreenName;
            AbstractScreenData nextScreen = null;

            // Try to load the prefered next screen from settings.
            if (NavigationData.LoadScreenHierarchy(_viewName, out nextScreenName))
            {
                // Support for browsing mode.
                if (nextScreenName == Consts.USE_BROWSE_MODE)
                {
                    SetBrowseMode();
                }

                if (_availableScreens != null)
                {
                    nextScreen = _availableScreens.FirstOrDefault(s => s.GetType().ToString() == nextScreenName);
                }
            }

            IEnumerable <Guid> optionalMIATypeIDs = MediaNavigationModel.GetMediaSkinOptionalMIATypes(MediaNavigationMode);

            if (_optionalMias != null)
            {
                optionalMIATypeIDs = optionalMIATypeIDs.Union(_optionalMias);
                optionalMIATypeIDs = optionalMIATypeIDs.Except(_necessaryMias);
            }

            IFilterTree filterTree = _rootRole.HasValue ? new RelationshipFilterTree(_rootRole.Value) : (IFilterTree) new SimpleFilterTree();

            filterTree.AddFilter(_filter);

            // Prefer custom view specification.
            ViewSpecification rootViewSpecification = _customRootViewSpecification ??
                                                      new MediaLibraryQueryViewSpecification(_viewName, filterTree, _necessaryMias, optionalMIATypeIDs, true)
            {
                MaxNumItems = Consts.MAX_NUM_ITEMS_VISIBLE
            };

            if (nextScreen == null)
            {
                nextScreen = _defaultScreen;
            }

            ScreenConfig nextScreenConfig;

            NavigationData.LoadLayoutSettings(nextScreen.GetType().ToString(), out nextScreenConfig);

            Sorting.Sorting nextSortingMode  = _availableSortings.FirstOrDefault(sorting => sorting.GetType().ToString() == nextScreenConfig.Sorting) ?? _defaultSorting;
            Sorting.Sorting nextGroupingMode = _availableGroupings == null || String.IsNullOrEmpty(nextScreenConfig.Grouping) ? null : _availableGroupings.FirstOrDefault(grouping => grouping.GetType().ToString() == nextScreenConfig.Grouping) ?? _defaultGrouping;

            navigationData = new NavigationData(null, _viewName, MediaNavigationRootState,
                                                MediaNavigationRootState, rootViewSpecification, nextScreen, _availableScreens, nextSortingMode, nextGroupingMode)
            {
                AvailableSortings  = _availableSortings,
                AvailableGroupings = _availableGroupings,
                LayoutType         = nextScreenConfig.LayoutType,
                LayoutSize         = nextScreenConfig.LayoutSize
            };
            mediaNavigationMode = MediaNavigationMode;
        }
示例#21
0
 public static IFilterTree MakeNot(IFilterTree child)
 {
     return(new FilterTree(new Token(TokenType.Not), new[] { child }));
 }
示例#22
0
        private static string FormatBooleanValue(IFilterTree tree)
        {
            var text = TokenMappings.TokenTypeToDefaultValueMap[tree.Token.Type];

            return(text);
        }
        public virtual void InitMediaNavigation(MediaNavigationConfig config, out string mediaNavigationMode, out NavigationData navigationData)
        {
            PrepareAsync().Wait();

            IFilterTree filterTree = _customFilterTree ?? (_rootRole.HasValue ? new RelationshipFilterTree(_rootRole.Value) : (IFilterTree) new SimpleFilterTree());

            if (_filter != null)
            {
                filterTree.AddFilter(_filter);
            }

            //Default configuration
            string viewName = _viewName;

            AbstractScreenData nextScreen = null;

            //Apply any custom configuration
            if (config != null)
            {
                if (_availableScreens != null)
                {
                    //Use the configured root screen to load the next screen from the hierarchy
                    //and remove it from the list of available screens
                    AbstractScreenData configRoot = config.RootScreenType != null?_availableScreens.FirstOrDefault(s => s.GetType() == config.RootScreenType) : null;

                    if (configRoot != null)
                    {
                        viewName = configRoot.GetType().ToString();
                        _availableScreens.Remove(configRoot);
                    }

                    //Use the configured default screen if there is no saved screen hierarchy
                    AbstractScreenData configDefault = config.DefaultScreenType != null?_availableScreens.FirstOrDefault(s => s.GetType() == config.DefaultScreenType) : null;

                    if (configDefault != null)
                    {
                        _defaultScreen = configDefault;
                        // If we want to force the default screen to be shown, set the next screen
                        // here to avoid loading it from the screen hierarchy below.
                        if (config.AlwaysUseDefaultScreen)
                        {
                            nextScreen = configDefault;
                        }
                    }
                }

                //Apply any additional filters
                if (config.LinkedId.HasValue)
                {
                    filterTree.AddLinkedId(config.LinkedId.Value, config.FilterPath);
                }
                if (config.Filter != null)
                {
                    filterTree.AddFilter(config.Filter, config.FilterPath);
                }
            }

            IEnumerable <Guid> optionalMIATypeIDs = MediaNavigationModel.GetMediaSkinOptionalMIATypes(MediaNavigationMode);

            if (_optionalMias != null)
            {
                optionalMIATypeIDs = optionalMIATypeIDs.Union(_optionalMias).Except(_necessaryMias);
            }

            // Try to load the prefered next screen from settings if not already set.
            if (nextScreen == null && NavigationData.LoadScreenHierarchy(viewName, out string nextScreenName))
            {
                // Support for browsing mode.
                if (nextScreenName == Consts.USE_BROWSE_MODE)
                {
                    SetBrowseMode(optionalMIATypeIDs);
                }

                if (_availableScreens != null)
                {
                    nextScreen = _availableScreens.FirstOrDefault(s => s.GetType().ToString() == nextScreenName);
                }
            }

            if (_applyUserFilter)
            {
                var userFilter = UserHelper.GetUserRestrictionFilter(_necessaryMias.ToList());
                if (userFilter != null)
                {
                    filterTree.AddFilter(userFilter);
                }
            }

            // Prefer custom view specification.
            ViewSpecification rootViewSpecification = _customRootViewSpecification ??
                                                      // Always use the default view name for the root view specification, not any custom name that may
                                                      // have been specified in a navigation config, otherwise toggling browse mode won't work correctly.
                                                      // To switch to browse mode we update the root screen hierarchy to point to the browse screen and
                                                      // then navigate to the root wf state. The name of the root screen hierarchy is determined by the
                                                      // name specified here so it should always point to the actual root view name.
                                                      new MediaLibraryQueryViewSpecification(_viewName, filterTree, _necessaryMias, optionalMIATypeIDs, true)
            {
                MaxNumItems = Consts.MAX_NUM_ITEMS_VISIBLE,
            };

            if (nextScreen == null)
            {
                nextScreen = _defaultScreen;
            }

            ScreenConfig nextScreenConfig;

            NavigationData.LoadLayoutSettings(nextScreen.GetType().ToString(), out nextScreenConfig);

            Sorting.Sorting nextSortingMode  = _availableSortings.FirstOrDefault(sorting => sorting.GetType().ToString() == nextScreenConfig.Sorting) ?? _defaultSorting;
            Sorting.Sorting nextGroupingMode = _availableGroupings == null || String.IsNullOrEmpty(nextScreenConfig.Grouping) ? null : _availableGroupings.FirstOrDefault(grouping => grouping.GetType().ToString() == nextScreenConfig.Grouping) ?? _defaultGrouping;

            navigationData = new NavigationData(null, viewName, MediaNavigationRootState,
                                                MediaNavigationRootState, rootViewSpecification, nextScreen, _availableScreens, nextSortingMode, nextGroupingMode)
            {
                AvailableSortings  = _availableSortings,
                AvailableGroupings = _availableGroupings,
                LayoutType         = nextScreenConfig.LayoutType,
                LayoutSize         = nextScreenConfig.LayoutSize
            };
            mediaNavigationMode = MediaNavigationMode;
        }