Search in sources :

Example 1 with Version

use of javax.persistence.Version in project cuba by cuba-platform.

the class XMLConverter method encodeEntityInstance.

/**
 * Encodes the closure of a persistent instance into a XML element.
 *
 * @param visited
 * @param entity    the managed instance to be encoded. Can be null.
 * @param parent    the parent XML element to which the new XML element be added. Must not be null. Must be
 *                  owned by a document.
 * @param isRef
 * @param metaClass @return the new element. The element has been appended as a child to the given parent in this method.
 * @param view view on which loaded the entity
 */
private Element encodeEntityInstance(HashSet<Entity> visited, final Entity entity, final Element parent, boolean isRef, MetaClass metaClass, View view) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    if (!readPermitted(metaClass))
        return null;
    if (parent == null)
        throw new NullPointerException("No parent specified");
    Document doc = parent.getOwnerDocument();
    if (doc == null)
        throw new NullPointerException("No document specified");
    if (entity == null) {
        return encodeRef(parent, entity);
    }
    isRef |= !visited.add(entity);
    if (isRef) {
        return encodeRef(parent, entity);
    }
    Element root = doc.createElement(ELEMENT_INSTANCE);
    parent.appendChild(root);
    root.setAttribute(ATTR_ID, ior(entity));
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    List<MetaProperty> properties = ConverterHelper.getOrderedProperties(metaClass);
    for (MetaProperty property : properties) {
        Element child;
        if (!attrViewPermitted(metaClass, property.getName()))
            continue;
        if (!isPropertyIncluded(view, property, metadataTools)) {
            continue;
        }
        Object value = entity.getValue(property.getName());
        switch(property.getType()) {
            case DATATYPE:
                String nodeType;
                if (property.equals(metadataTools.getPrimaryKeyProperty(metaClass)) && !property.getJavaType().equals(String.class)) {
                    // skipping id for non-String-key entities
                    continue;
                } else if (property.getAnnotatedElement().isAnnotationPresent(Version.class)) {
                    nodeType = "version";
                } else {
                    nodeType = "basic";
                }
                child = doc.createElement(nodeType);
                child.setAttribute(ATTR_NAME, property.getName());
                if (value == null) {
                    encodeNull(child);
                } else {
                    String str = property.getRange().asDatatype().format(value);
                    encodeBasic(child, str, property.getJavaType());
                }
                break;
            case ENUM:
                child = doc.createElement("enum");
                child.setAttribute(ATTR_NAME, property.getName());
                if (value == null) {
                    encodeNull(child);
                } else {
                    // noinspection unchecked
                    String str = property.getRange().asEnumeration().format(value);
                    encodeBasic(child, str, property.getJavaType());
                }
                break;
            case COMPOSITION:
            case ASSOCIATION:
                {
                    MetaClass meta = propertyMetaClass(property);
                    // checks if the user permitted to read a property
                    if (!readPermitted(meta)) {
                        child = null;
                        break;
                    }
                    View propertyView = (view == null ? null : view.getProperty(property.getName()).getView());
                    if (!property.getRange().getCardinality().isMany()) {
                        boolean isEmbedded = property.getAnnotatedElement().isAnnotationPresent(Embedded.class);
                        child = doc.createElement(isEmbedded ? "embedded" : property.getRange().getCardinality().name().replace(UNDERSCORE, DASH).toLowerCase());
                        child.setAttribute(ATTR_NAME, property.getName());
                        if (isEmbedded) {
                            encodeEntityInstance(visited, (Entity) value, child, false, property.getRange().asClass(), propertyView);
                        } else {
                            encodeEntityInstance(visited, (Entity) value, child, false, property.getRange().asClass(), propertyView);
                        }
                    } else {
                        child = doc.createElement(getCollectionReferenceTag(property));
                        child.setAttribute(ATTR_NAME, property.getName());
                        child.setAttribute(ATTR_MEMBER_TYPE, typeOfEntityProperty(property));
                        if (value == null) {
                            encodeNull(child);
                            break;
                        }
                        Collection<?> members = (Collection<?>) value;
                        for (Object o : members) {
                            Element member = doc.createElement(ELEMENT_MEMBER);
                            child.appendChild(member);
                            if (o == null) {
                                encodeNull(member);
                            } else {
                                encodeEntityInstance(visited, (Entity) o, member, true, property.getRange().asClass(), propertyView);
                            }
                        }
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Unknown property type");
        }
        if (child != null) {
            root.appendChild(child);
        }
    }
    return root;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Version(javax.persistence.Version) Embedded(javax.persistence.Embedded) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with Version

use of javax.persistence.Version in project jirm by agentgt.

the class SqlParameterDefinition method parameterDef.

static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
    final SqlParameterDefinition definition;
    String sn = null;
    ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
    if (manyToOne != null) {
        Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
        JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
        SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
        checkState(!od.getIdParameters().isEmpty(), "No id parameters");
        if (joinColumn != null)
            sn = joinColumn.name();
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        FetchType fetch = manyToOne.fetch();
        int depth;
        if (FetchType.LAZY == fetch) {
            depth = 1;
        } else {
            depth = config.getMaximumLoadDepth();
        }
        SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
        definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
    } else {
        Column col = getAnnotation(objectType, parameterName, Column.class);
        if (col != null && !isNullOrEmpty(col.name()))
            sn = col.name();
        Id id = getAnnotation(objectType, parameterName, Id.class);
        Version version = getAnnotation(objectType, parameterName, Version.class);
        GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
        Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
        boolean idFlag = id != null;
        boolean versionFlag = version != null;
        boolean generatedFlag = generated != null;
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
    }
    return definition;
}
Also used : FetchType(javax.persistence.FetchType) ManyToOne(javax.persistence.ManyToOne) GeneratedValue(javax.persistence.GeneratedValue) Enumerated(javax.persistence.Enumerated) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) Version(javax.persistence.Version) Id(javax.persistence.Id)

