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, AttributeConverterDescriptor 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.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Date.class)) {
isDate = true;
} else if (buildingContext.getBuildingOptions().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.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.Clob.class)) {
type = isNationalized ? StandardBasicTypes.NCLOB.getName() : StandardBasicTypes.CLOB.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.NClob.class)) {
type = StandardBasicTypes.NCLOB.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, java.sql.Blob.class)) {
type = "blob";
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, String.class)) {
type = isNationalized ? StandardBasicTypes.MATERIALIZED_NCLOB.getName() : StandardBasicTypes.MATERIALIZED_CLOB.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Character.class) && isArray) {
type = isNationalized ? CharacterArrayNClobType.class.getName() : CharacterArrayClobType.class.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, char.class) && isArray) {
type = isNationalized ? PrimitiveCharacterArrayNClobType.class.getName() : PrimitiveCharacterArrayClobType.class.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Byte.class) && isArray) {
type = WrappedMaterializedBlobType.class.getName();
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, byte.class) && isArray) {
type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
} else if (buildingContext.getBuildingOptions().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.getBuildingOptions().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.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, String.class)) {
// nvarchar
type = StringNVarcharType.INSTANCE.getName();
explicitType = type;
} else if (buildingContext.getBuildingOptions().getReflectionManager().equals(returnedClassOrElement, Character.class) || buildingContext.getBuildingOptions().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 beforeQuery 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);
}
use of javax.persistence.TemporalType in project cuba by cuba-platform.
the class DesktopDateField 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);
}
use of javax.persistence.TemporalType in project cuba by cuba-platform.
the class Param method createDateField.
protected Component createDateField(Class javaClass, final ValueProperty valueProperty) {
UserSession userSession = userSessionSource.getUserSession();
boolean supportTimezones = false;
boolean dateOnly = false;
if (property != null) {
TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
dateOnly = (tt == TemporalType.DATE);
Object ignoreUserTimeZone = metadataTools.getMetaAnnotationValue(property, IgnoreUserTimeZone.class);
supportTimezones = !dateOnly && !Boolean.TRUE.equals(ignoreUserTimeZone);
} else if (javaClass.equals(java.sql.Date.class)) {
dateOnly = true;
if (useUserTimeZone) {
supportTimezones = true;
}
} else {
supportTimezones = true;
}
if (inExpr) {
ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
ListEditor.ItemType itemType = dateOnly ? ListEditor.ItemType.DATE : ListEditor.ItemType.DATETIME;
listEditor.setItemType(itemType);
if (userSession.getTimeZone() != null && supportTimezones) {
listEditor.setTimeZone(userSession.getTimeZone());
}
initListEditor(listEditor, valueProperty);
return listEditor;
}
DateField dateField = componentsFactory.createComponent(DateField.class);
DateField.Resolution resolution;
String formatStr;
Messages messages = AppBeans.get(Messages.NAME);
if (dateOnly) {
resolution = com.haulmont.cuba.gui.components.DateField.Resolution.DAY;
formatStr = messages.getMainMessage("dateFormat");
} else {
resolution = com.haulmont.cuba.gui.components.DateField.Resolution.MIN;
formatStr = messages.getMainMessage("dateTimeFormat");
}
dateField.setResolution(resolution);
dateField.setDateFormat(formatStr);
if (userSession.getTimeZone() != null && supportTimezones) {
dateField.setTimeZone(userSession.getTimeZone());
}
dateField.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));
dateField.setValue(_getValue(valueProperty));
return dateField;
}
use of javax.persistence.TemporalType in project hibernate-orm by hibernate.
the class JPAOverriddenAnnotationReader method getMapKeyTemporal.
/**
* Adds a @MapKeyTemporal annotation to the specified annotationList if the specified element
* contains a map-key-temporal sub-element. This should only be the case for element-collection,
* many-to-many, or one-to-many associations.
*/
private void getMapKeyTemporal(List<Annotation> annotationList, Element element) {
Element subelement = element != null ? element.element("map-key-temporal") : null;
if (subelement != null) {
AnnotationDescriptor ad = new AnnotationDescriptor(MapKeyTemporal.class);
TemporalType value = TemporalType.valueOf(subelement.getTextTrim());
ad.setValue("value", value);
annotationList.add(AnnotationFactory.create(ad));
}
}
Aggregations