Search in sources :

Example 1 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class SimpleAuditExpression method addToQuery.

@Override
protected void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) {
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(enversService, entityName, propertyName);
    if (relatedEntity == null) {
        // HHH-9178 - Add support to component type equality.
        // This basically will allow = and <> operators to perform component-based equality checks.
        // Any other operator for a component type will not be supported.
        // Non-component types will continue to behave normally.
        final SessionImplementor session = versionsReader.getSessionImplementor();
        final Type type = getPropertyType(session, entityName, propertyName);
        if (type != null && type.isComponentType()) {
            if (!"=".equals(op) && !"<>".equals(op)) {
                throw new AuditException("Component-based criterion is not supported for op: " + op);
            }
            final ComponentType componentType = (ComponentType) type;
            for (int i = 0; i < componentType.getPropertyNames().length; i++) {
                final Object componentValue = componentType.getPropertyValue(value, i, session);
                parameters.addWhereWithParam(alias, propertyName + "_" + componentType.getPropertyNames()[i], op, componentValue);
            }
        } else {
            parameters.addWhereWithParam(alias, propertyName, op, value);
        }
    } else {
        if (!"=".equals(op) && !"<>".equals(op)) {
            throw new AuditException("This type of operation: " + op + " (" + entityName + "." + propertyName + ") isn't supported and can't be used in queries.");
        }
        Object id = relatedEntity.getIdMapper().mapToIdFromEntity(value);
        relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, alias, null, "=".equals(op));
    }
}
Also used : ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) AuditException(org.hibernate.envers.exception.AuditException)

Example 2 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method addValueInSecondPass.

private void addValueInSecondPass(Element parent, Value value, CompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData, boolean insertable, boolean processModifiedFlag) {
    final Type type = value.getType();
    if (type instanceof ComponentType) {
        componentMetadataGenerator.addComponent(parent, propertyAuditingData, value, currentMapper, entityName, xmlMappingData, false);
        // mod flag field has been already generated in first pass
        return;
    } else if (type instanceof ManyToOneType) {
        toOneRelationMetadataGenerator.addToOne(parent, propertyAuditingData, value, currentMapper, entityName, insertable);
    } else if (type instanceof OneToOneType) {
        final OneToOne oneToOne = (OneToOne) value;
        if (oneToOne.getReferencedPropertyName() != null) {
            toOneRelationMetadataGenerator.addOneToOneNotOwning(propertyAuditingData, value, currentMapper, entityName);
        } else {
            // @OneToOne relation marked with @PrimaryKeyJoinColumn
            toOneRelationMetadataGenerator.addOneToOnePrimaryKeyJoinColumn(propertyAuditingData, value, currentMapper, entityName, insertable);
        }
    } else if (type instanceof CollectionType) {
        final CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this, (Collection) value, currentMapper, entityName, xmlMappingData, propertyAuditingData);
        collectionMetadataGenerator.addCollection();
    } else {
        return;
    }
    addModifiedFlagIfNeeded(parent, propertyAuditingData, processModifiedFlag);
}
Also used : CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) OneToOneType(org.hibernate.type.OneToOneType) TimestampType(org.hibernate.type.TimestampType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) OneToOne(org.hibernate.mapping.OneToOne) ManyToOneType(org.hibernate.type.ManyToOneType) CollectionType(org.hibernate.type.CollectionType) Collection(org.hibernate.mapping.Collection) OneToOneType(org.hibernate.type.OneToOneType)

Example 3 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method addValueInFirstPass.

private void addValueInFirstPass(Element parent, Value value, CompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData, boolean insertable, boolean processModifiedFlag) {
    final Type type = value.getType();
    final boolean isBasic = basicMetadataGenerator.addBasic(parent, propertyAuditingData, value, currentMapper, insertable, false);
    if (isBasic) {
    // The property was mapped by the basic generator.
    } else if (type instanceof ComponentType) {
        componentMetadataGenerator.addComponent(parent, propertyAuditingData, value, currentMapper, entityName, xmlMappingData, true);
    } else {
        if (!processedInSecondPass(type)) {
            // If we got here in the first pass, it means the basic mapper didn't map it, and none of the
            // above branches either.
            throwUnsupportedTypeException(type, entityName, propertyAuditingData.getName());
        }
        return;
    }
    addModifiedFlagIfNeeded(parent, propertyAuditingData, processModifiedFlag);
}
Also used : CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) ComponentType(org.hibernate.type.ComponentType) OneToOneType(org.hibernate.type.OneToOneType) TimestampType(org.hibernate.type.TimestampType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType)

Example 4 with ComponentType

use of org.hibernate.type.ComponentType in project head by mifos.

