/** * Method for initializing required staff for commons-beanutils classes. */ @PostConstruct public void init() { DateTimeConverter dtConverter = new DateConverter(); dtConverter.setPattern(Constants.DEFAULT_DATEFORMAT); ConvertUtils.register(dtConverter, Date.class); }
public BaseServiceWithValidationImpl() { DateTimeConverter dtConverter = new DateConverter(); // TODO This list of patterns will need to be database driven in the future // Also this kind of thing could be handled in the validation layer as well. Do the convert, validate the converted // Date Obj etc.... String[] patterns = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy", "yyyy-MM-dd", "yyyy-M-d", "yyyy-M-dd", "yyyy-MM-d", "yyyy-MM-dd HH:mm:ss.SSS" }; dtConverter.setPatterns(patterns); ConvertUtils.register(dtConverter, java.util.Date.class); }
@Override public void processTuple(byte[] tuple) { if (tuple == null) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(null, "Blank/null tuple")); } errorTupleCount++; return; } String incomingString = new String(tuple); if (StringUtils.isBlank(incomingString)) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(incomingString, "Blank tuple")); } errorTupleCount++; return; } try { if (out.isConnected() && clazz != null) { Matcher matcher = pattern.matcher(incomingString); boolean patternMatched = false; Constructor<?> ctor = clazz.getConstructor(); Object object = ctor.newInstance(); if (matcher.find()) { for (int i = 0; i <= matcher.groupCount() - 1; i++) { if (delimitedParserSchema.getFields().get(i).getType() == DelimitedSchema.FieldType.DATE) { DateTimeConverter dtConverter = new DateConverter(); dtConverter.setPattern((String)delimitedParserSchema.getFields().get(i).getConstraints().get(DelimitedSchema.DATE_FORMAT)); ConvertUtils.register(dtConverter, Date.class); } BeanUtils.setProperty(object, delimitedParserSchema.getFields().get(i).getName(), matcher.group(i + 1)); } patternMatched = true; } if (!patternMatched) { throw new ConversionException("The incoming tuple do not match with the Regex pattern defined."); } out.emit(object); emittedObjectCount++; } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException | ConversionException e) { if (err.isConnected()) { err.emit(new KeyValPair<String, String>(incomingString, e.getMessage())); logger.debug("Regex Expression : {} Incoming tuple : {}", splitRegexPattern, incomingString); } errorTupleCount++; logger.error("Tuple could not be parsed. Reason {}", e.getMessage()); } }
public static <T extends DateTimeConverter> T setPatterns(T converter ,String... patterns) { converter.setPatterns(patterns); return converter; }
public static void SetupDateConverters() { DateTimeConverter dtConverter = new DateConverter(null); dtConverter.setPatterns(new String[] {"yyyy-MM-dd", "yyyy-MM-dd hh:mm:ss"}); ConvertUtils.register(dtConverter, Date.class); }