示例#1
0
  /**
   * 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();
  }
示例#2
0
  @Override
  public void initAnnotatedFields() {

    maxpos = 0;
    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;
          }

          int pos = dataField.pos();
          if (annotatedFields.containsKey(pos)) {
            Field f = annotatedFields.get(pos);
            LOG.warn(
                "Potentially invalid model: existing @DataField '{}' replaced by '{}'",
                f.getName(),
                field.getName());
          }
          dataFields.put(pos, dataField);
          annotatedFields.put(pos, field);
          maxpos = Math.max(maxpos, pos);
        }

        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);
      }
    }

    if (annotatedFields.size() < maxpos) {
      LOG.info(
          "Potentially incomplete model: some csv fields may not be mapped to @DataField members");
    }
  }
示例#3
0
  /**
   * Generate a table containing the data formatted and sorted with their position/offset If the
   * model is Ordered than a key is created combining the annotation @Section and Position of the
   * field If a relation @OneToMany is defined, than we iterate recursively through this function
   * The result is placed in the Map<Integer, List> results
   */
  private void generateCsvPositionMap(
      Class<?> clazz, Object obj, Map<Integer, List<String>> results) throws Exception {

    String result = "";

    for (Field field : clazz.getDeclaredFields()) {

      field.setAccessible(true);

      DataField datafield = field.getAnnotation(DataField.class);

      if (datafield != null) {

        if (obj != null) {

          // Retrieve the format, pattern and precision associated to the type
          Class<?> type = field.getType();

          // Create format
          Format<?> format = FormatFactory.getFormat(type, getLocale(), datafield);

          // Get field value
          Object value = field.get(obj);

          result = formatString(format, value);

          if (datafield.trim()) {
            result = result.trim();
          }

          if (datafield.clip() && result.length() > datafield.length()) {
            result = result.substring(0, datafield.length());
          }

          if (LOG.isDebugEnabled()) {
            LOG.debug(
                "Value to be formatted: {}, position: {}, and its formatted value: {}",
                new Object[] {value, datafield.pos(), result});
          }

        } else {
          result = "";
        }

        Integer key;

        if (isMessageOrdered() && obj != null) {

          // Generate a key using the number of the section
          // and the position of the field
          Integer key1 = sections.get(obj.getClass().getName());
          Integer key2 = datafield.position();
          Integer keyGenerated = generateKey(key1, key2);

          if (LOG.isDebugEnabled()) {
            LOG.debug("Key generated: {}, for section: {}", String.valueOf(keyGenerated), key1);
          }

          key = keyGenerated;

        } else {
          key = datafield.pos();
        }

        if (!results.containsKey(key)) {
          List<String> list = new LinkedList<String>();
          list.add(result);
          results.put(key, list);
        } else {
          List<String> list = results.get(key);
          list.add(result);
        }
      }

      OneToMany oneToMany = field.getAnnotation(OneToMany.class);
      if (oneToMany != null) {

        // Set global variable
        // Will be used during generation of CSV
        isOneToMany = true;

        List<?> list = (List<?>) field.get(obj);
        if (list != null) {

          Iterator<?> it = list.iterator();
          while (it.hasNext()) {
            Object target = it.next();
            generateCsvPositionMap(target.getClass(), target, results);
          }

        } else {

          // Call this function to add empty value
          // in the table
          generateCsvPositionMap(field.getClass(), null, results);
        }
      }
    }
  }