コード例 #1
0
  /**
   * Map a list of Computer to a list of ComputerDTO.
   *
   * @param computers the computers to map
   * @return the computers as dto
   */
  public List<ComputerDto> listFromModel(List<Computer> computers) {
    DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern(
            messageSource.getMessage("app.formatDate", null, LocaleContextHolder.getLocale()));
    List<ComputerDto> computersDto = new ArrayList<ComputerDto>();

    for (Computer computer : computers) {
      String introduced = null;
      String discontinued = null;
      long companyId = 0;
      String companyName = null;
      if (null != computer.getIntroduced()) {
        introduced = computer.getIntroduced().format(formatter);
      }
      if (null != computer.getDiscontinued()) {
        discontinued = computer.getDiscontinued().format(formatter);
      }
      if (null != computer.getCompany()) {
        companyId = computer.getCompany().getId();
        companyName = computer.getCompany().getName();
      }

      computersDto.add(
          new ComputerDto.ComputerDtoBuilder(computer.getName())
              .id(computer.getId())
              .companyId(companyId)
              .companyName(companyName)
              .introduced(introduced)
              .discontinued(discontinued)
              .build());
    }
    return computersDto;
  }
コード例 #2
0
  /**
   * map a computer to a ComputerDTO.
   *
   * @param computer the computer to map
   * @return the computer as a dto
   */
  public ComputerDto fromModel(Computer computer) {
    String companyName = null;
    long companyId = 0;
    String introduced = null;
    String discontinued = null;

    if (null != computer.getCompany()) {
      companyId = computer.getCompany().getId();
      companyName = computer.getCompany().getName();
    }

    if (null != computer.getIntroduced()) {
      introduced = computer.getIntroduced().toString();
    }
    if (null != computer.getDiscontinued()) {
      discontinued = computer.getDiscontinued().toString();
    }

    return new ComputerDto.ComputerDtoBuilder(computer.getName())
        .id(computer.getId())
        .companyId(companyId)
        .companyName(companyName)
        .introduced(introduced)
        .discontinued(discontinued)
        .build();
  }