Trying to get a collection of IFileItems from a known workspace and a known sandbox
Trying to use the following sort of code on all the files in a workspace, struggling to get hold of all the files in a workspace as a collection of IFileItems such that I can add tons of custom properties. Probably missing something obvious, help appreciated!! I have tried using scm for my task, but it takes about a second to add each property, which means my migrations take for ever, adding a tiny amount of data should not take a second a throw, hence using the java-api route.
IFileItem fileWorkingCopy = (IFileItem) aFile.getWorkingCopy(); fileWorkingCopy.setContent(storedzipContent); fileWorkingCopy.setUserProperty("MyProperty", "MyValue"); fWorkspace.commit(fChangeSet, Collections .singletonList(fWorkspace.configurationOpFactory() .save(fileWorkingCopy)), fMonitor);
Accepted answer
http://thescmlounge.blogspot.co.uk/2013/10/committing-content-to-rtc-scm-with-sdk.html
and
https://rsjazz.wordpress.com/2013/10/15/extracting-an-archive-into-jazz-scm-using-the-plain-java-client-libraries/
and
https://rsjazz.wordpress.com/2013/09/30/delivering-change-sets-and-baselines-to-a-stream-using-the-plain-java-client-libraries/
and the related links if needed.
Comments
Thanks Ralph. I think this is the way forward. I have written some code that I think should do the trick, will test and report back if I have any unexpected issues. Note that I did a search on all this and found your rsjazz links, but this scmlounge is blocked by the company firewall, because it thinks its a non work related blog I guess. Shame as it has the best example for my problem ;-)
Cheers Richard
3 other answers
I have had a chance to write the code, but am some how fundamentally misunderstanding something or have made a daft error I cannot see. The following code runs through, but no changes to the metadata are checked in to the workspace, stepped through the code and the right things are being extracted and passed to the set user properties code and nothing gives me an error, probably need to connect to something or disconnect to something to concrete the change. Anyway here is the relevant code, can someone tell me what is wrong with it. Note I am using global variables and a recursive procedure, then applying all the changes to the workspace at the same time using the save operations "ops" global variable filled out in the recursive procedure. Any pointers appreciated probably some daft mistake.
Note I expect my error is in this bit of code: -
v.setUserProperty(metaDataFieldsToExtract[i], metaDataStr);
// Save the change into a change set
ISaveOp saveOp = opFactory.save(file);
ops.add(saveOp);
Here comes the code listing : -
List<IWorkspaceHandle> workspaces = findConnectionByName(teamRepository,tempWorkSpace, IWorkspaceSearchCriteria.WORKSPACES, monitor);
IWorkspaceHandle firstFound = null;
IWorkspaceConnection connection = null;
if (!workspaces.isEmpty()) {
firstFound = workspaces.get(0);
connection = wm.getWorkspaceConnection(firstFound, monitor);
}
//If we find our workspace then we need to get our component from it - but component can be in many workspaces, but only one owner
//things could go horribly wrong here, can't easily search for components on workspace
//MAKING SWEEPING ASSUMPTION DANGER OF BITING ME IN ASS!
if(connection != null) {
IComponentHandle mycomp;
IComponentSearchCriteria fred = IComponentSearchCriteria.FACTORY.newInstance();
fred.setExactName(componentAndEcProjName);
List<IComponentHandle> comps = wm.findComponents(fred, 20, monitor);
mycomp = comps.get(0);
if(myTeamArea != null) {
wm.setComponentOwnerAndVisibility(mycomp, myTeamArea, IReadScope.FACTORY.createTeamAreaPrivateScope(), monitor);
}
else {
wm.setComponentOwnerAndVisibility(mycomp, projectArea, IReadScope.FACTORY.createProcessAreaScope(), monitor);
}
IConfiguration compConfig = connection.configuration(mycomp);
IFileContentManager contentManager = FileSystemCore.getContentManager(teamRepository);
IConfigurationOpFactory opFactory = connection.configurationOpFactory();
ops = new ArrayList<IWorkspaceConnection.IConfigurationOp>();
// Fetch the items at the root of each component. We do this to initialize our
// queue of stuff to download.
Map<String, IVersionableHandle> handles = compConfig.childEntriesForRoot(null);
List<IVersionable> items = compConfig.fetchCompleteItems(new ArrayList<IVersionableHandle>(handles.values()), null);
//Need to get childEntries from handles map too as does not seem to recurse down, may need recursive procedure - bit of a pain!!
getMetaDataRecurse(contentManager, compConfig, rootFolder + "/", items, opFactory);
// create a change set for our metadata changes
IComponent richardsComponent = findComponent(teamRepository, connection, componentAndEcProjName);
IChangeSetHandle cs = getOrCreateChangeSetInComponent(connection,richardsComponent);
System.out.println("Committing meta data to workspace");
connection.commit(cs, ops, null);
connection.closeChangeSets(Collections.singletonList(cs), null);
SCMPlatform.getWorkspaceManager(teamRepository).setComment(cs, "Added Dimensions metadata", null);
}
private static void getMetaDataRecurse(IFileContentManager contentManager, IConfiguration compConfig, String path,
List<IVersionable> toLoad, IConfigurationOpFactory opFactory)
throws FileNotFoundException, TeamRepositoryException, IOException {
String[] metaDataFieldsToExtract = {"file-version", "item-status", "project", "item-spec", "item-uid", "project-uid"};
Pattern[] MetaFieldPatterns = new Pattern[metaDataFieldsToExtract.length];
for(int i=0;i<metaDataFieldsToExtract.length;i++ ) {
MetaFieldPatterns[i] = Pattern.compile("^" + metaDataFieldsToExtract[i] + "=(.*)",Pattern.MULTILINE );
}
for (IVersionable v : toLoad) {
if (v == null) {
// The versionable will be null if we don't have permission to read it
continue;
}
if (v instanceof IFolder) {
// Write the directory
String dirPath = path + v.getName() + "/";
@SuppressWarnings("unchecked")
Map<String, IVersionableHandle> children = compConfig.childEntries((IFolderHandle)v, null);
@SuppressWarnings("unchecked")
List<IVersionable> completeChildren = compConfig.fetchCompleteItems(new ArrayList<IVersionableHandle>(children.values()), null);
getMetaDataRecurse(contentManager, compConfig, dirPath, completeChildren, opFactory);
}
else if (v instanceof IFileItem) {
// Get the file contents and write them into the directory
IFileItem file = (IFileItem) v;
file = (IFileItem) file.getWorkingCopy();
//need to grab metadata at this point can do this by getting the name then runniing dmmetadata then adding properties from here
String FullFile= path + v.getName();
System.out.println(FullFile);
String dmMetaCommand = dimensionsLocation + "\\dmmeta.exe get \"" + FullFile + "\" --file c:\\temp\\output.txt";
execConsoleCommand(dmMetaCommand);
FileInputStream readTempFile = new FileInputStream(new File("c:\\temp\\output.txt"));
String metaDataFieldsToProcess = IOUtils.toString(readTempFile);
for(int i=0;i<metaDataFieldsToExtract.length;i++ ) {
Matcher findMetaDataHex = MetaFieldPatterns[i].matcher(metaDataFieldsToProcess);
if(findMetaDataHex.find()) {
String metaDataStr = findMetaDataHex.group(1).toString();
if(Pattern.matches("^[A-F0-9]+$", metaDataStr) && metaDataStr.length() % 2==0 && metaDataStr.length()>4) {
metaDataStr = hexToASCII(metaDataStr);
}
v.setUserProperty(metaDataFieldsToExtract[i], metaDataStr);
}
}
// Save the change into a change set
ISaveOp saveOp = opFactory.save(file);
ops.add(saveOp);
}
else {
// The item could be a symlink or an even more exotic type of versionable. Skip it.
System.err.println("Cannot load " + v.getName() + " of type " + v.getClass().getSimpleName());
}
}
}
Comments
Surya Tripathi
May 01 '15, 5:58 p.m.Are you looking for code that you can use to get a list of file items?