Example 3 with Version

use of javax.persistence.Version in project hibernate-orm by hibernate.

the class AuditedPropertiesReader method fillPropertyData.

/**
 * Checks if a property is audited and if yes, fills all of its data.
 *
 * @param property Property to check.
 * @param propertyData Property data, on which to set this property's modification store.
 * @param accessType Access type for the property.
 *
 * @return False if this property is not audited.
 */
private boolean fillPropertyData(XProperty property, PropertyAuditingData propertyData, String accessType, Audited allClassAudited) {
    // check if a property is declared as not audited to exclude it
    // useful if a class is audited but some properties should be excluded
    final NotAudited unVer = property.getAnnotation(NotAudited.class);
    if ((unVer != null && !overriddenAuditedProperties.contains(property)) || overriddenNotAuditedProperties.contains(property)) {
        return false;
    } else {
        // is the optimistic locking field, don't audit it
        if (globalCfg.isDoNotAuditOptimisticLockingField()) {
            final Version jpaVer = property.getAnnotation(Version.class);
            if (jpaVer != null) {
                return false;
            }
        }
    }
    final String propertyName = propertyNamePrefix + property.getName();
    if (!this.checkAudited(property, propertyData, propertyName, allClassAudited, globalCfg.getModifiedFlagSuffix())) {
        return false;
    }
    validateLobMappingSupport(property);
    propertyData.setName(propertyName);
    propertyData.setBeanName(property.getName());
    propertyData.setAccessType(accessType);
    addPropertyJoinTables(property, propertyData);
    addPropertyAuditingOverrides(property, propertyData);
    if (!processPropertyAuditingOverrides(property, propertyData)) {
        // not audited due to AuditOverride annotation
        return false;
    }
    addPropertyMapKey(property, propertyData);
    setPropertyAuditMappedBy(property, propertyData);
    setPropertyRelationMappedBy(property, propertyData);
    return true;
}
Also used : NotAudited(org.hibernate.envers.NotAudited) Version(javax.persistence.Version)

Aggregations

Version (javax.persistence.Version)3 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Column (javax.persistence.Column)1 Embedded (javax.persistence.Embedded)1 Enumerated (javax.persistence.Enumerated)1 FetchType (javax.persistence.FetchType)1 GeneratedValue (javax.persistence.GeneratedValue)1 Id (javax.persistence.Id)1 JoinColumn (javax.persistence.JoinColumn)1 ManyToOne (javax.persistence.ManyToOne)1 NotAudited (org.hibernate.envers.NotAudited)1