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!
One answer
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".
Comments
> I am not sure I understand your first question about it not creating a new version of the file. Please elaborate further
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.
Comments
Remy Suen
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
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.
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