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));
}
}
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);
}
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);
}
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]);
}
}
}
}
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();
}
}
Aggregations