Skip to Content

Performing Free Form

Searches via MDM Java API

SAP NetWeaver MDM comes along with a
powerful Java API which
covers
almost any functionality offered through the client
applications. Using a few examples, this
blog explains how to perform free form searches
via the Java API.

The examples are based on a very
simple MDM Repository. The structure and contents are available at the end
of
the Blog.

The Java source is also available (exclusively for SDN users 😉  ) as a SAP NetWeaver Developer Studio project here.

</p>

Free Form Search in MDM Data Manager

The examples simulate a free form

search on the Products table. So let’s briefly have

a look on how this

is done using the MDM Data Manager.</p>

Besides the direct selection of values for searching, you can use the

Expression field in the Free-Form Search

pane.

!https://weblogs.sdn.sap.com/weblogs/images/7948/mdm-dm_1.gif|title=Free Form Search in MDM Data Manager|style=width: 309px; height: 182px;|alt=Free Form Search in MDM Data Manager|src=https://weblogs.sdn.sap.com/weblogs/images/7948/mdm-dm_1.gif!

A doubleclick on the highlighted area will bring up the +Search
Expression+ dialog. Here you can either directly type in the
search
expression or use the shortcut buttons to create an expression.

!https://weblogs.sdn.sap.com/weblogs/images/7948/mdm-dm_2.gif|title=Search Expression|style=width: 296px; height: 136px;|alt=Search Expression|src=https://weblogs.sdn.sap.com/weblogs/images/7948/mdm-dm_2.gif!

I included the equivalent search expression for each example
in the source code.

Getting Started: Using one single field

This first example will emulate the search expression

FIND(Name,"Contractor")

on table Products.

The MDM Java API exposes the free form search capabilities via

several types of free form search parameters. Each of them is

implemented as a separate class and derived from the super class

SearchParameter.

Search parameters are added to an instance of a Search object and they
will be taken into consideration for the search as soon as it is
executed.

So the first thing for us to do is to create a new Search

object.


Search search = new Search("Products");

This search object contains a collection of several instances of search
parameter classes. This collection is initially empty. We will now add
a new search parameter class to this collection.


FreeFormTableParameter fftpNames =
search.GetParameters().NewFreeFormTableParameter("Products");

As you see we are using the class FreeFormTableParameter since we want

to restrict our free form search based on fields of a given

table (Products). To create a new instance of this search parameter, we

use the method NewFreeFormTableParameter() of the

SearchParameters collection. This method requires the table name from

which we will later on use the fields and will also add this

new parameter to the collection.


FreeFormParameterField ffpfName = fftpNames.GetFields().New("Name");

This line of code adds a new FreeFormParameterField based on

the field Name of the Products table to the search parameter. This is

the field we are using in our expression above.


ffpfName.GetFreeForm().NewString("Contractor", FreeFormParameter.SubstringSearchType);

Here we use the FreeFormParameterField to add the restriction for the

value Contractor. We use NewString() because this is a string value and

we specify a substring search.

As with any other search


A2iResultSet result = catalog.GetResultSet(search, rsd, "Name", true, 0);

will execute the search and return the result.

The complete source code is available at the end of the blog.

Single Field, More Values

The next thing is to search for multiple values, but still using one

single field. Let’s take the expression

FIND(Name, "Contractor") AND FIND(Name, "Blade") AND FIND(Name,
"Standard")

on table Products.

The code is exactly the same as above until the instanciation of the

FreeFormParameterField:


FreeFormParameterField ffpfName = fftpNames.GetFields().New("Name",
FreeFormParameterField.SEARCH_OPERATOR_AND);

This line of code will combine the search with logical AND.

After that we can simply add each of the values, one after the other.

ffpfName.GetFreeForm().NewString("Contractor",
FreeFormParameter.SubstringSearchType);


ffpfName.GetFreeForm().NewString("Blade",
FreeFormParameter.SubstringSearchType);


ffpfName.GetFreeForm().NewString("Standard",
FreeFormParameter.SubstringSearchType);

Most probably you already guessed, that for the expression

FIND(Name, "Contractor") OR FIND(Name, "Blade") OR FIND(Name,
"Standard")

on table Products, we have to use the coding


FreeFormParameterField ffpfName = fftpNames.GetFields().New("Name",
FreeFormParameterField.SEARCH_OPERATOR_OR);

AND and OR together

The previous example covered expressions using either only OR

combinations or AND combinations of the values. Now we want to go for


