Using workflow to create editions

Build 1501 on 14/Nov/2017  This topic last edited on: 24/Oct/2016, at 18:49

You can use a workflow to automatically create editions in advance, with different strategies for different titles.

Note: such workflows will not copy the additional attributes of the edition, as editorialRest.CallServerEditionCopyFrom currently is unable to do it.

An example of such workflow is:

<codeWorkflow

  xmlns="http://www.teradp.com/schemas/GN4/1/WFRes.xsd">

 

  <References>

    <!-- Add here references to additional assemblies (DLL) -->

    <!-- <Reference>XXXX</Reference>-->

    <Reference>.\GNClient.dll</Reference>

  </References>

 

  <Imports>

    <!-- Add here additional namespaces to import -->

    <!-- <Import>XXXX</Import> -->

  </Imports>

 

  <Members>

    <![CDATA[

 

        Dim configname As String = "EPASAConfig"

 

        Function GetConfigFile(ByVal configname As String) As XDocument

            Try

                'Search for config data

                Dim searchConfigAct As Search = New Search(Context) With {.Name = "search config"}

                searchConfigAct.XQuery = String.Format("gn4:config[@name='{0}']", configname)

                Dim searchConfigRes As SearchResult = searchConfigAct.Do()

 

                If searchConfigRes.IdsCount = 0 Then

                    LogWarning(String.Format("Cannot find the configuration '{0}'", configname))

                    Return Nothing

                End If

 

                'load the config data

                Dim configId As Integer = searchConfigRes.IdsOut(0)

                Dim loadConfigDataAct As LoadData = New LoadData(Context) With {.Name = "load config data"}

                loadConfigDataAct.ObjectIds.Add(configId)

                loadConfigDataAct.AttributeName = "data"

                Dim loadConfigDataRes As LoadDataResult = loadConfigDataAct.Do()

                If loadConfigDataRes.DataOut Is Nothing Then

                    LogError(String.Format("Cannot load the data of the configuration '{0}' (id: {1}).", configname, configId))

                    Return Nothing

                End If

 

                'read the config xml

                Dim loadConfigXmlAct As LoadXml = New LoadXml(Context) With {.Name = "load config xml"}

                loadConfigXmlAct.Data = loadConfigDataRes.DataOut(0)

                Dim loadConfigXmlRes As LoadXmlResult = loadConfigXmlAct.Do()

                If loadConfigXmlRes.XmlOut Is Nothing Then

                    LogError(String.Format("Cannot load the xml of the configuration '{0}' (id: {1}).", configname, configId))

                    Return Nothing

                End If

 

                Return loadConfigXmlRes.XmlOut

 

            Catch ex As Exception

                Return Nothing

            End Try

        End Function

 

        Sub LogInfo(message As String)

            LogMessage(message, TeraDP.GN4.Workflow.LogEntry.LogCode.Info)

        End Sub

 

        Sub LogWarning(message As String)

            LogMessage(message, TeraDP.GN4.Workflow.LogEntry.LogCode.Warning)

        End Sub

 

        Sub LogError(message As String)

            LogMessage(message, TeraDP.GN4.Workflow.LogEntry.LogCode.Error)

        End Sub

 

        Sub LogMessage(message As String, code As TeraDP.GN4.Workflow.LogEntry.LogCode)

            If String.IsNullOrEmpty(message) Then Return 'nothing to do

 

            Dim logAct As Log = New Log(Context) With {.Name = "log message"}

            logAct.Code = code

            logAct.Message = message

            Dim logRes As LogResult = logAct.Do()

        End Sub

 

    ]]>

  </Members>

 

  <Sequential>

    <![CDATA[

 

            Try

                Dim xConfig As XDocument = GetConfigFile("EPASAConfig")

                If Not IsNothing(xConfig) Then

                    Dim xTitles As IEnumerable(Of XElement) = xConfig.XPathSelectElements("//EPASAConfig/Title")

                    If xTitles.Count > 0 Then

                        For Each xTitle As XElement In xTitles

                            'We process every title that is setup for edition automatic creation

                            Dim xNewEditionFrom As XElement = xTitle.Element("NewEditionFrom")

                            If Not IsNothing(xNewEditionFrom) Then

 

                                Dim xSourceEditionIdsRoot As XElement = xNewEditionFrom.Element("SourceEditionIds")

                                If Not IsNothing(xSourceEditionIdsRoot) Then

 

                                    Dim sEditionName As String = String.Empty

                                    Dim iDaysInAdvance As Integer = 1

                                    Dim iSourceEditionId As Integer = 1

 

                                    'We determine how many days in advance we have to create edition

                                    Dim xDaysInAdvance As XElement = xNewEditionFrom.Element("DaysInAdvance")

                                    If Not IsNothing(xDaysInAdvance) AndAlso Not String.IsNullOrEmpty(xDaysInAdvance.Value) Then

                                        Integer.TryParse(xDaysInAdvance.Value, iDaysInAdvance)

                                    End If

                                    Dim dtEditionDate As System.DateTime = System.DateTime.Now.AddDays(If(iDaysInAdvance > 0, iDaysInAdvance, iDaysInAdvance + 1))

 

                                    'We determine EditionId based on EditionDate 

                                    Dim EditionDayOfWeekName As String = dtEditionDate.DayOfWeek.ToString()

                                    Dim xSourceEditionId As XElement = xSourceEditionIdsRoot.Element(EditionDayOfWeekName)

                                    If IsNothing(xSourceEditionId) Then

                                        xSourceEditionId = xSourceEditionIdsRoot.Element("Default")

                                    End If

                                    If Not IsNothing(xSourceEditionId) AndAlso Not String.IsNullOrEmpty(xSourceEditionId.Value) Then

                                        Integer.TryParse(xSourceEditionId.Value, iSourceEditionId)

                                    End If

 

                                    If iSourceEditionId > 0 Then

                                        'We determine if edition name needs a prefix

                                        Dim xNewEditionNamePrefix As XElement = xNewEditionFrom.Element("NewEditionNamePrefix")

                                        If Not IsNothing(xNewEditionNamePrefix) AndAlso Not String.IsNullOrEmpty(xNewEditionNamePrefix.Value) Then

                                            sEditionName = If(String.IsNullOrEmpty(xNewEditionNamePrefix.Value), "", xNewEditionNamePrefix.Value + "-")

                                        End If

 

                                        'We complete edition name

                                        sEditionName = String.Format(sEditionName + "{0}-{1}-{2}", dtEditionDate.ToString("dd"), MonthName(dtEditionDate.Month), dtEditionDate.ToString("yyyy"))

 

                                        Dim edParams As List(Of String) = New List(Of String)

                                        edParams.Add(sEditionName) 'edition name

                                        edParams.Add(dtEditionDate.ToString("yyyy-MM-dd")) 'edition date

                                        edParams.Add(String.Empty) 'edition number id

                                        edParams.Add(String.Empty) 'workstate id

                                        edParams.Add(String.Empty) 'edition description

 

                                        Dim articleCopyParams As List(Of String) = New List(Of String)

                                        'Dim editorialRest As TeraDP.GN4.GNClient.EditorialRest = New TeraDP.GN4.GNClient.EditorialRest("http://localhost/gn4epasa", Context.LoginContext.Guid)

                                        Dim editorialRest As TeraDP.GN4.GNClient.EditorialRest = New TeraDP.GN4.GNClient.EditorialRest(Context.LoginContext.ServerUrl, Context.LoginContext.Guid)

                                        Dim result As TeraDP.GN4.GNClient.Result = editorialRest.CallServerEditionCopyFrom(iSourceEditionId, edParams, articleCopyParams)

                                        If result IsNot Nothing Then

                                            If IsNothing(result.Error) Then

                                                Dim message As String = "Successfully copied edition. "

                                                If result.WFResult IsNot Nothing Then

                                                    message &= result.WFResult.Description

                                                End If

                                                LogInfo(message)

                                            Else

                                                LogError("Error: " + result.Error.Message)

                                            End If

                                        End If

                                    End If

 

                                End If

                            End If

                        Next

                    Else

                        LogWarning(String.Format("No Title related parameters could be found in the xml of the configuration '{0}'", configname))

                    End If

                End If

 

            Catch ex As Exception

                LogError(String.Format("Error creating editions: {0}", ex.Message))

            End Try

 

    ]]>

  </Sequential>

 

</codeWorkflow>

See also

Create a new edition by scripting