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