Search in sources :

Example 1 with EnumeratedType

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;
}
Also used : JSONObject(org.json.JSONObject) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) FieldDefinition(com.qcadoo.model.api.FieldDefinition)

Example 2 with EnumeratedType

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"));
}
Also used : Entity(com.qcadoo.model.api.Entity) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Example 3 with EnumeratedType

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]);
}
Also used : ValueAndError(com.qcadoo.model.internal.api.ValueAndError) TranslationService(com.qcadoo.localization.api.TranslationService) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) EnumType(com.qcadoo.model.internal.types.EnumType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 4 with EnumeratedType

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]);
}
Also used : DictionaryService(com.qcadoo.model.api.DictionaryService) ValueAndError(com.qcadoo.model.internal.api.ValueAndError) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) DictionaryType(com.qcadoo.model.internal.types.DictionaryType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 5 with EnumeratedType

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;
    }
}
Also used : JSONObject(org.json.JSONObject) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) JSONArray(org.json.JSONArray) Map(java.util.Map)

Aggregations

EnumeratedType (com.qcadoo.model.api.types.EnumeratedType)8 Test (org.junit.Test)6 DataDefinition (com.qcadoo.model.api.DataDefinition)4 Entity (com.qcadoo.model.api.Entity)4 FieldDefinition (com.qcadoo.model.api.FieldDefinition)2 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)2 ValueAndError (com.qcadoo.model.internal.api.ValueAndError)2 JSONObject (org.json.JSONObject)2 TranslationService (com.qcadoo.localization.api.TranslationService)1 DictionaryService (com.qcadoo.model.api.DictionaryService)1 DictionaryType (com.qcadoo.model.internal.types.DictionaryType)1 EnumType (com.qcadoo.model.internal.types.EnumType)1 PluginStateResolver (com.qcadoo.plugin.api.PluginStateResolver)1 PluginUtilsService (com.qcadoo.plugin.internal.PluginUtilsService)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1