How to create a new file version programmatically?
![]()
Simon Hoare (11●2●2)
| asked Aug 09 '13, 5:35 p.m.
retagged Aug 13 '13, 7:14 p.m. by Te-Hsin Shih (285●4) 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
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.
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);