Search in sources :

Example 1 with TemporalType

use of javax.persistence.TemporalType in project cuba by cuba-platform.

the class FilterConditionUtils method formatParamValue.

public static String formatParamValue(Param param, Object value) {
    // noinspection unchecked
    Datatype datatype = Datatypes.get(param.getJavaClass());
    MetaProperty property = param.getProperty();
    if (property != null) {
        TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
        if (tt == TemporalType.DATE) {
            datatype = Datatypes.getNN(java.sql.Date.class);
        }
    }
    if (datatype != null) {
        UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
        return datatype.format(value, userSessionSource.getLocale());
    }
    return value.toString();
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Datatype(com.haulmont.chile.core.datatypes.Datatype) TemporalType(javax.persistence.TemporalType)

Example 2 with TemporalType

use of javax.persistence.TemporalType in project cuba by cuba-platform.

the class WebDateField method initDateFormat.

protected void initDateFormat(MetaProperty metaProperty) {
    TemporalType tt = null;
    if (metaProperty.getRange().asDatatype().getJavaClass().equals(java.sql.Date.class)) {
        tt = TemporalType.DATE;
    } else if (metaProperty.getAnnotations() != null) {
        tt = (TemporalType) metaProperty.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
    }
    setResolution(tt == TemporalType.DATE ? DateField.Resolution.DAY : Resolution.MIN);
    MessageTools messageTools = AppBeans.get(MessageTools.NAME);
    String formatStr = messageTools.getDefaultDateFormat(tt);
    setDateFormat(formatStr);
}
Also used : TemporalType(javax.persistence.TemporalType)

Example 3 with TemporalType

use of javax.persistence.TemporalType in project hibernate-orm by hibernate.

the class ProcedureParameterImpl method prepare.

@Override
public void prepare(CallableStatement statement, int startIndex) throws SQLException {
    // initially set up the Type we will use for binding as the explicit type.
    Type typeToUse = getHibernateType();
    int[] sqlTypesToUse = sqlTypes;
    final ParameterBind bind = getBind();
    // however, for Calendar binding with an explicit TemporalType we may need to adjust this...
    if (bind != null && bind.getExplicitTemporalType() != null) {
        if (Calendar.class.isInstance(bind.getValue())) {
            switch(bind.getExplicitTemporalType()) {
                case TIMESTAMP:
                    {
                        typeToUse = CalendarType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
                case DATE:
                    {
                        typeToUse = CalendarDateType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
                case TIME:
                    {
                        typeToUse = CalendarTimeType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
            }
        }
    }
    this.startIndex = startIndex;
    if (mode == ParameterMode.IN || mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
        if (mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
            if (sqlTypesToUse.length > 1) {
                // there is more than one column involved; see if the Hibernate Type can handle
                // multi-param extraction...
                final boolean canHandleMultiParamExtraction = ProcedureParameterExtractionAware.class.isInstance(typeToUse) && ((ProcedureParameterExtractionAware) typeToUse).canDoExtraction();
                if (!canHandleMultiParamExtraction) {
                    // it cannot...
                    throw new UnsupportedOperationException("Type [" + typeToUse + "] does support multi-parameter value extraction");
                }
            }
            // e.g., Oracle.
            if (sqlTypesToUse.length == 1 && procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                statement.registerOutParameter(getName(), sqlTypesToUse[0]);
            } else {
                for (int i = 0; i < sqlTypesToUse.length; i++) {
                    statement.registerOutParameter(startIndex + i, sqlTypesToUse[i]);
                }
            }
        }
        if (mode == ParameterMode.INOUT || mode == ParameterMode.IN) {
            if (bind == null || bind.getValue() == null) {
                // parameter defines a default value.  Deferring to that information would be the best option
                if (isPassNullsEnabled()) {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to true; binding NULL", procedureCall.getProcedureName(), this);
                    if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                        ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, null, this.getName(), procedureCall.getSession());
                    } else {
                        typeToUse.nullSafeSet(statement, null, startIndex, procedureCall.getSession());
                    }
                } else {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to false; assuming procedure defines default value", procedureCall.getProcedureName(), this);
                }
            } else {
                if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                    ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, bind.getValue(), this.getName(), procedureCall.getSession());
                } else {
                    typeToUse.nullSafeSet(statement, bind.getValue(), startIndex, procedureCall.getSession());
                }
            }
        }
    } else {
        // we have a REF_CURSOR type param
        if (procedureCall.getParameterStrategy() == ParameterStrategy.NAMED) {
            procedureCall.getSession().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, getName());
        } else {
            procedureCall.getSession().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, startIndex);
        }
    }
}
Also used : RefCursorSupport(org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport) TemporalType(javax.persistence.TemporalType) CalendarTimeType(org.hibernate.type.CalendarTimeType) CalendarType(org.hibernate.type.CalendarType) CalendarDateType(org.hibernate.type.CalendarDateType) Type(org.hibernate.type.Type) ProcedureParameterNamedBinder(org.hibernate.type.ProcedureParameterNamedBinder) ParameterBind(org.hibernate.procedure.ParameterBind) ProcedureParameterExtractionAware(org.hibernate.type.ProcedureParameterExtractionAware)

