cancel
Showing results for 
Search instead for 
Did you mean: 

Reg: Using JAVA API

Former Member
0 Kudos

Hi,

I am using java api to interact with mdm through webdynpro.

I am using MDM SP06 patch 02..

How can i implement search. I want to search in a table in MDM using multiple fields.

Regards,

Jyothi

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Jyothi,

Check the below code to search the records by text i.e. freeform search. You will get the basic idea for how to implement the search through Java API.

public class TestSearchByText

{

public static ConnectionAccessor simpleConnection = null;

public static String connection = "TEST_MDMSERVER";

public static TestAuthenticateUserSession testAuthenticateUserSession;

public static RepositorySchema repositorySchema;

public static String session;

public String repository = "TEST";

public String str = "Customers";

public String tabId;

public int totalRecord;

DBMSType dbmsType = DBMSType.MS_SQL;

RepositoryIdentifier repIdentifier;

//Main Function.

public static void main(String args[])

{

simpleConnection = SimpleConnectionFactory.getInstance(connection);

repIdentifier = new RepositoryIdentifier(repository, connection, dbmsType);

RegionProperties dataRegion = getRegion();

CreateUserSessionCommand createUserSessionCommand = new CreateUserSessionCommand(simpleConnection);

createUserSessionCommand.setRepositoryIdentifier(repIdentifier);

createUserSessionCommand.setDataRegion(dataRegion);

createUserSessionCommand.execute();

session = createUserSessionCommand.getUserSession();

AuthenticateUserSessionCommand authenticateUserSessionCmd = new AuthenticateUserSessionCommand(simpleConnection);

authenticateUserSessionCmd.setSession(session);

authenticateUserSessionCmd.setUserName("Admin");

authenticateUserSessionCmd.setUserPassword("");

authenticateUserSessionCmd.execute();

GetRepositorySchemaCommand getRepositorySchemaCommand = new GetRepositorySchemaCommand(simpleConnection);

getRepositorySchemaCommand.setSession(session);

getRepositorySchemaCommand.execute();

repositorySchema = getRepositorySchemaCommand.getRepositorySchema();

TableId tableId= repositorySchema.getTable("Customers").getId();

FieldId[] fields = new FieldId[2];

fields[0] = repositorySchema.getFieldId("Customers", "CustID");

fields[1] = repositorySchema.getFieldId("Customers", "CustName");

FieldSearchDimension fieldSearchDimension = new FieldSearchDimension(fields[1]);

TextSearchConstraint textSearchConstraint = new TextSearchConstraint("Mr", TextSearchConstraint.EQUALS);

Search search = new Search(tableId);

search.addSearchItem(fieldSearchDimension, textSearchConstraint);

ResultDefinition rd = new ResultDefinition(tableId);

rd.setSelectFields(fields);

RetrieveLimitedRecordsCommand retrieveLimitedRecordsCommand = new RetrieveLimitedRecordsCommand(simpleConnection);

retrieveLimitedRecordsCommand.setSession(authenticateUserSessionCmd.getSession());

retrieveLimitedRecordsCommand.setResultDefinition(rd);

retrieveLimitedRecordsCommand.setSearch(search);

try

{

retrieveLimitedRecordsCommand.execute();

totalRecord = retrieveLimitedRecordsCommand.getRecords().getCount();

System.out.println("Number of records found: " + totalRecord);

RecordResultSet records=retrieveLimitedRecordsCommand.getRecords());

for(int i=0; i<records.getCount(); i++)

{

Record record = records.getRecord(i);

System.out.println(record.getDisplayValue());

}

}

catch (CommandException e)

{

e.printStackTrace();

}

}

}

public RegionProperties getRegion() throws CommandException

{

RegionProperties regionProperties = new RegionProperties();

regionProperties.setLocale(Locale.ENGLISH);

regionProperties.setName("English [US]");

regionProperties.setRegionCode("engUSA");

System.out.println(" Region is " + regionProperties);

return regionProperties;

}

Regards,

Jitesh Talreja

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi ,

I had imported the respective imports for the below code.

TableId tableId= repositorySchema.getTable("Customers").getId(); and it is not showing any error.

Search search = new Search(TableId); for this line of code its giving error

and below are the imports i am using. Kindly suggest me if i am missing any imports.

import com.sap.mdm.commands.AuthenticateServerSessionCommand;

