无法理解VBScript代码集

时间:2011-10-31 11:09:27

标签: c# vbscript tridion

我需要将一些VBScript代码转换为C#2.0,下面是VBScript代码,需要转换为c#。

' Component Template titles handled by the summary templates
FeaturedCT = "Featured Summary"
SummaryCT = "Summary"

' Set the looping variables on the first component presentation
If IsFirstComponent = 0 Then
    IsFirstComponent = 1

    ' Start out left handed by default
    IsLeftHand = True

    matchCount = 0
    Dim components()
    ReDim components(Page.ComponentPresentations.Count)

    ' Build a list of all the matching component presentations to be rendered out as summaries
    For Each objCP In Page.ComponentPresentations
        ' Is this a Summary component template?
        If objCP.ComponentTemplateTitle = SummaryCT Or objCP.ComponentTemplateTitle = FeaturedCT Then
            ' Check if this object should be included based on its approval status
            If staging Or getFieldValue(objCP.Component.MetadataFields("ApprovalStatus"), "") = "" Then
                If getFieldValue(objCP.Component.MetadataFields("EndDate"), "") <> "" Then
                    If getDateSerial(objCP.Component.MetadataFields("EndDate").Value(1), False) > getDateSerial(Now, False) Then
                        Set components(matchCount) = objCP
                        matchCount = matchCount + 1
                    End If
                Else
                    Set components(matchCount) = objCP
                    matchCount = matchCount + 1
                End If
            End If
        End If
    Next

    ' Resize the array to the amount of matches
    ReDim Preserve components(matchCount)
End If

For i = 0 to UBound(components) - 1
    ' Determine which component to render from the pre-selected array
    If components(i).ComponentID = Component.ID And components(i).OrdinalPosition = ComponentPresentation.OrdinalPosition Then
        ' Featured summary is always left aligned and causes all other items to be right-aligned
        If ComponentPresentation.ComponentTemplateTitle = FeaturedCT Then
            HasFeaturedSummary = 1
            IsLeftHand = True           
        End If

        Call RenderEntry(components, i)

        ' If a featured summary was previously present all following items are right-aligned
        If HasFeaturedSummary = 1 Then
            IsLeftHand = False
        Else
            ' Update the left-handed status
            UpdatePositioning
        End If

        If i = UBound(Components) - 1 Then
            WriteOut "<div class=""clearBoth""></div>"
        End If
    End If
Next

以下是我在c#中尝试的代码。

  public string RenderSummaryCT()
    {
        string FeaturedCT = "Featured Summary CT";
        string SummaryCT = "Summary CT";
        int IsFirstComponent = 0;
        string result = string.Empty;
        int hasFeaturedSummary = 0;
        Component comp = null;
        bool IsLeftHand = false;
        StringBuilder sbOutput = new StringBuilder();

        List<tc.ComponentPresentation> cmp = new List<tc.ComponentPresentation>();

        if (IsFirstComponent == 0)
        {
            IsFirstComponent = 1;
            IsLeftHand = true;

            //m_Logger.Info("CMP Array-" + cmp.Count);
            foreach (tc.ComponentPresentation objCMP in m_Page.ComponentPresentations)
            {
                if ((objCMP.ComponentTemplate.Title == SummaryCT) || (objCMP.ComponentTemplate.Title == FeaturedCT))
                {
                    comp = objCMP.Component;
                    string approvalStatus = string.Empty;
                    string endDate = string.Empty;

                    if (comp.Metadata != null)
                    {
                        ItemFields compItemfields = new ItemFields(comp.Metadata, comp.MetadataSchema);
                        approvalStatus = th.GetSingleStringValue("ApprovalStatus", compItemfields);
                        endDate = th.GetSingleStringValue("EndDate", compItemfields);
                    }
                    if ((baseutility.GetStagingConstantValue(m_Engine, m_Package)) || (string.IsNullOrEmpty(approvalStatus)))
                    {
                        if (!string.IsNullOrEmpty(endDate))
                        {
                            DateTime eDate = Convert.ToDateTime(baseutility.GetDateSerial(Convert.ToDateTime(endDate), false));
                            DateTime currentDate = Convert.ToDateTime(baseutility.GetDateSerial(DateTime.Now, false));
                            if ((eDate) > (currentDate))
                            {
                                cmp.Add(objCMP);
                            }
                        }
                        else
                        {
                            cmp.Add(objCMP);
                            //m_Logger.Info("2. Adding cmp: " + maxCount.ToString() + "-- " + cmp[maxCount].Component.Title);                              
                        }
                    }
                }
            }
        }
        bool lastFlag = false;
        int cnt = 0;
        int totalLength = cmp.Count;

        foreach (tc.ComponentPresentation cm in cmp)
        {
            m_Logger.Info(cm.Component.Id + "--" + m_Component.Id);

            m_Logger.Info(cnt + "--" + totalLength);

            if (cm.Component.Id == m_Component.Id)
            {
                if (cm.ComponentTemplate.Title == FeaturedCT)
                {
                    m_Logger.Info("inside featured CT");
                    hasFeaturedSummary = 1;
                    IsLeftHand = true;
                }
                sbOutput.Append("" + SummaryBase.SummaryHelper.RenderEntry(cmp, cnt, IsLeftHand, lastFlag));

                m_Logger.Info("IsLeftHand -: " + IsLeftHand.ToString());

                if (hasFeaturedSummary == 1)
                {
                    IsLeftHand = false;
                }
                else
                {
                    //sbOutput.Append("" + SummaryBase.SummaryHelper.UpdatePositioning(IsLeftHand));
                    if (IsLeftHand)
                    {
                        IsLeftHand = false;
                    }
                    else
                    {
                        //m_Logger.Info("UpdatePositioning");
                        sbOutput.Append("<div class=\"clearBoth\"></div>");
                        IsLeftHand = true;
                    }
                }

                m_Logger.Info("CMP Title -: " + cm.Component.Title);

                cnt = cnt + 1;
                if (totalLength == cnt)
                {
                    m_Logger.Info("cnt-" + cnt);
                    lastFlag = true;
                }
                if (lastFlag)
                {
                    sbOutput.Append(" <div class=\"clearBoth\"></div>");
                }
            }
        }

        return sbOutput.ToString();
    }