(FIND(Name, "Contractor") OR FIND(Name, "Blade")) AND FIND(Name,
"Standard")

on table Products.

A litte bit more complicated but still straight forward:

Start with the first sub-expression (the OR combination in brackets):


FreeFormParameterField ffpfName1 = fftpNames.GetFields().New("Name",
FreeFormParameterField.SEARCH_OPERATOR_OR);


ffpfName1.GetFreeForm().NewString("Contractor", FreeFormParameter.SubstringSearchType);


ffpfName1.GetFreeForm().NewString("Blade", FreeFormParameter.SubstringSearchType);

After that add a new FreeFormParameterField.


FreeFormParameterField ffpfName2 = fftpNames.GetFields().New("Name");


ffpfName2.GetFreeForm().NewString("Standard", FreeFormParameter.SubstringSearchType);

Several FreeFormParameterField instances will be AND combined

automatically.

Two fields

The expression


FIND(Manufacturer, "Estwing") AND FIND(Name, "Polyurethane")

on table
Products uses two fields. But based on what you have already seen, the
implementation is easy.

One FreeFormParameterField for the first field:


FreeFormParameterField ffpfManu = fftpProducts.GetFields().New("Manufacturer");


ffpfManu.GetFreeForm().NewString("Estwing", FreeFormParameter.SubstringSearchType);

And one FreeFormParameterField for the second field:


FreeFormParameterField ffpfName = fftpProducts.GetFields().New("Name");


ffpfName.GetFreeForm().NewString("Polyurethane", FreeFormParameter.SubstringSearchType);

Again, the search parameters are AND combined by default.

OR Combination of Search Parameters

In order to override the default AND operator between fields, which is

necessary for the expression


FIND(Manufacturer, "Estwing") OR FIND(Name, "Polyurethane")

on table

Products, we need to change the default behaviour of the Search object we are

using.


search.SetSearchType(Search.GlobalOrSearchCombinationType);

This will have the effect, that all FreeFormParameterField will be OR

combined instead of AND.

The rest of the code is exactly the same as above.

Complete Java Source Code

import a2i.core.*;

import a2i.common.*;

import a2i.search.*;

/**

  • Examples for free form searches via the MDM Java API.

  • * <br>To run these examples, create the following

Classpath Variable in your Developer Studio to link to your local MDM

API JAR:

*Go to +Window -> Preferences -> Java
-> Classpath Variables+ and create this
variable:

*SAP_MDM_MDM4J – Path to the

folder containing MDM4J.jar

*

*Of course you need also an up-and-running MDM Server with a loaded
Repository.

*The connection details are directly maintained in the source
code.

*
*

  • *This code is copyrighted by SAP AG, Dietmar-Hopp-Allee 16, 69190

Walldorf, Germany.

*It has been written to serve educational purposes only. You may reuse,
modify and
*redistribute it as long as the following rules are obeyed:

**
*SAP AG assumes no responsibility for errors or omissions in this
sample code.

*It is provided “as is” without a warranty of any kind, either express
or implied,
*including, but not limited to, the implied warranties of
merchantability, fitness for a particular purpose,
*or non-infringement.

*SAP AG shall not be liable for damages of any kind including without
limitation direct, special,
*indirect, or consequential damages that may result from the use of
this code.

*SAP AG does not warrant the accuracy or completeness of information,

text, graphics, links or

*other items contained within these materials. SAP AG reserves the

right to modify, replace or

*supplement this documentation and coding without notice.

**

*

  • @author Andreas Seifried (D035740), Copyright SAP AG, 200

*/

