use of com.tomasio.projects.trainning.dao.OrganizacaoDAO in project trainning by fernandotomasio.
the class ImportUsersFromCSV method execute.
@Override
public void execute() {
OrganizacaoDAO organizacaoDAO = factory.getOrganizacaoDAO();
NetworkUserDAO userDAO = factory.getNetworkUserDAO();
String csvFile = "/com/tomasio/projects/trainning/migration/usuarios.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ";";
try {
br = new BufferedReader(new FileReader(new ClassPathResource("usuarios.csv").getFile()));
List<String> members = new ArrayList<String>();
while ((line = br.readLine()) != null) {
// use comma as separator
String[] userData = line.split(cvsSplitBy);
String posto = userData[0];
String nome = userData[3];
String sigla = userData[2];
String nomeGuerra = userData[4];
String email = userData[5];
String telefone = userData[6];
String login = userData[7];
String senha = userData[8];
Organizacao organizacao = organizacaoDAO.findBySigla(sigla);
if (organizacao != null) {
System.out.println(organizacao.getSigla());
} else {
System.out.println("ERRO : " + sigla);
System.exit(0);
}
NetworkUserDTO user = new NetworkUserDTO();
user.setEmail(email);
user.setLogin(login);
user.setPosto(posto);
user.setOrganizacao(organizacao.getSigla());
user.setTarjeta(posto + " " + nomeGuerra);
user.setNomeGuerra(nomeGuerra);
user.setPassword(senha);
if (telefone == null || telefone.equals("")) {
telefone = "9999-9999";
}
user.setTelefone(telefone);
user.setNome(nome);
if (userDAO.find(user.getLogin()) == null) {
userDAO.create(user);
System.out.println("criado usuário " + user.getLogin());
} else {
System.out.println("usuário já existe " + user.getLogin());
}
members.add("member: uid=" + user.getLogin() + ",ou=people,dc=decea,dc=intraer");
}
for (String member : members) {
System.out.println(member);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DAOException ex) {
Logger.getLogger(ImportUsersFromCSV.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
Aggregations