Skip to Content
avatar image
Former Member

Create Fields(using MDM Java API)

Hi all,

I created 2 fields in Main Table using 2 sessions.(MDM Java API)

Can we create morethan one field using single session.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Jun 27, 2008 at 09:52 AM

    Hi Santosh,

    You can create any number of fields in one session. Are you facing any problem in creating the fields?

    In fact you can create N number of fields with one statement. Just write the field creation statement inside the loop with field code and name appended by loop variable.

    Regards,

    Jitesh Talreja

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 27, 2008 at 10:34 AM

    Hi Jitesh,

    I want 2 create more than one different fields using single session.Iam facing small problem,

    Once check my code

    import com.sap.*;

    import java.util.Locale;

    /

    public class createfield1session {

    public createfield1session() {

    }

    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;

    }

    /////////////////////////// integer field

    private static IntegerFieldProperties createFixedWidthField(TableId tableId, RegionProperties[] regionPropertiesList)

    {

    MultilingualString fieldName = createMultilingualString(regionPropertiesList, "inetgerfield4");

    IntegerFieldProperties field = new IntegerFieldProperties();

    field.setTableId(tableId);

    field.setName(fieldName);

    field.setCode("NewCode" + System.currentTimeMillis());

    field.setMultiLingual(false);

    field.setModifyOnce(true);

    field.setRequired(true);

    field.setDescription("integer from java API");

    return field;

    }

    ////////////////////////boolean field

    private static BooleanFieldProperties createFixedWidthField1(TableId tableId, RegionProperties[] regionPropertiesList)

    {

    MultilingualString fieldName1 = createMultilingualString(regionPropertiesList, "booleanfield4");

    BooleanFieldProperties field1 = new BooleanFieldProperties();

    field1.setTableId(tableId);

    field1.setName(fieldName1);

    field1.setCode("NewCode" + System.currentTimeMillis());

    field1.setMultiLingual(false);

    field1.setModifyOnce(true);

    field1.setRequired(true);

    field1.setDescription("boolean from java API");

    return field1;

    }

    public static void main(String[] args) {

    // System Name

    String tag = "";

    ConnectionPool connections = null;

    try {

    connections = ConnectionPoolFactory.getInstance(tag);

    }

    catch (ConnectionException e)

    {

    e.printStackTrace();

    return;

    }

    //////////////// specify the repository to use

    //RepositoryName

    String repositoryName = "student";

    //DataBase Name

    String dbmsName = "";

    RepositoryIdentifier reposId = new RepositoryIdentifier(repositoryName,

    dbmsName, DBMSType.MS_SQL);

    TableProperties mainTable = null;

    ////// create a repository session

    {

    CreateRepositorySessionCommand sessionCommand = new CreateRepositorySessionCommand(connections);

    sessionCommand.setRepositoryIdentifier(reposId);

    try {

    sessionCommand.execute();

    }

    catch (CommandException e) {

    e.printStackTrace();

    return;

    }

    System.out.println("Currently connected to "+reposId);

    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[] 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

    int changeStamp = getFieldListCommand.getChangeStamp();

    GetRepositoryRegionListCommand gm = new GetRepositoryRegionListCommand(connections);

    gm.setRepositoryIdentifier(reposId);

    try {

    gm.execute();

    } catch (CommandException e) {

    e.printStackTrace();

    return;

    }

    RegionProperties[] r = gm.getRegions();

    FieldProperties newField = createFixedWidthField(mainTable.getId(), r);

    FieldProperties newField1 = createFixedWidthField1(mainTable.getId(), r);

    com.sap.mdm.schema.commands.CreateFieldCommand c= new com.sap.mdm.schema.commands.CreateFieldCommand(connections);

    com.sap.mdm.schema.commands.CreateFieldCommand c1 = new com.sap.mdm.schema.commands.CreateFieldCommand(connections);

    c.setSession(sessionId);

    c.setField(newField);

    c1.setSession(sessionId);

    c1.setField(newField1);

    c.setInChangeStamp(changeStamp);

    c1.setInChangeStamp(changeStamp);

    try {

    c.execute();

    c1.execute();

    }

    catch (CommandException e) {

    e.printStackTrace();

    return;

    }

    //destroy session

    DestroySessionCommand ds = new DestroySessionCommand(connections);

    ds.setSession(sessionId);

    try {

    ds.execute();

    } catch (CommandException e) {

    e.printStackTrace();

    return;

    }

    }

    }

    }

    My error is :

    MDM repository data is out-of-date or is locked by another MDM Server. Refresh the data and try the operation again

    Edited by: santhosh kumar on Jun 27, 2008 12:35 PM

    Add comment
    10|10000 characters needed characters exceeded