the class InterceptHelper method readComponenetTypeInCollectionTypeWithoutMerge.

private void readComponenetTypeInCollectionTypeWithoutMerge(Object obj, String firstName, String state, Type propertyType) {
    ComponentType componentType = (ComponentType) propertyType;
    Object[] propertyValues = componentType.getPropertyValues(obj, EntityMode.POJO);
    String[] propertyNames = componentType.getPropertyNames();
    for (int i = 0; i < propertyNames.length; i++) {
        if (state.equalsIgnoreCase(AuditConstants.TRANSACTIONBEGIN)) {
            String name = firstName.concat(propertyNames[i]);
            logger.debug("i readComponenetTypeInCollectionTypeWithoutMerge : " + name + " value : " + propertyValues[i]);
            String oldValue = getOldValueToKey(initialValues, name);
            if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) {
                String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, propertyValues[i], localeId);
                if (!oldValue.equals("")) {
                    initialValues.put(name, value.concat(",").concat(oldValue));
                } else {
                    initialValues.put(name, value);
                }
            } else {
                if (propertyValues[i] instanceof Calendar && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        initialValues.put(name, ((Calendar) propertyValues[i]).getTime().toString().concat(",").concat(oldValue));
                    } else {
                        initialValues.put(name, ((Calendar) propertyValues[i]).getTime());
                    }
                } else if (!(propertyValues[i] instanceof Calendar) && !(propertyValues[i] instanceof Date) && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        initialValues.put(name, propertyValues[i].toString().concat(",").concat(oldValue));
                    } else {
                        initialValues.put(name, propertyValues[i]);
                    }
                } else if (propertyValues[i] instanceof Date && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        try {
                            Date date = (Date) propertyValues[i];
                            initialValues.put(name, DateUtils.getUserLocaleDate(locale, new java.sql.Date(date.getTime()).toString()).toString().concat(",").concat(oldValue));
                        } catch (Exception e) {
                            initialValues.put(name, propertyValues[i].toString().concat(",").concat(oldValue));
                        }
                    } else {
                        try {
                            Date date = (Date) propertyValues[i];
                            initialValues.put(name, DateUtils.getUserLocaleDate(locale, new java.sql.Date(date.getTime()).toString()));
                        } catch (Exception e) {
                            initialValues.put(name, propertyValues[i].toString());
                        }
                    }
                } else {
                    if (!oldValue.equals("")) {
                        initialValues.put(name, oldValue);
                    } else {
                        initialValues.put(name, propertyValues[i]);
                    }
                }
            }
            String columnName = AuditConfiguration.getColumnNameForPropertyName(entityName, name);
            if (columnName != null && !columnName.equals("")) {
                columnNames.put(name, columnName);
            } else {
                columnNames.put(name, propertyNames[i]);
            }
        } else {
            String name = firstName.concat(propertyNames[i].toString());
            logger.debug("c readComponenetTypeInCollectionTypeWithoutMerge : " + name + " value : " + propertyValues[i]);
            String oldValue = getOldValueToKey(changedValues, name);
            if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) {
                String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, propertyValues[i], localeId);
                if (!value.equals("")) {
                    changedValues.put(name, value.concat(",").concat(oldValue));
                } else {
                    changedValues.put(name, oldValue);
                }
            } else {
                if (propertyValues[i] instanceof Calendar && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        changedValues.put(name, ((Calendar) propertyValues[i]).getTime().toString().concat(",").concat(oldValue));
                    } else {
                        changedValues.put(name, ((Calendar) propertyValues[i]).getTime());
                    }
                } else if (!(propertyValues[i] instanceof Calendar) && !(propertyValues[i] instanceof Date) && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        changedValues.put(name, propertyValues[i].toString().concat(",").concat(oldValue));
                    } else {
                        changedValues.put(name, propertyValues[i]);
                    }
                } else if (propertyValues[i] instanceof Date && propertyValues[i] != null) {
                    if (!oldValue.equals("")) {
                        try {
                            Date date = (Date) propertyValues[i];
                            changedValues.put(name, DateUtils.getUserLocaleDate(locale, new java.sql.Date(date.getTime()).toString()).toString().concat(",").concat(oldValue));
                        } catch (Exception e) {
                            changedValues.put(name, propertyValues[i].toString().concat(",").concat(oldValue));
                        }
                    } else {
                        try {
                            Date date = (Date) propertyValues[i];
                            changedValues.put(name, DateUtils.getUserLocaleDate(locale, new java.sql.Date(date.getTime()).toString()));
                        } catch (Exception e) {
                            changedValues.put(name, propertyValues[i].toString());
                        }
                    }
                } else {
                    if (!oldValue.equals("")) {
                        changedValues.put(name, oldValue);
                    } else {
                        changedValues.put(name, propertyValues[i]);
                    }
                }
            }
            String columnName = AuditConfiguration.getColumnNameForPropertyName(entityName, name);
            if (columnName != null && !columnName.equals("")) {
                columnNames.put(name, columnName);
            } else {
                columnNames.put(name, propertyNames[i]);
            }
        }
    }
}
Also used : ComponentType(org.hibernate.type.ComponentType) Calendar(java.util.Calendar) AbstractBusinessObject(org.mifos.framework.business.AbstractBusinessObject) Date(java.util.Date)

