Search in sources :

Example 1 with ClassMetadata

use of org.hibernate.metadata.ClassMetadata in project head by mifos.

the class InterceptHelper method populateAndMergeValueForObjectsOfTypeMasterDataEntityInCollections.

private void populateAndMergeValueForObjectsOfTypeMasterDataEntityInCollections(Object obj, String state, String name) {
    Class clazz = getClazz(obj);
    ClassMetadata customMeta = StaticHibernateUtil.getSessionFactory().getClassMetadata(clazz);
    setPrimaryKeyValueForCollectionTypeAndMerge(customMeta, obj, name, state);
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata)

Example 2 with ClassMetadata

use of org.hibernate.metadata.ClassMetadata in project head by mifos.

the class InterceptHelper method readFurtherMetaForCollectionType.

private void readFurtherMetaForCollectionType(Object obj, String firstName, String state) {
    Class l = getClazz(obj);
    ClassMetadata customMeta = StaticHibernateUtil.getSessionFactory().getClassMetadata(l);
    Object[] propertyValues = customMeta.getPropertyValues(obj, EntityMode.POJO);
    String[] propertyNames = customMeta.getPropertyNames();
    Type[] propertyTypes = customMeta.getPropertyTypes();
    setPrimaryKeyValueForCollectionType(customMeta, obj, firstName.concat(customMeta.getIdentifierPropertyName()), state);
    for (int i = 0; i < propertyNames.length; i++) {
        if (!propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && !propertyTypes[i].isCollectionType()) {
            if (state.equalsIgnoreCase(AuditConstants.TRANSACTIONBEGIN)) {
                String name = firstName.concat(propertyNames[i]);
                logger.debug("i readFurtherMetaForCollectionType : " + name + " : " + 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 readFurtherMetaForCollectionType : " + name + " : " + 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]);
                }
            }
        }
        if (propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && propertyValues[i] instanceof MasterDataEntity && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            populateValueForObjectsOfTypeMasterDataEntityInCollections(propertyValues[i], state, firstName.concat(propertyNames[i]));
        }
        if (propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && !(propertyValues[i] instanceof MasterDataEntity) && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            Object object = propertyValues[i];
            if (object != null) {
                if (object instanceof MeetingBO) {
                    MeetingBO meeting = (MeetingBO) object;
                    if (propertyNames[i].equalsIgnoreCase("meeting") && meeting.getMeetingId() != null) {
                        readMeetingCollection(meeting, propertyNames[i], state);
                    } else {
                        readFurtherMetaForCollectionType(object, propertyNames[i], state);
                    }
                } else {
                    readFurtherMetaForCollectionType(object, propertyNames[i], state);
                }
            }
        }
        // Reading further component type
        if (!propertyTypes[i].isEntityType() && propertyTypes[i].isComponentType() && !(propertyValues[i] instanceof MasterDataEntity) && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            Object obj1 = propertyValues[i];
            if (obj1 != null) {
                readComponenetTypeInCollectionTypeWithoutMerge(obj1, propertyNames[i], state, propertyTypes[i]);
            }
        }
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) MasterDataEntity(org.mifos.application.master.business.MasterDataEntity) MeetingBO(org.mifos.application.meeting.business.MeetingBO) Calendar(java.util.Calendar) Date(java.util.Date) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) AbstractBusinessObject(org.mifos.framework.business.AbstractBusinessObject)

Example 3 with ClassMetadata

use of org.hibernate.metadata.ClassMetadata in project head by mifos.

the class InterceptHelper method readAndMergeCollectionTypes.

