コード例 #1
0
        //@SuppressWarnings({"unchecked"})
        public void AddComponent(XmlElement parent, PropertyAuditingData propertyAuditingData,
                                 IValue value, ICompositeMapperBuilder mapper, String entityName,
                                 EntityXmlMappingData xmlMappingData, bool firstPass)
        {
            Component prop_component = (Component)value;

            ICompositeMapperBuilder componentMapper = mapper.AddComponent(propertyAuditingData.getPropertyData(),
                                                                          prop_component.ComponentClassName);

            // The property auditing data must be for a component.
            ComponentAuditingData componentAuditingData = (ComponentAuditingData)propertyAuditingData;

            // Adding all properties of the component
            IEnumerator <Property> properties = (IEnumerator <Property>)prop_component.PropertyIterator.GetEnumerator();

            while (properties.MoveNext())
            {
                Property property = properties.Current;

                PropertyAuditingData componentPropertyAuditingData =
                    componentAuditingData.getPropertyAuditingData(property.Name);

                // Checking if that property is audited
                if (componentPropertyAuditingData != null)
                {
                    mainGenerator.AddValue(parent, property.Value, componentMapper, entityName, xmlMappingData,
                                           componentPropertyAuditingData, property.IsInsertable, firstPass);
                }
            }
        }
コード例 #2
0
        //@SuppressWarnings({"unchecked"})
        public void AddToOne(XmlElement parent, PropertyAuditingData propertyAuditingData, IValue value,
                             ICompositeMapperBuilder mapper, String entityName, bool insertable)
        {
            String referencedEntityName = ((ToOne)value).ReferencedEntityName;

            IdMappingData idMapping = mainGenerator.GetReferencedIdMappingData(entityName, referencedEntityName,
                                                                               propertyAuditingData, true);

            String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(propertyAuditingData.Name);

            // Generating the id mapper for the relation
            IIdMapper relMapper = idMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix);

            // Storing information about this relation
            mainGenerator.EntitiesConfigurations[entityName].AddToOneRelation(
                propertyAuditingData.Name, referencedEntityName, relMapper, insertable);

            // If the property isn't insertable, checking if this is not a "fake" bidirectional many-to-one relationship,
            // that is, when the one side owns the relation (and is a collection), and the many side is non insertable.
            // When that's the case and the user specified to store this relation without a middle table (using
            // @AuditMappedBy), we have to make the property insertable for the purposes of Envers. In case of changes to
            // the entity that didn't involve the relation, it's value will then be stored properly. In case of changes
            // to the entity that did involve the relation, it's the responsibility of the collection side to store the
            // proper data.
            bool nonInsertableFake;

            if (!insertable && propertyAuditingData.ForceInsertable)
            {
                nonInsertableFake = true;
                insertable        = true;
            }
            else
            {
                nonInsertableFake = false;
            }


            // Adding an element to the mapping corresponding to the references entity id's
            // Use OwnerDocument.ImportNode() instead of XmlNode.Clone();
            XmlElement properties = (XmlElement)parent.OwnerDocument.ImportNode(idMapping.XmlRelationMapping, true);

            properties.SetAttribute("name", propertyAuditingData.Name);

            MetadataTools.PrefixNamesInPropertyElement(properties, lastPropertyPrefix,
                                                       MetadataTools.GetColumnNameEnumerator((IEnumerator <ISelectable>)value.ColumnIterator.GetEnumerator()), false, insertable);
            parent.AppendChild(properties);


            // Adding mapper for the id
            PropertyData propertyData = propertyAuditingData.getPropertyData();

            mapper.AddComposite(propertyData, new ToOneIdMapper(relMapper, propertyData, referencedEntityName, nonInsertableFake));
        }
コード例 #3
0
        private void AddSimpleValue(XmlElement parent, PropertyAuditingData propertyAuditingData,
                                    IValue value, ISimpleMapperBuilder mapper, bool insertable, bool key)
        {
            if (parent != null)
            {
                XmlElement prop_mapping = MetadataTools.AddProperty(parent, propertyAuditingData.Name,
                                                                    value.Type.Name, propertyAuditingData.ForceInsertable || insertable, key);
                MetadataTools.AddColumns(prop_mapping, (IEnumerator <ISelectable>)value.ColumnIterator.GetEnumerator());
            }

            // A null mapper means that we only want to add xml mappings
            if (mapper != null)
            {
                mapper.Add(propertyAuditingData.getPropertyData());
            }
        }
