public ContentPropertyToTypedAttribute(AbstractFluentMappingEngine engine, bool ignoreAttributeDef = false)
            : base(engine)
        {
            _ignoreAttributeDef = ignoreAttributeDef;

            MappingContext
            .ForMember(x => x.UtcCreated, opt => opt.MapUsing <UtcCreatedMapper>())
            .ForMember(x => x.UtcModified, opt => opt.MapUsing <UtcModifiedMapper>())
            .ForMember(x => x.AttributeDefinition, opt => opt.MapFrom(x => _ignoreAttributeDef ? null : x.DocTypeProperty))
            .AfterMap((source, dest) =>
            {
                var propEditor = ((EditorModel)source.PropertyEditorModel);
                if (propEditor != null)
                {
                    dest.Values.Clear();
                    foreach (var i in propEditor.GetSerializedValue())
                    {
                        dest.Values.Add(i.Key, i.Value);
                    }
                }
                else
                {
                    dest.DynamicValue = null;
                }
            });
        }
 public MemberToMemberEditorModel(AbstractFluentMappingEngine engine, MapResolverContext resolverContext, Action <TMember, TMemberEditorModel> additionalAfterMap = null)
     : base(engine, resolverContext, additionalAfterMap)
 {
     MappingContext
     //ignore all custom properties as these need to be mapped by the underlying attributes
     .ForMember(x => x.Name, opt => opt.Ignore())
     .ForMember(x => x.Username, opt => opt.Ignore())
     .ForMember(x => x.Password, opt => opt.Ignore())
     .ForMember(x => x.ConfirmPassword, opt => opt.Ignore())
     .ForMember(x => x.Email, opt => opt.Ignore())
     .ForMember(x => x.IsApproved, opt => opt.Ignore())
     .ForMember(x => x.LastLoginDate, opt => opt.Ignore())
     .ForMember(x => x.LastActivityDate, opt => opt.Ignore())
     .ForMember(x => x.LastPasswordChangeDate, opt => opt.Ignore());
 }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentEditorModelToTypedEntity&lt;TContentModel, TTypedEntity&gt;"/> class.
        /// </summary>
        /// <param name="engine">The engine.</param>
        /// <param name="resolverContext">The resolver context.</param>
        /// <param name="ignoreAttributeAliases">The attribute aliases to ignore during attribute mapping</param>
        public ContentEditorModelToTypedEntity(AbstractFluentMappingEngine engine, MapResolverContext resolverContext, params string[] ignoreAttributeAliases)
            : base(engine)
        {
            MappingContext
            .ForMember(x => x.UtcCreated, opt => opt.MapUsing <UtcCreatedMapper>())
            .ForMember(x => x.UtcModified, opt => opt.MapUsing <UtcModifiedMapper>())
            .IgnoreMember(x => x.RelationProxies)
            .MapMemberUsing(x => x.EntitySchema, new ContentEditorModelToEntitySchema <TContentModel>(MappingContext.Engine, resolverContext))
            .AfterMap((source, dest) =>
            {
                if (!source.ParentId.IsNullValueOrEmpty())
                {
                    // Enlist the specified parent
                    dest.RelationProxies.EnlistParentById(source.ParentId, FixedRelationTypes.DefaultRelationType, source.OrdinalRelativeToParent);
                }

                //add or update all properties in the model.
                //We need to manually create a mapper for this operation in order to keep the object graph consistent
                //with the correct instances of objects. This mapper will ignore mapping the AttributeDef on the TypedAttribute
                //so that we can manually assign it from our EntitySchema.
                var compositeSchema = dest.EntitySchema as CompositeEntitySchema;
                var propertyMapper  = new ContentPropertyToTypedAttribute(MappingContext.Engine, true);
                foreach (var contentProperty in source.Properties.Where(x => !ignoreAttributeAliases.Contains(x.Alias)))
                {
                    var typedAttribute = propertyMapper.Map(contentProperty);
                    //now we need to manually assign the same attribute definition instance from our already mapped schema.
                    Func <AttributeDefinition, bool> findAttrib = x => x.Id.Value == contentProperty.DocTypePropertyId.Value;
                    var attDef = dest.EntitySchema.AttributeDefinitions.SingleOrDefault(findAttrib);

                    if (compositeSchema != null && attDef == null)
                    {
                        attDef = compositeSchema.AllAttributeDefinitions.Single(findAttrib);
                    }

                    typedAttribute.AttributeDefinition = attDef;

                    dest.Attributes.SetValueOrAdd(typedAttribute);
                }
                //now we need to remove any properties that don't exist in the model, excluding the 'special' internal fields
                var allAliases = source.Properties.Select(x => x.Alias).ToArray();
                var toRemove   = dest.Attributes.Where(x => !allAliases.Contains(x.AttributeDefinition.Alias))
                                 .Select(x => x.Id).ToArray();
                dest.Attributes.RemoveAll(x => toRemove.Contains(x.Id));
            });
        }
