Example #1
0
        public static TemplateLink CreateInstance(XmlTemplateLink xmlTemplateLink)
        {
            if (xmlTemplateLink == null)
            {
                return(null);
            }

            TemplateLink templateLink = new TemplateLink();

            templateLink.Name        = xmlTemplateLink.Name;
            templateLink.Description = xmlTemplateLink.Description;

            templateLink.To                  = xmlTemplateLink.To.EmptyIfNull().Trim();
            templateLink.With                = xmlTemplateLink.With.EmptyIfNull().Trim();
            templateLink.Positioning         = xmlTemplateLink.Position;
            templateLink.MinOccurencesMethod = xmlTemplateLink.MinOccurences;

            if (string.IsNullOrEmpty(templateLink.To))
            {
                throw new EtkException("Attribut 'To' cannot be null or empty", false);
            }

            return(templateLink);
        }
        public LinkedTemplateDefinition(ITemplateDefinition parent, ITemplateDefinition templateDefinition, TemplateLink linkDefinition)
        {
            try
            {
                Parent             = parent;
                TemplateDefinition = templateDefinition;
                Positioning        = linkDefinition.Positioning;
                if (!string.IsNullOrEmpty(linkDefinition.With))
                {
                    Type type = parent.MainBindingDefinition != null ? parent.MainBindingDefinition.BindingType : null;
                    BindingDefinitionDescription definitionDescription = new BindingDefinitionDescription()
                    {
                        BindingExpression = linkDefinition.With,
                        Description       = linkDefinition.Description,
                        Name       = linkDefinition.Name,
                        IsReadOnly = true
                    };
                    BindingDefinition = BindingDefinitionFactory.CreateInstance(type, definitionDescription);
                }

                if (!string.IsNullOrEmpty(linkDefinition.MinOccurencesMethod))
                {
                    try
                    {
                        if (templateDefinition.Header != null && ((TemplateDefinitionPart)templateDefinition.Header).HasLinkedTemplates ||
                            templateDefinition.Body != null && ((TemplateDefinitionPart)templateDefinition.Body).HasLinkedTemplates ||
                            templateDefinition.Footer != null && ((TemplateDefinitionPart)templateDefinition.Footer).HasLinkedTemplates)
                        {
                            throw new Exception("'MinOccurencesMethod' is not supported with templates linked with other templates");
                        }

                        Type type = TemplateDefinition.MainBindingDefinition == null ? null : TemplateDefinition.MainBindingDefinition.BindingType;
                        MinOccurencesMethod = TypeHelpers.GetMethod(type, linkDefinition.MinOccurencesMethod);
                        if (MinOccurencesMethod.GetParameters().Length > 2)
                        {
                            throw new Exception("The min occurences resolver method signature must be 'int <MethodName>([instance of element of the collection that owned the link declaration])'");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Cannot retrieve the min occurences resolver method:{ex.Message}");
                    }
                }
            }
            catch (Exception ex)
            {
                string message = $"Cannot resolve linked template definition 'To={linkDefinition.To}, With='{linkDefinition.With}'";
                throw new Exception(message, ex);
            }
        }