package com.sap.mdm.examples; 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.data.RegionProperties; import com.sap.mdm.data.ResultDefinition; import com.sap.mdm.data.commands.RetrieveLimitedRecordsCommand; import com.sap.mdm.ids.TableId; import com.sap.mdm.net.ConnectionException; import com.sap.mdm.net.ConnectionPool; import com.sap.mdm.net.ConnectionPoolFactory; import com.sap.mdm.search.Search; import com.sap.mdm.server.DBMSType; import com.sap.mdm.server.RepositoryIdentifier; /* * Copyright © 2004-2006 by SAP AG. * All Rights Reserved. * * SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP * products and services mentioned herein as well as their respective logos are * trademarks or registered trademarks of SAP AG in Germany and in several other * countries all over the world. All other product and service names mentioned * are the trademarks of their respective companies. Data contained in this * document serves informational purposes only. National product specifications * may vary. * * These materials are subject to change without notice. These materials are * provided by SAP AG and its affiliated companies (SAP Group) for informational * purposes only, without representation or warranty of any kind, and SAP Group * shall not be liable for errors or omissions with respect to the materials. * The only warranties for SAP Group products and services are those that are * set forth in the express warranty statements accompanying such products and * services, if any. Nothing herein should be construed as constituting an * additional warranty. * */ /** * This example demonstrates using a user session to retrieves the number of * records for the main table. *
    *
  1. Establish connection with the MDM server
  2. *
  3. Retrieve the list of languages for the targeted repository
  4. *
  5. Create a user session for the targeted repository
  6. *
  7. Authenticate the user session
  8. *
  9. Retrieve the record count for the main table
  10. *
  11. Destroy the user session
  12. *
* Commands used:
* CreateUserSessionCommand
* AuthenticateUserSessionCommand
* GetRepositoryRegionListCommand
* RetrieveLimitedRecordsCommand
* DestroySessionCommand
*
* Source code for * RetrieveLimitedRecords.java */ public class RetrieveLimitedRecords { 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 = "MyRepos"; String dbmsName = "LOCALHOST"; RepositoryIdentifier reposId = new RepositoryIdentifier(repositoryName, dbmsName, DBMSType.MS_SQL); // 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 = "me"; String userPassword = "me"; 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); // specify the result definition (what to retrieve); in this example, nothing ResultDefinition rd = new ResultDefinition(mainTableId); // select all records Search search = new Search(mainTableId); // retrieve the records RetrieveLimitedRecordsCommand limitingCommand = new RetrieveLimitedRecordsCommand(connections); limitingCommand.setSession(sessionId); limitingCommand.setResultDefinition(rd); limitingCommand.setSearch(search); //limitingCommand.setPageSize(10); try { limitingCommand.execute(); } catch (CommandException e) { e.printStackTrace(); return; } System.out.println("Record count is " + limitingCommand.getRecords().getCount()); // finally destroy the session DestroySessionCommand destroySessionCommand = new DestroySessionCommand(connections); destroySessionCommand.setSession(sessionId); try { destroySessionCommand.execute(); } catch (CommandException e) { e.printStackTrace(); return; } } }