Example usage for org.hibernate.usertype CompositeUserType getPropertyNames

List of usage examples for org.hibernate.usertype CompositeUserType getPropertyNames

Introduction

In this page you can find the example usage for org.hibernate.usertype CompositeUserType getPropertyNames.

Prototype

String[] getPropertyNames();

Source Link

Document

Get the "property names" that may be used in a query.

Usage

From source file:org.hl7.hibernate.MultiColumnPersistedDataTypeUserType.java

License:Open Source License

/** Gets called by Hibernate to pass the configured type parameters to the implementation. */
public void setParameterValues(Properties parameters) {
    try {//  ww w .  j a v  a 2s  .c o  m

        /** Mandatory parameter **/
        _typeSpec = (String) parameters.get("type");
        if (_typeSpec == null) {
            throw new Error("type parameter is missing");
        }

        String className = (String) parameters.get("class");
        String interfaceName = (String) parameters.get("interface");
        int propertyCount = Integer.parseInt((String) parameters.get("properties"));

        // requiring this makes our job of analyzing the parameters easier for now

        int exposedPropertiesCount = propertyCount;

        _properties = new Property[propertyCount];

        // we need this separately in order to find the constructor or static factory method
        Class constructorSignature[] = new Class[propertyCount];

        // this is the length of the exposed property arrays, initially same as argument count and decremented for each anonymous property
        List<String> exposedPropertyNameList = new ArrayList<String>();
        List<Type> exposedPropertyTypeList = new ArrayList<Type>();
        List<Property> exposedPropertyList = new ArrayList<Property>();

        for (int i = 0; i < propertyCount; i++) {
            Property property = _properties[i] = new Property();

            // class is mandatory
            constructorSignature[i] = property._class = Class
                    .forName((String) parameters.get("property." + i + ".class"));

            // name is optional, and only named properties are exposed to hibernate
            String name = (String) parameters.get("property." + i + ".name");
            if (name != null) {
                exposedPropertyList.add(property);
                property._name = name;
                property._type = (String) parameters.get("property." + i + ".type");
                property._accessor = (String) parameters.get("property." + i + ".accessor");
                if (property._accessor == null)
                    property._accessor = property._name;
                property._accessorTool = (String) parameters.get("property." + i + ".accessorTool");
                property._accessorMethod = (String) parameters.get("property." + i + ".accessorMethod");
                String params = (String) parameters.get("property." + i + ".type.params");
                if (params != null) {
                    Properties propertyParams = new Properties();
                    Iterator<Map.Entry<Object, Object>> paramIter = parameters.entrySet().iterator();
                    String prefix = "property." + i + ".type.param.";
                    while (paramIter.hasNext()) {
                        Map.Entry<Object, Object> entry = paramIter.next();
                        String paramKey = (String) entry.getKey();
                        if (paramKey.startsWith(prefix)) {
                            propertyParams.put("type", entry.getValue());
                        }
                    }

                    if (property._type != null) {
                        property._hibernateType = TypeFactory.heuristicType(property._type, propertyParams);
                    } else
                        throw new Error("property type must be set");
                } else {
                    property._hibernateType = TypeFactory.heuristicType(property._type);
                }

                if (property._hibernateType instanceof CompositeUserType) {
                    CompositeUserType compositeUserType = (CompositeUserType) property._hibernateType;
                    String propertyNames[] = compositeUserType.getPropertyNames();
                    for (int j = 0; j < propertyNames.length; j++)
                        exposedPropertyNameList.add(propertyNames[j]);
                    Type propertyTypes[] = compositeUserType.getPropertyTypes();
                    for (int j = 0; j < propertyTypes.length; j++)
                        exposedPropertyTypeList.add(propertyTypes[j]);
                } else {
                    exposedPropertyNameList.add(property._name);
                    exposedPropertyTypeList.add(property._hibernateType);
                }
            }
            String constLiteral = (String) parameters.get("property." + i + ".const");
            if (constLiteral != null) {
                String typeSpec = (String) parameters.get("property." + i + ".typeSpec");
                if (typeSpec == null) {
                    /** assume the type is java.lang.String **/
                    property._constantValue = constLiteral;
                } else if (typeSpec != null) {
                    try {
                        property._constantValue = ValueFactory.getInstance().valueOfLiteral(typeSpec,
                                constLiteral);
                    } catch (ValueFactoryException vfe) {
                        vfe.printStackTrace();
                    }
                }
            }

        }

        _flatExposedPropertyNames = exposedPropertyNameList.toArray(STRING_ARRAY_PROTOTYPE);
        _flatExposedPropertyTypes = exposedPropertyTypeList.toArray(TYPE_ARRAY_PROTOTYPE);
        _exposedProperties = exposedPropertyList.toArray(PROPERTY_ARRAY_PROTOTYPE);

        // find the constructor or static factory method
        String staticFactoryMethodName = (String) parameters.get("staticFactoryMethod");
        String valueFactoryMethodName = (String) parameters.get("valueFactoryMethod");

        if (valueFactoryMethodName == null) {
            _class = Class.forName(className);

            // interface is optional, if not provided it's same as class
            if (interfaceName != null)
                _interface = Class.forName(interfaceName);
            else
                _interface = _class;
        } else {
            _interface = Class.forName(interfaceName);
            _class = null;
        }

        if (valueFactoryMethodName != null) {
            ValueFactory vfact = ValueFactory.getInstance();
            _constructor = new StaticallyCallable(
                    ClassMethodTool.getMethod(vfact.getClass(), valueFactoryMethodName, constructorSignature),
                    vfact);
        } else if (staticFactoryMethodName != null) {
            _constructor = new StaticallyCallable(
                    ClassMethodTool.getMethod(_class, staticFactoryMethodName, constructorSignature));
        } else {
            _constructor = new StaticallyCallable(ClassMethodTool.getConstructor(_class, constructorSignature));
        }

    } catch (ClassNotFoundException x) {
        throw new Error(x);
    } catch (NoSuchMethodException x) {
        throw new Error(x);
    } catch (InstantiationException x) {
        throw new Error(x);
    } catch (IllegalAccessException x) {
        throw new Error(x);
    }
}