Navigating a Model Manager Database
Starting from a connected database, you can navigate to repositories, branches, items, and other objects stored in the database using the objects’ identifying keys. Some common objects are also available via shortcut methods. The keys themselves are typically obtained from individual elements in query and search results — see Querying Database Objects — or from other database objects.
In the remainder of this section, it is assumed that all code example snippets are preceded by the statements:
DatabaseApi api = DatabaseApiUtil.api();
Database database = api.databaseByAlias(<alias>);
with <alias> some alias for a Model Manager database.
Examples
Get the latest versions of 100 models and files in the default repository and in the default branch. This is the same search result you would obtain in the Model Manager window when not writing a search term in the search field or applying any search filters.
Repository repository = database.defaultRepository();
Branch branch = repository.defaultBranch();
SearchBranchItemResult[] result = branch
  .searchItems("")
  .toArray(100);
If you want to access the default branch in some other repository, for example, you will first need its repository key. One way, which is rather cumbersome, is to pairwise write the repository keys and repository names of all available repositories in the database to the Debug Log window in the Application Builder workspace.
for (QueryRepositoryResult r : database.queryRepositories()) {
  debugLog(r.name());
  debugLog(r.repositoryKey().value());
}
Select the key string of the desired repository in the window and press CTRL+C, or right-click and select Copy. Paste the key string into your code by pressing CTRL+V, or right-click and select Paste.
RepositoryKey key = DatabaseApiValueFactory
  .repositoryKey(
<keyString>);
 
Repository repository = database.repositoryByKey(key);
Branch branch = repository.defaultBranch();
 
SearchBranchItemResult[] result = branch
  .searchItems("")
  .toArray(100);
When using the Model Manager API from a standalone Java® application, you can also use the alternative syntax:
If you already have a reference to some other database object in the repository, for example a model version, you can often navigate to the repository from that object directly.
// The modelLocationUri string could have been obtained, for
// example, using Copy Location in the Model Manager workspace.
ModelItemVersion version =   api.modelVersionByLocationUri(modelLocationUri);
 
Repository repository = version.repository();
The same is true for other objects related to the model version.
// The model item that the version belongs to.
ModelItem item = version.item();
 
// The branch that the version was saved in.
Branch branch = version.branch();
 
// The latest version of the item on the branch.
BranchModelItem latest = version.branchItem();
 
// The commit that the version was saved in.
Commit commit = version.commit();
 
// The database that the version was saved in.
Database database = version.database();
You may think of the model version as providing a stable “anchor” in your code from which you can quickly navigate to other objects. This will hold true as long as the model version is not permanently deleted in the Model Manager database.
Search for the latest version of a model item in the default repository and in the default branch. Query for the most recently saved version in the database — in any repository and in any branch — of the corresponding model item. Write the names of the corresponding repository and branch the version belongs to in the Debug Log window.
Branch branch = database.defaultRepository().defaultBranch();
 
SearchBranchItemResult searchResult = branch
  .searchItems("My Model")
  .single();
ItemKey itemKey = searchResult.itemKey();
ModelItem modelItem = database.modelByKey(itemKey);
 
QueryItemVersionResult queryResult = modelItem
  .queryVersions()
  .firstOrNull();
ItemVersionKey versionKey = queryResult.itemVersionKey();
ModelItemVersion version = database.modelVersionByKey(versionKey);
 
debugLog(version.repository().name());
debugLog(version.branch().name());