package com.sap.mdm.examples;
import java.util.Locale;
import com.sap.mdm.commands.AuthenticateRepositorySessionCommand;
import com.sap.mdm.commands.CommandException;
import com.sap.mdm.commands.CreateRepositorySessionCommand;
import com.sap.mdm.commands.DestroySessionCommand;
import com.sap.mdm.commands.GetRepositoryRegionListCommand;
import com.sap.mdm.data.MultilingualString;
import com.sap.mdm.data.RegionProperties;
import com.sap.mdm.data.RegionalString;
import com.sap.mdm.net.ConnectionException;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.net.ConnectionPoolFactory;
import com.sap.mdm.schema.TableProperties;
import com.sap.mdm.schema.commands.CreateTableCommand;
import com.sap.mdm.schema.commands.GetTableListCommand;
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 creating a new table for a repository. The steps
* are:
*
* - Establish connection with the MDM server
* - Create a repository session for the targeted repository
* - Authenticate the session
* - Create the new table
* - Destroy the session
*
* Commands used:
* CreateRepositorySessionCommand
* AuthenticateRepositorySessionCommand
* GetTableListCommand
* GetRepositoryRegionListCommand
* CreateTableCommand
* DestroySessionCommand
*
* Source code for CreateTable.java
*/
public class CreateTable {
private CreateTable() {
}
/**
* Creates a multi-lingual string based on the list of regions.
* @param regionPropertiesList the list of regions
* @param baseString the base string
* @return a multi-lingual string
*/
private static MultilingualString createMultilingualString(RegionProperties[] regionPropertiesList, String baseString) {
MultilingualString mlString = new MultilingualString();
for (int i = 0; i < regionPropertiesList.length; i++) {
Locale locale = regionPropertiesList[i].getLocale();
String regionCode = regionPropertiesList[i].getRegionCode();
String string = baseString + "_" + locale.getLanguage() + "_" + locale.getCountry();
RegionalString regionalString = new RegionalString(string, regionCode);
mlString.set(regionalString);
}
return mlString;
}
/**
* Creates a flat lookup table.
* @param regionPropertiesList the list of regions
* @return a flat table
*/
private static TableProperties createFlatTable(RegionProperties[] regionPropertiesList) {
MultilingualString tableName = createMultilingualString(regionPropertiesList, "NewTable" + System.currentTimeMillis());
TableProperties table = new TableProperties(TableProperties.FLAT);
table.setName(tableName);
table.setCode("NewCode" + System.currentTimeMillis());
table.setKeyMappable(true);
table.setDescription("");
return table;
}
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 = "TestRepos";
String dbmsName = "LOCALHOST";
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;
}
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
// this is useful for resolving conflicting table name the new table might create
GetTableListCommand tableListCommand = new GetTableListCommand(connections);
tableListCommand.setSession(sessionId);
try {
tableListCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
// get change stamp
// this is required when we make any kind of changes to the repository
int changeStamp = tableListCommand.getChangeStamp();
// retrieve the available regions (languages) for the repository
// we need this to set up the table name for each region
GetRepositoryRegionListCommand getReposRegionListCommand = new GetRepositoryRegionListCommand(connections);
getReposRegionListCommand.setRepositoryIdentifier(reposId);
try {
getReposRegionListCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
RegionProperties[] regionPropertiesList = getReposRegionListCommand.getRegions();
// set up the table to create
TableProperties newTable = createFlatTable(regionPropertiesList);
// create the table on repository
CreateTableCommand createTableCommand = new CreateTableCommand(connections);
createTableCommand.setSession(sessionId);
createTableCommand.setTable(newTable);
createTableCommand.setInChangeStamp(changeStamp);
try {
createTableCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
// finally destroy the session
DestroySessionCommand destroySessionCommand = new DestroySessionCommand(connections);
destroySessionCommand.setSession(sessionId);
try {
destroySessionCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
}
}