cancel
Showing results for 
Search instead for 
Did you mean: 

Search using MDM java API

Former Member
0 Kudos

Hi,

I need to perform search with 2 fields in MDM table.

Can you please give me the sample code.

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Jyoti,

you need to build Search Object based on your inputs;

based on type of inputs ( integer,Text,boolean), you need to use appropriate searchConstraint;

http://help.sap.com/javadocs/MDM/SP06P2/com/sap/mdm/search/package-frame.html

any way, find some example code stuff here;

// Build Search Object as follows

Search search = new Search(productsTableId); // you can change the TableID which you need

// Create FiledIds for example

FieldId taxonomyFieldId= new FiledID(FiledCode);

FieldId manufacturerFieldId= new FiledID(FiledCode);

FieldSearchDimension glmDimension=new FieldSearchDimension(glmPartNumberFieldId);

glmSearchConstraint = new TextSearchConstraint(glmPartNumbers<i>,partSearchRequest.getGlmPartNumberSearchType());

SearchParameter mpnSearchParameter=new SearchParameter(glmDimension, glmSearchConstraint);

search.addSearchItem(mpnSearchParameter);

// get ResultSet

TableId productsTableId = mdmConnection.getTableId("Products");

//Compose array of the fields to retrieve

FieldId[] fields = new FieldId[2];

fields[0] = mdmConnection.getFieldId(MDMFields.PRODUCT_IMAGES.getTableCode(),MDMFields.PRODUCT_IMAGES.getFieldCode() );

fields[1] =

//Create the result definition for the search table

ResultDefinition rd = new ResultDefinition(productsTableId);

rd.setSelectFields(fields);

RetrieveLimitedRecordsCommand recordsCommand =

new RetrieveLimitedRecordsCommand(mdmConnection.getConnection());

recordsCommand.setSession(mdmConnection.getAuthenticatedUserSession().getSession());

recordsCommand.setResultDefinition(rd);

recordsCommand.setSearch(search);

recordsCommand.execute();

recordsCommand.getRecords(); // here u will get records..

follow the approach , i mentioned above;

Regards

Raj-/

Answers (12)

Answers (12)

Former Member
0 Kudos

Hi Jitesh,

I used below code and it is working for me.

repIdentifier = new RepositoryIdentifier(repository,"M10",dbmsType);

Thanks.

Former Member
0 Kudos

Hi Jitesh,

The code you have given for version is working fine...

But the search one is failing....can u please check the code.... in the code i have print statements in between. I am not getting the print statement that is after session execution. Till that it is working.

ConnectionAccessor simpleConnection=null;

String connection ="servername";

//AuthenticateServerSessionCommand testAuthenticateUserSession;

RepositorySchema repositorySchema;

String session;

String repository ="repname";

String str ="table name";

String tabId;

int totalRecord;

DBMSType dbmsType =DBMSType.ORACLE;

RepositoryIdentifier repIdentifier;

RegionProperties regionProperties;

// regionProperties.setLocale(Locale.ENGLISH);

// regionProperties.setName("English US");

// regionProperties.setRegionCode("engUSA");

// RegionProperties dataRegion=regionProperties;

//wdComponentAPI.getMessageManager().reportSuccess("test1"+Region());