Example 5 with ComponentType

use of org.hibernate.type.ComponentType in project hibernate-orm by hibernate.

the class QueryParameters method processFilters.

@SuppressWarnings({ "unchecked" })
public void processFilters(String sql, Map filters, SessionFactoryImplementor factory) {
    if (filters.size() == 0 || !sql.contains(ParserHelper.HQL_VARIABLE_PREFIX)) {
        // HELLA IMPORTANT OPTIMIZATION!!!
        processedPositionalParameterValues = getPositionalParameterValues();
        processedPositionalParameterTypes = getPositionalParameterTypes();
        processedSQL = sql;
    } else {
        final StringTokenizer tokens = new StringTokenizer(sql, SYMBOLS, true);
        StringBuilder result = new StringBuilder();
        List parameters = new ArrayList();
        List parameterTypes = new ArrayList();
        int positionalIndex = 0;
        while (tokens.hasMoreTokens()) {
            final String token = tokens.nextToken();
            if (token.startsWith(ParserHelper.HQL_VARIABLE_PREFIX)) {
                final String filterParameterName = token.substring(1);
                final String[] parts = LoadQueryInfluencers.parseFilterParameterName(filterParameterName);
                final FilterImpl filter = (FilterImpl) filters.get(parts[0]);
                final Object value = filter.getParameter(parts[1]);
                final Type type = filter.getFilterDefinition().getParameterType(parts[1]);
                if (value != null && Collection.class.isAssignableFrom(value.getClass())) {
                    Iterator itr = ((Collection) value).iterator();
                    while (itr.hasNext()) {
                        final Object elementValue = itr.next();
                        result.append('?');
                        parameters.add(elementValue);
                        parameterTypes.add(type);
                        if (itr.hasNext()) {
                            result.append(", ");
                        }
                    }
                } else {
                    result.append('?');
                    parameters.add(value);
                    parameterTypes.add(type);
                }
            } else {
                result.append(token);
                if ("?".equals(token) && positionalIndex < getPositionalParameterValues().length) {
                    final Type type = getPositionalParameterTypes()[positionalIndex];
                    if (type.isComponentType()) {
                        // should process tokens till reaching the number of "?" corresponding to the
                        // numberOfParametersCoveredBy of the compositeType
                        int paramIndex = 1;
                        final int numberOfParametersCoveredBy = getNumberOfParametersCoveredBy(((ComponentType) type).getSubtypes());
                        while (paramIndex < numberOfParametersCoveredBy) {
                            final String nextToken = tokens.nextToken();
                            if ("?".equals(nextToken)) {
                                paramIndex++;
                            }
                            result.append(nextToken);
                        }
                    }
                    parameters.add(getPositionalParameterValues()[positionalIndex]);
                    parameterTypes.add(type);
                    positionalIndex++;
                }
            }
        }
        processedPositionalParameterValues = parameters.toArray();
        processedPositionalParameterTypes = (Type[]) parameterTypes.toArray(new Type[parameterTypes.size()]);
        processedSQL = result.toString();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) FilterImpl(org.hibernate.internal.FilterImpl) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ComponentType (org.hibernate.type.ComponentType)22 Component (org.hibernate.mapping.Component)9 Type (org.hibernate.type.Type)9 Test (org.junit.Test)9 RootClass (org.hibernate.mapping.RootClass)8 ComponentMetamodel (org.hibernate.tuple.component.ComponentMetamodel)8 ClassType (org.hibernate.type.ClassType)8 IType (org.jboss.tools.hibernate.runtime.spi.IType)8 ManyToOneType (org.hibernate.type.ManyToOneType)5 MetadataSources (org.hibernate.boot.MetadataSources)4 MetadataBuilderImpl (org.hibernate.boot.internal.MetadataBuilderImpl)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 MetadataBuildingOptions (org.hibernate.boot.spi.MetadataBuildingOptions)4 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 CollectionType (org.hibernate.type.CollectionType)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 OneToOneType (org.hibernate.type.OneToOneType)2