private void readAndMergeCollectionTypes(Object obj, String firstName, String parentName, String state) {
    Class l = getClazz(obj);
    ClassMetadata customMeta = StaticHibernateUtil.getSessionFactory().getClassMetadata(l);
    Object[] propertyValues = customMeta.getPropertyValues(obj, EntityMode.POJO);
    String[] propertyNames = customMeta.getPropertyNames();
    Type[] propertyTypes = customMeta.getPropertyTypes();
    setPrimaryKeyValueForCollectionTypeAndMerge(customMeta, obj, firstName.concat(customMeta.getIdentifierPropertyName()), state);
    for (int i = 0; i < propertyNames.length; i++) {
        logger.debug("property Name : " + propertyNames[i] + " value : " + propertyValues[i]);
        if (!propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && !propertyTypes[i].isCollectionType()) {
            if (state.equalsIgnoreCase(AuditConstants.TRANSACTIONBEGIN)) {
                String name = firstName.concat(propertyNames[i]);
                logger.debug("i readFurtherMetaForCollectionType " + name + " value : " + propertyValues[i]);
                if (isValueLoggable(propertyNames[i], firstName)) {
                    if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) {
                        String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, propertyValues[i], localeId);
                        if (initialArray.toString().trim().length() == 0 || initialArray.toString().endsWith(",")) {
                            initialArray.append(value);
                        } else if (value.trim().length() != 0) {
                            initialArray.append("-").append(value);
                        }
                    } else {
                        if (propertyValues[i] != null) {
                            if (initialArray.toString().trim().length() == 0 || initialArray.toString().endsWith(",")) {
                                initialArray.append(propertyValues[i]);
                            } else if (propertyValues[i].toString().trim().length() != 0) {
                                initialArray.append("-").append(propertyValues[i]);
                            }
                        }
                    }
                }
            } else {
                String name = firstName.concat(propertyNames[i].toString());
                logger.debug("c readFurtherMetaForCollectionType " + name + " value : " + propertyValues[i]);
                if (isValueLoggable(propertyNames[i], firstName)) {
                    if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) {
                        String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, propertyValues[i], localeId);
                        if (changeArray.toString().trim().length() == 0 || changeArray.toString().endsWith(",")) {
                            changeArray.append(value);
                        } else if (value.trim().length() != 0) {
                            changeArray.append("-").append(value);
                        }
                    } else {
                        if (propertyValues[i] != null) {
                            if (changeArray.toString().trim().length() == 0 || changeArray.toString().endsWith(",")) {
                                changeArray.append(propertyValues[i]);
                            } else if (propertyValues[i].toString().trim().length() != 0) {
                                changeArray.append("-").append(propertyValues[i]);
                            }
                        }
                    }
                }
            }
        }
        if (propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && propertyValues[i] instanceof MasterDataEntity && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            populateAndMergeValueForObjectsOfTypeMasterDataEntityInCollections(propertyValues[i], state, firstName.concat(propertyNames[i]));
        }
        if (propertyTypes[i].isEntityType() && !propertyTypes[i].isComponentType() && !(propertyValues[i] instanceof MasterDataEntity) && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            Object object = propertyValues[i];
            if (object != null) {
                if (object instanceof MeetingBO) {
                    MeetingBO meeting = (MeetingBO) object;
                    if (propertyNames[i].equalsIgnoreCase("meeting") && meeting.getMeetingId() != null) {
                        readAndMergeMeetingCollection(meeting, propertyNames[i], state);
                    } else {
                        readAndMergeCollectionTypes(object, propertyNames[i], firstName, state);
                    }
                } else {
                    readAndMergeCollectionTypes(object, propertyNames[i], firstName, state);
                }
            }
        }
        // Reading further component type
        if (!propertyTypes[i].isEntityType() && propertyTypes[i].isComponentType() && !(propertyValues[i] instanceof MasterDataEntity) && AuditConfiguration.isObjectToBeLogged(entityName, propertyNames[i], firstName)) {
            Object obj1 = propertyValues[i];
            if (obj1 != null) {
                readComponenetTypeInCollectionTypeWithMerge(obj1, propertyNames[i], state, propertyTypes[i]);
            }
        }
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) MasterDataEntity(org.mifos.application.master.business.MasterDataEntity) MeetingBO(org.mifos.application.meeting.business.MeetingBO) AbstractBusinessObject(org.mifos.framework.business.AbstractBusinessObject)

Example 4 with ClassMetadata

use of org.hibernate.metadata.ClassMetadata in project yyl_example by Relucent.

the class HibernateTest method test3.

private static void test3(SessionFactory sessionFactory) {
    System.out.println("--------------------------------------------------");
    ClassMetadata metadata = sessionFactory.getClassMetadata(Table1.class);
    System.out.println("EntityName: " + metadata.getMappedClass());
    System.out.println("IdProperty: " + metadata.getIdentifierPropertyName());
    for (String propertyName : metadata.getPropertyNames()) {
        Type type = metadata.getPropertyType(propertyName);
        System.out.println("Property: " + propertyName);
        System.out.println(" isComponentType: " + type.isComponentType());
        System.out.println(" isAnyType: " + type.isAnyType());
        System.out.println(" isCollection: " + type.isCollectionType());
        System.out.println(" isAssociationType: " + type.isAssociationType());
        if (type instanceof AssociationType) {
            AssociationType associationType = (AssociationType) type;
            String associatedEntityName = associationType.getAssociatedEntityName((SessionFactoryImpl) sessionFactory);
            ClassMetadata associatedClassMetadata = sessionFactory.getClassMetadata(associatedEntityName);
            Class<?> mappedClass = associatedClassMetadata.getMappedClass();
            System.out.println(" associatedClass: " + mappedClass);
        } else {
            System.out.println(" propertyClass: " + type.getReturnedClass());
        }
        System.out.println();
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType)

Example 5 with ClassMetadata

use of org.hibernate.metadata.ClassMetadata in project midpoint by Evolveum.

the class RUtil method fixCompositeIdentifierInMetaModel.

private static void fixCompositeIdentifierInMetaModel(SessionFactory sessionFactory, Class clazz) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(clazz);
    if (classMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) classMetadata;
        EntityMetamodel model = persister.getEntityMetamodel();
        IdentifierProperty identifier = model.getIdentifierProperty();
        try {
            Field field = IdentifierProperty.class.getDeclaredField("hasIdentifierMapper");
            field.setAccessible(true);
            field.set(identifier, true);
            field.setAccessible(false);
        } catch (Exception ex) {
            throw new SystemException("Attempt to fix entity meta model with hack failed, reason: " + ex.getMessage(), ex);
        }
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) Field(java.lang.reflect.Field) SystemException(com.evolveum.midpoint.util.exception.SystemException) IdentifierProperty(org.hibernate.tuple.IdentifierProperty) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) AbstractEntityPersister(org.hibernate.persister.entity.AbstractEntityPersister) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SystemException(com.evolveum.midpoint.util.exception.SystemException)

Aggregations

ClassMetadata (org.hibernate.metadata.ClassMetadata)61 IClassMetadata (org.jboss.tools.hibernate.runtime.spi.IClassMetadata)24 PersistentClass (org.hibernate.mapping.PersistentClass)19 Test (org.junit.Test)19 RootClass (org.hibernate.mapping.RootClass)16 SessionFactory (org.hibernate.SessionFactory)11 Type (org.hibernate.type.Type)10 Configuration (org.hibernate.cfg.Configuration)8 ISessionFactory (org.jboss.tools.hibernate.runtime.spi.ISessionFactory)8 Before (org.junit.Before)8 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)7 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)7 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)7 Field (java.lang.reflect.Field)5 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)5 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)5 ArrayList (java.util.ArrayList)4 Session (org.hibernate.Session)4 SessionImpl (org.hibernate.internal.SessionImpl)4 ComponentType (org.hibernate.type.ComponentType)4