I am new to the MDM Java API. For one of the existing project I was asked to explore the new MDM Java API. I do not have any experience with the earlier MDM4J but still I was able to understand and explore the new MDM Java API. I think this API is excellent and I am almost fallen in love with it. The best part I like about this API is the Commands. Initially it took me time to understand the concept but very soon it was all clear. With this blog I would like to share my knowledge about how to work with Commands. Also in this blog you will see an example of Validation Command.
Why you need a Command? What is a Command?
I command the MDM Java API to get me the list of Validations ….. /<br />package demo.validation;</p><p style="margin: 0cm 0cm 0pt" class="MsoNormal">import com.sap.mdm.commands.AuthenticateUserSessionCommand;<br />import com.sap.mdm.commands.CommandException;<br />import com.sap.mdm.commands.CreateUserSessionCommand;<br />import com.sap.mdm.commands.GetRepositoryRegionListCommand;<br />import com.sap.mdm.data.RegionProperties;<br />import com.sap.mdm.ids.TableId;<br />import com.sap.mdm.net.ConnectionException;<br />import com.sap.mdm.net.ConnectionPool;<br />import com.sap.mdm.net.ConnectionPoolFactory;<br />import com.sap.mdm.server.DBMSType;<br />import com.sap.mdm.server.RepositoryIdentifier;<br />import com.sap.mdm.validation.ValidationProperties;<br />import com.sap.mdm.validation.ValidationPropertiesResult;<br />import com.sap.mdm.validation.commands.RetrieveValidationsCommand;</p><p style="margin: 0cm 0cm 0pt" class="MsoNormal">/*
*
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class GetListOfValidations {
public static void main(String[] args) {
// create connection pool to a MDM server
String serverName = "LOCALHOST";
ConnectionPool connections = null;
try {
connections = ConnectionPoolFactory.getInstance(serverName);
} catch (ConnectionException e) {
e.printStackTrace();
return;
}
// specify the repository to use
// alternatively, a repository identifier can be obtain from the GetMountedRepositoryListCommand
String repositoryName = "INQDemo";
String dbmsName = "localhost";
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, hard-coded
TableId mainTableId = new TableId(1);
// Get the list of validations
RetrieveValidationsCommand objRtvVldCmd =
new RetrieveValidationsCommand(connections);
// set the user session
objRtvVldCmd.setSession(sessionId);
// get validation for the following tables.
objRtvVldCmd.setTableId(mainTableId);
try {
objRtvVldCmd.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
ValidationPropertiesResult objVldPropRslt =
objRtvVldCmd.getValidationPropertiesResult();
ValidationProperties[] validations = objVldPropRslt.getValidations();
//disply --> Validation ID | error/warning message | Validation Name
for (int i = 0; i < validations.length; i++) {
System.out.println(
validations[i].getId()
+ " | "
+ validations[i].getMessage()
+ " | "
+ validations[i].getName());
}
}
}