on 08-20-2008 8:40 AM
Can anyone please correct my code..this is just to search a field in position 2 i.e 2nd fieldin Customer repository.
I want ot retreive all the records which have the field value as Mr
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.commands.GetRepositoryRegionListCommand;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.server.RepositoryIdentifier;
import com.sap.mdm.search.SearchDimension;
import java.util.Locale;
import com.sap.mdm.commands.SetUnicodeNormalizationCommand;
import com.sap.mdm.commands.TrustedUserSessionCommand;
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.search.FieldSearchDimension;
import com.sap.mdm.search.Search;
import com.sap.mdm.search.TextSearchConstraint;
import com.sap.mdm.server.DBMSType;
import com.sap.mdm.data.RecordResultSet;
public class ex {
public static void main(String[] args) {
// create connection pool to a MDM server
String serverName = "mdmserver";
ConnectionPool connections = null;
try {
connections = ConnectionPoolFactory.getInstance(serverName);
} catch (ConnectionException e) {
e.printStackTrace();
return;
}
System.out.println("Server connected");
// specify the repository to use
// alternatively, a repository identifier can be obtain from the GetMountedRepositoryListCommand
String repositoryName = "CUSTOMER";
String dbmsName = "DDD";
RepositoryIdentifier reposId = new RepositoryIdentifier(repositoryName, dbmsName, DBMSType.ORACLE);
// get list of available regions for the repository
GetRepositoryRegionListCommand regionListCommand = new GetRepositoryRegionListCommand(connections);
regionListCommand.setRepositoryIdentifier(reposId);
try {
regionListCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
RegionProperties[] regions = regionListCommand.getRegions();
// create a user session
CreateUserSessionCommand sessionCommand = new CreateUserSessionCommand(connections);
sessionCommand.setRepositoryIdentifier(reposId);
sessionCommand.setDataRegion(regions[0]); // use the first region
try {
sessionCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
String sessionId = sessionCommand.getUserSession();
// authenticate the user session
String userName = "Admin";
String userPassword = "Admin";
AuthenticateUserSessionCommand authCommand = new AuthenticateUserSessionCommand(connections);
authCommand.setSession(sessionId);
authCommand.setUserName(userName);
authCommand.setUserPassword(userPassword);
try {
authCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
// the main table,
TableId mainTableId = new TableId(1);
ResultDefinition rdMain = new ResultDefinition(mainTableId);
//Trying to search for Mr value records
rdMain.addSelectField(new FieldId(2));
FieldSearchDimension fsdMaintableType = new FieldSearchDimension(new FieldId(2));
TextSearchConstraint tscTypeRoot = new TextSearchConstraint("Mr", TextSearchConstraint.CONTAINS);
Search seSearchTypeRoot = new Search(new TableId(1));
seSearchTypeRoot.addSearchItem(fsdMaintableType, tscTypeRoot);
RetrieveLimitedRecordsCommand rlrcGetRecordsOfTypeRoot = new RetrieveLimitedRecordsCommand(connections);
// Set the search to use for command
rlrcGetRecordsOfTypeRoot.setSearch(seSearchTypeRoot);
// Set the session to use for command
rlrcGetRecordsOfTypeRoot.setSession(sessionId);
// Set the result definition to use
rlrcGetRecordsOfTypeRoot.setResultDefinition(rdMain);
// Try to execute the command
try {
rlrcGetRecordsOfTypeRoot.execute();
} catch (CommandException e) {
// Do something with the exception
e.printStackTrace();
}
// Return the result
RecordResultSet n= rlrcGetRecordsOfTypeRoot.getRecords();
System.out.println(n.toString());
}
}
Hi,
Please compare your code with the code given 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 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.