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");
}
}
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 + ")");
}
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]);
}
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]);
}
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 + ")");
}
Aggregations