public String editConsulta(Agendamento o) {
   Dao dao = new Dao();
   agendamentoEdit = (Agendamento) dao.rebind(o);
   agendamentoEdit.setHoraAgenda(DataHoje.livre(new Date(), "HH:mm"));
   agendamentoEdit.setDataAgenda(new Date());
   for (int i = 0; i < listSelectItem[0].size(); i++) {
     if (o.getEvento().getId() == Integer.parseInt(listSelectItem[0].get(i).getDescription())) {
       index[0] = i;
       break;
     }
   }
   return null;
 }
 public void delete(Agendamento o) {
   Dao dao = new Dao();
   Logger logger = new Logger();
   if (o.getId() != -1) {
     if (dao.delete(o, true)) {
       Messages.info("Sucesso", "Registro removido");
       logger.delete(
           "ID: "
               + o.getId()
               + "]"
               + " - Contrato: ["
               + o.getContrato()
               + " - Agenda: "
               + o.getDataAgendaString()
               + " - Hora: "
               + o.getHoraAgenda());
       clear(0);
     } else {
       Messages.warn("Erro", "Ao remover registro!");
     }
   }
 }
 public String edit(Object o) {
   Dao dao = new Dao();
   agendamento = (Agendamento) dao.rebind(o);
   horaAgenda = agendamento.getHoraAgenda();
   for (int i = 0; i < listSelectItem[0].size(); i++) {
     if (agendamento.getEvento().getId()
         == Integer.parseInt(listSelectItem[0].get(i).getDescription())) {
       index[0] = i;
       break;
     }
   }
   if (agendamento.getFuncaoEquipe() != null) {
     for (int i = 0; i < listSelectItem[1].size(); i++) {
       if (agendamento.getFuncaoEquipe().getId()
           == Integer.parseInt(listSelectItem[1].get(i).getDescription())) {
         index[3] = i;
         break;
       }
     }
   }
   return null;
 }
  public void save() {
    ConfiguracaoCoordenacao cc =
        new ConfiguracaoCoordenacaoDao().findByCliente(SessaoCliente.get().getId());
    if (cc == null) {
      Messages.warn("Sistema", "Configurar Coordernação! Segurança > Configuração > Coordenação");
      return;
    }
    Dao dao = new Dao();
    Logger logger = new Logger();
    if (index[0] == null) {
      Messages.warn("Validação", "Selecionar evento!");
      return;
    }
    List list = new FotosDao().findFotosByContrato(contrato.getId());
    if (list.isEmpty()) {
      Messages.warn("Validação", "Cadastrar nova foto de evolução para realizar agendamento!");
      return;
    }
    list = new FotosDao().findFotosByContrato(contrato.getId(), 30);
    if (list.isEmpty()) {
      Messages.warn("Validação", "Cadastrar nova foto de evolução para realizar agendamento!");
      return;
    }
    visibleCadastrarFoto = false;
    agendamento.setEvento(
        (Evento)
            dao.find(
                new Evento(), Integer.parseInt(listSelectItem[0].get(index[0]).getDescription())));
    if (getHabilitaFuncaoEquipe()) {
      if (listSelectItem[1].isEmpty()) {
        Messages.warn("Validação", "Nenhuma função encontrada!");
        return;
      }
      if (index[3] == null) {
        Messages.warn("Validação", "Selecionar função equipe!");
        return;
      }
      agendamento.setFuncaoEquipe(
          (FuncaoEquipe)
              dao.find(
                  new FuncaoEquipe(),
                  Integer.parseInt(listSelectItem[1].get(index[3]).getDescription())));
    }
    if (contrato.getId() == null) {
      Messages.warn("Validação", "Pesquisar contrato!");
      return;
    }
    if (agendamento.getDataAgendaString().isEmpty()) {
      Messages.warn("Validação", "Informar data do agendamento!");
      return;
    }

    // INICIA - CONFIGURAÇÃO > COORDENAÇÃO
    DataHoje dataHoje = new DataHoje();
    Integer dt1 = DataHoje.converteDataParaInteger(DataHoje.data());
    Integer dt2 = DataHoje.converteDataParaInteger(agendamento.getDataAgendaString());
    Integer dt3 = dt2;
    Integer dt4 =
        DataHoje.converteDataParaInteger(
            dataHoje.incrementarMeses(cc.getAgendamentoMaxMesesAgenda(), DataHoje.data()));
    // VERIFICA SE PERMITE AGENDAMENTO RETROATIVO
    if (cc.getAgendamentoDataRetroativo() != null) {
      dt2 =
          DataHoje.converteDataParaInteger(
              DataHoje.converteData(cc.getAgendamentoDataRetroativo()));
      if (dt2 < dt1) {
        dt2 = dt3;
      }
    }
    if (dt2 <= dt1) {
      Messages.warn("Validação", "A data do agendamento deve ser superior a data de hoje!");
      return;
    }
    // VERIFICA PERÍODO MÁXIMO DE MESES PARA AGENDAMENTO FUTURO
    if (dt3 > dt4) {
      Messages.warn(
          "Validação",
          "A data do agendamento não deve ser superior a "
              + cc.getAgendamentoMaxMesesAgenda()
              + " mese(s)");
      return;
    }
    // TERMINA - CONFIGURAÇÃO > COORDENAÇÃO
    if (horaAgenda.isEmpty()) {
      Messages.warn("Validação", "Informar horário válido!");
      return;
    }
    agendamento.setHoraAgenda(horaAgenda);
    if (agendamento.getId() == -1) {
      agendamento.setStatus((Status) dao.find(new Status(), 1));
      agendamento.setAgendador((Usuario) Sessions.getObject("sessaoUsuario"));
      agendamento.setContrato(contrato);
      AgendamentoDao agendamentoDao = new AgendamentoDao();
      if (agendamentoDao.exists(
          contrato.getId(), agendamento.getDataAgenda(), agendamento.getHoraAgenda())) {
        Messages.warn("Validação", "Agendamento já cadastrado!");
        return;
      }
      if (dao.save(agendamento, true)) {
        Messages.info("Sucesso", "Registro adicionado");
        logger.save(
            "ID: "
                + agendamento.getId()
                + "]"
                + " - Contrato: ["
                + agendamento.getContrato()
                + " - Agenda: "
                + agendamento.getDataAgendaString()
                + " - Hora: "
                + agendamento.getHoraAgenda());
        clear(0);
      } else {
        Messages.warn("Erro", "Ao adicionar registro!");
      }
    } else {
      Agendamento a = (Agendamento) dao.find(agendamento);
      String beforeUpdate =
          "ID: "
              + a.getId()
              + "]"
              + " - Contrato: ["
              + a.getContrato()
              + " - Agenda: "
              + a.getDataAgendaString()
              + " - Hora: "
              + a.getHoraAgenda();
      if (dao.update(agendamento, true)) {
        Messages.info("Sucesso", "Registro atualizado");
        logger.update(
            beforeUpdate,
            "ID: "
                + agendamento.getId()
                + "]"
                + " - Contrato: ["
                + agendamento.getContrato()
                + " - Agenda: "
                + agendamento.getDataAgendaString()
                + " - Hora: "
                + agendamento.getHoraAgenda());
        clear(0);
      } else {
        Messages.warn("Erro", "Ao atualizar registro!");
      }
    }
  }