Resolve path of IVersionableHandle in RTC - jazz

How to get path of file which isn't in newest version but is a part of previous changelist in RTC scm.
All I could achieve so far is this:
IFileItemHandle fileItemHandle = (IFileItemHandle) IFileItem.ITEM_TYPE.createItemHandle(change.afterState().getItemId(), change.afterState().getStateId());
file = versionableManager.fetchCompleteState(fileItemHandle, monitor);
if (file instanceof IFolder) {
IFolder folder = (IFolder) file;
relativePath = getFilePath(file, workspaceConnection.configuration(changeSet.getComponent()), monitor);
fileName = folder.getName();
} else {
relativePath = getFilePath(file, workspaceConnection.configuration(changeSet.getComponent()), monitor);
fileName = ((FileItem) file).getName();
}
Where getFilePath is:
private String getFilePath(IVersionableHandle folder, IConfiguration config, IProgressMonitor monitor, Boolean searchInHistory) throws TeamRepositoryException {
List lst = new ArrayList<IVersionableHandle>(), ancestors;
lst.add(folder);
if (searchInHistory) {
ancestors = config.determineAncestorsInHistory(lst, monitor);
} else {
ancestors = config.locateAncestors(lst, monitor);
}
return getFullPath(ancestors);
}
private String getFullPath(List ancestor) throws TeamRepositoryException {
String directoryPath = "";
for (Object ancestorObj : ancestor) {
IAncestorReport ancestorImpl = (IAncestorReport) ancestorObj;
for (Object nameItemPairObj : ancestorImpl.getNameItemPairs()) {
INameItemPair nameItemPair = (INameItemPair) nameItemPairObj;
String pathName = nameItemPair.getName();
if (pathName != null && !pathName.equals("")) {
directoryPath = directoryPath + "\\" + pathName;
}
}
}
return directoryPath;
}
Unfortunately it doesn't work perfectly. If filename is changed in following changelists like on this example:
Changelist 1:
add file: src/newFile.java
Changelist 2:
modify file: src/newFile.java
Changelist 3:
rename file: src/newFile.java -> src/newFile_rename.java
The relative path resolved in first changelist would be:
src/newFile_rename.java
instead of
src/newFile.java
How to make it works good?

