如何从集合中删除内容并将其添加到另一个 [英] How to remove content from collection and add it to another

查看:148
本文介绍了如何从集合中删除内容并将其添加到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用smartform创建的供应商:

  ID标题
90医生A
102医生B
10医生C
26医生D
495医生E

我在CMS中有三个集合:

  ID集合标题
12 IM集合
43 UR集合
9 EC集合

以下代码检索集合的内容对我来说:

  ContentManager contentManager = new ContentManager(); 
ContentCollectionCriteria criteria = new ContentCollectionCriteria(ContentProperty.Id,EkEnumeration.OrderByDirection.Ascending);
criteria.AddFilter(Convert.ToInt64(ddlCollection.SelectedValue));
List< ContentData> contentList = contentManager.GetList(criteria);

我将包括以下变量:

  iPName(int)=提供者ID 
sColl(List< string>)=提供者应该进入的集合

如何为每个提供者编写一个递归函数,它将获取iPName,从提供者存在的任何集合中删除,并使用sCol

解决方案

由于您要修改集合您需要查看集合特定API
管理器类位于 Ektron.Cms.Framework.Organization 命名空间内。



基本思想是:


  1. 获取提供者与他们所在的每个集合之间的映射

  2. iPName ,获取此提供商当前所在的收藏列表

  3. 将列表从#2更改为 sColl (eg

  4. 任何当前不包含提供商但应添加的集合

  5. 当前包含提供商的任何集合,


  6. 这里有一些粗略的代码,让你开始(未测试)

      //这是你现有的代码,包装成一个函数
    List< ContentData> GetCollectionContent(long collectionID)
    {
    var contentManager = new ContentManager();
    var criteria = new ContentCollectionCriteria(ContentProperty.Id,EkEnumeration.OrderByDirection.Ascending);
    criteria.AddFilter(collectionID);
    return contentManager.GetList(criteria);
    }

    //从上面的步骤#1构建映射
    字典< ContentData,List< ContentCollectionData>> BuildCollectionMapping()
    {
    //获取系统中的所有集合(使用一个新的默认条件对象)
    var collectionManager = new CollectionManager();
    var allCollections = collectionManager.GetList(new CollectionCriteria());

    //构建映射,将内容项与其在
    中的每个集合相关联var mapping = new Dictionary< ContentData,List< ContentCollectionData>>();
    foreach(allCollections中的var集合)
    {
    var contentItems = GetCollectionContent(collection.Id);

    foreach(contentItems中的var contentItem)
    {
    if(!mapping.ContainsKey(contentItem))
    {
    mapping.Add(contentItem,new List< ContentCollectionData>());
    }

    mapping [contentItem] .Add(collection);
    }
    }

    返回映射;
    }

    //从上面的步骤#2-3,使用你定义的变量
    void Reconcile(long iPName,List< string> sColl)
    {
    var mapping = BuildCollectionMapping();

    if(mapping.Keys.Any(content => content.Id == iPName))
    {
    var collections = mapping.Single(kvp => kvp。 Key.Id == iPName).Value;
    var collectionTitles = collections.Select(c => c.Title);

    //这些是必须添加此内容项的集合的名称
    var toAdd = sColl.Except(collectionTitles);
    //这些是必须从中删除内容项的集合的名称
    var toDelete = collectionTitles.Except(sColl);
    }
    }

    我会留给你填写#4-5的细节。



    稍微一点,我想指出,Ektron中的ID应始终表示为 (您上面的示例 iPName int )。
    我们刚刚修改了我们的环境,使得内容ID现在在64位范围内生成(例如 53687091658 ),我们遇到了一些情况其中sloppy解析为 int 导致运行时错误。我提供的示例代码使用


    I have couple of providers created with a smartform:

    ID          Title
    90          Doctor A
    102         Doctor B
    10          Doctor C
    26          Doctor D
    495         Doctor E
    

    I have three collections in CMS:

    ID      Title of Collection
    12      IM Collection
    43      UR Collection
    9       EC Collection
    

    The following code retrieves the content for the collection which is working for me:

    ContentManager contentManager = new ContentManager();
    ContentCollectionCriteria criteria = new ContentCollectionCriteria(ContentProperty.Id, EkEnumeration.OrderByDirection.Ascending);
    criteria.AddFilter(Convert.ToInt64(ddlCollection.SelectedValue));
    List<ContentData> contentList = contentManager.GetList(criteria);
    

    I will be including the following variable:

    iPName (int) = the provider ID
    sColl (List<string>) = The collection(s) the provider should go in
    

    How can I code a recursive function for each provider, which will take the iPName, remove from any collection the provider exist and use the sCol to add the provider to the collection(s).

    解决方案

    Since you want to modify the collections, you'll want to take a look at the Collection specific APIs The manager class is found inside the Ektron.Cms.Framework.Organization namespace.

    The basic idea is:

    1. Get a mapping between providers and each collection they are in
    2. Given iPName, get a list of Collections this provider is currently in
    3. Compare the list from #2 to sColl (e.g. take the difference)
    4. Any collections that don't currently contain the provider but should, add it
    5. Any collections that do currently contain the provider but shouldn't, delete it

    Here's some rough code to get you started (untested)

    //this is your existing code, wrapped into a function
    List<ContentData> GetCollectionContent(long collectionID)
    {
        var contentManager = new ContentManager();
        var criteria = new ContentCollectionCriteria(ContentProperty.Id, EkEnumeration.OrderByDirection.Ascending);
        criteria.AddFilter(collectionID);
        return contentManager.GetList(criteria);
    }
    
    //builds the mapping from step #1 above
    Dictionary<ContentData, List<ContentCollectionData>> BuildCollectionMapping()
    {
        //get all the collections in the system (using a new, "default" criteria object)
        var collectionManager = new CollectionManager();
        var allCollections = collectionManager.GetList(new CollectionCriteria());
    
        //build the mapping, associate a content item with each collection it is in
        var mapping = new Dictionary<ContentData, List<ContentCollectionData>>();
        foreach (var collection in allCollections)
        {
            var contentItems = GetCollectionContent(collection.Id);
    
            foreach (var contentItem in contentItems)
            {
                if (!mapping.ContainsKey(contentItem))
                {
                    mapping.Add(contentItem, new List<ContentCollectionData>());
                }
    
                mapping[contentItem].Add(collection);
            }
        }
    
        return mapping;
    }
    
    //steps #2-3 from above, using the variables you defined
    void Reconcile(long iPName, List<string> sColl)
    {
        var mapping = BuildCollectionMapping();
    
        if (mapping.Keys.Any(content => content.Id == iPName))
        {
            var collections = mapping.Single(kvp => kvp.Key.Id == iPName).Value;
            var collectionTitles = collections.Select(c => c.Title);
    
            //these are the names of collections to which this content item must be added
            var toAdd = sColl.Except(collectionTitles);
            //these are the names of collections from which the content item must be deleted
            var toDelete = collectionTitles.Except(sColl);
        }
    }
    

    I'll leave it up to you to fill in the details of #4-5.

    As a slight aside, I want to point out that IDs in Ektron should always be represented as long (your example above iPName is int). We have just modified our environment such that content IDs are now being generated in the 64-bit range (e.g. 53687091658) and we've run into a few cases where sloppy parsing to int is resulting in runtime errors. The sample code I've provided uses long.

    这篇关于如何从集合中删除内容并将其添加到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