コード例 #4
0
        //@SuppressWarnings({"unchecked"})
        public void AddOneToOneNotOwning(PropertyAuditingData propertyAuditingData, IValue value,
                                         ICompositeMapperBuilder mapper, String entityName)
        {
            OneToOne propertyValue = (OneToOne)value;

            String owningReferencePropertyName = propertyValue.ReferencedPropertyName; // mappedBy
            EntityConfiguration configuration  = mainGenerator.EntitiesConfigurations[entityName];

            if (configuration == null)
            {
                throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
            }

            IdMappingData ownedIdMapping = configuration.IdMappingData;

            if (ownedIdMapping == null)
            {
                throw new MappingException("An audited relation to a non-audited entity " + entityName + "!");
            }

            String lastPropertyPrefix   = MappingTools.createToOneRelationPrefix(owningReferencePropertyName);
            String referencedEntityName = propertyValue.ReferencedEntityName;

            // Generating the id mapper for the relation
            IIdMapper ownedIdMapper = ownedIdMapping.IdMapper.PrefixMappedProperties(lastPropertyPrefix);

            // Storing information about this relation
            mainGenerator.EntitiesConfigurations[entityName].AddToOneNotOwningRelation(
                propertyAuditingData.Name, owningReferencePropertyName,
                referencedEntityName, ownedIdMapper);

            // Adding mapper for the id
            PropertyData propertyData = propertyAuditingData.getPropertyData();

            mapper.AddComposite(propertyData, new OneToOneNotOwningMapper(owningReferencePropertyName,
                                                                          referencedEntityName, propertyData));
        }
コード例 #5
0
        private void AddCustomValue(XmlElement parent, PropertyAuditingData propertyAuditingData,
                                    IValue value, ISimpleMapperBuilder mapper, bool insertable, bool key)
        {
            if (parent != null)
            {
                XmlElement prop_mapping = MetadataTools.AddProperty(parent, propertyAuditingData.Name,
                                                                    null, insertable, key);

                //CustomType propertyType = (CustomType) value.getType();

                XmlElement type_mapping = parent.OwnerDocument.CreateElement("type");
                prop_mapping.AppendChild(type_mapping);
                type_mapping.SetAttribute("name", value.GetType().Name);

                if (value is SimpleValue)
                {
                    IDictionary <string, string> typeParameters = ((SimpleValue)value).TypeParameters;
                    if (typeParameters != null)
                    {
                        foreach (KeyValuePair <string, string> paramKeyValue in typeParameters)
                        {
                            XmlElement type_param = parent.OwnerDocument.CreateElement("param");
                            type_param.SetAttribute("name", (String)paramKeyValue.Key);
                            type_param["name"].Value = paramKeyValue.Value;
                        }
                    }
                }

                MetadataTools.AddColumns(prop_mapping, (IEnumerator <ISelectable>)value.ColumnIterator);
            }

            if (mapper != null)
            {
                mapper.Add(propertyAuditingData.getPropertyData());
            }
        }
コード例 #6
0
        private void AddOneToManyAttached(bool fakeOneToManyBidirectional)
        {
            //throw new NotImplementedException();

            log.Debug("Adding audit mapping for property " + referencingEntityName + "." + propertyName +
                      ": one-to-many collection, using a join column on the referenced entity.");

            String mappedBy = GetMappedBy(propertyValue);

            IdMappingData referencedIdMapping = mainGenerator.GetReferencedIdMappingData(referencingEntityName,
                                                                                         referencedEntityName, propertyAuditingData, false);
            IdMappingData referencingIdMapping = referencingEntityConfiguration.IdMappingData;

            // Generating the id mappers data for the referencing side of the relation.
            MiddleIdData 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).
            MiddleIdData referencedIdData = CreateMiddleIdData(referencedIdMapping,
                                                               null, referencedEntityName);

            // Generating the element mapping.
            MiddleComponentData 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.
            MiddleComponentData indexComponentData = AddIndex(null, null);

            // Generating the query generator - it should read directly from the related entity.
            IRelationQueryGenerator queryGenerator = new OneAuditEntityQueryGenerator(mainGenerator.GlobalCfg,
                                                                                      mainGenerator.VerEntCfg, referencingIdData, referencedEntityName,
                                                                                      referencedIdMapping.IdMapper);

            // Creating common mapper data.
            CommonCollectionMapperData 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).
                String auditMappedBy = propertyAuditingData.AuditMappedBy;

                // Creating a prefixed relation mapper.
                IIdMapper relMapper = referencingIdMapping.IdMapper.PrefixMappedProperties(
                    MappingTools.createToOneRelationPrefix(auditMappedBy));

                fakeBidirectionalRelationMapper = new ToOneIdMapper(
                    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, ModificationStore._NULL),
                    referencedEntityName, false);

                // Checking if there's an index defined. If so, adding a mapper for it.
                if (propertyAuditingData.PositionMappedBy != null)
                {
                    String positionMappedBy = propertyAuditingData.PositionMappedBy;
                    fakeBidirectionalRelationIndexMapper = new SinglePropertyMapper(new PropertyData(positionMappedBy, null, null, ModificationStore._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);
        }