Example usage for org.hibernate.type Type isAnyType

List of usage examples for org.hibernate.type Type isAnyType

Introduction

In this page you can find the example usage for org.hibernate.type Type isAnyType.

Prototype

boolean isAnyType();

Source Link

Document

Return true if the implementation is castable to AnyType .

Usage

From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java

License:Open Source License

private void cascadeAssociation(final Object child, final Type type, final CascadeStyle style,
        final Object anything, final boolean isCascadeDeleteEnabled) {
    if (type.isEntityType() || type.isAnyType()) {
        cascadeToOne(child, type, style, anything, isCascadeDeleteEnabled);
    } else if (type.isCollectionType()) {
        cascadeCollection(child, style, anything, (CollectionType) type);
    }//from w ww .ja  v  a  2 s.  c o m
}

From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java

License:Open Source License

/**
 * Cascade an action to a collection/*from w  w w. ja  v a  2  s. co  m*/
 */
private void cascadeCollection(final Object child, final CascadeStyle style, final Object anything,
        final CollectionType type) {
    CollectionPersister persister = eventSource.getFactory().getCollectionPersister(type.getRole());
    Type elemType = persister.getElementType();

    final int oldCascadeTo = cascadeTo;
    if (cascadeTo == AFTER_INSERT_BEFORE_DELETE) {
        cascadeTo = AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION;
    }

    //cascade to current collection elements
    if (elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType()) {
        cascadeCollectionElements(child, type, style, elemType, anything, persister.isCascadeDeleteEnabled());
    }

    cascadeTo = oldCascadeTo;
}

From source file:com.reignite.parser.Join.java

License:Open Source License

/**
 * Gets all the basic fields of the criteria entity
 * /*from   ww w  .ja  v  a 2  s .  c  o  m*/
 * @throws ClassNotFoundException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws IntrospectionException
 */
public void populateFields()
        throws ClassNotFoundException, NoSuchMethodException, SecurityException, IntrospectionException {
    Class<?> clazz = Class.forName(entity);
    Method getter = clazz.getMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1));

    Class<?> joined = (Class<?>) ((ParameterizedType) getter.getGenericReturnType())
            .getActualTypeArguments()[0];

    ClassMetadata metadata = session.getSessionFactory().getClassMetadata(joined);
    expectedFields.add(name + "." + metadata.getIdentifierPropertyName());
    projections.add(Projections.property(name + "." + metadata.getIdentifierPropertyName()));
    for (String fieldName : metadata.getPropertyNames()) {
        Type type = metadata.getPropertyType(fieldName);
        if (!type.isAnyType() && !type.isAssociationType() && !type.isComponentType()
                && !type.isCollectionType()) {
            String field = name + "." + fieldName;
            expectedFields.add(field);
            projections.add(Projections.property(field));
        }
    }
}

From source file:org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java

License:Apache License

