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.ids.TableId; import com.sap.mdm.net.ConnectionException; import com.sap.mdm.net.ConnectionPool; import com.sap.mdm.net.ConnectionPoolFactory; import com.sap.mdm.schema.FieldKeywordType; import com.sap.mdm.schema.FieldProperties; import com.sap.mdm.schema.FieldSortType; import com.sap.mdm.schema.TableProperties; import com.sap.mdm.schema.commands.CreateFieldCommand; import com.sap.mdm.schema.commands.GetFieldListCommand; import com.sap.mdm.schema.commands.GetTableListCommand; import com.sap.mdm.schema.fields.FixedWidthTextFieldProperties; 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 field in the main table. The steps * are: *
    *
  1. Establishes connection with server
  2. *
  3. Creates a session for the targeted repository
  4. *
  5. Authenticates the session
  6. *
  7. Selects the main table
  8. *
  9. Creates the new field
  10. *
  11. Destroys the session
  12. *
* Commands used:
* CreateRepositorySessionCommand
* AuthenticateRepositorySessionCommand
* GetTableListCommand
* GetFieldListCommand
* GetRepositoryRegionListCommand
* CreateFieldCommand
* DestroySessionCommand
*
* Source code for CreateField.java * */ public class CreateField { private CreateField() { } /** * 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 fixed-width field. * @param tableId the table from which the field will be create in * @param regionPropertiesList the list of regions * @return the new fixed-width field */ private static FixedWidthTextFieldProperties createFixedWidthField(TableId tableId, RegionProperties[] regionPropertiesList) { MultilingualString fieldName = createMultilingualString(regionPropertiesList, "NewField" + System.currentTimeMillis()); FixedWidthTextFieldProperties field = new FixedWidthTextFieldProperties(); field.setTableId(tableId); field.setName(fieldName); field.setCode("NewCode" + System.currentTimeMillis()); field.setKeywordType(FieldKeywordType.NORMAL); field.setMultiLingual(true); field.setModifyOnce(true); field.setRequired(true); field.setSortType(FieldSortType.CASE_SENSITIVE); field.setWidth(60); field.setDescription(""); return field; } 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 and pick the main table for creating a new field GetTableListCommand tableListCommand = new GetTableListCommand(connections); tableListCommand.setSession(sessionId); try { tableListCommand.execute(); } catch (CommandException e) { e.printStackTrace(); return; } TableProperties mainTable = null; TableProperties[] tables = tableListCommand.getTables(); for (int i = 0; i < tables.length; i++) { if (tables[i].getType() == TableProperties.MAIN) mainTable = tables[i]; } // retrieve the list of fields from the main table // this is useful for resolving conflicting field names the new field might create GetFieldListCommand getFieldListCommand = new GetFieldListCommand(connections); getFieldListCommand.setSession(sessionId); getFieldListCommand.setTableId(mainTable.getId()); try { getFieldListCommand.execute(); } catch (CommandException e) { e.printStackTrace(); return; } // get the change stamp // this is required when we make any kind of changes to the repository int changeStamp = getFieldListCommand.getChangeStamp(); // retrieve the available regions (languages) for the repository // we need this to set up the field 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 field to create FieldProperties newField = createFixedWidthField(mainTable.getId(), regionPropertiesList); // create the new field CreateFieldCommand createFieldCommand = new CreateFieldCommand(connections); createFieldCommand.setSession(sessionId); createFieldCommand.setField(newField); createFieldCommand.setInChangeStamp(changeStamp); try { createFieldCommand.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; } } }