Exemplo n.º 4
0
        public DocumentTypeToNewContentModel(AbstractFluentMappingEngine engine,
                                             Action <DocumentTypeEditorModel, TTarget> additionalAfterMap = null)
            : base(engine)
        {
            //set all of the member expressions
            MappingContext
            .ForMember(x => x.DocumentTypeName, opt => opt.MapFrom(x => x.Name))
            .ForMember(x => x.DocumentTypeId, opt => opt.MapFrom(x => x.Id))
            .ForMember(x => x.Id, opt => opt.Ignore())
            .ForMember(x => x.ActiveTabIndex, opt => opt.Ignore())
            .ForMember(x => x.UpdatedBy, opt => opt.Ignore())
            .ForMember(x => x.CreatedBy, opt => opt.Ignore())
            .ForMember(x => x.UtcCreated, opt => opt.Ignore())
            .ForMember(x => x.UtcModified, opt => opt.Ignore())
            .ForMember(x => x.ParentId, opt => opt.Ignore())
            .ForMember(x => x.Name, opt => opt.Ignore())
            .ForMember(x => x.Properties, opt => opt.MapFrom(x => x.Properties))
            .ForMember(x => x.NoticeBoard, x => x.Ignore())
            .ForMember(x => x.UIElements, x => x.Ignore())
            .AfterMap((source, dest) =>
            {
                dest.DocumentTypeId    = source.Id;
                dest.DocumentTypeName  = source.Name;
                dest.DocumentTypeAlias = source.Alias;
                dest.Tabs = MappingContext.Engine.Map <HashSet <Tab>, HashSet <Tab> >(source.DefinedTabs);

                //now, update the PropertyEditor context for each property with this item
                foreach (var p in dest.Properties.Where(p => p.DocTypeProperty.DataType.InternalPropertyEditor != null)
                         .Where(p => p.DocTypeProperty.DataType.InternalPropertyEditor is IContentAwarePropertyEditor))
                {
                    var contentAwarePropEditor = (IContentAwarePropertyEditor)p.DocTypeProperty.DataType.InternalPropertyEditor;
                    contentAwarePropEditor.SetDocumentType(source);
                }

                if (additionalAfterMap != null)
                {
                    additionalAfterMap(source, dest);
                }
            });
        }
        public SchemaEditorModelToEntitySchema(AbstractFluentMappingEngine engine, MapResolverContext resolverContext, Action <TEditorModel, EntitySchema> additionAfterMap = null)
            : base(engine)
        {
            _resolverContext = resolverContext;
            MappingContext
            .ForMember(x => x.UtcCreated, opt => opt.MapUsing <UtcCreatedMapper>())
            .ForMember(x => x.UtcModified, opt => opt.MapUsing <UtcModifiedMapper>())
            .ForMember(x => x.SchemaType, opt => opt.MapFrom(x => FixedSchemaTypes.Content))
            .AfterMap((from, to) =>
            {
                // Commented out the below as working on multi-inheritance and the blow assumes one parent only (MB - 2011-11-15)

                //if (!from.ParentId.IsNullValueOrEmpty())
                //{
                //    // Enlist the specified parent
                //    // TODO: The ContentEditorModel needs to supply us with the ordinal of the existing relationship, if it exists
                //    // otherwise we're always going to reset the Ordinal to 0
                //    to.RelationProxies.EnlistParentById(from.ParentId, FixedRelationTypes.DefaultRelationType, 0);
                //}

                // AttributeGroups on a schema are a union between manually added groups and those referenced in AttributeDefinitions
                // so rather than clearing the AttributeGroups and the definitions and remapping, first let's create a list of groups
                // separately
                var mappedGroups = engine.Map <IEnumerable <Tab>, IEnumerable <AttributeGroup> >(from.DefinedTabs);
                var existingOrNewGeneralGroup = to.AttributeGroups.Where(x => x.Alias == FixedGroupDefinitions.GeneralGroupAlias)
                                                .SingleOrDefault() ?? FixedGroupDefinitions.GeneralGroup;

                // Now let's go through the attribute definitions
                to.AttributeDefinitions.Clear();
                to.AttributeGroups.Clear();
                var definitionMapper = new DocumentTypePropertyToAttributeDefinition(engine, _resolverContext, false);
                foreach (var p in from.Properties)
                {
                    var attrDef = definitionMapper.Map(p);
                    if (p.TabId.IsNullValueOrEmpty())
                    {
                        attrDef.AttributeGroup = existingOrNewGeneralGroup;
                    }
                    else
                    {
                        var found = mappedGroups.SingleOrDefault(x => x.Id == p.TabId);
                        attrDef.AttributeGroup = found ?? existingOrNewGeneralGroup;
                    }

                    to.TryAddAttributeDefinition(attrDef);
                }

                // Now if there are any groups left that aren't linked to attributes add those too
                var unusedGroups = mappedGroups.Except(to.AttributeGroups).ToArray();
                to.AttributeGroups.AddRange(unusedGroups);

                ////we'll map the attribute groups from defined tabs
                //to.AttributeDefinitions.Clear();
                //to.AttributeGroups.Clear();
                //from.DefinedTabs.ForEach(y =>
                //                           to.AttributeGroups.Add(
                //                               engine.Map<Tab, AttributeGroup>(y)));

                ////see if there is a general group, if not create one
                //var generalGroup = to.AttributeGroups.Where(x => x.Alias == FixedGroupDefinitions.GeneralGroupAlias).SingleOrDefault();
                //if(generalGroup == null)
                //{
                //    to.AttributeGroups.Add(FixedGroupDefinitions.GeneralGroup);
                //}

                ////Here we need to make sure the same AttributeGroup instances that were just created
                //// are assigned to the AttributeDefinition, to do this we create a new mapper instance and tell it
                //// not to map AttributeGroups and instead we'll assign the group manually.
                //var attDefMapper = new DocumentTypePropertyToAttributeDefinition(engine, _resolverContext, false);
                //foreach(var p in from.Properties)
                //{
                //    var attrDef = attDefMapper.Map(p);
                //    if (p.TabId.IsNullValueOrEmpty())
                //    {
                //        attrDef.AttributeGroup = generalGroup;
                //    }
                //    else
                //    {
                //        var found = to.AttributeGroups.SingleOrDefault(x => x.Id == p.TabId);
                //        attrDef.AttributeGroup = found ?? generalGroup;
                //    }
                //    to.TryAddAttributeDefinition(attrDef);
                //}

                //ensure the 'in built' properties exist
                to.TryAddAttributeDefinition(new NodeNameAttributeDefinition(existingOrNewGeneralGroup));
                to.TryAddAttributeDefinition(new SelectedTemplateAttributeDefinition(existingOrNewGeneralGroup));

                to.SetXmlConfigProperty("thumb", from.Thumbnail);

                //save the description,icon
                to.SetXmlConfigProperty("description", from.Description);
                to.SetXmlConfigProperty("icon", from.Icon);

                //save the allowed children as a list of guids (for now)
                to.SetXmlConfigProperty("allowed-children",
                                        (from.AllowedChildren != null &&
                                         from.AllowedChildren.Any())
                                                  ? from.AllowedChildren
                                        .Where(x => x.Selected)
                                        .Select(x => x.Value ?? string.Empty).ToArray()
                                                  : new string[] { });

                //save the is abstract/inheritable only setting
                to.SetXmlConfigProperty("is-abstract", from.IsAbstract);

                if (additionAfterMap != null)
                {
                    additionAfterMap(from, to);
                }
            });
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentEditorModelToTypedEntity&lt;TContentModel, TTypedEntity&gt;"/> class.
        /// </summary>
        /// <param name="engine">The engine.</param>
        /// <param name="resolverContext">The resolver context.</param>
        /// <param name="ignoreAttributeAliases">The attribute aliases to ignore during attribute mapping</param>
        public ContentEditorModelToTypedEntity(AbstractFluentMappingEngine engine, MapResolverContext resolverContext, params string[] ignoreAttributeAliases)
            : base(engine)
        {
            MappingContext
            .ForMember(x => x.UtcCreated, opt => opt.MapUsing <UtcCreatedMapper>())
            .ForMember(x => x.UtcModified, opt => opt.MapUsing <UtcModifiedMapper>())
            .IgnoreMember(x => x.RelationProxies)
            .MapMemberUsing(x => x.EntitySchema, new ContentEditorModelToEntitySchema <TContentModel>(MappingContext.Engine, resolverContext))
            .AfterMap((source, dest) =>
            {
                if (!source.ParentId.IsNullValueOrEmpty())
                {
                    var ordinalRelativeToParent = 0;

                    using (var uow = resolverContext.Hive.OpenReader <IContentStore>(source.ParentId.ToUri()))
                    {
                        var siblingRelations = uow.Repositories.GetChildRelations(source.ParentId, FixedRelationTypes.DefaultRelationType)
                                               .OrderBy(x => x.Ordinal)
                                               .ToArray();

                        var myRelation = siblingRelations.SingleOrDefault(x => x.DestinationId.Value == source.Id.Value);
                        if (myRelation != null)
                        {
                            ordinalRelativeToParent = myRelation.Ordinal;
                        }
                        else if (siblingRelations.Count() > 0)
                        {
                            ordinalRelativeToParent = siblingRelations.Max(x => x.Ordinal) + 1;
                        }
                    }

                    // Enlist the specified parent
                    dest.RelationProxies.EnlistParentById(source.ParentId, FixedRelationTypes.DefaultRelationType, ordinalRelativeToParent);
                }

                //add or update all properties in the model.
                //We need to manually create a mapper for this operation in order to keep the object graph consistent
                //with the correct instances of objects. This mapper will ignore mapping the AttributeDef on the TypedAttribute
                //so that we can manually assign it from our EntitySchema.
                var compositeSchema   = dest.EntitySchema as CompositeEntitySchema;
                var propertyMapper    = new ContentPropertyToTypedAttribute(MappingContext.Engine, true);
                var contentProperties = source.Properties.Where(x => !ignoreAttributeAliases.Contains(x.Alias)).ToArray();
                foreach (var contentProperty in contentProperties)
                {
                    var typedAttribute = propertyMapper.Map(contentProperty);
                    //now we need to manually assign the same attribute definition instance from our already mapped schema.
                    var localProperty   = contentProperty;
                    var localPropertyId = localProperty.DocTypePropertyId;
                    if (localPropertyId == HiveId.Empty)
                    {
                        throw new Exception("Catastrophic failure: The content property to be mapped does not have an Id against the document type. Alias: {0}".InvariantFormat(localProperty.Alias));
                    }
                    Func <AttributeDefinition, bool> findAttrib = x => x.Id.Value == localPropertyId.Value;
                    var attDef = dest.EntitySchema.AttributeDefinitions.SingleOrDefault(findAttrib);

                    if (compositeSchema != null && attDef == null)
                    {
                        attDef = compositeSchema.AllAttributeDefinitions.Single(findAttrib);
                    }

                    typedAttribute.AttributeDefinition = attDef;

                    dest.Attributes.SetValueOrAdd(typedAttribute);
                }
                //now we need to remove any properties that don't exist in the model, excluding the 'special' internal fields
                var allAliases = source.Properties.Select(x => x.Alias).ToArray();
                var toRemove   = dest.Attributes.Where(x => !allAliases.Contains(x.AttributeDefinition.Alias))
                                 .Select(x => x.Id).ToArray();
                dest.Attributes.RemoveAll(x => toRemove.Contains(x.Id));
            });
        }