protected void buildProperties(Class<?> targetClass, ForeignKey foreignField,
        ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties,
        MergedPropertyType mergedPropertyType, Map<String, FieldPresentationAttributes> presentationAttributes,
        List<Property> componentProperties, Map<String, FieldMetadata> fields, List<String> propertyNames,
        List<Type> propertyTypes, String idProperty, Boolean populateManyToOneFields, String[] includeFields,
        String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname,
        List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded)
        throws HibernateException, ClassNotFoundException, SecurityException, IllegalArgumentException,
        NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    int j = 0;// w  ww . j  a  v  a2  s  . co m
    for (String propertyName : propertyNames) {
        Type type = propertyTypes.get(j);
        boolean isPropertyForeignKey = testForeignProperty(foreignField, prefix, propertyName);
        int additionalForeignKeyIndexPosition = findAdditionalForeignKeyIndex(additionalForeignFields, prefix,
                propertyName);
        j++;
        if (!type.isAnyType() && !type.isCollectionType() || isPropertyForeignKey
                || additionalForeignKeyIndexPosition >= 0 || presentationAttributes.containsKey(propertyName)) {
            FieldPresentationAttributes presentationAttribute = presentationAttributes.get(propertyName);
            Boolean amIExcluded = isParentExcluded || !testPropertyInclusion(presentationAttribute);
            Boolean includeField = testPropertyRecursion(prefix, parentClasses, propertyName, targetClass,
                    ceilingEntityFullyQualifiedClassname);

            SupportedFieldType explicitType = null;
            if (presentationAttribute != null) {
                explicitType = presentationAttribute.getExplicitFieldType();
            }
            Class<?> returnedClass = type.getReturnedClass();
            checkProp: {
                if (type.isComponentType() && includeField) {
                    buildComponentProperties(targetClass, foreignField, additionalForeignFields,
                            additionalNonPersistentProperties, mergedPropertyType, fields, idProperty,
                            populateManyToOneFields, includeFields, excludeFields, configurationKey,
                            ceilingEntityFullyQualifiedClassname, propertyName, type, returnedClass,
                            parentClasses, amIExcluded, prefix);
                    break checkProp;
                }
                /*
                 * Currently we do not support ManyToOne fields whose class type is the same
                 * as the target type, since this forms an infinite loop and will cause a stack overflow.
                 */
                if (type.isEntityType() && !returnedClass.isAssignableFrom(targetClass)
                        && populateManyToOneFields && includeField) {
                    buildEntityProperties(fields, foreignField, additionalForeignFields,
                            additionalNonPersistentProperties, populateManyToOneFields, includeFields,
                            excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, propertyName,
                            returnedClass, targetClass, parentClasses, prefix, amIExcluded);
                    break checkProp;
                }
            }
            //Don't include this property if it failed manyToOne inclusion and is not a specified foreign key
            if (includeField || isPropertyForeignKey || additionalForeignKeyIndexPosition >= 0) {
                buildProperty(targetClass, foreignField, additionalForeignFields, mergedPropertyType,
                        componentProperties, fields, idProperty, prefix, propertyName, type,
                        isPropertyForeignKey, additionalForeignKeyIndexPosition, presentationAttribute,
                        explicitType, returnedClass);
            }
        }
    }
}

From source file:org.broadleafcommerce.openadmin.server.service.persistence.entitymanager.HibernateCleaner.java

License:Apache License

protected void performConvert(Object originalBean, Object targetBean, Field[] fields, Method method,
        HibernateEntityManager em, PlatformTransactionManager txManager) throws Throwable {
    SessionFactory sessionFactory = em.getSession().getSessionFactory();
    ClassMetadata metadata = sessionFactory.getClassMetadata(originalBean.getClass());
    String idProperty = metadata.getIdentifierPropertyName();
    if (!typePool.containsKey(originalBean.getClass().getName())) {
        List<String> propertyNames = new ArrayList<String>();
        for (String propertyName : metadata.getPropertyNames()) {
            propertyNames.add(propertyName);
        }//w  w w.j  ava2  s. c  o m
        propertyNames.add(idProperty);
        List<Type> propertyTypes = new ArrayList<Type>();
        Type idType = metadata.getIdentifierType();
        for (Type propertyType : metadata.getPropertyTypes()) {
            propertyTypes.add(propertyType);
        }
        propertyTypes.add(idType);
        Map<String, Type> types = new HashMap<String, Type>();
        int j = 0;
        for (String propertyName : propertyNames) {
            types.put(propertyName, propertyTypes.get(j));
            j++;
        }
        typePool.put(originalBean.getClass().getName(), types);
    }
    Map<String, Type> types = (Map<String, Type>) typePool.get(originalBean.getClass().getName());
    Field idField = null;
    for (Field field : fields) {
        if (types.containsKey(field.getName())) {
            field.setAccessible(true);
            Type fieldType = types.get(field.getName());
            if (fieldType.isCollectionType() || fieldType.isAnyType()) {
                //field.set(targetBean, null);
                //do nothing
            } else if (fieldType.isEntityType()) {
                Object newOriginalBean = field.get(originalBean);
                if (newOriginalBean == null) {
                    field.set(targetBean, null);
                } else {
                    Object newTargetBean = newOriginalBean.getClass().newInstance();
                    field.set(targetBean, newTargetBean);
                    Field[] newFields = getFields(newOriginalBean.getClass());
                    performConvert(newOriginalBean, newTargetBean, newFields, method, em, txManager);
                }
            } else {
                field.set(targetBean, field.get(originalBean));
            }
            if (field.getName().equals(idProperty)) {
                idField = field;
            }
        }
    }
    if (txManager != null) {
        Object temp = null;
        if (idField == null) {
            throw new Exception(
                    "Unable to find an identity field for the entity: " + originalBean.getClass().getName());
        }
        final Serializable primaryKey = (Serializable) idField.get(originalBean);
        if (primaryKey != null) {
            temp = em.find(originalBean.getClass(), primaryKey);
        }

        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

        TransactionStatus status = txManager.getTransaction(def);
        try {
            if (primaryKey != null) {
                if (temp != null && method.getName().equals("merge")) {
                    targetBean = em.merge(targetBean);
                } else {
                    SessionImplementor session = (SessionImplementor) em.getDelegate();
                    EntityPersister persister = session.getEntityPersister(targetBean.getClass().getName(),
                            targetBean);
                    IdentifierProperty ip = persister.getEntityMetamodel().getIdentifierProperty();
                    synchronized (ip) {
                        IdentifierValue backupUnsavedValue = setUnsavedValue(ip, IdentifierValue.ANY);
                        em.persist(targetBean);
                        setUnsavedValue(ip, backupUnsavedValue);
                    }
                }
            } else {
                targetBean = method.invoke(em, targetBean);
            }
        } catch (Throwable ex) {
            txManager.rollback(status);
            throw ex;
        }
        txManager.commit(status);
    }
}

