Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

How to create a new file version programmatically?

Hello,

Following this article I can create a file in a workspace/component - https://jazz.net/wiki/bin/view/Main/RTCSDK20_SourceControl

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!

0 votes

Comments

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

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.

Simon

----------------------

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(
   IFileContent.ENCODING_US_ASCII,
   FileLineDelimiter.LINE_DELIMITER_PLATFORM,
     new VersionedContentManagerByteArrayInputStreamPovider(content.getBytes()),
    null, null);
        
file.setContentType(IFileItem.CONTENT_TYPE_TEXT);
file.setContent(storedContent);
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);

1 vote



One answer

Permanent link
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".

1 vote

Comments

  > 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 :)

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.

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.

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 log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,938

Question asked: Aug 09 '13, 5:35 p.m.

Question was seen: 6,651 times

Last updated: Aug 22 '13, 8:20 p.m.

Confirmation Cancel Confirm