/** * @return collection of all Java enums used as a type of an entity attribute */ public Collection<Class> getAllEnums() { if (enums == null) { synchronized (this) { enums = new HashSet<>(); for (MetaClass metaClass : metadata.getSession().getClasses()) { for (MetaProperty metaProperty : metaClass.getProperties()) { if (metaProperty.getRange() != null && metaProperty.getRange().isEnum()) { Class c = metaProperty.getRange().asEnumeration().getJavaClass(); enums.add(c); } } } } } return enums; }
protected void convertEnumValues(KeyValueEntity entity, List<MetaProperty> enumProperties) { try { for (MetaProperty enumProperty : enumProperties) { Object enumValue = entity.getValue(enumProperty.getName()); if (enumValue != null) { Enumeration enumeration = enumProperty.getRange().asEnumeration(); entity.setValue(enumProperty.getName(), enumeration.parse(String.valueOf(enumValue))); } } } catch (ParseException e) { throw new RuntimeException("Unable to convert enum id to enum instance for EnumClass"); } } }
/** * Returns an old value of an enum attribute changed in the current transaction. The entity must be in the Managed state. * <p> * Unlike {@link #getOldValue(Entity, String)}, returns enum value and not its id. * * @param entity entity instance * @param attribute attribute name * @return an old value stored in the database. For a new entity returns null. * @throws IllegalArgumentException if the entity is not persistent or not in the Managed state */ @Nullable public EnumClass getOldEnumValue(Entity entity, String attribute) { Object value = getOldValue(entity, attribute); if (value == null) return null; MetaClass metaClass = metadata.getClassNN(entity.getClass()); MetaProperty metaProperty = metaClass.getPropertyNN(attribute); if (metaProperty.getRange().isEnum()) { for (Object o : metaProperty.getRange().asEnumeration().getValues()) { EnumClass enumValue = (EnumClass) o; if (value.equals(enumValue.getId())) return enumValue; } } return null; }
@SuppressWarnings("unchecked") protected void writeFields(JsonWriter out, Entity entity) throws IOException { for (MetaProperty property : entity.getMetaClass().getProperties()) { if (!"id".equalsIgnoreCase(property.getName()) && !property.isReadOnly() && !exclude(entity.getClass(), property.getName()) && PersistenceHelper.isLoaded(entity, property.getName())) { Range propertyRange = property.getRange(); if (propertyRange.isDatatype()) { writeSimpleProperty(out, entity, property); } else if (propertyRange.isClass()) { Object value = entity.getValue(property.getName()); if (value instanceof Entity) { out.name(property.getName()); writeEntity(out, (Entity) value); } else if (value instanceof Collection) { out.name(property.getName()); writeCollection(out, (Collection) value); } } else if (propertyRange.isEnum()) { out.name(property.getName()); out.value(propertyRange.asEnumeration().format(entity.getValue(property.getName()))); } } } }
} else { String str = property.getRange().asEnumeration().format(value); encodeBasic(child, str, property.getJavaType());
protected String getDisplayValue(String value) { if (StringUtils.isEmpty(value)) { return value; } final String entityName = getLogItem().getEntity(); com.haulmont.chile.core.model.MetaClass metaClass = getClassFromEntityName(entityName); if (metaClass != null) { com.haulmont.chile.core.model.MetaProperty property = metaClass.getProperty(getName()); if (property != null) { if (property.getRange().isDatatype()) { return value; } else if (property.getRange().isEnum()) { String nameKey = property.getRange().asEnumeration().getJavaClass().getSimpleName() + "." + value; String packageName = property.getRange().asEnumeration().getJavaClass().getPackage().getName(); Messages messages = AppBeans.get(Messages.NAME); return messages.getMessage(packageName, nameKey); } else { return value; } } else { return value; } } else { return value; } }
case ENUM: if (!json.isNull(key)) { setField(instance, key, property.getRange().asEnumeration().parse(json.getString(key))); } else { setField(instance, key, null);
encodeNull(fieldEl); } else { fieldEl.setText(property.getRange().asEnumeration().format(value));
/** * @return map representing Enumeration with localized Enumeration item values */ public TemplateHashModel getEnumValues() { if (property.getRange().isEnum()) { Enumeration<?> enumeration = property.getRange().asEnumeration(); SimpleHash wrappedEnum = new SimpleHash(); wrappedEnum.put("name", enumeration.toString()); SimpleSequence values = new SimpleSequence(); for (Enum enumItem : enumeration.getValues()) { SimpleHash wrappedEnumElement = new SimpleHash(); try { wrappedEnumElement.put("idObj", ObjectWrapper.BEANS_WRAPPER.wrap(enumItem)); //Some enums don't implement EnumClass interface so we'll get id field here via reflection. Messages messages = AppBeans.get(Messages.NAME); wrappedEnumElement.put("name", messages.getMessage(enumItem)); values.add(wrappedEnumElement); } catch (TemplateModelException e) { throw new RuntimeException(e); } } wrappedEnum.put("values", values); return wrappedEnum; } return null; }
/** * Java class of the target MetaProperty range */ public Class getRangeJavaClass() { if (metaProperties.length < 1) { throw new RuntimeException("Empty property path at metaclass " + metaClass.getName() + " - possibly wrong property name"); } Range range = metaProperties[metaProperties.length - 1].getRange(); if (range.isDatatype()) { return range.asDatatype().getJavaClass(); } else if (range.isClass()) { return range.asClass().getJavaClass(); } else if (range.isEnum()) { return range.asEnumeration().getJavaClass(); } else { throw new UnsupportedOperationException(); } }
String stringValue = in.nextString(); try { Object value = propertyRange.asEnumeration().parse(stringValue); entity.setValue(propertyName, value); } catch (ParseException e) {
if (value != null) { root.set(property.getName(), property.getRange().asEnumeration().format(value)); } else { root.set(property.getName(), null);
property.setJavaType(info.getObject().asEnumeration().getJavaClass()); } else { property.setJavaType(field.getType());
case ENUM: if (!json.isNull(propertyName)) { setField(entity, propertyName, property.getRange().asEnumeration().parse(json.getString(propertyName))); } else { setField(entity, propertyName, null);
@SuppressWarnings("unchecked") @Override protected void valueBindingConnected(ValueSource<V> valueSource) { super.valueBindingConnected(valueSource); if (valueSource instanceof EntityValueSource) { MetaPropertyPath propertyPath = ((EntityValueSource) valueSource).getMetaPropertyPath(); MetaProperty metaProperty = propertyPath.getMetaProperty(); if (metaProperty.getRange().isEnum()) { //noinspection unchecked setOptions(new EnumOptions(metaProperty.getRange().asEnumeration().getJavaClass())); } if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) { CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty); if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) { setOptionsMap((Map<String, I>) categoryAttribute.getLocalizedEnumerationMap()); } } } }
break; case ENUM: value = property.getRange().asEnumeration().parse(stringValue); entity.setValue(propertyName, value); break;
public MetaPropertyInfo(MetaProperty metaProperty) { Messages messages = AppBeans.get(Messages.class); Metadata metadata = AppBeans.get(Metadata.class); this.name = metaProperty.getName(); this.attributeType = metaProperty.getType(); switch (attributeType) { case DATATYPE: Datatype<Object> datatype = metaProperty.getRange().asDatatype(); try { this.type = metadata.getDatatypes().getId(datatype); } catch (Exception e) { this.type = datatype.toString(); } break; case ASSOCIATION: case COMPOSITION: this.type = metaProperty.getRange().asClass().getName(); break; case ENUM: this.type = metaProperty.getRange().asEnumeration().getJavaClass().getName(); break; } this.cardinality = metaProperty.getRange().getCardinality(); this.readOnly = metaProperty.isReadOnly(); this.mandatory = metaProperty.isMandatory(); this.isPersistent = metadata.getTools().isPersistent(metaProperty); this.isTransient = metadata.getTools().isNotPersistent(metaProperty); this.description = messages.getTools().getPropertyCaption(metaProperty); }
break; case ENUM: value = property.getRange().asEnumeration().parse(xmlValue); setField(bean, fieldName, value); break;
@SuppressWarnings("unchecked") @Override protected void valueBindingConnected(ValueSource<V> valueSource) { super.valueBindingConnected(valueSource); if (valueSource instanceof EntityValueSource) { MetaPropertyPath propertyPath = ((EntityValueSource) valueSource).getMetaPropertyPath(); MetaProperty metaProperty = propertyPath.getMetaProperty(); if (metaProperty.getRange().isEnum()) { //noinspection unchecked setOptions(new EnumOptions(metaProperty.getRange().asEnumeration().getJavaClass())); } if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) { CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty); if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) { setOptionsMap((Map<String, I>) categoryAttribute.getLocalizedEnumerationMap()); } } } }