From source file:org.sparkcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java

License:Apache License

protected void buildProperties(Class<?> targetClass, ForeignKey foreignField,
        ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties,
        MergedPropertyType mergedPropertyType, Map<String, FieldMetadata> presentationAttributes,
        List<Property> componentProperties, Map<String, FieldMetadata> fields, List<String> propertyNames,
        List<Type> propertyTypes, String idProperty, Boolean populateManyToOneFields, String[] includeFields,
        String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname,
        List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, Boolean isComponentPrefix) {
    int j = 0;//from  w w w .  j  a  va2 s  . c o  m
    Comparator<String> propertyComparator = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            //check for property name equality and for map field properties
            if (o1.equals(o2) || o1.startsWith(o2 + FieldManager.MAPFIELDSEPARATOR)
                    || o2.startsWith(o1 + FieldManager.MAPFIELDSEPARATOR)) {
                return 0;
            }
            return o1.compareTo(o2);
        }
    };
    List<String> presentationKeyList = new ArrayList<String>(presentationAttributes.keySet());
    Collections.sort(presentationKeyList);

    for (String propertyName : propertyNames) {
        final Type type = propertyTypes.get(j);
        boolean isPropertyForeignKey = testForeignProperty(foreignField, prefix, propertyName);
        int additionalForeignKeyIndexPosition = findAdditionalForeignKeyIndex(additionalForeignFields, prefix,
                propertyName);
        j++;
        Field myField = getFieldManager().getField(targetClass, propertyName);
        if (myField == null) {
            //try to get the field with the prefix - needed for advanced collections that appear in @Embedded classes
            myField = getFieldManager().getField(targetClass, prefix + propertyName);
        }
        if (!type.isAnyType() && !type.isCollectionType() || isPropertyForeignKey
                || additionalForeignKeyIndexPosition >= 0
                || Collections.binarySearch(presentationKeyList, propertyName, propertyComparator) >= 0) {
            if (myField != null) {
                boolean handled = false;
                for (FieldMetadataProvider provider : fieldMetadataProviders) {
                    FieldMetadata presentationAttribute = presentationAttributes.get(propertyName);
                    if (presentationAttribute != null) {
                        setExcludedBasedOnShowIfProperty(presentationAttribute);
                    }
                    FieldProviderResponse response = provider
                            .addMetadataFromFieldType(
                                    new AddMetadataFromFieldTypeRequest(myField, targetClass, foreignField,
                                            additionalForeignFields, mergedPropertyType, componentProperties,
                                            idProperty, prefix, propertyName, type, isPropertyForeignKey,
                                            additionalForeignKeyIndexPosition, presentationAttributes,
                                            presentationAttribute, null, type.getReturnedClass(), this),
                                    fields);
                    if (FieldProviderResponse.NOT_HANDLED != response) {
                        handled = true;
                    }
                    if (FieldProviderResponse.HANDLED_BREAK == response) {
                        break;
                    }
                }
                if (!handled) {
                    buildBasicProperty(myField, targetClass, foreignField, additionalForeignFields,
                            additionalNonPersistentProperties, mergedPropertyType, presentationAttributes,
                            componentProperties, fields, idProperty, populateManyToOneFields, includeFields,
                            excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname,
                            parentClasses, prefix, isParentExcluded, propertyName, type, isPropertyForeignKey,
                            additionalForeignKeyIndexPosition, isComponentPrefix);
                }
            }
        }
    }
}