Search in sources :

Example 11 with NetworkUserDAO

use of com.tomasio.projects.trainning.dao.NetworkUserDAO in project trainning by fernandotomasio.

the class UserServiceSimpleImpl method updateUser.

@Override
@Transactional(readOnly = true)
public void updateUser(NetworkUserDTO user, String[] roles) {
    NetworkUserDAO userDAO = factory.getNetworkUserDAO();
    NetworkGroupDAO groupDAO = factory.getNetworkGroupDAO();
    OrganizacaoDAO organizacaoDAO = factory.getOrganizacaoDAO();
    try {
        NetworkUserDTO oldUser = userDAO.find(user.getLogin());
        userDAO.update(user);
        if (!oldUser.getOrganizacao().equals(user.getOrganizacao())) {
            // REMOVER DO GRUPO DA ORGANIZAÇÃO ANTIGA
            NetworkGroupDTO oldOrganizationGroup = groupDAO.findOrganization(oldUser.getOrganizacao());
            if (oldOrganizationGroup != null) {
                oldOrganizationGroup.getMembers().remove(oldUser.getId());
                if (oldOrganizationGroup.getMembers().size() > 0) {
                    groupDAO.update(oldOrganizationGroup);
                } else {
                    groupDAO.removeOrganization(oldUser.getOrganizacao());
                }
            }
            // ADICIONAR AO GRUPO DA ORGANIZAÇÃO NOVA
            Organizacao organizacao = organizacaoDAO.findBySigla(user.getOrganizacao());
            NetworkGroupDTO organizationGroup = groupDAO.findOrganization(organizacao.getSigla());
            if (organizationGroup == null) {
                organizationGroup = new NetworkGroupDTO();
                organizationGroup.setNome(organizacao.getSigla());
                organizationGroup.setDescricao(organizacao.getNome());
                organizationGroup.getMembers().add(user.getId());
                groupDAO.createOrganization(organizationGroup);
            } else {
                organizationGroup.getMembers().add(user.getId());
                groupDAO.update(organizationGroup);
            }
        }
        // REMOVER DE TODAS AS ROLES
        List<NetworkGroupDTO> roleGroups = groupDAO.findAllRoles();
        for (NetworkGroupDTO roleGroup : roleGroups) {
            NetworkGroupDTO r = groupDAO.findRole(roleGroup.getNome());
            r.getMembers().remove(oldUser.getId());
            groupDAO.update(r);
        }
        // ADICIONAR NOVAS ROLES
        if (roles != null) {
            for (String role : roles) {
                NetworkGroupDTO roleGroup = groupDAO.findRole(role);
                roleGroup.getMembers().add(user.getId());
                groupDAO.update(roleGroup);
            }
        }
    } catch (DAOException ex) {
        throw new CoreException("Erro de acesso ao serviço de diretórios: " + ex.getMessage());
    }
}
Also used : NetworkGroupDTO(com.tomasio.projects.trainning.dto.NetworkGroupDTO) DAOException(com.tomasio.projects.trainning.exception.DAOException) OrganizacaoDAO(com.tomasio.projects.trainning.dao.OrganizacaoDAO) NetworkUserDTO(com.tomasio.projects.trainning.dto.NetworkUserDTO) Organizacao(com.tomasio.projects.trainning.model.Organizacao) CoreException(com.tomasio.projects.trainning.exeption.CoreException) NetworkUserDAO(com.tomasio.projects.trainning.dao.NetworkUserDAO) NetworkGroupDAO(com.tomasio.projects.trainning.dao.NetworkGroupDAO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with NetworkUserDAO

use of com.tomasio.projects.trainning.dao.NetworkUserDAO 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");
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NetworkUserDAO(com.tomasio.projects.trainning.dao.NetworkUserDAO) ClassPathResource(org.springframework.core.io.ClassPathResource) DAOException(com.tomasio.projects.trainning.exception.DAOException) OrganizacaoDAO(com.tomasio.projects.trainning.dao.OrganizacaoDAO) Organizacao(com.tomasio.projects.trainning.model.Organizacao) NetworkUserDTO(com.tomasio.projects.trainning.dto.NetworkUserDTO) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Aggregations

NetworkUserDAO (com.tomasio.projects.trainning.dao.NetworkUserDAO)12 DAOException (com.tomasio.projects.trainning.exception.DAOException)11 NetworkUserDTO (com.tomasio.projects.trainning.dto.NetworkUserDTO)9 CoreException (com.tomasio.projects.trainning.exeption.CoreException)9 NetworkGroupDAO (com.tomasio.projects.trainning.dao.NetworkGroupDAO)5 OrganizacaoDAO (com.tomasio.projects.trainning.dao.OrganizacaoDAO)5 NetworkGroupDTO (com.tomasio.projects.trainning.dto.NetworkGroupDTO)5 Organizacao (com.tomasio.projects.trainning.model.Organizacao)5 Transactional (org.springframework.transaction.annotation.Transactional)4 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1