Ejemplo n.º 1
0
        private bool CreateItemProperty(string propertyName, TemplateFieldItem sitecoreField, CodeTypeMemberCollection members)
        {
            var type = _fieldMappingProvider.GetFieldType(sitecoreField.Type);

            if (type == null)
            {
                Log.Warn("Synthesis: Field type resolution for " + sitecoreField.InnerItem.Parent.Parent.Name + "::" + sitecoreField.Name + " failed; no mapping found for field type " + sitecoreField.Type, this);
                return(false);
            }

            var backingFieldName = "_" + propertyName[0].ToString(CultureInfo.InvariantCulture).ToLower() + propertyName.Substring(1);
            var backingField     = new CodeMemberField(new CodeTypeReference(type), backingFieldName);

            backingField.Attributes = MemberAttributes.Private;

            var property = new CodeMemberProperty
            {
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
// ReSharper restore BitwiseOperatorOnEnumWithoutFlags
                Type   = new CodeTypeReference(type),
                Name   = propertyName,
                HasGet = true
            };

            // add [IndexField] attribute
            property.CustomAttributes.Add(GetIndexFieldAttribute(sitecoreField.Name));

            // if(backingField == null)
            //	backingField = new SynthesisFieldType(new Lazy<Field>(() => InnerItem.Fields["xxx"], GetSearchFieldValue("index-field-name"));

            var initializerLambda          = new CodeSnippetExpression(string.Format("new global::System.Lazy<global::Sitecore.Data.Fields.Field>(() => InnerItem.Fields[\"{0}\"])", sitecoreField.ID));
            var initializerSearchReference = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(),
                                                                            "GetSearchFieldValue",
                                                                            new CodePrimitiveExpression(_indexFieldNameTranslator.GetIndexFieldName(sitecoreField.Name)));

            var backingFieldNullCheck = new CodeConditionStatement();

            backingFieldNullCheck.Condition = new CodeSnippetExpression(string.Format("{0} == null", backingFieldName));
            backingFieldNullCheck.TrueStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(backingFieldName), new CodeObjectCreateExpression(property.Type, initializerLambda, initializerSearchReference)));
            property.GetStatements.Add(backingFieldNullCheck);

            // return backingField;
            property.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression(backingFieldName)));

            AddCommentsToFieldProperty(property, sitecoreField);

            members.Add(backingField);
            members.Add(property);

            return(true);
        }
Ejemplo n.º 2
0
        public TemplateGenerationMetadata GenerateMetadata()
        {
            var timer = new Stopwatch();

            timer.Start();

            // load the templates we'll be generating into a state storage collection
            var templateData = CreateTemplateData();

            foreach (var template in templateData.Templates)
            {
                HashSet <string> fieldKeys = GetBaseFieldSet();                             // get fields on base type

                fieldKeys.Add(template.TypeName);                                           // member names cannot be the same as their enclosing type so we add the type name to the fields collection

                foreach (var baseTemplate in template.Template.AllNonstandardBaseTemplates) // similarly names can't be the same as any of their base templates' names (this would cause an incompletely implemented interface)
                {
                    if (templateData.Contains(baseTemplate.TemplateId))
                    {
                        fieldKeys.Add(templateData[baseTemplate.TemplateId].TypeName);
                    }
                    else
                    {
                        fieldKeys.Add(baseTemplate.Name.AsIdentifier());                      // NOTE: you could break this if you have a base template called Foo and a field called Foo that IS NOT on the Foo template (but why would you have that?)
                    }
                }

                // generate item properties
                foreach (var field in template.Template.Fields)
                {
                    if (_templateInputProvider.IsFieldIncluded(field.Id))                     // query the template input provider and make sure the field is included
                    {
                        string propertyName = field.Name.AsNovelIdentifier(fieldKeys);

                        var fieldInfo = new FieldPropertyInfo(field);
                        fieldInfo.FieldPropertyName = propertyName;

                        if (_parameters.EnableContentSearch)
                        {
                            fieldInfo.SearchFieldName = _indexFieldNameMapper.MapToSearchField(field);
                        }
                        fieldInfo.FieldType = _fieldMappingProvider.GetFieldType(field);

                        if (fieldInfo.FieldType == null)
                        {
                            Log.Warn("Synthesis: Field type resolution for " + field.Template.Name + "::" + field.Name + " failed; no mapping found for field type " + field.Type, this);
                            continue;                             // skip adding the field for generation
                        }

                        // record usage of the property name
                        fieldKeys.Add(propertyName);

                        // add the field to the metadata
                        template.FieldsToGenerate.Add(fieldInfo);
                    }
                }

                // generates interfaces to represent the Sitecore template inheritance hierarchy
                TemplateInfo baseInterface = GenerateInheritedInterfaces(template.Template, templateData);
                if (baseInterface != null)
                {
                    template.InterfacesImplemented.Add(baseInterface);
                }
            }

            timer.Stop();
            Log.Info($"Synthesis: Generated metadata for {templateData.Templates.Count} concrete templates and {templateData.Interfaces.Count} interface templates in {timer.ElapsedMilliseconds} ms", this);

            return(templateData);
        }