try {

regionProperties = new RegionProperties();

regionProperties.setLocale(Locale.ENGLISH);

regionProperties.setName("English US");

regionProperties.setRegionCode("engUSA");

RegionProperties dataRegion=regionProperties;

simpleConnection = SimpleConnectionFactory.getInstance("servername");

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

wdComponentAPI.getMessageManager().reportSuccess("test2"+repIdentifier);

CreateUserSessionCommand createUserSessionCommand = new CreateUserSessionCommand(simpleConnection);

createUserSessionCommand.setRepositoryIdentifier(repIdentifier);

createUserSessionCommand.setDataRegion(dataRegion);

wdComponentAPI.getMessageManager().reportSuccess("test2");

createUserSessionCommand.execute();

wdComponentAPI.getMessageManager().reportSuccess("test3");

session = createUserSessionCommand.getUserSession();

AuthenticateUserSessionCommand authenticateUserSessionCmd = new AuthenticateUserSessionCommand(simpleConnection);

authenticateUserSessionCmd.setSession(session);

authenticateUserSessionCmd.setUserName("Admin");

authenticateUserSessionCmd.setUserPassword("");

wdComponentAPI.getMessageManager().reportSuccess(""+simpleConnection);

wdComponentAPI.getMessageManager().reportSuccess(""+session);

authenticateUserSessionCmd.execute();

GetRepositorySchemaCommand getRepositorySchemaCommand = new GetRepositorySchemaCommand(simpleConnection);

getRepositorySchemaCommand.setSession(session);

getRepositorySchemaCommand.execute();

repositorySchema = getRepositorySchemaCommand.getRepositorySchema();

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

//

//

//

//

//

//

FieldId[] fields = new FieldId[2];

fields[0] = repositorySchema.getFieldId("tablename", "Name");

fields[1] = repositorySchema.getFieldId("tablename", "Father's Name");

//

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

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

FieldSearchDimension fieldSearchDimension1 = new FieldSearchDimension(fields[0]);

TextSearchConstraint textSearchConstraint1 = new TextSearchConstraint("value2", TextSearchConstraint.EQUALS);

//

Search search = new Search(tableId);

search.addSearchItem(fieldSearchDimension, textSearchConstraint);

search.addSearchItem(fieldSearchDimension1, textSearchConstraint1);

//

ResultDefinition rd = new ResultDefinition(tableId);

rd.setSelectFields(fields);

//

//

RetrieveLimitedRecordsCommand retrieveLimitedRecordsCommand = new RetrieveLimitedRecordsCommand(simpleConnection);

retrieveLimitedRecordsCommand.setSession(authenticateUserSessionCmd.getSession());

retrieveLimitedRecordsCommand.setResultDefinition(rd);

retrieveLimitedRecordsCommand.setSearch(search);

//

retrieveLimitedRecordsCommand.execute();

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

wdComponentAPI.getMessageManager().reportSuccess("records are"+totalRecord);

RecordResultSet records=retrieveLimitedRecordsCommand.getRecords();

} catch (ConnectionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (CommandException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

is there any other parameter that is to be set for session. Kindly suggest.

Thanks,

Prasanthi.

Former Member
0 Kudos

If i print the simple connection, What should i get.

I am getting some value like this...com.sap.mdm.internal.net.SimpleConnectionImpl@1dffebb

Thanks.

Former Member
0 Kudos

Hi Jyothi,

Try the below code to verify the connection

public class Search2

{

public static ConnectionAccessor simpleConnection = null;

public static String connection = "TEST_MDMSERVER";

public static void main(String args[]) throws Exception

{

simpleConnection = SimpleConnectionFactory.getInstance(connection);

GetServerVersionCommand gs=new GetServerVersionCommand(simpleConnection);

gs.execute();

System.out.print(gs.getVersion());

}

}

If the above code does not work then cheeck the MDM Server name properly else you have used the incorrect jars.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

I am getting the session value as null. What could be the problem for this. Kindly suggest.

Former Member
0 Kudos

Hi Jyothi,

Check the parameter values

Servername, repository identifier, Main table name etc. I think your connection is not getting created.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

I am getting null pointer exception for the below piece of code

authenticateUserSessionCmd.execute();

Kindly suggest.

Former Member
0 Kudos

Hi Jyothi,

Hope you have set the password for Admin user, if not then try setting it since my code is working fine

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

When i use your code it is giving me below error. I had verified the field names and values i am searching for.

java.lang.NullPointerException

at com.sap.mdm.internal.SessionConvertHelper.convertToLong(SessionConvertHelper.java:11)

at com.sap.mdm.commands.AuthenticateUserSessionCommand.execute(AuthenticateUserSessionCommand.java:62)

at com.sap.search.SearchView.onActionsubmit(SearchView.java:304)

at com.sap.search.wdp.InternalSearchView.wdInvokeEventHandler(InternalSearchView.java:160)

at com.sap.tc.webdynpro.progmodel.generation.DelegatingView.invokeEventHandler(DelegatingView.java:87)

at com.sap.tc.webdynpro.progmodel.controller.Action.fire(Action.java:67)

at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.doHandleActionEvent(WindowPhaseModel.java:420)

at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.processRequest(WindowPhaseModel.java:132)

at com.sap.tc.webdynpro.clientserver.window.WebDynproWindow.processRequest(WebDynproWindow.java:335)

at com.sap.tc.webdynpro.clientserver.cal.AbstractClient.executeTasks(AbstractClient.java:143)

at com.sap.tc.webdynpro.clientserver.session.ApplicationSession.doProcessing(ApplicationSession.java:319)

at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessingStandalone(ClientSession.java:713)

at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessing(ClientSession.java:666)

at com.sap.tc.webdynpro.clientserver.session.ClientSession.doProcessing(ClientSession.java:250)

at com.sap.tc.webdynpro.clientserver.session.RequestManager.doProcessing(RequestManager.java:149)

at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doContent(DispatcherServlet.java:62)

at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doPost(DispatcherServlet.java:53)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.runServlet(HttpHandlerImpl.java:401)

at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.handleRequest(HttpHandlerImpl.java:266)

at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:386)

at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:364)

