0%

loadRepository_and_modify_table_and_field

LoadRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.sap.mdm.examples;

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.net.ConnectionException;
import com.sap.mdm.net.ConnectionPool;
import com.sap.mdm.net.ConnectionPoolFactory;
import com.sap.mdm.repository.commands.LoadRepositoryCommand;
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 loading a repository. The steps are:
* <ol>
* <li>Establishes connection with the MDM server</li>
* <li>Creates a repository session for the targeted repository</li>
* <li>Authenticates the session</li>
* <li>Loads the repository</li>
* <li>Destroys the session</li>
* </ol>
* Commands used:<br>
* <code>CreateRepositorySessionCommand</code><br>
* <code>AuthenticateRepositorySessionCommand</code><br>
* <code>LoadRepositoryCommand</code><br>
* <code>DestroySessionCommand</code><br>
* <br>
* <a href="LoadRepository.java">Source code for LoadRepository.java</a>
*/
public class LoadRepository {
private LoadRepository() {
}

public static void main(String[] args) {
// create connection pool
String connectionTag = "LOCALHOST";
ConnectionPool connections = null;
try {
connections = ConnectionPoolFactory.getInstance(connectionTag);
} catch (ConnectionException e) {
e.printStackTrace();
return;
}

// alternatively, a repository identifier can be obtain from the GetMountedRepositoryListCommand
RepositoryIdentifier reposId = new RepositoryIdentifier("EmptyRepos", "LOCALHOST", DBMSType.MS_SQL);

// create 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;
}

// load the repository
LoadRepositoryCommand loadReposCommand = new LoadRepositoryCommand(connections);
loadReposCommand.setSession(sessionId);
try {
loadReposCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}

// finally destroy the repository session
DestroySessionCommand destroySessionCommand = new DestroySessionCommand(connections);
destroySessionCommand.setSession(sessionId);
try {
destroySessionCommand.execute();
} catch (CommandException e) {
e.printStackTrace();
return;
}
}
}

Create Table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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:
* <ol>
* <li>Establish connection with the MDM server</li>
* <li>Create a repository session for the targeted repository</li>
* <li>Authenticate the session</li>
* <li>Create the new table</li>
* <li>Destroy the session</li>
* </ol>
* Commands used:<br>
* <code>CreateRepositorySessionCommand</code><br>
* <code>AuthenticateRepositorySessionCommand</code><br>
* <code>GetTableListCommand</code><br>
* <code>GetRepositoryRegionListCommand</code><br>
* <code>CreateTableCommand</code><br>
* <code>DestroySessionCommand</code><br>
* <br>
* <a href="CreateTable.java">Source code for CreateTable.java</a>
*/
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;
}
}
}

Create Field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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:
* <ol>
* <li>Establishes connection with server</li>
* <li>Creates a session for the targeted repository</li>
* <li>Authenticates the session</li>
* <li>Selects the main table</li>
* <li>Creates the new field</li>
* <li>Destroys the session</li>
* </ol>
* Commands used:<br>
* <code>CreateRepositorySessionCommand</code><br>
* <code>AuthenticateRepositorySessionCommand</code><br>
* <code>GetTableListCommand</code><br>
* <code>GetFieldListCommand</code><br>
* <code>GetRepositoryRegionListCommand</code><br>
* <code>CreateFieldCommand</code><br>
* <code>DestroySessionCommand</code><br>
* <br>
* <a href="CreateField.java">Source code for CreateField.java</a>
*
*/
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;
}
}
}

Modify Field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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.commands.ModifyFieldCommand;
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 modifying a new field in the main table. The steps
* are:
* <ol>
* <li>Establishes connection with MDM server</li>
* <li>Creates a session for the targeted repository</li>
* <li>Authenticates the session</li>
* <li>Selects the main table</li>
* <li>Creates the new field</li>
* <li>Modifies the new field</li>
* <li>Destroys the session</li>
* </ol>
* Commands used:<br>
* <code>CreateRepositorySessionCommand</code><br>
* <code>AuthenticateRepositorySessionCommand</code><br>
* <code>GetTableListCommand</code><br>
* <code>GetFieldListCommand</code><br>
* <code>GetRepositoryRegionListCommand</code><br>
* <code>CreateFieldCommand</code><br>
* <code>ModifyFieldCommand</code><br>
* <code>DestroySessionCommand</code><br>
* <br>
* <a href="ModifyField.java">Source code for ModifyField.java</a>
*/
public class ModifyField {
private ModifyField() {
}

/**
* 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.setKeywordType(FieldKeywordType.NORMAL);
field.setMultiLingual(true);
field.setModifyOnce(true);
field.setRequired(true);
field.setSortType(FieldSortType.CASE_SENSITIVE);
field.setWidth(60);

return field;
}

public static void main(String[] args) {
// create connection pool to a MDM server
String tag = "LOCALHOST";
ConnectionPool connections = null;
try {
connections = ConnectionPoolFactory.getInstance(tag);
} 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;
}

// a new change stamp is required.
changeStamp = createFieldCommand.getOutChangeStamp();

// setup the field for modification
FixedWidthTextFieldProperties modifiedField = (FixedWidthTextFieldProperties) newField;
modifiedField.setWidth(100);
modifiedField.setRequired(false);

// modify the field
ModifyFieldCommand modifyFieldCommand = new ModifyFieldCommand(connections);
modifyFieldCommand.setSession(sessionId);
modifyFieldCommand.setInChangeStamp(changeStamp);
modifyFieldCommand.setField(modifiedField);
try {
modifyFieldCommand.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;
}
}
}