It's all about the answers!

Ask a question

How to create a new file version programmatically?

Simon Hoare (1122) | asked Aug 09 '13, 5:35 p.m.
retagged Aug 13 '13, 7:14 p.m. by Te-Hsin Shih (2854)


Following this article I can create a file in a workspace/component -

However, if the file already exists I get an error. What I want to do, in this case, is create a new version of the file.  Appreciate any pointers or sample code on how to achieve that!

Remy Suen commented Aug 20 '13, 10:16 a.m.

What exception did you get? What does your code look like? Did you get a working copy of your IFileItem first?

Simon Hoare commented Aug 20 '13, 3:52 p.m. | edited Aug 20 '13, 4:15 p.m.

Hi Remy.

The code below is what I currently have. My understanding has advanced a little since I posted the question, and I can now create a file or update the content of an existing one (which is what the code below does). However I am unclear on a couple of points -

- The code over-writes the content of the file and does not appear to create a new version of the file. Do I have to do something differently to create a version versus changing an existing version? (I'm not that familiar with the RTC client, so it may be that I am getting a new version and I just can't figure out how to see it)

- When I run this code I seem to be left with an 'active' change set. if I run it again I can find the active change set and use it,, but I find this confusing.. I would have expected my active change set to disappear after I submit the changes and I would expect to create a new one for each set of changes I want to make.. Do I have to do something programmatically to get that behavior?

Appreciate any pointers.



void CreateOrUpdateFile(IWorkspaceConnection workspace, IComponentHandle component, IFolderHandle rootFolder,IVersionable versionable, String filename,  String content)

// Get the currently active change set if it exists or create a new one
List<IChangeSetHandle> listChangeSets =  workspace.activeChangeSets(component);
IChangeSetHandle cs1 = null;
if (listChangeSets != null) {
  if (listChangeSets.size() > 0) {
   Object[] changeSetObjects =  listChangeSets.toArray();
   cs1 = (IChangeSetHandle) changeSetObjects[0];
if (cs1 == null)
 cs1 = workspace.createChangeSet(component, null);

// find or create a file
IFileItem file = null;
if (versionable instanceof FileItem) {
  FileItem rtcitem = (FileItem) versionable;
  file = (IFileItem) rtcitem.getWorkingCopy();   
  } else {
  file = (IFileItem) IFileItem.ITEM_TYPE.createItem();
  file.setName(filename); file.setParent(rootFolder);
// update the file content
IFileContentManager contentManager = FileSystemCore.getContentManager(teamRepository);
IFileContent storedContent = contentManager.storeContent(
     new VersionedContentManagerByteArrayInputStreamPovider(content.getBytes()),
    null, null);
file.setFileTimestamp(new Date());
workspace.commit(cs1, Collections.singletonList(workspace.configurationOpFactory().save(file)), null);

// deliver the changes to the stream
IChangeHistorySyncReport sync =
workspace.compareTo(workspace, WorkspaceComparisonFlags.CHANGE_SET_COMPARISON_ONLY, Collections.EMPTY_LIST, null);
 workspace.deliver(workspace, sync, Collections.EMPTY_LIST, sync.outgoingChangeSets(component), null);

One answer

permanent link
Remy Suen (426224) | answered Aug 20 '13, 4:08 p.m.
I am not sure I understand your first question about it not creating a new version of the file. Please elaborate further.

As to your second question, I am not sure your deliver code is working.
workspace.deliver(workspace... is weird. You are not delivering to another workspace or stream. You are just delivering "to yourself".

Simon Hoare commented Aug 20 '13, 4:42 p.m. | edited Aug 21 '13, 9:29 a.m.

  > I am not sure I understand your first question about it not creating a new version of the file. Please elaborate further

So I have a workspace containing a component containing files. I would expect to be able to check in a new version of the file such that the old version(s) and new version are all present, like most version control systems would do. Looking at the file through the RTC client UI doesn't show file versions though.. or even anyway of creating one.  Perhaps I need to add the file to version control in some manner for that to happen? I had thought my code would result in a new version automatically.. but I don't see any evidence of that.

> As to your second question, I am not sure your deliver code is working. 
What I'm trying to do is, in effect, a 'check in'.. i.e. apply my change set. I take it from your comment, though, I should be specifying a stream here..?

Apologies if my understanding of RTC is too rudimentary to even ask useful questions :)

Remy Suen commented Aug 20 '13, 5:19 p.m.

Check-in and deliver means two different things in Jazz SCM. I suggest you use replace your deliver() call with closeChangeSets() first to see if it gets you any closer to your desired behaviour.

Tim Mok commented Aug 21 '13, 10:24 a.m.
workspace.commit(cs1, Collections.singletonList(workspace.configurationOpFactory().save(file)), null);

That's your commit to the change set (aka check-in). It returns a report where you can get a handle to the previous and current versionable states. I'm not sure what you mean by the old and new version being present. You get the current state, which is what you checked in. Do you mean the history of the file?

FYI, you can post comments to answers instead of creating a new answer. Posting new comments as answers makes it confusing for others trying to follow the answer. I've moved the posts around to organize the responses.

Simon Hoare commented Aug 22 '13, 8:20 p.m.

Thanks very much for the clarifications - correcting the deliver to closeChangeSet has resolved my ChangeSet confusion and this code appears to work correctly now.

And yes, I did mean history of the file by 'old and new version'.. I see based on your comment how to use the history command in the UI to see what I was expecting in terms of versions :)

Again, thank you very much!

Your answer

Register or to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.