While the javadoc for IConfiguration.determineAncestorsInHistory doesn't specify, the server side equivalent IScmService.configurationDetermineAncestorsInHistory (which is what ends up being called) says this in the javadoc:
* #param versionableItemHandles
* a list of versionable items; only the item ids are needed;
* must not be <code>null</code>
Basically, the determineAncestorsInHistory is not looking at the state id on the file handles, it only looks at the item id.
The particular state of the file that will be considered is determined by the configuration. This is mostly because while the state of the file in the changeset will tell you the name of that file, it does not tell you the name of the parent folder. That will depend on the state of the folder that is present in the workspace at a particular time and could be different for different workspaces
You basically need to get an IConfiguration that represents your workspace at the time the change set was accepted/created.
The way I see to do this is to get the IWorkspaceConnection.changeHistory(component) (this is actually defined on IFlowNodeConnection). You will need to walk back through the histories by calling IChangeHistory.previousHistory(monitor) until you find one that contains your changeset in IChangeHistory.recent(monitor).
Once you find the appropriate IChangeHistory use IChangeHistory.configuration() for the determineAncestorsInHistory call.
Note that this configuration represents the state of the workspace at the end of that particular IChangeHistory, so to be completely accurate you would need to inspect the changesets that occur after your change in the IChangeHistory.recent to see if any of them modified your file name (or also any of the containing folders' file names).
(Another alternative is to use the history configuration 1 previous to the history that contains your changeset, and then look at the effects of the changes that happen before your change)

Related

TypeSafe Config overriding values with Environment Variables works, from System Properties does not, when

I have a project that uses TypeSafe config. It loads multiple configuration files. One is loaded the easy way:
private val config = ConfigFactory.load( "test-parameters" )
This one seems to honor the foo = ${?ENV_VAR} override syntax, whether ENV_VAR is passed in via the environment, or via Java System Properties (-DXXX=).
The other config file(s) are loaded using .parseResources() followed by an eventual .resolve(). The override syntax works fine if the variables are specified as environment variables, but nothing happens when they are system properties. The code looks like this:
// we define a layered config, so that you can define values in one layer, and customize them in another
// if your environment includes sub-folders, config files are read from deepest folder first
// so that you can define values in the higher folders and override them in the more specific folder
// e.g. if the environment is "foo/bar/baz", then the order of loading config files is:
// * application.conf
// * conf/foo/bar/baz/config.conf
// * conf/foo/bar/config.conf
// * conf/foo/config.conf
// * default.conf
def loadConfig(env: String): Config = {
// do not allow the user to supply a relative path that tries to escape the conf directory hierarchy
if (env != null && env.matches("(?:^|/)\\.\\./"))
throw new Exception("Illegal Environment String")
var c = userConfig
if (env != null && env != "") {
val parts = env.split("/")
var n = parts.length
while (n > 0) {
c = c.withFallback(ConfigFactory.parseResources(environmentConfigFile(parts.take(n).mkString("/"))))
n -= 1
}
}
c.withFallback(defaultConfig).resolve()
}
def loadConfig: Config = loadConfig(Test.environment)
At first blush, it looks like a bug. .parseResources().resolve() and .load() behave slightly differently.
Or did I just miss something?
The problem is the lack of ConfigFactory.load(), which automatically merges in the system properties.
By changing the initialization, as follows, it works as intended:
var c = ConfigFactory.load().withFallback( userConfig )
YAY!

splitting xpo into multiple xpo files

Is there a tool that allows an xpo file to be split into multiple xpo files? The exact opposite of what CombineXPOs does.
The reason I'm asking is because I need to put multiple single xpo/models releases into source control and I want to avoid having to import them first every time as that's too time consuming.
After understanding I think a bit more of what you wanted. Here is a job that gives you pretty much most of the examples of how to do what you want.
Note the TODO's that need completed.
static void JobImportXPOs(Args _args)
{
SysVersionControlSystem vcs = versionControl.parmSysVersionControlSystem();
SysImportElements sysImportElements = new SysImportElements();
TmpAotImport tmpAotImport;
Filename fileName = #"C:\Temp\testXPO.xpo";
SysVersionControllable controllable;
TreeNode treeNode;
sysImportElements.newFile(fileName);
sysImportElements.parmAddToProject(false);
sysImportElements.parmImportAot(true);
tmpAotImport = sysImportElements.getTmpImportAot();
while select tmpAotImport
{
treeNode = TreeNode::findNode(tmpAotImport.TreeNodePath);
if (!treeNode)
{
// New object being added to AX
// TODO - Remember this object and add this to VCS system at the end for check-in
continue;
}
controllable = SysTreeNode::newTreeNode(treeNode);
if (!controllable)
{
error(strFmt("Error processing %1 (%2) from file %3", tmpAotImport.TreeNodeName, tmpAotImport.TreeNodePath, Filename));
continue;
}
if (vcs.allowCreate(controllable))
{
info(strFmt("Planning to add to VCS %1 for import", tmpAotImport.TreeNodePath));
// TODO - Remember to add this to VCS at the end of the import
}
else if (vcs.allowCheckOut(controllable))
{
info(strFmt("Checking out %1 for import", tmpAotImport.TreeNodePath));
// TODO - Remember to check this specific object back in at the end of the import
}
}
// Do the actual import
sysImportElements.import();
// TODO - Check in all of the objects we just handled
info("Done");
}

How should I get and set a folder's group and user permission settings via Core Service?

I can get strings representing group and user permissions for a given folder with the following.
Code
// assumes Core Service client "client"
var folderData = client.Read("tcm:5-26-2", new ReadOptions()) as FolderData;
var accessControlEntryDataArray =
folderData.AccessControlList.AccessControlEntries;
Console.WriteLine(folderData.Title);
foreach (var accessControlEntryData in accessControlEntryDataArray)
{
Console.WriteLine("{0} has {1}",
accessControlEntryData.Trustee.Title,
accessControlEntryData.AllowedPermissions.ToString());
}
Output
Some Folder
Everyone has Read
Editor has None
Chief Editor has None
Publication Manager has None
Interaction Manager has None
T2011-CB-R2\areyes has All
[scope] Editor 020 Create has Read, Write
T2011-CB-R2\local1 has Read, Write, Delete
[rights] Author - Content has None
Seems like the four possible values for `AllowedPermissions are:
None
Read
Read, Write
Read, Write, Delete
All
This works great for my use case to create a folder permissions report. I can .Replace() these to a familiar notation (e.g. rw-- or rwdl).
But is manipulating these string values the right approach to set permissions as well? I'd imagine I'd want objects or maybe enums instead. Could someone point me in the right direction?
Also I noticed I get some, but not all non-applicable groups set as None. I don't specifically need them here, but I'm curious at what determines whether those get returned--did I miss something in my code?
Rights and Permissions are enums, indeed. You can set using the method below. If you want to set multiple rights you should do something like "Rights.Read | Rights.Write"
Keep in mind that this method will return you object that you have to save \ update \ create after
public static OrganizationalItemData SetPermissionsOnOrganizationalItem(
OrganizationalItemData organizationalItem,
TrusteeData trustee,
Permissions allowedPermissions,
Permissions deniedPermissions = Permissions.None)
{
if (organizationalItem.AccessControlList == null)
{
organizationalItem.AccessControlList
= new AccessControlListData
{AccessControlEntries = new AccessControlEntryData[0]};
}
var entries = organizationalItem.AccessControlList
.AccessControlEntries.ToList();
// First check if this trustee already has some permissions
var entry = entries.SingleOrDefault(
ace => ace.Trustee.IdRef == trustee.Id);
if (entry != null)
{
// Remove this entry
entries.Remove(entry);
}
entries.Add(new AccessControlEntryData
{
AllowedPermissions = allowedPermissions,
DeniedPermissions = deniedPermissions,
Trustee = new LinkToTrusteeData { IdRef = trustee.Id }
});
organizationalItem.AccessControlList.AccessControlEntries
= entries.ToArray();
return organizationalItem;
}

How to rename a file in C#

Consider:
strPath= c:\images\gallery\add.gif
I need to rename this file from add.gif to thumb1.gid, and I should write one command method, whatever the file name. We need to
replace that name with this like below.
string strfilename = **"thumb"**
****Result thum.gif**
strPath= c:\images\gallery\thum.gif **
You have several problems, looking up the value in the XML file, and renaming the file.
To look up the number corresponding to Gallery2 or whatever, I would recommend having a look at Stack Overflow question How to implement a simple XPath lookup which explains how to look up nodes/values in an XML file.
To rename a file in .NET, use something like this:
using System.IO;
FileInfo fi = new FileInfo("c:\\images\\gallery\\add.gif");
if (fi.Exists)
{
fi.MoveTo("c:\\images\\gallery\\thumb3.gif");
}
Of course, you would use string variables instead of string literals for the paths.
That should give you enough information to piece it together and solve your particular lookup-rename problem.
I created a utility method to help encapsulate how to rename a file.
public class FileUtilities
{
public static void RenameFile(string oldFilenameWithPathWithExtension, string newFilenameWithoutPathWithExtension)
{
try
{
string directoryPath = Path.GetDirectoryName(oldFilenameWithPathWithExtension);
if (directoryPath == null)
{
throw new Exception($"Directory not found in given path value:{oldFilenameWithPathWithExtension}");
}
var newFilenameWithPath = Path.Combine(directoryPath, newFilenameWithoutPathWithExtension);
FileInfo fileInfo = new FileInfo(oldFilenameWithPathWithExtension);
fileInfo.MoveTo(newFilenameWithPath);
}
catch (Exception e)
{
//Boiler plate exception handling
Console.WriteLine(e);
throw;
}
}
}
I omitted several other file system checks that could optionally be done, but as #JoelCoehoorn pointed out in a comment on this page, the File System is Volatile, so wrapping it in a try-catch may be all that is necessary.
With that class in your library, now you can simply call:
var fullFilename = #"C:\images\gallery\add.gif";
var newFilename = "Thumb.gif";
FileHelper.RenameFile(fullFilename,newFilename);

How do I convert a file path to a URL in ASP.NET

Basically I have some code to check a specific directory to see if an image is there and if so I want to assign a URL to the image to an ImageControl.
if (System.IO.Directory.Exists(photosLocation))
{
string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg");
if (files.Length > 0)
{
// TODO: return the url of the first file found;
}
}
this is what i use:
private string MapURL(string path)
{
string appPath = Server.MapPath("/").ToLower();
return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(#"\", "/"));
}
As far as I know, there's no method to do what you want; at least not directly. I'd store the photosLocation as a path relative to the application; for example: "~/Images/". This way, you could use MapPath to get the physical location, and ResolveUrl to get the URL (with a bit of help from System.IO.Path):
string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation);
if (Directory.Exists(photosLocationPath))
{
string[] files = Directory.GetFiles(photosLocationPath, "*.jpg");
if (files.Length > 0)
{
string filenameRelative = photosLocation + Path.GetFilename(files[0])
return Page.ResolveUrl(filenameRelative);
}
}
The problem with all these answers is that they do not take virtual directories into account.
Consider:
Site named "tempuri.com/" rooted at c:\domains\site
virtual directory "~/files" at c:\data\files
virtual directory "~/files/vip" at c:\data\VIPcust\files
So:
Server.MapPath("~/files/vip/readme.txt")
= "c:\data\VIPcust\files\readme.txt"
But there is no way to do this:
MagicResolve("c:\data\VIPcust\files\readme.txt")
= "http://tempuri.com/files/vip/readme.txt"
because there is no way to get a complete list of virtual directories.
I've accepted Fredriks answer as it appears to solve the problem with the least amount of effort however the Request object doesn't appear to conatin the ResolveUrl method.
This can be accessed through the Page object or an Image control object:
myImage.ImageUrl = Page.ResolveUrl(photoURL);
myImage.ImageUrl = myImage.ResolveUrl(photoURL);
An alternative, if you are using a static class as I am, is to use the VirtualPathUtility:
myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);
This worked for me:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName";
Maybe this is not the best way, but it works.
// Here is your path
String p = photosLocation + "whatever.jpg";
// Here is the page address
String pa = Page.Request.Url.AbsoluteUri;
// Take the page name
String pn = Page.Request.Url.LocalPath;
// Here is the server address
String sa = pa.Replace(pn, "");
// Take the physical location of the page
String pl = Page.Request.PhysicalPath;
// Replace the backslash with slash in your path
pl = pl.Replace("\\", "/");
p = p.Replace("\\", "/");
// Root path
String rp = pl.Replace(pn, "");
// Take out same path
String final = p.Replace(rp, "");
// So your picture's address is
String path = sa + final;
Edit: Ok, somebody marked as not helpful. Some explanation: take the physical path of the current page, split it into two parts: server and directory (like c:\inetpub\whatever.com\whatever) and page name (like /Whatever.aspx). The image's physical path should contain the server's path, so "substract" them, leaving only the image's path relative to the server's (like: \design\picture.jpg). Replace the backslashes with slashes and append it to the server's url.
So far as I know there's no single function which does this (maybe you were looking for the inverse of MapPath?). I'd love to know if such a function exists. Until then, I would just take the filename(s) returned by GetFiles, remove the path, and prepend the URL root. This can be done generically.
The simple solution seems to be to have a temporary location within the website that you can access easily with URL and then you can move files to the physical location when you need to save them.
For get the left part of the URL:
?HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
"http://localhost:1714"
For get the application (web) name:
?HttpRuntime.AppDomainAppVirtualPath
"/"
With this, you are available to add your relative path after that obtaining the complete URL.
I think this should work. It might be off on the slashes. Not sure if they are needed or not.
string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];

Resources