Updating a file using java API
7 answers
Once you've setup your development environment following the instructions in https://jazz.net/wiki/bin/view/Main/RTCSDK20_DevelopmentEnvironmentSetup, there are snippets that show how to deliver.
If you take a look the com.ibm.team.filesystem.client.tests.workload project in the SDK, you'll see several tasks and checkin.deliver.add files.
Jean-Michel
If you take a look the com.ibm.team.filesystem.client.tests.workload project in the SDK, you'll see several tasks and checkin.deliver.add files.
Jean-Michel
List components = workspace.getComponents();
for (Iterator a = components.iterator(); a.hasNext();) {
IComponentHandle component = (IComponentHandle) a.next();
IComponent comp = (IComponent)repo.itemManager().fetchCompleteItem(component, IItemManager.DEFAULT, MONITOR);
// The root folder is created when the component is created.
// add a folder called 'project' to the workspace
IChangeSetHandle cs1 = workspace.createChangeSet(component, MONITOR);
IFolder rootFolder = (IFolder) workspace.configuration(component).rootFolderHandle(MONITOR);
// Find file handle
IConfiguration iconfig = (IConfiguration) workspace.configuration(component);
IFolderHandle root = iconfig.rootFolderHandle(MONITOR);
IVersionableHandle filePathHandle = iconfig.resolvePath(root,nameSegments, MONITOR);
// Check if file found
if (filePathHandle == null) {
throw new TeamRepositoryException("Could not find file " + nameSegments);
}
// Fetch file complete item
IVersionable filePath = (IVersionable) iconfig.fetchCompleteItem(filePathHandle, MONITOR);
if (filePath.hasFullState()) {
file = (IFileItem) filePath.getFullState();
} else {
throw new TeamRepositoryException("Could not find file " + nameSegments + " item");
}
// Get file content
IFileContent content = file.getContent();
contentManager.retrieveContent(file, content, outputStream, MONITOR);
file = (IFileItem) file.getWorkingCopy();
VersionedContentManagerByteArrayInputStreamPovider jack =
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes());
/* IContentService contentService = getService(IContentService.class);
byte[] notesTextBytes;
notesTextBytes = notes.getBytes(CONTENT_ENCODING_UTF8);
ByteArrayInputStream inputStream = new ByteArrayInputStream(
notesTextBytes);
contentService.storeContent(IContent.CONTENT_TYPE_TEXT,
CONTENT_ENCODING_UTF8, inputStream, notesTextBytes.length);
*/
IFileContent storedContent = contentManager.storeContent(content.getCharacterEncoding(),content.getLineDelimiter(),
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes()), content.getHash(), MONITOR);
file.setContent(storedContent);
file.setFileTimestamp(new Date());
workspace.commit(cs1,Collections.singletonList(workspace.configurationOpFactory().save(file)), MONITOR);
// deliver the changes to the stream
IChangeHistorySyncReport sync =
workspace.compareTo(stream, WorkspaceComparisonFlags.CHANGE_SET_COMPARISON_ONLY,Collections.EMPTY_LIST, MONITOR);
workspace.deliver(stream, sync, Collections.EMPTY_LIST,
sync.outgoingChangeSets(component), MONITOR);
MONITOR.subTask("Created changes and delivered to " + stream.getName());
}
for (Iterator a = components.iterator(); a.hasNext();) {
IComponentHandle component = (IComponentHandle) a.next();
IComponent comp = (IComponent)repo.itemManager().fetchCompleteItem(component, IItemManager.DEFAULT, MONITOR);
// The root folder is created when the component is created.
// add a folder called 'project' to the workspace
IChangeSetHandle cs1 = workspace.createChangeSet(component, MONITOR);
IFolder rootFolder = (IFolder) workspace.configuration(component).rootFolderHandle(MONITOR);
// Find file handle
IConfiguration iconfig = (IConfiguration) workspace.configuration(component);
IFolderHandle root = iconfig.rootFolderHandle(MONITOR);
IVersionableHandle filePathHandle = iconfig.resolvePath(root,nameSegments, MONITOR);
// Check if file found
if (filePathHandle == null) {
throw new TeamRepositoryException("Could not find file " + nameSegments);
}
// Fetch file complete item
IVersionable filePath = (IVersionable) iconfig.fetchCompleteItem(filePathHandle, MONITOR);
if (filePath.hasFullState()) {
file = (IFileItem) filePath.getFullState();
} else {
throw new TeamRepositoryException("Could not find file " + nameSegments + " item");
}
// Get file content
IFileContent content = file.getContent();
contentManager.retrieveContent(file, content, outputStream, MONITOR);
file = (IFileItem) file.getWorkingCopy();
VersionedContentManagerByteArrayInputStreamPovider jack =
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes());
/* IContentService contentService = getService(IContentService.class);
byte[] notesTextBytes;
notesTextBytes = notes.getBytes(CONTENT_ENCODING_UTF8);
ByteArrayInputStream inputStream = new ByteArrayInputStream(
notesTextBytes);
contentService.storeContent(IContent.CONTENT_TYPE_TEXT,
CONTENT_ENCODING_UTF8, inputStream, notesTextBytes.length);
*/
IFileContent storedContent = contentManager.storeContent(content.getCharacterEncoding(),content.getLineDelimiter(),
new VersionedContentManagerByteArrayInputStreamPovider("Update file".getBytes()), content.getHash(), MONITOR);
file.setContent(storedContent);
file.setFileTimestamp(new Date());
workspace.commit(cs1,Collections.singletonList(workspace.configurationOpFactory().save(file)), MONITOR);
// deliver the changes to the stream
IChangeHistorySyncReport sync =
workspace.compareTo(stream, WorkspaceComparisonFlags.CHANGE_SET_COMPARISON_ONLY,Collections.EMPTY_LIST, MONITOR);
workspace.deliver(stream, sync, Collections.EMPTY_LIST,
sync.outgoingChangeSets(component), MONITOR);
MONITOR.subTask("Created changes and delivered to " + stream.getName());
}