@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String formattedDate = json.getAsJsonPrimitive().getAsString(); try { return Strings.isNullOrEmpty(formattedDate) ? null : dateDatatype.parse(formattedDate); } catch (ParseException e) { throw new EntitySerializationException("Cannot parse date " + formattedDate); } } }
private static Object parseByDatatype(String value, Class<?> type) throws ParseException { Datatype datatype = Datatypes.getNN(type); return datatype.parse(value); }
protected Object readSimpleProperty(JsonReader in, Class<?> propertyType) throws IOException { String value = in.nextString(); Object parsedValue = null; try { Datatype<?> datatype = Datatypes.get(propertyType); if (datatype != null) { parsedValue = datatype.parse(value); } return parsedValue; } catch (ParseException e) { throw new RuntimeException( format("An error occurred while parsing property. Class [%s]. Value [%s].", propertyType, value), e); } }
/** * Parses string value into specific type * * @param value value to parse * @param typeName Datatype name * @return parsed object */ public static Object parseByTypename(String value, String typeName) { Datatype datatype = Datatypes.get(typeName); try { return datatype.parse(value); } catch (ParseException e) { throw new IllegalArgumentException(String.format("Cannot parse specified parameter of type '%s'", typeName), e); } }
/** * Parse Long using {@code integerFormat} string specified in the main message pack. * @return Long value or null if a blank string is provided */ @Nullable public Long parseLong(String str) throws ParseException { return datatypeRegistry.getNN(Long.class).parse(str, uss.getLocale()); } }
/** * Parse Date (time without date) using {@code timeFormat} string specified in the main message pack. * @return Date value or null if a blank string is provided */ @Nullable public Date parseTime(String str) throws ParseException { return datatypeRegistry.getNN(Time.class).parse(str, uss.getLocale()); }
/** * Parse BigDecimal using {@code decimalFormat} string specified in the main message pack. * @return BigDecimal value or null if a blank string is provided */ @Nullable public BigDecimal parseBigDecimal(String str) throws ParseException { return datatypeRegistry.getNN(BigDecimal.class).parse(str, uss.getLocale()); }
/** * Parse Integer using {@code integerFormat} string specified in the main message pack. * @return Integer value or null if a blank string is provided */ @Nullable public Integer parseInteger(String str) throws ParseException { return datatypeRegistry.getNN(Integer.class).parse(str, uss.getLocale()); }
/** * Parse Date (date without time) using {@code dateFormat} string specified in the main message pack. * @return Date value or null if a blank string is provided */ @Nullable public Date parseDate(String str) throws ParseException { return datatypeRegistry.getNN(java.sql.Date.class).parse(str, uss.getLocale()); }
/** * Parse Double using {@code doubleFormat} string specified in the main message pack. * @return Double value or null if a blank string is provided */ @Nullable public Double parseDouble(String str) throws ParseException { return datatypeRegistry.getNN(Double.class).parse(str, uss.getLocale()); }
/** * Parse Boolean using {@code trueString} and {@code falseString} strings specified in the main message pack. * @return Boolean value or null if a blank string is provided */ @Nullable public Boolean parseBoolean(String str) throws ParseException { return datatypeRegistry.getNN(Boolean.class).parse(str, uss.getLocale()); }
protected Object readSimpleProperty(JsonElement valueElement, Datatype propertyType) { String value = valueElement.getAsString(); if (value == null) return null; try { Class javaClass = propertyType.getJavaClass(); if (BigDecimal.class.isAssignableFrom(javaClass)) { return valueElement.getAsBigDecimal(); } else if (Long.class.isAssignableFrom(javaClass)) { return valueElement.getAsLong(); } else if (Integer.class.isAssignableFrom(javaClass)) { return valueElement.getAsInt(); } else if (Double.class.isAssignableFrom(javaClass)) { return valueElement.getAsDouble(); } return propertyType.parse(value); } catch (ParseException e) { throw new EntitySerializationException(String.format("An error occurred while parsing property. Type [%s]. Value [%s].", propertyType, value), e); } }
protected Object getXmlAnnotationAttributeValue(String value, String className, String datatypeName) { if (className == null && datatypeName == null) return inferMetaAnnotationType(value); if (className != null) { Class aClass = ReflectionHelper.getClass(className); if (aClass.isEnum()) { //noinspection unchecked return Enum.valueOf(aClass, value); } else { throw new UnsupportedOperationException("Class " + className + " is not Enum"); } } else { try { return datatypes.get(datatypeName).parse(value); } catch (ParseException e) { throw new RuntimeException("Unable to parse XML meta-annotation value", e); } } }
@Nullable protected Object getItemId(CollectionDatasource datasource, String itemIdString) { MetaProperty pkProp = metadata.getTools().getPrimaryKeyProperty(datasource.getMetaClass()); if (pkProp != null) { Datatype<Object> datatype = pkProp.getRange().asDatatype(); try { return datatype.parse(itemIdString); } catch (ParseException e) { throw new RuntimeException("Error parsing item ID", e); } } return null; }
protected void loadEntityId(Element element, InstanceLoader<Entity> loader) { String entityIdStr = element.attributeValue("entityId"); if (Strings.isNullOrEmpty(entityIdStr)) { return; } MetaProperty property = metadataTools.getPrimaryKeyProperty(loader.getContainer().getEntityMetaClass()); if (property == null) { throw new IllegalStateException("Cannot determine id property for " + loader.getContainer().getEntityMetaClass()); } if (property.getRange().isDatatype()) { try { Object value = property.getRange().asDatatype().parse(entityIdStr); loader.setEntityId(value); } catch (ParseException e) { throw new RuntimeException("Error parsing entityId for " + loader, e); } } else { throw new IllegalStateException("Cannot assign id to " + loader + " because the entity has a composite PK"); } }
/** * Parse Date (date and time) using {@code dateTimeFormat} string specified in the main message pack. * <p>Takes into account time zone if it is set for the current user session.</p> * @return Date value or null if a blank string is provided */ @Nullable public Date parseDateTime(String str) throws ParseException { TimeZone timeZone = uss.getUserSession().getTimeZone(); Datatype<Date> datatype = datatypeRegistry.getNN(Date.class); if (datatype instanceof DateTimeDatatype) { return ((DateTimeDatatype) datatype).parse(str, uss.getLocale(), timeZone); } return datatype.parse(str, uss.getLocale()); }
/** * Sets the value from the {@code formParam} to the field. Before setting is performed the value is * parsed or if an UEL expression is stored in {@code formParam} this expression is evaluated. */ protected void setFieldValue(Field field, ProcFormParam formParam, Datatype datatype, String actExecutionId) { if (!Strings.isNullOrEmpty(formParam.getValue())) { try { Object value; if (isExpression(formParam.getValue())) { value = processRuntimeService.evaluateExpression(formParam.getValue(), actExecutionId); } else { value = datatype.parse(formParam.getValue()); } field.setValue(value); } catch (ParseException e) { throw new BpmException("Error when parsing process form parameter value", e); } } }
@Override public void commitAndClose() { SessionAttribute item = datasource.getItem(); if (item.getStringValue() != null) { Datatype dt = Datatypes.get(item.getDatatype()); try { Object object = dt.parse(item.getStringValue()); item.setStringValue(object == null ? "" : object.toString()); } catch (IllegalArgumentException | ParseException e) { showNotification(getMessage("unableToParseValue"), NotificationType.ERROR); return; } } super.commitAndClose(); } }
protected Object parseValue(MetaProperty metaProperty, String stringValue) throws RestFilterParseException { if (metaProperty.getRange().isDatatype()) { try { return metaProperty.getRange().asDatatype().parse(stringValue); } catch (ParseException e) { throw new RestFilterParseException("Cannot parse property value: " + stringValue, e); } } else if (metaProperty.getRange().isEnum()) { try { return Enum.valueOf((Class<Enum>) metaProperty.getJavaType(), stringValue); } catch (IllegalArgumentException e) { throw new RestFilterParseException("Cannot parse enum value: " + stringValue, e); } } throw new RestFilterParseException("Cannot parse the condition value: " + stringValue); }
protected Object loadEntity(String id) { Metadata metadata = AppBeans.get(Metadata.class); MetaProperty pkProp = metadata.getTools().getPrimaryKeyProperty(metadata.getClassNN(javaClass)); Object objectId = null; if (pkProp != null) { Datatype<Object> datatype = pkProp.getRange().asDatatype(); try { objectId = datatype.parse(id); } catch (ParseException e) { throw new RuntimeException("Error parsing entity ID", e); } } LoadContext ctx = new LoadContext(javaClass).setId(objectId); DataService dataService = AppBeans.get(DataService.NAME); return dataService.load(ctx); }