Advanced Save Operations for Items
The options available for the parameter objects used when saving items enables you to perform a variety of advanced operations that go beyond what you have seen so far. A few examples:
Examples
Save a model loaded into COMSOL Multiphysics as a version of another model in the database.
// The targetItemKey variable is initialized elsewhere as the
// identifying key of an existing model in the database.
SaveModelItemParamGenerator p = DatabaseApiUtil.param()
  .forSaveModel()
  .withSourceModel(model.getTag())
  .withTargetItemKey(targetItemKey);
ModelItemVersion version = branch
  .saveModel(p, "Saved a model as a version of another model.");
Combine three file resources from two separate file versions into a new file.
DatabaseApiParamGenerators param = DatabaseApiUtil.param();
 
// The itemVersionKey1 and itemVersionKey2 variables are obtained
// elsewhere as the identifying keys of two existing file versions.
SourceFileParamGenerator p1 = param
  .forSourceFile()
  .withItemVersionKey(itemVersionKey1)
  .withFileResourcePath("casing.prt");
SourceFileParamGenerator p2 = param
  .forSourceFile()
  .withItemVersionKey(itemVersionKey1)
  .withFileResourcePath("cover.prt");
SourceFileParamGenerator p3 = param
  .forSourceFile()
  .withItemVersionKey(itemVersionKey2)
  .withFileResourcePath("stator_coil.prt");
 
SaveFileItemParamGenerator p = param
  .forSaveFile()
  .withSourceFiles(p1, p2, p3)
  .withTitle("My Induction Motor Assembly")
  .withTargetAsNew();
FileItemVersion version = branch.saveFile(p, "Saved a new file.");
Export a file version containing table data to a temporary file using the temp file scheme. Append a table row to the temporary file using the writeFile method from the Method Editor’s built-in method library. Save the updated table data as a new file version.
String tempFilePath = "temp:///table.tmp";
 
// Make sure that there is no file at the export location.
deleteFile(tempFilePath);
 
// The location variable identifying the file version is
// initialized elsewhere.
FileItemVersion fileVersion = api
  .fileVersionByLocationUri(location);
FileResourcePath fileResourcePath = fileVersion
  .firstFileResourceOrNull()
  .path();
 
DatabaseApiParamGenerators param = DatabaseApiUtil.param();
 
ExportFileItemVersionParamGenerator p1 = param
  .forExportFileVersion()
  .withSourceFileResourcePath(fileResourcePath)
  .withTargetLocationModel(model.tag())
  .withTargetLocation(tempFilePath);
fileVersion.export(p1);
 
// Append a table row to the exported file.
writeFile(tempFilePath, new String[][]{{"1", "2", "3"}}, true);
 
// Save the updated table data as a new file version.
SourceFileParamGenerator p2 = param
  .forSourceFile()
  .withLocationModel(model.tag())
  .withLocation(tempFilePath);
UpdateFileItemParamGenerator p3 = param
  .forUpdateFile()
  .withSourceFiles(p2);
 
fileVersion
  .branchItem()
  .update(p3, "Appended a table row.");
Export table data from a model and then save both the model and the table data as new versions in a single commit.
String location = branch.newFileLocationUri("my_table.txt");
 
ExportFeature e = model.result().export("table1");
e.set("filename", location);
e.run();
 
DatabaseApiParamGenerators param = DatabaseApiUtil.param();
 
SaveModelItemParamGenerator p1 = param
  .forSaveModel()
  .withSourceModel(model.tag());
SaveFileItemParamGenerator p2 = param
  .forSaveFile()
  .withSourceItemVersionWorkingCopy(model.tag(), location);
SaveCommitParamGenerator p = param
  .forSaveCommit()
  .withModelsToSave(p1)
  .withFilesToSave(p2);
 
Commit commit = branch.saveCommit(p);