cancel
Showing results for 
Search instead for 
Did you mean: 

getting records using mdm java api

Former Member
0 Kudos

Hi All,

i had created a programme to get the data form the mdm using java api's. i am getting the tables details , field details .

but i am not getting proper code for geting records . send any sample code to get the records from the table. please help me.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Please check out the below code

FieldProperties[] fields = getFieldListCommand.getFields();

FieldId[] fid=new FieldId[69];

Search s=new Search(mainTableId);

ResultDefinition rd=new ResultDefinition(mainTableId);

for(int i=0;i<fields.length;i++)

{

fid<i>=fields<i>.getId();

}

rd.setSelectFields(fid);

RetrieveLimitedRecordsCommand ret=new RetrieveLimitedRecordsCommand(simpleConnection);

ret.setSession(session);

ret.setResultDefinition(rd);

try

{

ret.execute();

}

catch(Exception e)

{

}

RecordResultSet rs=ret.getRecords();

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

{

Record r=rs.getRecord(i);

System.out.print(rs.getDisplayValue()); //this will display only the display fields of the repository

}

//you can also use the beolw method to display all the field values irrespective of whether the field is Display field or not

for(int j=0;j<fields.length;j++)

{

if(fields[j].getType()==7 || fields[j].getType()==42)

{

System.out.print(fields[j].getCode()" - "r.getLookupDisplayValue(fields[j].getId())+"# ");

}

else

{

System.out.print( fields[j].getCode()" - "r.getFieldValue(fields[j].getId())+"# ");

}

}

Hope it helps

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

i had tried the code which you had given .

"FieldId[] fid=new FieldId69;"

but i did not understand this line ."

FieldId[] fid=new FieldId[69];" . i had used in this way .

i did not getting any error's but when i run the programme it is going to catch block . plese exaplain

Former Member
0 Kudos

Hi,

Here i am creating the array of FieldId and 69 are the number of fields present in my repository. You have to pass the number of fields present in your repository.

FieldId[] fid=new FieldId;

just put the 100 in [] brackets

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

i ahd tried but then also it going to catch block

i am sending my code please veriy it .

ia m underling the catch block

String repositoryName = "employee_ratna";

String dbmsName = "10.10.30.20";

RepositoryIdentifier reposId = new RepositoryIdentifier(repositoryName, dbmsName, DBMSType.MS_SQL);

// create a repository session

CreateRepositorySessionCommand sessionCommand = new CreateRepositorySessionCommand(connections);

sessionCommand.setRepositoryIdentifier(reposId);

try {

sessionCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

return;

}

System.out.println("Currently connected to "+reposId);

String sessionId = sessionCommand.getRepositorySession();

// authenticate the repository session

String userName = "Admin";

String userPassword = "";

AuthenticateRepositorySessionCommand authCommand = new AuthenticateRepositorySessionCommand(connections);

authCommand.setSession(sessionId);

authCommand.setUserName(userName);

authCommand.setUserPassword(userPassword);

try {

authCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

return;

}

// retrieve the list of tables and pick the main table

GetTableListCommand tableListCommand = new GetTableListCommand(connections);

tableListCommand.setSession(sessionId);

try {

tableListCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

return;

}

TableProperties mainTable = null;

//TableProperties[] tables = tableListCommand.getTables();

TableProperties[] tables = tableListCommand.getTables();

TableProperties tabname;

FieldProperties[] fields;

for (int i = 0; i < tables.length; i++)

