public INodeMappingExpression <TDestination> SingleProperty <TSourceProperty>( Expression <Func <TDestination, object> > destinationProperty, SinglePropertyMapping <TSourceProperty> mapping, string propertyAlias = null ) { if (mapping == null) { throw new ArgumentNullException("mapping"); } else if (destinationProperty == null) { throw new ArgumentNullException("destinationProperty"); } var mapper = new SinglePropertyMapper( (context, value) => mapping((TSourceProperty)value), typeof(TSourceProperty), _nodeMapper, destinationProperty.GetPropertyInfo(), propertyAlias ); _nodeMapper.InsertPropertyMapper(mapper); return(this); }
public INodeMappingExpression <TDestination> SingleProperty( Expression <Func <TDestination, object> > destinationProperty, Func <int, int?> mapping ) { if (mapping == null) { throw new ArgumentNullException("mapping"); } else if (destinationProperty == null) { throw new ArgumentNullException("destinationProperty"); } var mapper = new SinglePropertyMapper( (context, sourceValue) => mapping(context.Id), null, _nodeMapper, destinationProperty.GetPropertyInfo(), null ); _nodeMapper.InsertPropertyMapper(mapper); return(this); }
public INodeMappingExpression <TDestination> SingleProperty( Expression <Func <TDestination, object> > destinationProperty, string propertyAlias ) { if (string.IsNullOrEmpty(propertyAlias)) { throw new ArgumentException("Property alias cannot be null", "propertyAlias"); } else if (destinationProperty == null) { throw new ArgumentNullException("destinationProperty"); } var mapper = new SinglePropertyMapper( null, null, _nodeMapper, destinationProperty.GetPropertyInfo(), propertyAlias ); _nodeMapper.InsertPropertyMapper(mapper); return(this); }
public INodeMappingExpression <TDestination> ForProperty <TProperty>( Expression <Func <TDestination, TProperty> > destinationProperty, string nodeTypeAlias ) { if (string.IsNullOrEmpty(nodeTypeAlias)) { throw new ArgumentException("Node type alias cannot be null", "nodeTypeAlias"); } var destinationPropertyInfo = destinationProperty.GetPropertyInfo(); var propertyType = destinationPropertyInfo.PropertyType; PropertyMapperBase mapper = null; if (propertyType.GetMappedPropertyType() == MappedPropertyType.SystemOrEnum) { mapper = new BasicPropertyMapper( null, null, _nodeMapper, destinationPropertyInfo, nodeTypeAlias ); } else if (propertyType.GetMappedPropertyType() == MappedPropertyType.Model) { mapper = new SinglePropertyMapper( null, null, _nodeMapper, destinationPropertyInfo, nodeTypeAlias ); } else if (propertyType.GetMappedPropertyType() == MappedPropertyType.Collection) { mapper = new CollectionPropertyMapper( null, null, _nodeMapper, destinationPropertyInfo, nodeTypeAlias ); } else { throw new ArgumentOutOfRangeException( "TProperty", string.Format("The property type {0} on model {1} is not supported.", propertyType.FullName, typeof(TDestination).FullName) ); } _nodeMapper.InsertPropertyMapper(mapper); return(this); }
public NodeMapper(NodeMappingEngine engine, Type destinationType, IContentType sourceDocumentType) { if (sourceDocumentType == null) { throw new ArgumentNullException("sourceDocumentType"); } else if (engine == null) { throw new ArgumentNullException("engine"); } else if (destinationType == null) { throw new ArgumentNullException("destinationType"); } SourceDocumentType = sourceDocumentType; Engine = engine; DestinationType = destinationType; PropertyMappers = new List <PropertyMapperBase>(); // See if base properties have been mapped already. var baseNodeMapper = Engine.GetBaseNodeMapperForType(destinationType); if (baseNodeMapper != null) { // Use the property mappings of the closest parent // (ideally they will already have the mappings of all their ancestors) PropertyMappers.AddRange(baseNodeMapper.PropertyMappers); } // Map properties foreach (var destinationProperty in destinationType.GetProperties()) { if (PropertyMappers.Any(mapper => mapper.DestinationInfo.Name == destinationProperty.Name) || !destinationProperty.CanWrite) { // A mapping already exists for this property on a base type or its a ReadOnly property. continue; } PropertyMapperBase propertyMapper = null; var sourcePropertyAlias = GetPropertyAlias(destinationProperty); // Check if this property is equivalent to a default Node property. var defaultNodeProperty = DefaultPropertyMapper.GetDefaultMappingForName(destinationProperty); if (sourcePropertyAlias != null && // check corresponding source property alias was found destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.SystemOrEnum) { propertyMapper = new BasicPropertyMapper( x => x, // direct mapping destinationProperty.PropertyType, // using the desired type this, destinationProperty, sourcePropertyAlias ); } else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Model) { propertyMapper = new SinglePropertyMapper( null, null, this, destinationProperty, sourcePropertyAlias // can be null to map ancestor ); } else if (destinationProperty.PropertyType.GetMappedPropertyType() == MappedPropertyType.Collection) { propertyMapper = new CollectionPropertyMapper( null, null, this, destinationProperty, sourcePropertyAlias // can be null to map descendants ); } else if (defaultNodeProperty != null) { propertyMapper = new DefaultPropertyMapper( this, destinationProperty, defaultNodeProperty, null ); } if (propertyMapper != null) { PropertyMappers.Add(propertyMapper); } } }
private void addOneToManyAttached(bool fakeOneToManyBidirectional) { log.Debug("Adding audit mapping for property {0}. {1}" + ": one-to-many collection, using a join column on the referenced entity.", _referencingEntityName, _propertyName); var mappedBy = getMappedBy(_propertyValue); var referencedIdMapping = _mainGenerator.GetReferencedIdMappingData(_referencingEntityName, _referencedEntityName, _propertyAuditingData, false); var referencingIdMapping = _referencingEntityConfiguration.IdMappingData; // Generating the id mappers data for the referencing side of the relation. var referencingIdData = createMiddleIdData(referencingIdMapping, mappedBy + "_", _referencingEntityName); // And for the referenced side. The prefixed mapper won't be used (as this collection isn't persisted // in a join table, so the prefix value is arbitrary). var referencedIdData = createMiddleIdData(referencedIdMapping, null, _referencedEntityName); // Generating the element mapping. var elementComponentData = new MiddleComponentData( new MiddleRelatedComponentMapper(referencedIdData), 0); // Generating the index mapping, if an index exists. It can only exists in case a javax.persistence.MapKey // annotation is present on the entity. So the middleEntityXml will be not be used. The queryGeneratorBuilder // will only be checked for nullnes. var indexComponentData = addIndex(null, null); // Generating the query generator - it should read directly from the related entity. var queryGenerator = new OneAuditEntityQueryGenerator(_mainGenerator.VerEntCfg, _mainGenerator.GlobalCfg.AuditStrategy, referencingIdData, _referencedEntityName, referencedIdData, isEmbeddableElementType()); // Creating common mapper data. var commonCollectionMapperData = new CommonCollectionMapperData( _mainGenerator.VerEntCfg, _referencedEntityName, _propertyAuditingData.GetPropertyData(), referencingIdData, queryGenerator); IPropertyMapper fakeBidirectionalRelationMapper; IPropertyMapper fakeBidirectionalRelationIndexMapper; if (fakeOneToManyBidirectional) { // In case of a fake many-to-one bidirectional relation, we have to generate a mapper which maps // the mapped-by property name to the id of the related entity (which is the owner of the collection). var auditMappedBy = _propertyAuditingData.MappedBy; // Creating a prefixed relation mapper. var relMapper = referencingIdMapping.IdMapper.PrefixMappedProperties( MappingTools.CreateToOneRelationPrefix(auditMappedBy)); fakeBidirectionalRelationMapper = new ToOneIdMapper( _mainGenerator.GlobalCfg.EnversProxyFactory, relMapper, // The mapper will only be used to map from entity to map, so no need to provide other details // when constructing the PropertyData. new PropertyData(auditMappedBy, null, null), _referencedEntityName, false); // Checking if there's an index defined. If so, adding a mapper for it. if (_propertyAuditingData.PositionMappedBy != null) { var positionMappedBy = _propertyAuditingData.PositionMappedBy; fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(new PropertyData(positionMappedBy, null, null)); // Also, overwriting the index component data to properly read the index. indexComponentData = new MiddleComponentData(new MiddleStraightComponentMapper(positionMappedBy), 0); } else { fakeBidirectionalRelationIndexMapper = null; } } else { fakeBidirectionalRelationMapper = null; fakeBidirectionalRelationIndexMapper = null; } // Checking the type of the collection and adding an appropriate mapper. addMapper(commonCollectionMapperData, elementComponentData, indexComponentData); // Storing information about this relation. _referencingEntityConfiguration.AddToManyNotOwningRelation(_propertyName, mappedBy, _referencedEntityName, referencingIdData.PrefixedMapper, fakeBidirectionalRelationMapper, fakeBidirectionalRelationIndexMapper); }