Search in sources :

Example 1 with Element

use of org.dom4j.Element in project hibernate-orm by hibernate.

the class IdMetadataGenerator method addId.

@SuppressWarnings({ "unchecked" })
IdMappingData addId(PersistentClass pc, boolean audited) {
    // Xml mapping which will be used for relations
    final Element relIdMapping = new DefaultElement("properties");
    // Xml mapping which will be used for the primary key of the versions table
    final Element origIdMapping = new DefaultElement("composite-id");
    final Property idProp = pc.getIdentifierProperty();
    final Component idMapper = pc.getIdentifierMapper();
    // Checking if the id mapping is supported
    if (idMapper == null && idProp == null) {
        return null;
    }
    SimpleIdMapperBuilder mapper;
    if (idMapper != null) {
        // Multiple id
        final Class componentClass = ReflectionTools.loadClass(((Component) pc.getIdentifier()).getComponentClassName(), mainGenerator.getClassLoaderService());
        mapper = new MultipleIdMapper(componentClass, pc.getServiceRegistry());
        if (!addIdProperties(relIdMapping, (Iterator<Property>) idMapper.getPropertyIterator(), mapper, false, audited)) {
            return null;
        }
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        if (!addIdProperties(origIdMapping, (Iterator<Property>) idMapper.getPropertyIterator(), null, true, audited)) {
            return null;
        }
    } else if (idProp.isComposite()) {
        // Embedded id
        final Component idComponent = (Component) idProp.getValue();
        final Class embeddableClass = ReflectionTools.loadClass(idComponent.getComponentClassName(), mainGenerator.getClassLoaderService());
        mapper = new EmbeddedIdMapper(getIdPropertyData(idProp), embeddableClass, pc.getServiceRegistry());
        if (!addIdProperties(relIdMapping, (Iterator<Property>) idComponent.getPropertyIterator(), mapper, false, audited)) {
            return null;
        }
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        if (!addIdProperties(origIdMapping, (Iterator<Property>) idComponent.getPropertyIterator(), null, true, audited)) {
            return null;
        }
    } else {
        // Single id
        mapper = new SingleIdMapper(pc.getServiceRegistry());
        // Last but one parameter: ids are always insertable
        mainGenerator.getBasicMetadataGenerator().addBasic(relIdMapping, getIdPersistentPropertyAuditingData(idProp), idProp.getValue(), mapper, true, false);
        // null mapper - the mapping where already added the first time, now we only want to generate the xml
        mainGenerator.getBasicMetadataGenerator().addBasic(origIdMapping, getIdPersistentPropertyAuditingData(idProp), idProp.getValue(), null, true, true);
    }
    origIdMapping.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName());
    // Adding a relation to the revision entity (effectively: the "revision number" property)
    mainGenerator.addRevisionInfoRelation(origIdMapping);
    return new IdMappingData(mapper, origIdMapping, relIdMapping);
}
Also used : SingleIdMapper(org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper) DefaultElement(org.dom4j.tree.DefaultElement) SimpleIdMapperBuilder(org.hibernate.envers.internal.entities.mapper.id.SimpleIdMapperBuilder) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Iterator(java.util.Iterator) PersistentClass(org.hibernate.mapping.PersistentClass) Component(org.hibernate.mapping.Component) EmbeddedIdMapper(org.hibernate.envers.internal.entities.mapper.id.EmbeddedIdMapper) Property(org.hibernate.mapping.Property) MultipleIdMapper(org.hibernate.envers.internal.entities.mapper.id.MultipleIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Example 2 with Element

use of org.dom4j.Element in project hibernate-orm by hibernate.

the class MetadataTools method createSubclassEntity.

public static Element createSubclassEntity(Document document, String subclassType, AuditTableData auditTableData, String extendsEntityName, String discriminatorValue, Boolean isAbstract) {
    final Element classMapping = createEntityCommon(document, subclassType, auditTableData, discriminatorValue, isAbstract);
    classMapping.addAttribute("extends", extendsEntityName);
    return classMapping;
}
Also used : Element(org.dom4j.Element)

Example 3 with Element

use of org.dom4j.Element in project hibernate-orm by hibernate.

the class MetadataTools method addColumn.

public static Element addColumn(Element parent, String name, Integer length, Integer scale, Integer precision, String sqlType, String customRead, String customWrite, boolean quoted) {
    final Element columnMapping = parent.addElement("column");
    columnMapping.addAttribute("name", quoted ? "`" + name + "`" : name);
    if (length != null) {
        columnMapping.addAttribute("length", length.toString());
    }
    if (scale != null) {
        columnMapping.addAttribute("scale", Integer.toString(scale));
    }
    if (precision != null) {
        columnMapping.addAttribute("precision", Integer.toString(precision));
    }
    if (!StringTools.isEmpty(sqlType)) {
        columnMapping.addAttribute("sql-type", sqlType);
    }
    if (!StringTools.isEmpty(customRead)) {
        columnMapping.addAttribute("read", customRead);
    }
    if (!StringTools.isEmpty(customWrite)) {
        columnMapping.addAttribute("write", customWrite);
    }
    return columnMapping;
}
Also used : Element(org.dom4j.Element)

Example 4 with Element

use of org.dom4j.Element in project hibernate-orm by hibernate.

the class MetadataTools method addProperty.

public static Element addProperty(Element parent, String name, String type, boolean insertable, boolean updateable, boolean key) {
    final Element propMapping;
    if (key) {
        propMapping = parent.addElement("key-property");
    } else {
        propMapping = parent.addElement("property");
        propMapping.addAttribute("insert", Boolean.toString(insertable));
        propMapping.addAttribute("update", Boolean.toString(updateable));
    }
    propMapping.addAttribute("name", name);
    if (type != null) {
        propMapping.addAttribute("type", type);
    }
    return propMapping;
}
Also used : Element(org.dom4j.Element)

Example 5 with Element

use of org.dom4j.Element in project hibernate-orm by hibernate.

the class ToOneRelationMetadataGenerator method addToOne.

@SuppressWarnings({ "unchecked" })
void addToOne(Element parent, PropertyAuditingData propertyAuditingData, Value value, CompositeMapperBuilder mapper, String entityName, boolean insertable) {
    final String referencedEntityName = ((ToOne) value).getReferencedEntityName();
    final IdMappingData idMapping = mainGenerator.getReferencedIdMappingData(entityName, referencedEntityName, propertyAuditingData, true);
    final String lastPropertyPrefix = MappingTools.createToOneRelationPrefix(propertyAuditingData.getName());
    // Generating the id mapper for the relation
    final IdMapper relMapper = idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
    // Storing information about this relation
    mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(propertyAuditingData.getName(), referencedEntityName, relMapper, insertable, MappingTools.ignoreNotFound(value));
    // 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.
    boolean nonInsertableFake;
    if (!insertable && propertyAuditingData.isForceInsertable()) {
        nonInsertableFake = true;
        insertable = true;
    } else {
        nonInsertableFake = false;
    }
    // Adding an element to the mapping corresponding to the references entity id's
    final Element properties = (Element) idMapping.getXmlRelationMapping().clone();
    properties.addAttribute("name", propertyAuditingData.getName());
    MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, MetadataTools.getColumnNameIterator(value.getColumnIterator()), false, insertable);
    // Extracting related id properties from properties tag
    for (Object o : properties.content()) {
        final Element element = (Element) o;
        element.setParent(null);
        parent.add(element);
    }
    // Adding mapper for the id
    final PropertyData propertyData = propertyAuditingData.getPropertyData();
    mapper.addComposite(propertyData, new ToOneIdMapper(relMapper, propertyData, referencedEntityName, nonInsertableFake));
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Element(org.dom4j.Element) OneToOne(org.hibernate.mapping.OneToOne) ToOne(org.hibernate.mapping.ToOne) IdMapper(org.hibernate.envers.internal.entities.mapper.id.IdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) ToOneIdMapper(org.hibernate.envers.internal.entities.mapper.relation.ToOneIdMapper) IdMappingData(org.hibernate.envers.internal.entities.IdMappingData)

Aggregations

Element (org.dom4j.Element)2191 Document (org.dom4j.Document)495 ArrayList (java.util.ArrayList)296 List (java.util.List)246 SAXReader (org.dom4j.io.SAXReader)194 Iterator (java.util.Iterator)156 IQ (org.xmpp.packet.IQ)142 HashMap (java.util.HashMap)134 IOException (java.io.IOException)114 File (java.io.File)101 StringReader (java.io.StringReader)88 Attribute (org.dom4j.Attribute)88 DefaultElement (org.dom4j.tree.DefaultElement)87 JID (org.xmpp.packet.JID)87 Test (org.junit.jupiter.api.Test)78 DocumentException (org.dom4j.DocumentException)74 QName (org.dom4j.QName)68 Test (org.junit.Test)65 AnnotatedElement (java.lang.reflect.AnnotatedElement)64 Map (java.util.Map)63