public class FreeFormSearchExamples {

CatalogData catalog;

public void login() {

catalog = new CatalogData();

catalog.Login(“localhost”, 2010, “Admin”, “”, “English “);

}

public void logout() {

try {

catalog.Logout();

} catch (StringException e) {

System.out.println(“Logout Failed!”);

e.printStackTrace();

}

}

/**

  • Search on one single field for one single value

*/

public void searchSingleFieldSimple() {

//FIND(Name, “Contractor”) on table Products

// case sensitive substring search in the field “Name”

System.out.println();

System.out.println(“Performing searchSingleFieldSimple()”);

System.out.println(“FIND(Name, “Contractor”) on table Products”);

System.out.println(“Expected Hits: 18”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

Search search = new Search(“Products”);

FreeFormTableParameter fftpNames =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfName = fftpNames.GetFields().New(“Name”);

ffpfName.GetFreeForm().NewString(“Contractor”,

FreeFormParameter.SubstringSearchType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n<result.GetRecordCount(); n++) {

// System.out.println(result.GetValueAt(n, “Name”).GetStringValue());

// }

} catch (StringException e) {

System.out.println(“Search Failed!”);

e.printStackTrace();

}

}

/**

  • Search on one single field for 3 values, AND combination

*/ public void searchSingleFieldAnd() {

//FIND(Name, “Contractor”) AND FIND(Name, “Blade”) AND FIND(Name,

“Standard”) on table Products

System.out.println();

System.out.println(“Performing searchSingleFieldAnd()”);

System.out.println(“FIND(Name, “Contractor”) AND FIND(Name,

“Blade”) AND FIND(Name, “Standard”) on table Products”);

System.out.println(“Expected Hits: 5”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

Search search = new Search(“Products”);

FreeFormTableParameter fftpNames =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfName = fftpNames.GetFields().New(“Name”,

FreeFormParameterField.SEARCH_OPERATOR_AND);

ffpfName.GetFreeForm().NewString(“Contractor”,

FreeFormParameter.SubstringSearchType);

ffpfName.GetFreeForm().NewString(“Blade”,

FreeFormParameter.SubstringSearchType);

ffpfName.GetFreeForm().NewString(“Standard”,

FreeFormParameter.SubstringSearchType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n<result.GetRecordCount(); n++) {

// System.out.println(result.GetValueAt(n, “Name”).GetStringValue());

// }

} catch (StringException e) {

System.out.println(“Search Failed!”);

e.printStackTrace();

}

}

/**

  • Search on one single field for 3 values, OR combination

*/ public void searchSingleFieldOr() {

//FIND(Name, “Contractor”) OR FIND(Name, “Blade”) OR FIND(Name,

“Standard”) on table Products

System.out.println();

System.out.println(“Performing searchSingleFieldOr()”);

System.out.println(“FIND(Name, “Contractor”) OR FIND(Name, “Blade”)

OR FIND(Name, “Standard”) on table Products”);

System.out.println(“Expected Hits: 48”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

Search search = new Search(“Products”);

FreeFormTableParameter fftpNames =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfName = fftpNames.GetFields().New(“Name”,

FreeFormParameterField.SEARCH_OPERATOR_OR);

ffpfName.GetFreeForm().NewString(“Contractor”,

FreeFormParameter.SubstringSearchType);

ffpfName.GetFreeForm().NewString(“Blade”,

FreeFormParameter.SubstringSearchType);

ffpfName.GetFreeForm().NewString(“Standard”,

FreeFormParameter.SubstringSearchType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n<result.GetRecordCount(); n++) {

// System.out.println(result.GetValueAt(n, “Name”).GetStringValue());

// }

} catch (StringException e) {

System.out.println(“Search Failed!”);

e.printStackTrace();

}

}

/**

  • Search on one single field for 3 values, nested OR / AND combination

*/ public void searchSingleFieldOrAnd() {

//(FIND(Name, “Contractor”) OR FIND(Name, “Blade”)) AND FIND(Name,

“Standard”) on table Products

System.out.println();

System.out.println(“Performing searchSingleFieldOrAnd()”);

System.out.println(“(FIND(Name, “Contractor”) OR FIND(Name,

“Blade”)) AND FIND(Name, “Standard”) on table Products”);

System.out.println(“Expected Hits: 11”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

Search search = new Search(“Products”);

FreeFormTableParameter fftpNames =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfName1 = fftpNames.GetFields().New(“Name”,

FreeFormParameterField.SEARCH_OPERATOR_OR);

ffpfName1.GetFreeForm().NewString(“Contractor”,

FreeFormParameter.SubstringSearchType);

ffpfName1.GetFreeForm().NewString(“Blade”,

FreeFormParameter.SubstringSearchType);

FreeFormParameterField ffpfName2 = fftpNames.GetFields().New(“Name”);

ffpfName2.GetFreeForm().NewString(“Standard”,

FreeFormParameter.SubstringSearchType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n<result.GetRecordCount(); n++) {

// System.out.println(result.GetValueAt(n, “Name”).GetStringValue());

// }

} catch (StringException e) {

System.out.println(“Search Failed!”);

e.printStackTrace();

}

}

/**

  • Search on two fields for single values, AND combination

*/ public void searchTwoFieldsAnd() {

//FIND(Manufacturer, “Estwing”) AND FIND(Name, “Polyurethane”) on table

Products

System.out.println();

System.out.println(“Performing searchTwoFieldsAnd()”);

System.out.println(“FIND(Manufacturer, “Estwing”) AND FIND(Name,

“Polyurethane”) on table Products”);

System.out.println(“Expected Hits: 3”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

rsd.AddField(“Manufacturer”);

Search search = new Search(“Products”);

FreeFormTableParameter fftpProducts =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfManu =

fftpProducts.GetFields().New(“Manufacturer”);

ffpfManu.GetFreeForm().NewString(“Estwing”,

FreeFormParameter.SubstringSearchType);

FreeFormParameterField ffpfName = fftpProducts.GetFields().New(“Name”);

ffpfName.GetFreeForm().NewString(“Polyurethane”,

FreeFormParameter.SubstringSearchType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n<result.GetRecordCount(); n++) {

// System.out.println(result.GetValueAt(n, “Name”).GetStringValue() +

”          ” + result.GetValueAt(n, “Manufacturer”).GetStringValue());

// }

} catch (StringException e) {

System.out.println(“Search Failed!”);

e.printStackTrace();

}

}

/**

  • Search on two fields for single values, OR combination

*/ public void searchTwoFieldsOr() {

//FIND(Manufacturer, “Estwing”) OR FIND(Name, “Polyurethane”) on table

Products

System.out.println();

System.out.println(“Performing searchTwoFieldsOr()”);

System.out.println(“FIND(Manufacturer, “Estwing”) OR FIND(Name,

“Polyurethane”) on table Products”);

System.out.println(“Expected Hits: 67”);

System.out.println(“=========================”);

ResultSetDefinition rsd = new ResultSetDefinition(“Products”);

rsd.AddField(“Name”);

rsd.AddField(“Manufacturer”);

Search search = new Search(“Products”);

search.SetSearchType(Search.GlobalOrSearchCombinationType);

FreeFormTableParameter fftpProducts =

search.GetParameters().NewFreeFormTableParameter(“Products”);

FreeFormParameterField ffpfManu =

fftpProducts.GetFields().New(“Manufacturer”);

ffpfManu.GetFreeForm().NewString(“Estwing”,

FreeFormParameter.SubstringSearchType);

FreeFormParameterField ffpfName = fftpProducts.GetFields().New(“Name”);

ffpfName.GetFreeForm().NewString(“Polyurethane”,

FreeFormParameter.SubstringSearchType);

search.SetSearchType(Search.GlobalOrSearchCombinationType);

A2iResultSet result;

try {

result = catalog.GetResultSet(search, rsd, “Name”, true, 0);

System.out.println(“Found ” + result.GetRecordCount() + ” records.”);

// for(int n=0; n

Example Repository

Structure

These examples are using a very

simple MDM Repository ApiSampleRepositry, which is based on a main

table called Products and a flat lookup table Manufacturers. Here

are the details:</p>

Tables of the ApiSampleRepository

Products table:

Fields of the Products table

The Manufacturer field references the
Manufacturers table.

Manufacturers table:

Fields of the Manufacturers table

Contents

<Manufactu

To report this post you need to login first.

11 Comments

You must be Logged on to comment or reply to a post.

    1. Andreas Seifried Post author
      Hi Hari,

      no problem. The API is available for download on the SAP Service Marketplace, quick link /swdc (same location from where you can download the other MDM components).

      Andreas

      (0) 
    1. Andreas Seifried Post author
      Hi Martin,
      the API in general allows only retrieval of display fields from related lookup tables. To get access to all the other fields, get the record id(s) in the related lookup table with A2iResultSet.GetLookupIDAt() and then retreive the referenced record(s) with CatalogData.GetRecordsById().
      Regards
      Andreas
      (0) 
  1. Tom Spishock
    Hi Andreas!
    We have MDM 5.5 (SP04) and I’ve downloaded Developer Studio 7.0 (09). I’m new to the Java/MDM environment. We do have the MDM SP04 Java APIs; where do they get downloaded to so that Developer Studio can use them for your exercises here?
    (0) 
      1. Tom Spishock
        Thanks, Andreas!!
        I followed your instructions and performed the exercise in your blog. Everything went fine!

        Only one further question… are there more examples of how to use the Java APIs which we can borrow from? Eespecially the Hierarchy access.. we want to access the Categories hierarchy starting at a specific node then output the children nodes down thru all leafs from the starting node to an XML file. The Java API manual is not clear enough or give enough examples on ‘how-to’.

        Thanks again!

        (0) 

Leave a Reply