0

如何使用 javascript 将类别应用于 Alfresco 中的文档?

像这样尝试:

document.properties["cm:categories"] = "testCat";

更新:

像这样我可以获得“根类别”但不能获得“子类别”:

var rootCats =  classification.getRootCategories("cm:generalclassifiable");
document. properties["cm:categories"] = rootCats[3];

这不起作用:

document. properties["cm:categories"] = rootCats[3][1];
4

3 回答 3

2

我发现了,如果有人需要它,这就是它的完成方式

获取所有根类别 [audio, video, image, ...]

var rootCats =  classification.getRootCategories("cm:generalclassifiable");

从所需的根类别中获取子类别 -> 从“视频”[恐怖、喜剧、...] 中获取子类别

var singleCat = rootCats[1].subCategories; 

将类别应用于文档 -> 将“恐怖”应用于文档

doc. properties["cm:categories"] = singleCat[0]; //app

保存

doc.save();
于 2013-03-25T11:26:28.867 回答
1

首先确保文档具有可分类的方面(即具有 cm:generalClassifiable 方面)。

然后查找您要添加的类别,即:

更新:查找某个名称的类别类型节点的示例查询:

例如,像这样搜索您的类别,您也可以添加一个 PATH 子句以确保您在正确的层次结构中。

var nodes = var categories= search.luceneSearch('+TYPE:"http://www.alfresco.org/model/content/1.0}category" +@name:testCat');

然后从返回的数组中取出元素...

获取节点的现有类别:

categories= document.properties["cm:categories"];

将新类别推送到数组中:

categories.push(categoryToAdd);

将此分配给文档:

document.properties["cm:categories"] = categories;

document.save();

于 2013-03-21T13:45:07.297 回答
0

We had a similar requirement, and although slightly different to OP question, it is close enough to warrant inclusion here we feel.

We occasionally have a need to bulk apply categories. ie:new business acquired, migrate all their docs in, add categories to match existing content.

Adding categories to large numbers of documents is tedious in the interface. It needs to enable the aspect (classifiable), then edit properties to select the categories required. For every document individually. Worse, the add aspect isn't available for documents from the explorer view in share, so entering the preview is required. For large docs, entering the preview is itself a few more seconds delay.

So we built a simple script that applies the categories of the parent folder to everything placed under it. Then we only need to set the categories of that folder and start moving content through it (ie:move content in to obtain the category/ies, then move back to where it came, but now all have aspect and category/ies applied).

Actually we have 2 at the moment:

  1. The first folder/rule overrides whatever categories the objects had and replaces them with just those from the parent folder - we name this folder BulkCategorise
  2. The 2nd is cumulative, so it keeps existing categories and only adds those from the parent folder, which we name AccumulateCategories

We will create a third to selectively remove them when time permits.

A couple of notes:

  • adding the classifiable aspect is not actually required to be done manually or in code, it "Just Works" - at least in 5.1.
  • this may not be the best way to do this
  • accumulating a category that is already applied doesn't appear to cause any issues, they appear to coalesce/merge automatically

To use these scripts, create 2 folders and apply a rule to each to run the associated script.

The 1st script completely replaces the categories of the object with those of the parent folder and is trivially simple [Set the @name and SITE to match your installation]:

var inheritFromDir = search.luceneSearch('TYPE:"http://www.alfresco.org/model/content/1.0}folder" +@name:BulkCategorise +SITE:YOUR_SITE_NAME');
// We need to deref the array of noderefs result using inheritFromDir[0]
document.properties["cm:categories"] = inheritFromDir[0].properties["cm:categories"];
document.save();

The 2nd script is a bit more involved - we left the logging code in (commented out) for an example to see how to do that, because it took some time to figure out also (and is an extremely useful feature). As before update the @name and SITE on the first line to match your system.

var inheritFromDir = search.luceneSearch('TYPE:"http://www.alfresco.org/model/content/1.0}folder" +@name:AccumulateCategories +SITE:YOUR_SITE_NAME');
// We need to deref the single array result using inheritFromDir[0] to get categories from topmost parent (folder) node
var parentCatArray = inheritFromDir[0].properties["cm:categories"];
//and any existing categories of the doc, or null if none exist
var thisCatArray = document.properties["cm:categories"];
var thisLen = 0;

if (thisCatArray != null) {
   thisLen = thisCatArray.length;
} else {
   thisCatArray = new Array();
}

// Some logging to find out what  is going on...
//var logFile = space.childByNamePath("log.txt");

// If the log file does not already exist, create it in current folder.
//if (logFile == null) {
//    logFile = space.createFile("log.txt");
//}

//if (logFile != null) {
//    logFile.content += new Date().toGMTString() + "\tRun started. Length of existing array is: " + thisLen + "\r\n";
//    logFile.content += new Date().toGMTString() + "\tFound parent node categories:\r\n";
      for (var i=0; i < parentCatArray.length; i++)
      {
         thisCatArray[thisLen]=parentCatArray[i];
         thisLen += 1;
      }
//}

// Push the new array of categories to the document
thisCatArray.push(document);
// apply and save the doc
document.properties["cm:categories"] = thisCatArray;
document.save();

Et Voila! In a similar way we have implemented BulkTag and BulkAccumulateTags. Now adding arbitrary categories is as simple as applying them to the folder and moving the content through with drag and drop.

于 2017-04-06T02:16:21.567 回答