Example 4 with TemporalType

use of javax.persistence.TemporalType in project hibernate-orm by hibernate.

the class SimpleValueBinder method setType.

// TODO execute it lazily to be order safe
public void setType(XProperty property, XClass returnedClass, String declaringClassName, ConverterDescriptor attributeConverterDescriptor) {
    if (returnedClass == null) {
        // we cannot guess anything
        return;
    }
    XClass returnedClassOrElement = returnedClass;
    boolean isArray = false;
    if (property.isArray()) {
        returnedClassOrElement = property.getElementClass();
        isArray = true;
    }
    this.xproperty = property;
    Properties typeParameters = this.typeParameters;
    typeParameters.clear();
    String type = BinderHelper.ANNOTATION_STRING_DEFAULT;
    if (getDialect().supportsNationalizedTypes()) {
        isNationalized = property.isAnnotationPresent(Nationalized.class) || buildingContext.getBuildingOptions().useNationalizedCharacterData();
    }
    Type annType = null;
    if ((!key && property.isAnnotationPresent(Type.class)) || (key && property.isAnnotationPresent(MapKeyType.class))) {
        if (key) {
            MapKeyType ann = property.getAnnotation(MapKeyType.class);
            annType = ann.value();
        } else {
            annType = property.getAnnotation(Type.class);
        }
    }
    if (annType != null) {
        setExplicitType(annType);
        type = explicitType;
    } else if ((!key && property.isAnnotationPresent(Temporal.class)) || (key && property.isAnnotationPresent(MapKeyTemporal.class))) {
        boolean isDate;
        if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Date.class)) {
            isDate = true;
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Calendar.class)) {
            isDate = false;
        } else {
            throw new AnnotationException("@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + StringHelper.qualify(persistentClassName, propertyName));
        }
        final TemporalType temporalType = getTemporalType(property);
        switch(temporalType) {
            case DATE:
                type = isDate ? "date" : "calendar_date";
                break;
            case TIME:
                type = "time";
                if (!isDate) {
                    throw new NotYetImplementedException("Calendar cannot persist TIME only" + StringHelper.qualify(persistentClassName, propertyName));
                }
                break;
            case TIMESTAMP:
                type = isDate ? "timestamp" : "calendar";
                break;
            default:
                throw new AssertionFailure("Unknown temporal type: " + temporalType);
        }
        explicitType = type;
    } else if (!key && property.isAnnotationPresent(Lob.class)) {
        isLob = true;
        if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.Clob.class)) {
            type = isNationalized ? StandardBasicTypes.NCLOB.getName() : StandardBasicTypes.CLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.NClob.class)) {
            type = StandardBasicTypes.NCLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, java.sql.Blob.class)) {
            type = "blob";
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            type = isNationalized ? StandardBasicTypes.MATERIALIZED_NCLOB.getName() : StandardBasicTypes.MATERIALIZED_CLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Character.class) && isArray) {
            type = isNationalized ? CharacterArrayNClobType.class.getName() : CharacterArrayClobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, char.class) && isArray) {
            type = isNationalized ? PrimitiveCharacterArrayNClobType.class.getName() : PrimitiveCharacterArrayClobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Byte.class) && isArray) {
            type = WrappedMaterializedBlobType.class.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, byte.class) && isArray) {
            type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
        } else if (buildingContext.getBootstrapContext().getReflectionManager().toXClass(Serializable.class).isAssignableFrom(returnedClassOrElement)) {
            type = SerializableToBlobType.class.getName();
            typeParameters.setProperty(SerializableToBlobType.CLASS_NAME, returnedClassOrElement.getName());
        } else {
            type = "blob";
        }
        defaultType = type;
    } else if ((!key && property.isAnnotationPresent(Enumerated.class)) || (key && property.isAnnotationPresent(MapKeyEnumerated.class))) {
        final Class attributeJavaType = buildingContext.getBootstrapContext().getReflectionManager().toClass(returnedClassOrElement);
        if (!Enum.class.isAssignableFrom(attributeJavaType)) {
            throw new AnnotationException(String.format("Attribute [%s.%s] was annotated as enumerated, but its java type is not an enum [%s]", declaringClassName, xproperty.getName(), attributeJavaType.getName()));
        }
        type = EnumType.class.getName();
        explicitType = type;
    } else if (isNationalized) {
        if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, String.class)) {
            // nvarchar
            type = StringNVarcharType.INSTANCE.getName();
            explicitType = type;
        } else if (buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, Character.class) || buildingContext.getBootstrapContext().getReflectionManager().equals(returnedClassOrElement, char.class)) {
            if (isArray) {
                // nvarchar
                type = StringNVarcharType.INSTANCE.getName();
            } else {
                // nchar
                type = CharacterNCharType.INSTANCE.getName();
            }
            explicitType = type;
        }
    }
    // implicit type will check basic types and Serializable classes
    if (columns == null) {
        throw new AssertionFailure("SimpleValueBinder.setColumns should be set before SimpleValueBinder.setType");
    }
    if (BinderHelper.ANNOTATION_STRING_DEFAULT.equals(type)) {
        if (returnedClassOrElement.isEnum()) {
            type = EnumType.class.getName();
        }
    }
    defaultType = BinderHelper.isEmptyAnnotationValue(type) ? returnedClassName : type;
    this.typeParameters = typeParameters;
    applyAttributeConverter(property, attributeConverterDescriptor);
}
Also used : Serializable(java.io.Serializable) AssertionFailure(org.hibernate.AssertionFailure) WrappedMaterializedBlobType(org.hibernate.type.WrappedMaterializedBlobType) Properties(java.util.Properties) XClass(org.hibernate.annotations.common.reflection.XClass) Date(java.util.Date) NotYetImplementedException(org.hibernate.cfg.NotYetImplementedException) PrimitiveCharacterArrayNClobType(org.hibernate.type.PrimitiveCharacterArrayNClobType) EnumType(org.hibernate.type.EnumType) AccessType(org.hibernate.cfg.AccessType) WrappedMaterializedBlobType(org.hibernate.type.WrappedMaterializedBlobType) DynamicParameterizedType(org.hibernate.usertype.DynamicParameterizedType) Type(org.hibernate.annotations.Type) CharacterArrayNClobType(org.hibernate.type.CharacterArrayNClobType) PrimitiveCharacterArrayNClobType(org.hibernate.type.PrimitiveCharacterArrayNClobType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) PrimitiveCharacterArrayClobType(org.hibernate.type.PrimitiveCharacterArrayClobType) CharacterNCharType(org.hibernate.type.CharacterNCharType) CharacterArrayClobType(org.hibernate.type.CharacterArrayClobType) StringNVarcharType(org.hibernate.type.StringNVarcharType) TemporalType(javax.persistence.TemporalType) MapKeyType(org.hibernate.annotations.MapKeyType) PrimitiveCharacterArrayClobType(org.hibernate.type.PrimitiveCharacterArrayClobType) SerializableToBlobType(org.hibernate.type.SerializableToBlobType) EnumType(org.hibernate.type.EnumType) AnnotationException(org.hibernate.AnnotationException) XClass(org.hibernate.annotations.common.reflection.XClass) MapKeyType(org.hibernate.annotations.MapKeyType) Lob(javax.persistence.Lob) TemporalType(javax.persistence.TemporalType)

