public ImportarArvore recuperarArvore(Grade grade, boolean consideraCo) {

    for (ImportarArvore importarArvore : todasArvores) {
      if (importarArvore.getGrade().getId() == grade.getId()) {
        importarArvore.importarDisciplinas(grade, consideraCo);
        return importarArvore;
      }
    }

    ImportarArvore importador = new ImportarArvore();
    importador.setGrade(grade);

    /*if (reseta == true){
    	grade = gradeDAO.recuperarPorId(grade.getId());
    	reseta = false;
    	//importador.setResetarStance(true);
    	gradeDAO = new GradeDAOImpl();

    }*/

    todasArvores.add(importador);
    importador.importarDisciplinas(grade, consideraCo);

    List<Aluno> listaAluno = grade.getGrupoAlunos();

    // List<Aluno> listaAluno = alunoDAO.buscarTodosAlunoCursoGradeObjeto(grade.getCurso().getId(),
    // grade.getId());
    for (Aluno aluno : listaAluno) {
      List<Historico> listaHistorico = aluno.getGrupoHistorico();
      importador.importarHistorico(listaHistorico);
    }
    return importador;
  }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    AlunoDAO dao = new AlunoDAO();
    Aluno aluno = new Aluno();

    String operacao = request.getParameter("operacao");

    switch (operacao) {
      case "incluir":
        aluno.setNome(request.getParameter("nome"));
        aluno.setAtivo(Boolean.parseBoolean(request.getParameter("ativo")));
        aluno.setEmail(request.getParameter("email"));
        aluno.setEndereco(request.getParameter("endereco"));
        aluno.setIdade(Integer.parseInt(request.getParameter("idade")));
        aluno.setMatricula(Long.parseLong(request.getParameter("matricula")));
        aluno.setTelefone(Long.parseLong(request.getParameter("telefone")));
        dao.save(aluno);

        response.sendRedirect("listar.jsp");
        break;

      case "deletar":
        aluno.setId(Integer.parseInt(request.getParameter("id")));
        dao.delete(aluno);
        response.sendRedirect("listar.jsp");

        break;

      case "atualizar":
        aluno.setId(Integer.parseInt(request.getParameter("id")));
        aluno.setNome(request.getParameter("nome"));
        aluno.setEmail(request.getParameter("email"));
        aluno.setEndereco(request.getParameter("endereco"));
        aluno.setIdade(Integer.parseInt(request.getParameter("idade")));
        aluno.setMatricula(Long.parseLong(request.getParameter("matricula")));
        aluno.setTelefone(Long.parseLong(request.getParameter("telefone")));
        aluno.setAtivo(Boolean.parseBoolean(request.getParameter("ativo")));

        dao.update(aluno);

        response.sendRedirect("listar.jsp");
        break;
    }
  }
  @SuppressWarnings({"unchecked"})
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
    Date dataAtual = new Date(System.currentTimeMillis());
    String data = sd.format(dataAtual);
    data = data.replace("/", "");
    fileSavePath = "C:\\entregas";
    if (!(new File(fileSavePath)).exists()) {
      (new File(fileSavePath)).mkdir();
    }

    String resp = "";
    Aluno a = new Aluno();
    boolean erro = false;
    List<FileItem> items;
    resp += "Arquivo carregado:";
    try {
      int cont = 0;
      items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
      Trabalho t = null;
      for (FileItem item : items) {
        if (item.isFormField()) {
          String nomeCampo = item.getFieldName();
          if (nomeCampo.equalsIgnoreCase("ra")) {
            a.setRa(item.getString());
          } else {
            if (nomeCampo.equalsIgnoreCase("trabalhoselecionado")) {
              if (item.getString().trim().equals("")) {
                resp = "Selecione um trabalho";
                erro = true;
                request.setAttribute("message", resp);
                getServletContext()
                    .getRequestDispatcher("/entregatrabalhos.jsp")
                    .forward(request, response);
              }
              t = new Trabalho();
              t.setCodigo(Integer.parseInt(item.getString()));
              TrabalhoDao tDao = new TrabalhoDao();
              t = tDao.consultaTrabalho(t);
            }
          }
          if (t != null && !a.getRa().equals("")) {
            fileSavePath += File.separator + t.getDisciplina().getPasta();
            File newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
            fileSavePath += File.separator + t.getTitulo();
            newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
            fileSavePath += File.separator + a.getRa();
            newDir = new File(fileSavePath);
            if (!newDir.exists()) {
              newDir.mkdir();
            }
          }
        } else {
          if (!erro) {
            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
              cont++;
              String filename = FilenameUtils.getName(item.getName());
              String extensao = filename.substring(filename.lastIndexOf("."), filename.length());
              inputStream = item.getInputStream();
              StringBuffer nomeArquivo = new StringBuffer();
              nomeArquivo.append(a.getRa());
              nomeArquivo.append("_");
              nomeArquivo.append(data);
              nomeArquivo.append(extensao);
              File arquivo = new File(fileSavePath, nomeArquivo.toString());
              if (arquivo.exists()) {
                arquivo.delete();
              }
              outputStream = new FileOutputStream(arquivo);

              int read = 0;
              byte[] bytes = new byte[1024];

              while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
              }
              resp += cont + ". " + filename;
            } finally {
              if (inputStream != null) {
                try {
                  inputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
              if (outputStream != null) {
                try {
                  outputStream.flush();
                  outputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
          }
        }
      }
    } catch (FileUploadException | SQLException e) {
      if (erro) {
        resp = "Erro ao carregar arquivo.";
      } else {
        resp = e.getMessage();
      }
    }
    if (!erro) {
      request.setAttribute("message", resp);
      getServletContext().getRequestDispatcher("/entregatrabalhos.jsp").forward(request, response);
    }
  }