我确定有问题,请您使用上面的VBScript逻辑建议合适的逻辑。

2 个答案:

答案 0 :(得分:3)

除了“将VBScript转换为.NET”之外,还有更多内容,您还可以从基于COM的Tridion对象模型转移到.NET TOM,它的工作方式完全不同。

我在这里的建议是对它进行非常分析的观点:

  1. 分解你的代码并在纸上写下逻辑
  2. 尝试理解为什么需要发生以及是否仍然如此
  3. 这样可以让您以后更轻松地重写代码。

    我当然会借此机会重写部分内容以便更有意义 - 比如将IsFirstComponent变量转换为布尔值而不是int,并从c#代码中删除所有HTML(不是说你有很多内容) ,但是下一个处理此代码的人可能想知道HTML不是由服务器上某处“隐藏”的程序集创建的。

    我稍后会尝试看一下这个VBScript,但我认为在这一点上理解代码所做的事情可能更重要。

答案 1 :(得分:1)

老实说,我看不出ordinalPosition正在做什么。我的猜测是,当您在每个组件表示上调用render时,Component和ComponentPresentation的当前值会更新。 (在旧式模板中,组件和页面共享相同的渲染堆栈。)但我猜这里,我仍然不确定预期的逻辑是什么。 @Nuno - 在从“从页面中提取组件”中的Page.ComponentPresentations填充后,通常不会更新组件集合,因此我怀疑黑魔法是否会如此轻松地移植。

所以Manu - 您需要检查此功能的功能规格是什么。这可能只是为了测试列表中是否有任何特色摘要组件演示文稿,如果是,请将其放在左侧,其余部分放在右侧。 (如果是这样,那么到达那里是一个相当曲折的逻辑。)在实践中,您可能必须查看使用此模板的现有页面,查看它们如何运行,并承诺仅支持端口中的那些页面。 / p>

(您可能还想查看该工作流代码的用途。也许在您的实时目标上配置最低审批状态可以删除此代码。)

我全心全意地赞同Nuno的原始建议,借此机会重写一些事情。即使你有时间限制,除非你理解了问题,然后实现代码,否则你永远不会成功地获得正确的逻辑。说真的 - 拥有像这样的不可维护的代码只会在你遇到困难的最后期限时受到更多伤害。

处理不同类型的组件演示是Tridion页面模板中的常见要求。复合模板中的模板成语非常不同,但如果正确完成,它比这里显示的方法更容易理解。当您有机会以惯用方式编写代码而不是逐行端口时,有一个base class available at the Tridion practice project可能会对您有所帮助。