use of com.qcadoo.model.api.types.EnumeratedType in project qcadoo by qcadoo.
the class TranslatedFieldComponentState method renderContent.
@Override
protected JSONObject renderContent() throws JSONException {
JSONObject json = new JSONObject();
String value = (String) getFieldValue();
FieldDefinition fieldDefinition = pattern.getFieldComponentFieldDefinition();
if (fieldDefinition != null && EnumeratedType.class.isAssignableFrom(fieldDefinition.getType().getClass())) {
value = ((EnumeratedType) fieldDefinition.getType()).activeValues(getLocale()).get(value);
}
json.put(JSON_VALUE, value);
json.put(JSON_REQUIRED, isRequired());
return json;
}
use of com.qcadoo.model.api.types.EnumeratedType in project qcadoo by qcadoo.
the class ModuleIntegrationTest method shouldHaveAdditionalModelsFieldsAndHooks.
@Test
public void shouldHaveAdditionalModelsFieldsAndHooks() throws Exception {
// given
DataDefinition productDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_PRODUCT);
DataDefinition machineDao = dataDefinitionService.get(PLUGIN_MACHINES_NAME, ENTITY_NAME_MACHINE);
DataDefinition componentDao = dataDefinitionService.get(PLUGIN_PRODUCTS_NAME, ENTITY_NAME_COMPONENT);
Entity machine = machineDao.save(createMachine("asd"));
Entity product = createProduct("asd", "asd");
product.setField("changeableName", "xxx");
product = productDao.save(product);
Entity component = createComponent("name", product, machine);
component.setField("machineName", "test");
// when
component = componentDao.save(component);
// then
assertEquals("test", component.getField("machineName"));
assertEquals("XXX", product.getField("changeableName"));
assertNotNull(component.getField("machine"));
Map<String, Object> componentResult = jdbcTemplate.queryForMap("select * from " + TABLE_NAME_COMPONENT);
assertNotNull(componentResult);
assertEquals("test", componentResult.get("machineName"));
Map<String, Object> productResult = jdbcTemplate.queryForMap("select * from " + TABLE_NAME_PRODUCT);
assertNotNull(productResult);
assertEquals("XXX", productResult.get("changeableName"));
assertThat(((EnumeratedType) productDao.getField("enum").getType()).activeValues(Locale.ENGLISH).keySet(), JUnitMatchers.hasItems("one", "two", "three"));
}
use of com.qcadoo.model.api.types.EnumeratedType in project qcadoo by qcadoo.
the class FieldTypeFactoryTest method shouldReturnEnumType.
@Test
public void shouldReturnEnumType() throws Exception {
// given
TranslationService translationService = mock(TranslationService.class);
given(translationService.translate("path.value.val1", Locale.ENGLISH)).willReturn("i18nVal1");
given(translationService.translate("path.value.val2", Locale.ENGLISH)).willReturn("i18nVal2");
given(translationService.translate("path.value.val3", Locale.ENGLISH)).willReturn("i18nVal3");
// when
EnumeratedType fieldType = new EnumType(translationService, "path", true, "val1", "val2", "val3");
// then
assertEquals(fieldType.activeValues(Locale.ENGLISH).keySet(), Sets.newHashSet("val1", "val2", "val3"));
assertEquals(Lists.newArrayList(fieldType.activeValues(Locale.ENGLISH).values()), Lists.newArrayList("i18nVal1", "i18nVal2", "i18nVal3"));
assertEquals(String.class, fieldType.getType());
ValueAndError valueAndError1 = fieldType.toObject(fieldDefinition, "val1");
ValueAndError valueAndError2 = fieldType.toObject(fieldDefinition, "val4");
assertTrue(valueAndError1.isValid());
assertFalse(valueAndError2.isValid());
assertNotNull(valueAndError1.getValue());
assertNull(valueAndError2.getValue());
assertEquals("qcadooView.validate.field.error.invalidDictionaryItem", valueAndError2.getMessage());
assertEquals("[val1, val2, val3]", valueAndError2.getArgs()[0]);
}
use of com.qcadoo.model.api.types.EnumeratedType in project qcadoo by qcadoo.
the class FieldTypeFactoryTest method shouldReturnDictionaryType.
@Test
public void shouldReturnDictionaryType() throws Exception {
// given
DictionaryService dictionaryService = mock(DictionaryService.class);
given(dictionaryService.getActiveValues("dictionary", Locale.ENGLISH)).willReturn(ImmutableMap.of("val1", "val1", "val2", "val2", "val3", "val3"));
given(dictionaryService.getKeys("dictionary")).willReturn(Lists.newArrayList("val1", "val2", "val3"));
// when
EnumeratedType fieldType = new DictionaryType("dictionary", dictionaryService, true);
// then
assertEquals(fieldType.activeValues(Locale.ENGLISH).keySet(), Sets.newHashSet("val1", "val2", "val3"));
assertEquals(Lists.newArrayList(fieldType.activeValues(Locale.ENGLISH).values()), Lists.newArrayList("val1", "val2", "val3"));
assertEquals(String.class, fieldType.getType());
ValueAndError valueAndError1 = fieldType.toObject(fieldDefinition, "val1");
ValueAndError valueAndError2 = fieldType.toObject(fieldDefinition, "val4");
assertNotNull(valueAndError1.getValue());
assertNull(valueAndError2.getValue());
assertEquals("qcadooView.validate.field.error.invalidDictionaryItem", valueAndError2.getMessage());
assertEquals("[val1, val2, val3]", valueAndError2.getArgs()[0]);
}
use of com.qcadoo.model.api.types.EnumeratedType in project qcadoo by qcadoo.
the class GridComponentPattern method getFilterValuesForColumn.
private JSONArray getFilterValuesForColumn(final GridComponentColumn column, final Locale locale) throws JSONException {
if (column.getFields().size() != 1) {
return null;
}
String field = GridComponentFilterUtils.getFieldNameByColumnName(columns, column.getName());
if (field == null) {
return null;
}
FieldDefinition fieldDefinition = GridComponentFilterUtils.getFieldDefinition(getDataDefinition(), field);
if (fieldDefinition == null) {
return null;
}
JSONArray values = new JSONArray();
if (fieldDefinition.getType() instanceof EnumeratedType) {
EnumeratedType type = (EnumeratedType) fieldDefinition.getType();
Map<String, String> sortedValues = type.values(locale);
for (Map.Entry<String, String> value : sortedValues.entrySet()) {
JSONObject obj = new JSONObject();
obj.put("key", value.getKey());
obj.put("value", value.getValue());
values.put(obj);
}
} else if (fieldDefinition.getType().getType().equals(Boolean.class)) {
JSONObject obj1 = new JSONObject();
obj1.put("key", "1");
obj1.put("value", getTranslationService().translate("qcadooView.true", locale));
values.put(obj1);
JSONObject obj0 = new JSONObject();
obj0.put("key", "0");
obj0.put("value", getTranslationService().translate("qcadooView.false", locale));
values.put(obj0);
}
if (values.length() > 0) {
return values;
} else {
return null;
}
}
Aggregations