/** * Set the default values for the non defined fields. * @param model the model which has its default fields set. * @throws IllegalAccessException if the underlying fields are inaccessible * @throws Exception In case the field cannot be parsed */ private void setDefaultValuesForFields(final Map<String, Object> model) throws IllegalAccessException, Exception { // Set the default values, if defined for (int i = 1; i <= dataFields.size(); i++) { Field field = annotatedFields.get(i); field.setAccessible(true); DataField dataField = dataFields.get(i); Object modelField = model.get(field.getDeclaringClass().getName()); if (field.get(modelField) == null && !dataField.defaultValue().isEmpty()) { FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale()); Format<?> format = formatFactory.getFormat(formattingOptions); Object value = format.parse(dataField.defaultValue()); field.set(modelField, value); } } }
/** * Generate for the first line the headers of the columns * * @return the headers columns */ public String generateHeader() { Map<Integer, DataField> dataFieldsSorted = new TreeMap<Integer, DataField>(dataFields); Iterator<Integer> it = dataFieldsSorted.keySet().iterator(); StringBuilder builderHeader = new StringBuilder(); while (it.hasNext()) { DataField dataField = dataFieldsSorted.get(it.next()); // Retrieve the field Field field = annotatedFields.get(dataField.pos()); // Change accessibility to allow to read protected/private fields field.setAccessible(true); // Get dataField if (!dataField.columnName().equals("")) { builderHeader.append(dataField.columnName()); } else { builderHeader.append(field.getName()); } if (it.hasNext()) { builderHeader.append(separator); } } return builderHeader.toString(); }
public static FormattingOptions convert(DataField dataField, Class<?> clazz, BindyConverter converter, String locale) { return new FormattingOptions() .forClazz(clazz) .withPattern(dataField.pattern()) .withLocale(locale) .withTimezone(dataField.timezone()) .withPrecision(dataField.precision()) .withRounding(dataField.rounding()) .withImpliedDecimalSeparator(dataField.impliedDecimalSeparator()) .withDecimalSeparator(dataField.decimalSeparator()) .withBindyConverter(converter) .withGroupingSeparator(dataField.groupingSeparator()); }
@Override public void initAnnotatedFields() { for (Class<?> cl : models) { List<Field> linkFields = new ArrayList<Field>(); if (LOG.isDebugEnabled()) { LOG.debug("Class retrieved: {}", cl.getName()); } for (Field field : cl.getDeclaredFields()) { DataField dataField = field.getAnnotation(DataField.class); if (dataField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Position defined in the class: {}, position: {}, Field: {}", new Object[]{cl.getName(), dataField.pos(), dataField}); } if (dataField.required()) { ++numberMandatoryFields; } else { ++numberOptionalFields; } dataFields.put(dataField.pos(), dataField); annotatedFields.put(dataField.pos(), field); } Link linkField = field.getAnnotation(Link.class); if (linkField != null) { if (LOG.isDebugEnabled()) { LOG.debug("Class linked: {}, Field: {}", cl.getName(), field); } linkFields.add(field); } } if (!linkFields.isEmpty()) { annotatedLinkFields.put(cl.getName(), linkFields); } totalFields = numberMandatoryFields + numberOptionalFields; if (LOG.isDebugEnabled()) { LOG.debug("Number of optional fields: {}", numberOptionalFields); LOG.debug("Number of mandatory fields: {}", numberMandatoryFields); LOG.debug("Total: {}", totalFields); } } }
@Override public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception { int pos = 1; int counterMandatoryFields = 0; for (String data : tokens) { // Get DataField from model DataField dataField = dataFields.get(pos); ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field: " + data + ", line: " + line); if (dataField.trim()) { data = data.trim(); } if (dataField.required()) { // Increment counter of mandatory fields ++counterMandatoryFields; // Check if content of the field is empty // This is not possible for mandatory fields if (data.equals("")) { throw new IllegalArgumentException("The mandatory field defined at the position " + pos + " is empty for the line: " + line); } } // Get Field to be setted Field field = annotatedFields.get(pos); field.setAccessible(true); if (LOG.isDebugEnabled()) { LOG.debug("Pos: {}, Data: {}, Field type: {}", new Object[]{pos, data, field.getType()}); } // Create format object to format the field FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale()); Format<?> format = formatFactory.getFormat(formattingOptions); // field object to be set Object modelField = model.get(field.getDeclaringClass().getName()); // format the data received Object value = null; if (!data.equals("")) { try { value = format.parse(data); } catch (FormatException ie) { throw new IllegalArgumentException(ie.getMessage() + ", position: " + pos + ", line: " + line, ie); } catch (Exception e) { throw new IllegalArgumentException("Parsing error detected for field defined at the position: " + pos + ", line: " + line, e); } } else { if (!dataField.defaultValue().isEmpty()) { value = format.parse(dataField.defaultValue()); } else { value = getDefaultValueForPrimitive(field.getType()); } } field.set(modelField, value); ++pos; } LOG.debug("Counter mandatory fields: {}", counterMandatoryFields); if (counterMandatoryFields < numberMandatoryFields) { throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line); } if (pos < totalFields) { setDefaultValuesForFields(model); } }