Search in sources :

Example 1 with ValueAndError

use of com.qcadoo.model.internal.api.ValueAndError in project qcadoo by qcadoo.

the class GanttChartComponentState method initializeContent.

@Override
protected void initializeContent(final JSONObject json) throws JSONException {
    JSONObject headerDataObject = json.getJSONObject("headerParameters");
    ZoomLevel zoomLevel = ZoomLevel.valueOf(headerDataObject.getString("scale"));
    String dateFromString = headerDataObject.getString("dateFrom");
    String dateToString = headerDataObject.getString("dateTo");
    DateTime now = new DateTime().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
    Date dateFrom = now.plusDays(defaultStartDay).toDate();
    Date dateTo = now.plusDays(defaultEndDay).toDate();
    if (dateFromString == null || "".equals(dateFromString)) {
        dateFromErrorMessage = translate("errorMessage.emptyDate");
    } else {
        ValueAndError dateFromVaE = DATETYPE.toObject(null, dateFromString);
        if (dateFromVaE.getMessage() == null) {
            dateFrom = (Date) dateFromVaE.getValue();
        } else {
            dateFromErrorMessage = translate("errorMessage.dateNotValid");
        }
    }
    if (dateToString == null || "".equals(dateToString)) {
        dateToErrorMessage = translate("errorMessage.emptyDate");
    } else {
        ValueAndError dateToVaE = DATETYPE.toObject(null, dateToString);
        if (dateToVaE.getMessage() == null) {
            dateTo = (Date) dateToVaE.getValue();
        } else {
            dateToErrorMessage = translate("errorMessage.dateNotValid");
        }
    }
    scale = new GanttChartScaleImpl(this, zoomLevel, dateFrom, dateTo);
    if (dateFromErrorMessage == null && globalErrorMessage == null) {
        if (scale.isFromLargerThanTo()) {
            globalErrorMessage = translate("errorMessage.fromLargerThanTo");
        } else if (scale.isTooLargeRange()) {
            globalErrorMessage = translate("errorMessage.tooLargeRange", String.valueOf(scale.getMaxRangeInMonths()));
        }
    }
    if (json.has("selectedEntityId")) {
        selectedEntityId = json.getLong("selectedEntityId");
    }
}
Also used : ValueAndError(com.qcadoo.model.internal.api.ValueAndError) JSONObject(org.json.JSONObject) ZoomLevel(com.qcadoo.view.internal.components.ganttChart.GanttChartScaleImpl.ZoomLevel) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 2 with ValueAndError

use of com.qcadoo.model.internal.api.ValueAndError in project qcadoo by qcadoo.

the class DefaultEntity method getIntegerField.

@Override
public Integer getIntegerField(final String fieldName) {
    final Object fieldValue = getField(fieldName);
    if (fieldValue == null) {
        return null;
    }
    if (fieldValue instanceof Integer) {
        return (Integer) fieldValue;
    }
    final FieldDefinition fieldDefinition = dataDefinition.getField(fieldName);
    if (fieldValue instanceof String && Integer.class.equals(fieldDefinition.getType().getType())) {
        if (StringUtils.isBlank((String) fieldValue)) {
            return null;
        }
        final ValueAndError valueAndError = fieldDefinition.getType().toObject(fieldDefinition, fieldValue);
        if (valueAndError.isValid()) {
            return (Integer) valueAndError.getValue();
        }
    }
    throw new IllegalArgumentException("Field " + fieldName + " in " + dataDefinition.getPluginIdentifier() + '.' + dataDefinition.getName() + " does not contain correct Integer value (current field value: " + fieldValue + ")");
}
Also used : ValueAndError(com.qcadoo.model.internal.api.ValueAndError)

Example 3 with ValueAndError

use of com.qcadoo.model.internal.api.ValueAndError 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 ValueAndError

use of com.qcadoo.model.internal.api.ValueAndError 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 ValueAndError

use of com.qcadoo.model.internal.api.ValueAndError in project qcadoo by qcadoo.

the class DefaultEntity method getDecimalField.

@Override
public BigDecimal getDecimalField(final String fieldName) {
    final Object fieldValue = getField(fieldName);
    if (fieldValue == null) {
        return null;
    }
    if (fieldValue instanceof BigDecimal) {
        return (BigDecimal) fieldValue;
    }
    final FieldDefinition fieldDefinition = dataDefinition.getField(fieldName);
    if (fieldValue instanceof String && BigDecimal.class.equals(fieldDefinition.getType().getType())) {
        if (StringUtils.isBlank((String) fieldValue)) {
            return null;
        }
        final ValueAndError valueAndError = fieldDefinition.getType().toObject(fieldDefinition, fieldValue);
        if (valueAndError.isValid()) {
            return (BigDecimal) valueAndError.getValue();
        }
    }
    throw new IllegalArgumentException("Field " + fieldName + " in " + dataDefinition.getPluginIdentifier() + '.' + dataDefinition.getName() + " does not contain correct BigDecimal value (current field value: " + fieldValue + ")");
}
Also used : ValueAndError(com.qcadoo.model.internal.api.ValueAndError) BigDecimal(java.math.BigDecimal)

Aggregations

ValueAndError (com.qcadoo.model.internal.api.ValueAndError)6 EnumeratedType (com.qcadoo.model.api.types.EnumeratedType)2 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)2 Test (org.junit.Test)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 ZoomLevel (com.qcadoo.view.internal.components.ganttChart.GanttChartScaleImpl.ZoomLevel)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 DateTime (org.joda.time.DateTime)1 JSONObject (org.json.JSONObject)1