import com.sap.mdm.commands.AuthenticateUserSessionCommand;

import com.sap.mdm.commands.CommandException;

import com.sap.mdm.commands.CreateServerSessionCommand;

import com.sap.mdm.commands.CreateUserSessionCommand;

import com.sap.mdm.data.ResultDefinition;

import com.sap.mdm.data.commands.RetrieveLimitedRecordsCommand;

import com.sap.mdm.ids.FieldId;

import com.sap.mdm.ids.TableId;

import com.sap.mdm.net.ConnectionAccessor;

import com.sap.mdm.net.ConnectionException;

import com.sap.mdm.net.ConnectionPool;

import com.sap.mdm.net.ConnectionPoolFactory;

import com.sap.mdm.net.SimpleConnectionFactory;

import com.sap.mdm.schema.RepositorySchema;

import com.sap.mdm.schema.commands.GetRepositorySchemaCommand;

import com.sap.mdm.search.FieldSearchDimension;

import com.sap.mdm.search.TextSearchConstraint;

import com.sap.mdm.server.DBMSType;

import com.sap.mdm.server.RepositoryIdentifier;

Thanks.

Former Member
0 Kudos

Hi Jyothi,

It would be better if you Mark the Thread as Answered, so that if any one looking for same sample code can get it easily.

Regards,

Jitesh Talreja

nitin_mahajan2
Contributor
0 Kudos

I understand you are already working with MDM, so will just tell you the solution to it.

e.g you are doing a flat search using text field. means, you have an text field on the screen, and you want to search for the entered text in all the MDM records and all the fields of each record. then use the following approach:

1. Use the entered value as searchtext.

2. Use limitedrecordscommand()

3. Also in the SEARCH object that you need to set into the result definition, set the FIELDIDs of all the fields of the table (any table on which you want to search), into the search object.

4. Set the table Id to the search,

5. set the searchtext into the search

6. Execute the command and get the result.

scenario 2: if you want to search in limited fields say e.g. field1, field2, field3, in the step 3 above, change to following

3.a: get the field ids of the field1,2 and 3.

3.b. Set the fieldsids to the search

The code required to do it is available in above replies or if required, i can give you the exact code. Let me know if you need any.

But i would suggest you to play around with the code using the above approach, will help you learn the APIs in a better way.

Regards,

Nitin

Former Member
0 Kudos

Hi Jyoti,

Kindly find the link to all the associated packages and methods you need to use in MDM java Api for the SPO6 version:

https://help.sap.com/javadocs/MDM/current/index.html

https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b02f4f88-7bbb-2a10-67ad-d435b9f0...

Hope It Helped

Thanks & Regards

Simona Pinto

Former Member
0 Kudos

Hi,

Can you please explain me the below part of code

FieldId[] fields = new FieldId[2];

fields[0] = repositorySchema.getFieldId("Customers", "CustID");

fields[1] = repositorySchema.getFieldId("Customers", "CustName");

FieldSearchDimension fieldSearchDimension = new FieldSearchDimension(fields[1]);

TextSearchConstraint textSearchConstraint = new TextSearchConstraint("Mr", TextSearchConstraint.EQUALS);

Search search = new Search(tableId);

search.addSearchItem(fieldSearchDimension, textSearchConstraint);

Former Member
0 Kudos

Hi,

In the code given above i am implementing the search on fields CusetID and CustName, so first we need to retrieve the fields on which Search is to be implemented.

FieldId[] fields = new FieldId[2];

fields[0] = repositorySchema.getFieldId("Customers", "CustID");

fields[1] = repositorySchema.getFieldId("Customers", "CustName");

After retrieving you need to specify on which fields you will going to implement the search. Specify the dimension type

FieldSearchDimension fieldSearchDimension = new FieldSearchDimension(fields[1]);

Actual search criteria is to be specified in TextSearchConstraint

TextSearchConstraint textSearchConstraint = new TextSearchConstraint("Mr", TextSearchConstraint.EQUALS);

Include the criteria and search dimensions to the Search class.

Search search = new Search(tableId);

search.addSearchItem(fieldSearchDimension, textSearchConstraint);

For more information, you can refer the MDM Java API

http://help.sap.com/saphelp_mdm550/helpdata/en/47/9f23e5cf9e3c5ce10000000a421937/frameset.htm

Regards,

Jitesh Talreja