at com.sap.engine.services.httpserver.server.RequestAnalizer.invokeWebContainer(RequestAnalizer.java:1039)

at com.sap.engine.services.httpserver.server.RequestAnalizer.handle(RequestAnalizer.java:265)

at com.sap.engine.services.httpserver.server.Client.handle(Client.java:95)

at com.sap.engine.services.httpserver.server.Processor.request(Processor.java:175)

at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:33)

at com.sap.engine.core.cluster.impl6.session.MessageRunner.run(MessageRunner.java:41)

at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)

at java.security.AccessController.doPrivileged(Native Method)

at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:102)

at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:172)

Please suggest

Former Member
0 Kudos

Yes, I have imported that already

Former Member
0 Kudos

Hi Jitesh,

Even now it is giving error The Constructor Search(TableId) is not defined. Please do not mind if the question is silly.

Thanks

Former Member
0 Kudos

Hi jyothi,

Please check the import statement of Search class. are you importing this package or not.

com.sap.mdm.search.Search

Thanks.

Former Member
0 Kudos

Hi Jyothi,

Verify your code with the code below

import java.util.Locale;

import com.sap.mdm.commands.AuthenticateUserSessionCommand;

import com.sap.mdm.commands.CommandException;

import com.sap.mdm.commands.CreateUserSessionCommand;

import com.sap.mdm.commands.DestroySessionCommand;

import com.sap.mdm.data.Record;

import com.sap.mdm.data.RecordResultSet;

import com.sap.mdm.data.RegionProperties;

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.SimpleConnectionFactory;

import com.sap.mdm.schema.FieldProperties;

import com.sap.mdm.schema.RepositorySchema;

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

import com.sap.mdm.search.FieldSearchDimension;

import com.sap.mdm.search.Search;

import com.sap.mdm.search.TextSearchConstraint;

import com.sap.mdm.server.DBMSType;

import com.sap.mdm.server.RepositoryIdentifier;

import com.sap.mdm.valuetypes.MdmValue;

public class Search2

{

public static ConnectionAccessor simpleConnection = null;

public static String connection = "TEST_MDMSERVER";

//public static TestAuthenticateUserSession testAuthenticateUserSession;

public static RepositorySchema repositorySchema;

public static String session;

public static String repository = "TEST_REPO";

public static String str = "Title";

public static String tabId;

public static int totalRecord;

public static DBMSType dbmsType = DBMSType.MS_SQL;

public static RepositoryIdentifier repIdentifier;

//Main Function.

public static void main(String args[]) throws Exception

{

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("Pass1234");

authenticateUserSessionCmd.execute();

GetRepositorySchemaCommand getRepositorySchemaCommand = new GetRepositorySchemaCommand(simpleConnection);

getRepositorySchemaCommand.setSession(session);

getRepositorySchemaCommand.execute();

repositorySchema = getRepositorySchemaCommand.getRepositorySchema();

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

FieldId[] fields = new FieldId[2];

fields[0] = repositorySchema.getFieldId("Title", "Material_Number");

fields[1] = repositorySchema.getFieldId("Title", "Description");

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

TextSearchConstraint textSearchConstraint = new TextSearchConstraint("test", 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 static 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

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,

From your statement I have noticed that you are passing TableId instead you need to pass tableId i.e. the variable of type TableId. Check the case difference.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

I used the same code posted by you. But i am getting the error "Table id can not be resolved" for the below line.

Search search = new Search(TableId);

Please suggest.

Regards,

Jyothi

Former Member
0 Kudos

Hi Jyothi,

Check the below line

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

If you are getting the error on TableId then right click on it and select Organize imports this will add the required import statement. Also the value Customers is the main table name, you should pass your main table name instead of customers.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

I think you are looking for same info you asked in the thread

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

Check the below thread

Regards,

Jitesh Talreja