Example 5 with TemporalType

use of javax.persistence.TemporalType in project hibernate-orm by hibernate.

the class AbstractParameterRegistrationImpl method prepare.

@Override
public void prepare(CallableStatement statement, int startIndex) throws SQLException {
    // initially set up the Type we will use for binding as the explicit type.
    Type typeToUse = hibernateType;
    int[] sqlTypesToUse = sqlTypes;
    // however, for Calendar binding with an explicit TemporalType we may need to adjust this...
    if (bind != null && bind.getExplicitTemporalType() != null) {
        if (Calendar.class.isInstance(bind.getValue())) {
            switch(bind.getExplicitTemporalType()) {
                case TIMESTAMP:
                    {
                        typeToUse = CalendarType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(session().getFactory());
                        break;
                    }
                case DATE:
                    {
                        typeToUse = CalendarDateType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(session().getFactory());
                        break;
                    }
                case TIME:
                    {
                        typeToUse = CalendarTimeType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(session().getFactory());
                        break;
                    }
            }
        }
    }
    this.startIndex = startIndex;
    if (mode == ParameterMode.IN || mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
        if (mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
            if (sqlTypesToUse.length > 1) {
                // there is more than one column involved; see if the Hibernate Type can handle
                // multi-param extraction...
                final boolean canHandleMultiParamExtraction = ProcedureParameterExtractionAware.class.isInstance(hibernateType) && ((ProcedureParameterExtractionAware) hibernateType).canDoExtraction();
                if (!canHandleMultiParamExtraction) {
                    // it cannot...
                    throw new UnsupportedOperationException("Type [" + hibernateType + "] does support multi-parameter value extraction");
                }
            }
            // e.g., Oracle.
            if (sqlTypesToUse.length == 1 && procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding()) {
                statement.registerOutParameter(getName(), sqlTypesToUse[0]);
            } else {
                for (int i = 0; i < sqlTypesToUse.length; i++) {
                    statement.registerOutParameter(startIndex + i, sqlTypesToUse[i]);
                }
            }
        }
        if (mode == ParameterMode.INOUT || mode == ParameterMode.IN) {
            if (bind == null || bind.getValue() == null) {
                // parameter defines a default value.  Deferring to that information would be the best option
                if (passNulls) {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to true; binding NULL", procedureCall.getProcedureName(), this);
                    if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding()) {
                        ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, null, this.getName(), session());
                    } else {
                        typeToUse.nullSafeSet(statement, null, startIndex, session());
                    }
                } else {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to false; assuming procedure defines default value", procedureCall.getProcedureName(), this);
                }
            } else {
                if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding()) {
                    ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, bind.getValue(), this.getName(), session());
                } else {
                    typeToUse.nullSafeSet(statement, bind.getValue(), startIndex, session());
                }
            }
        }
    } else {
        // we have a REF_CURSOR type param
        if (procedureCall.getParameterStrategy() == ParameterStrategy.NAMED) {
            session().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, getName());
        } else {
            session().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, startIndex);
        }
    }
}
Also used : RefCursorSupport(org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport) TemporalType(javax.persistence.TemporalType) CalendarTimeType(org.hibernate.type.CalendarTimeType) CalendarType(org.hibernate.type.CalendarType) CalendarDateType(org.hibernate.type.CalendarDateType) Type(org.hibernate.type.Type) ProcedureParameterNamedBinder(org.hibernate.type.ProcedureParameterNamedBinder) ProcedureParameterExtractionAware(org.hibernate.type.ProcedureParameterExtractionAware)

Aggregations

TemporalType (javax.persistence.TemporalType)9 Serializable (java.io.Serializable)2 Date (java.util.Date)2 Properties (java.util.Properties)2 Lob (javax.persistence.Lob)2 AnnotationException (org.hibernate.AnnotationException)2 AssertionFailure (org.hibernate.AssertionFailure)2 MapKeyType (org.hibernate.annotations.MapKeyType)2 Type (org.hibernate.annotations.Type)2 XClass (org.hibernate.annotations.common.reflection.XClass)2 AccessType (org.hibernate.cfg.AccessType)2 NotYetImplementedException (org.hibernate.cfg.NotYetImplementedException)2 RefCursorSupport (org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport)2 CalendarDateType (org.hibernate.type.CalendarDateType)2 CalendarTimeType (org.hibernate.type.CalendarTimeType)2 CalendarType (org.hibernate.type.CalendarType)2 CharacterArrayClobType (org.hibernate.type.CharacterArrayClobType)2 CharacterArrayNClobType (org.hibernate.type.CharacterArrayNClobType)2 CharacterNCharType (org.hibernate.type.CharacterNCharType)2 EnumType (org.hibernate.type.EnumType)2