Search in sources :

Example 1 with UserService

use of com.tomasio.projects.trainning.interfaces.UserService in project trainning by fernandotomasio.

the class EXC016CSV method makeReport.

@Override
public void makeReport() {
    UserService userService = (UserService) services.get("userService");
    Writer writter = null;
    try {
        writter = new OutputStreamWriter(os, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(EXC016CSV.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedWriter bufferedWriter = new BufferedWriter(writter);
    // recuperar paramentro
    String organizacao = "";
    organizacao += (String) params.get("organizacao");
    // busca todos usuários
    NetworkUserDTO[] usuarios = userService.findAllUsers();
    // jogo no ArrayList para trabalhar
    List<NetworkUserDTO> usuariosFiltrado = new ArrayList();
    usuariosFiltrado.addAll(Arrays.asList(usuarios));
    // verificar usuário filtrou por searchForm.getOrganizacaoId (OM)
    if (!(organizacao.equals(""))) {
        // corre os usuários verifcando se a OM igual a FILTRADA
        for (Iterator<NetworkUserDTO> iterator = usuariosFiltrado.iterator(); iterator.hasNext(); ) {
            NetworkUserDTO next = iterator.next();
            String usuarioOM = next.getOrganizacao();
            if (!(organizacao.equals(usuarioOM))) {
                // comparar se a OM é igual filtro passado
                iterator.remove();
            }
        }
    }
    // ordenar a lista de usuários
    Collections.sort(usuariosFiltrado, new Comparator<NetworkUserDTO>() {

        @Override
        public int compare(NetworkUserDTO o1, NetworkUserDTO o2) {
            return o1.getNome().compareTo(o2.getNome());
        }
    });
    Collections.sort(usuariosFiltrado, new Comparator<NetworkUserDTO>() {

        @Override
        public int compare(NetworkUserDTO o1, NetworkUserDTO o2) {
            return o1.getOrganizacao().compareTo(o2.getOrganizacao());
        }
    });
    // correr o ArrayList para montar o arquivo CSV
    for (NetworkUserDTO usuario : usuariosFiltrado) {
        try {
            // organizacao
            bufferedWriter.write(usuario.getOrganizacao());
            bufferedWriter.write("|");
            // nome
            bufferedWriter.write(usuario.getNome());
            bufferedWriter.write("|");
            // posto
            bufferedWriter.write(usuario.getPosto());
            bufferedWriter.write("|");
            // nomeGuerra
            bufferedWriter.write(usuario.getNomeGuerra());
            bufferedWriter.write("|");
            // email
            bufferedWriter.write(usuario.getEmail());
            bufferedWriter.write("|");
            // login
            bufferedWriter.write(usuario.getLogin());
            bufferedWriter.write("|");
            bufferedWriter.newLine();
        } catch (IOException ex) {
            Logger.getLogger(EXC016CSV.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException ex) {
        Logger.getLogger(EXC016CSV.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : UserService(com.tomasio.projects.trainning.interfaces.UserService) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) NetworkUserDTO(com.tomasio.projects.trainning.dto.NetworkUserDTO) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

NetworkUserDTO (com.tomasio.projects.trainning.dto.NetworkUserDTO)1 UserService (com.tomasio.projects.trainning.interfaces.UserService)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1