{

// to get the name of the tables

if (tables<i>.getType() == TableProperties.MAIN)

{

String TableType= tables<i>.getTableTypeName();

TableId tid = tables<i>.getId();

System.out.println(TableType":TableType"tid+":TableId");

String MainDis = tables<i>.getDescription();

TableId x;

x = tables<i>.getId();

// GetFieldListCommand filedPro = new

GetFieldListCommand getFieldListCommand = new GetFieldListCommand(connections);

getFieldListCommand.setSession(sessionId);

getFieldListCommand.setTableId(x);

try {

getFieldListCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

return;

}

fields = getFieldListCommand.getFields();

Search s=new Search(x);

ResultDefinition rd=new ResultDefinition(x);

System.out.println("kkkkkkkkkkkkkk");

System.out.println(fields.length);

//System.out.println(fields.length+"kkkkkkkkkkkkkk");

RetrieveLimitedRecordsCommand ret=new RetrieveLimitedRecordsCommand(connections);

ret.setSession(sessionId);

ret.setResultDefinition(rd);

FieldId[] fid=new FieldId [100];

Search serch = new Search(x);

rd.setSelectFields(fid);

System.out.println("only world");

for(int k=0;k<fields.length;k++)

{

fid[k]=fields[k].getId();

}

try{

ret.execute();

}

catch(Exception e)

{

System.out.println(e.toString());

}

RecordResultSet rs=ret.getRecords();

System.out.println(rs.getCount()+"Count is ");

for(int m=0;m<rs.getCount();m++)

{

//com.sap.mdm.internal.protocol.resultset.Record r =rs.getRecord(m);

com.sap.mdm.data.Record r = rs.getRecord(m);

//Record r=rs.getRecord(m);

System.out.print(r.getDisplayValue());

//this will display only the display fields of the repository

}

Former Member
0 Kudos

Hi,

Instead of passing "100" pass the exact number of fields.

Take the number of fields in one int variable say int flds=fields.length and pass this value in

FieldId[] fid=new FieldId[flds];

Are you able to get the Fields of the main table?Try above code and kindly let us know

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi,

I am getting the field names but not records.

former_member201266
Contributor
0 Kudos

Hi Ratna,

I think to retrive data you need to create an user session, try creating a user sesison, it might be usefull, the following code can be used for creating and authen... a user session..

CreateUserSessionCommand userSessionCommand =new CreateUserSessionCommand(connection);

userSessionCommand.setRepositoryIdentifier(reposId);

RegionProperties dataRegion = new RegionProperties();

dataRegion.setRegionCode("engUSA");

dataRegion.setLocale(new Locale("en", "US"));

dataRegion.setName("US");

userSessionCommand.setDataRegion(dataRegion);

try {

userSessionCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

}

String userSession = userSessionCommand.getUserSession();

AuthenticateUserSessionCommand userAuthCommand =new AuthenticateUserSessionCommand(connection);

userAuthCommand.setSession(userSession);

userAuthCommand.setUserName("Admin");

userAuthCommand.setUserPassword("");

try {

userAuthCommand.execute();

} catch (CommandException e) {

e.printStackTrace();

}

If required replace the username,pwd, the connection object and the reposid objects....

If you are creating this user session, then using this user session object implement the above given code by Talreja, then, i hope you might be able to retrieve the records. Let us know if you find any other problem..

Regards,

Cherry.

Former Member
0 Kudos

Hi,

I agreed with Cherry. In order to deal with records we need to create the User Session. Create the User session object with the help of the code given by Cherry and then append the code given by me.

Check this out

public class resultsetiView

{

public static ConnectionAccessor simpleConnection;

public static String connection = "TEST_MDM";

public static String repository = "Test";

public static RepositorySchema repSchema;

public static RepositoryIdentifier repIdentifier;

public static DBMSType dbmsType = DBMSType.MS_SQL;

public static String session;

public static void main (String args[])throws CommandException, ConnectionException

{

simpleConnection = SimpleConnectionFactory.getInstance(connection);

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

RegionProperties regionProperties = new RegionProperties();

regionProperties.setLocale(Locale.ENGLISH);

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

regionProperties.setRegionCode("engUSA");

CreateUserSessionCommand createUSesCmd = new CreateUserSessionCommand(simpleConnection);

createUSesCmd.setDataRegion(regionProperties);

createUSesCmd.setRepositoryIdentifier(repIdentifier);

createUSesCmd.execute();

session = createUSesCmd.getUserSession();

AuthenticateUserSessionCommand authUSesCmd = new AuthenticateUserSessionCommand(simpleConnection);

authUSesCmd.setSession(session);

authUSesCmd.setUserName("Admin");

authUSesCmd.setUserPassword("Admin");

authUSesCmd.execute();

GetRepositorySchemaCommand getRepSchCmd = new GetRepositorySchemaCommand(simpleConnection);

getRepSchCmd.setSession(session);

getRepSchCmd.execute();

repSchema = getRepSchCmd.getRepositorySchema();

TableId mainTableId = repSchema.getTableId("Title");

GetFieldListCommand getFieldListCommand = new GetFieldListCommand(simpleConnection);

getFieldListCommand.setSession(session);

getFieldListCommand.setTableId(mainTableId);

try

{

getFieldListCommand.execute();

}

catch (Exception e)

{

System.out.println(e);

}

FieldProperties[] fields = getFieldListCommand.getFields();

FieldId[] fid=new FieldId[fields.length];

Search s=new Search(mainTableId);

ResultDefinition rd=new ResultDefinition(mainTableId);

System.out.println(fields.length);

for(int i=0;i<fields.length;i++)

{

fid<i>=fields<i>.getId();

}

rd.setSelectFields(fid);

RetrieveLimitedRecordsCommand ret=new RetrieveLimitedRecordsCommand(simpleConnection);

ret.setSession(session);

ret.setResultDefinition(rd);

try

{

ret.execute();

}

catch(Exception e)

{

}

RecordResultSet rs=ret.getRecords();

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

{

Record r=rs.getRecord(i);

for(int j=0;j<fields.length;j++)

{

}

if(fields[j].getType()==7 || fields[j].getType()==42)

{

System.out.print(fields[j].getCode()" - "r.getLookupDisplayValue(fields[j].getId())+"# ");

}

else

{

System.out.print( fields[j].getCode()" - "r.getFieldValue(fields[j].getId())+"# ");

}

System.out.println();

}

}

}

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

my problam has resolved . thanks for sending the code .

Answers (0)