Element Search

With the collection of dictionary elements it is necessary to get only required elements, for example, to organize them into dictionary selection. Consider various options of element search in the dictionary.

Search by Values of the ID Attribute

The FindById method is used to search by values of the Identifier attribute in the collection.

Var
    MB: IMetabase;
    DimInst: IDimInstance;
    Elements: IDimElements;
    Sel: IDimSelection;
    i: Integer;
Begin
    MB := MetabaseClass.Active;
    DimInst := MB.ItemById("Dim_1").Open(NullAs IDimInstance;
    Elements := DimInst.Elements;
    Sel := DimInst.CreateSelection;
    i := Elements.FindById("10");
    If i <> -1 Then
        Sel.SelectElement(i, True);
    End If;

This example shows search of element with value of the ID attribute -10. If the element is found, it is added to the selection with all its child elements.

To search elements by values of other attributes, one of the methods below should be used.

Search by Attribute Values

The simplest search method is search by attribute value. The LookupValue method searches by attribute value. This method searches for the first element with the specified attribute value.

Var
    MB: IMetabase;
    DimInst: IDimInstance;
    Elements: IDimElements;
    Attr: IDimAttributeInstance;
    Elem: Integer;
Begin
    MB := MetabaseClass.Active;
    DimInst := MB.ItemById("Dim_1").Open(NullAs IDimInstance;
    Elements := DimInst.Elements;
    Attr := DimInst.Attributes.FindById("NAME");
    Elem := Attr.LookupValue("Russian Federation");
    If Elem <> -1 Then
        Debug.WriteLine("ID of element: " + Elements.Id(Elem));
    End If;

Disadvantage of this method is in its ability to find only one element. It does not search cyclically. It can be used if only one element with required attribute value exists. If there are several elements, use search types below.

When searching in MDM dictionaries certain situations require search by actual attribute values, but not by shown ones (search by values of connection attributes, displayed value of which depends upon  selected format). In this case the LookupDisplayValue method should be used.

Search by Indexed Values of Attributes

To search for element collections united by a certain property (for example, common parent element), methods of the IDimIndexInstance and IDimIndexInstance and Lookup interfaces are used. To make this search available, create an index in the dictionary, and include all attributes where the search is to be performed, in this index.

Var
    MB: IMetabase;
    Dimen: IDimInstance;
    IndxsInst: IDimIndexesInstance;
    IndexInst: IDimIndexInstance;
    InstLookUp: IDimIndexInstanceLookup;
    LookUpElem: IDimElementArray;
    Elements: IDimElements;
    i: Integer;
Begin
    MB := MetabaseClass.Active;
    Dimen := MB.ItemById("Dim_1").Open(NullAs IDimInstance;
    IndxsInst := Dimen.Indexes;
    //Index, by attributes of which search  is executed
    IndexInst := IndxsInst.FindById("PARENTID_IDX");
    InstLookUp := IndexInst.CreateDimIndexLookup;
    //Required value
    InstLookUp.AttributeValue(0) := "0";
    //Search of elements
    LookUpElem := InstLookUp.LookUp;
    Elements := LookUpElem.Elements;
    Debug.WriteLine("Found " + LookUpElem.Count.ToString + " element(s)");
    For Each i In LookUpElem Do
        //Names of found elements
        Debug.WriteLine(Elements.Name(i));
    End For;

This example assumes that the dictionary should contain index with the PARENTID_IDX identifier. This index is neither unique nor primary. It includes one attribute, that is, parental element ID. After executing the example the method searches for the elements with common parent element with the 0 identifier. Names of the found elements are displayed in the development environment console.

All above described methods have one common disadvantage: elements are found only in case of full match of attribute value and required value. If partial values are required, the search settings described below should be used.

Search with Search Conditions Settings

The IDimOrmManager interface methods are used for more flexible search of elements with opportunity of setting up search conditions. Properties and methods of the IDimOrmClass, IDimOrmCondition, IDimTextCriteria and IDimConditionSelect interfaces are also used for setting up search conditions.

Var
    MB: IMetabase;
    Dimen: IDimInstance;
    OrmMan: IDimOrmManager;
    Criteria: IDimTextCriteria;
    SelSetFactory: IDimSelectionSetFactory;
    SelSet: IDimSelectionSet;
    Sel: IDimSelection;
Begin
    MB := MetabaseClass.Active;
    Dimen := MB.ItemById("Dim_1").Open(NullAs IDimInstance;
    OrmMan := New DimOrmManager.Create;
    //Create dictionary selection that can be changed on searching
    SelSetFactory := New DimSelectionSetFactory.Create;
    SelSet := SelSetFactory.CreateDimSelectionSet;
    Sel := SelSet.Add(Dimen);
    //Search criterion
    Criteria := New DimTextCriteria.Create;
    //Search by the Name attribute
    Criteria.CriteriaOptions := TextCriteriaOptions.SearchName;
    //Required text
    Criteria.Text := "Republic";
    //Search and selection of found elements
    OrmMan.SelectElements(Criteria, SelSet);

This example shows the search by the Name attribute. The search text is Republic. Elements containing this word in their names are included into SelSet output selection. This selection can be later used for elements in visual components containing dictionary tree.

More detailed information on possible search conditions can be found in properties and methods of interfaces described above.

See also:

Working with Dictionary Data