我正在尝试通过 .NET API 连接到 INCA,以便导航 ASAP2 项目的文件夹结构。具体来说,我想获得一个代表我在下面突出显示的“目标”文件夹的对象。
这被证明是相当棘手的——API 提供了大量的类,其中包含名称“文件夹”: Folder
、Asap2ProjectFolder
和IncaFolder
. 那么为了检索“目标”文件夹,我需要知道什么?
我正在尝试通过 .NET API 连接到 INCA,以便导航 ASAP2 项目的文件夹结构。具体来说,我想获得一个代表我在下面突出显示的“目标”文件夹的对象。
这被证明是相当棘手的——API 提供了大量的类,其中包含名称“文件夹”: Folder
、Asap2ProjectFolder
和IncaFolder
. 那么为了检索“目标”文件夹,我需要知道什么?
您的第一个冲动可能是认为 Target 文件夹存在于长路径上,例如USER A\Demo\Demo03\Foo\Bar\Baz\Target
. 不幸的是,INCA 没有这样做的机制——相反,您必须检索路径的数据库对象部分中的最低项目,然后查询该项目以获取路径的数据集部分。
从数据库根目录,我们需要深入到 Demo03,它是一个Asap2Project
:
USER A\Demo\Demo03
从 Demo03 中,我们需要深入到 Target,它是一个Asap2ProjectFolder
:
Foo\Bar\Baz\Target
第一部分非常简单——我们只需几行代码就可以连接到 INCA 并获得 Demo03 Asap2Project:
Asap2ProjectFolder targetFolder = null;
Inca myIncaInstance = new Inca();
DataBase myDB = myIncaInstance.GetCurrentDataBase();
DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");
if ((tempItem != null) &&
(tempItem.IsAsap2Project()))
{
Asap2Project demoProject = (Asap2Project)tempItem;
// Next step: grab the "Target folder!"
}
现在,事情变得有点棘手。
如果我们试图获得DataSet
类似的 Demo03_1,我们可以使用类似demoProject.GetDataSetForName("Demo03\\Demo03_1")
. 问题是,此方法仅适用于 DataSet 类型的对象。所以我们不能用它来检索像Foo\Bar\Baz\Target
. 如果您稍微研究过 API,您可能已经尝试过这样的事情:
Asap2ProjectFolder tempProjFolder = demoProject.GetTopFolderNamed("Foo");
tempItem = tempProjFolder.GetDataBaseItem("Bar\\Baz\\Target"); // Won't work.
tempItem = tempProjFolder.GetSubFolder("Bar\\Baz\\Target"); // This won't either.
事实证明,我们需要更加足智多谋。让我们为和类创建GetItemInFolder()
扩展方法,因此这些类将具有与该类相同的项目检索灵活性:Asap2Project
Asap2ProjectFolder
DataBase
namespace IncaExtensions
{
public static class GetItemInFolderExtensions
{
/// <summary>
/// Gets a DataBaseItem from an Asap2Project in an arbitrary subfolder.
/// </summary>
/// <param name="project">The current Asap2Project</param>
/// <param name="itemName">The name of the item that we want to retrieve</param>
/// <param name="folderName">The path, delimited by backslashes ('\\')</param>
/// <returns>A DataBaseItem matching the request.
/// If no matching item is found, or if the path is invalid, return null.
/// </returns>
public static DataBaseItem GetItemInFolder(this Asap2Project project,
string itemName,
string folderName)
{
DataBaseItem returnItem = null;
if ((folderName != null) &&
(itemName != null))
{
string folderToken = PluckPathToken(ref folderName);
Asap2ProjectFolder subFolder =
project.GetTopFolderNamed(folderToken);
if (subFolder != null)
{
returnItem = subFolder.GetItemInFolder(itemName, folderName);
}
}
return returnItem;
}
/// <summary>
/// Recursive call that returns a DataBaseItem in the target path.
/// </summary>
/// <param name="folder">The Asap2ProjectFolder to drill down</param>
/// <param name="itemName">The name of the item that we want to retrieve</param>
/// <param name="folderName">The path, delimited by backslashes ('\\')</param>
/// <returns>A DataBaseItem matching the request.
/// If no matching item is found, or if the path is invalid, return null.
/// </returns>
public static DataBaseItem GetItemInFolder(this Asap2ProjectFolder folder,
string itemName,
string folderName)
{
DataBaseItem returnItem = null;
if ((folderName != null) &&
(itemName != null))
{
string folderToken = PluckPathToken(ref folderName);
Asap2ProjectFolder subFolder =
(Asap2ProjectFolder)folder.GetSubFolder(folderToken);
if (subFolder != null)
{
returnItem = subFolder.GetItemInFolder(itemName, folderName);
}
}
else
{
returnItem = folder.GetDataBaseItem(itemName);
}
return returnItem;
}
/// <summary>
/// Removes a backslash-delimited token from a string and shortens the
/// input string.
/// </summary>
/// <param name="folderName">Backslash-delimited path. This will be
/// shortened each time a token is removed.</param>
/// <returns>The latest path token</returns>
static string PluckPathToken(ref string folderName)
{
int slashIdx = folderName.IndexOf('\\');
// If folderName has path tokens, extract the first token and
// shorten folderName accordingly.
string folderToken = (slashIdx > -1) ?
folderName.Substring(0, slashIdx) : folderName;
folderName = (slashIdx > -1) ?
folderName.Substring(slashIdx + 1) : null;
return folderToken;
}
}
}
Asap2Project
这使得从or中抓取物品变得非常容易Asap2ProjectFolder
。要获取 Target 文件夹,我们只需要以下调用:
using IncaExtensions;
...
Asap2ProjectFolder targetFolder = null;
Inca myIncaInstance = new Inca();
DataBase myDB = myIncaInstance.GetCurrentDataBase();
DataBaseItem tempItem = myDB.GetItemInFolder("Demo03", "USER A\\Demo");
if ((tempItem != null) &&
(tempItem.IsAsap2Project()))
{
Asap2Project demoProject = (Asap2Project)tempItem;
targetFolder = (Asap2ProjectFolder)demoProject.GetItemInFolder("Target", "Foo\\Bar\\Baz");
}