Created
June 3, 2019 00:54
-
-
Save HumbertoMarthan/50b623e8fde615f73cce2e3a29b6dac2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Estou Utilizando o componente Schedule do Primefaces(6.2) | |
Meu projeto utiliza: | |
Postgres, Hibernate, JSF. | |
Com o código desta maneira eu consigo cadastrar um novo agendamento e não consigo adicionar um novo agendamento para o mesmo médico no mesmo dataInicio e dataFim de um agendamento. | |
(O objetivo era esse) | |
Problema 1: | |
O problema atual é que quando vou fazer a edição do mesmo agendamento não consigo salvar, pois é entendido pelo código que estou tentando salvar outro agendamento no mesmo dataInicio e dataFim de um agendamento já existente! | |
Problema 3: Ao editar um agendamento já criado e tentar salva-lo o registro é duplicado, o registro antes da edição se mantém e o novo é salvo alterado. | |
Classe Evento(Agendamento) | |
/** | |
* @author Humberto | |
* | |
*/ | |
@Audited | |
@Entity | |
@Table(name = "evento") | |
@SequenceGenerator(name = "evento_seq", sequenceName = "evento_seq", initialValue = 1, allocationSize = 1) | |
public class Evento implements Serializable { | |
private static final long serialVersionUID = 1089332436469136104L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "evento_seq") | |
private Long id; | |
private String titulo; | |
private Date dataInicio; | |
private Date dataFim; | |
private boolean diaInteiro; | |
private TipoEvento tipoEvento; | |
private String descricao; | |
@ManyToOne(cascade = CascadeType.ALL) | |
@JoinColumn() | |
private Paciente paciente; | |
@ManyToOne(cascade = CascadeType.ALL) | |
@JoinColumn() | |
private Medico medico; | |
public Evento() { | |
this.tipoEvento = TipoEvento.CONSULTA; | |
this.titulo = ""; | |
this.diaInteiro = false; | |
} | |
public Evento(Long id, | |
String titulo, | |
Date dataInicio, | |
Date dataFim, | |
boolean diaInteiro, | |
TipoEvento tipoEvento, | |
String descricao, | |
Paciente paciente, | |
Medico medico) | |
{ | |
this.id = id; | |
this.titulo = titulo; | |
this.dataInicio = dataInicio; | |
this.dataFim = dataFim; | |
this.diaInteiro = diaInteiro; | |
this.tipoEvento = tipoEvento; | |
this.setDescricao(descricao); | |
this.paciente = paciente; | |
this.medico = medico; | |
} | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getTitulo() { | |
return titulo; | |
} | |
public void setTitulo(String titulo) { | |
this.titulo = titulo; | |
} | |
public Date getDataInicio() { | |
return dataInicio; | |
} | |
public void setDataInicio(Date dataInicio) { | |
//Calendar calendar = java.util.Calendar.getInstance() ; | |
// calendar.set(Calendar.HOUR_OF_DAY, 8) ; | |
//calendar.set(Calendar); | |
this.dataInicio = dataInicio; | |
} | |
public Date getDataFim() { | |
return dataFim; | |
} | |
public void setDataFim(Date dataFim) { | |
this.dataFim = dataFim; | |
} | |
public boolean isDiaInteiro() { | |
return diaInteiro; | |
} | |
public void setDiaInteiro(boolean diaInteiro) { | |
this.diaInteiro = diaInteiro; | |
} | |
public TipoEvento getTipoEvento() { | |
return tipoEvento; | |
} | |
public void setTipoEvento(TipoEvento tipoEvento) { | |
this.tipoEvento = tipoEvento; | |
} | |
public Medico getMedico() { | |
return medico; | |
} | |
public void setMedico(Medico medico) { | |
this.medico = medico; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 3; | |
hash = 29 * hash + Objects.hashCode(this.id); | |
return hash; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) { | |
return true; | |
} | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
final Evento other = (Evento) obj; | |
if (!Objects.equals(this.id, other.id)) { | |
return false; | |
} | |
return true; | |
} | |
public Paciente getPaciente() { | |
return paciente; | |
} | |
public void setPaciente(Paciente paciente) { | |
this.paciente = paciente; | |
} | |
public String getDescricao() { | |
return descricao; | |
} | |
public void setDescricao(String descricao) { | |
this.descricao = descricao; | |
} | |
} | |
ScheduleBean | |
@ManagedBean(name = "scheduleBean") | |
@Controller | |
@Scope(value = "session") | |
public class ScheduleBean extends BeanManagedViewAbstract { | |
private static final long serialVersionUID = 1L; | |
private ScheduleModel model; | |
private Evento evento; | |
private ScheduleEvent event; | |
private List<ScheduleEvent> scheduleEvents; | |
private Date dataInicioSelecionada; | |
private Date dataFimSelecionada; | |
Date dataAtual = new Date(); | |
private CarregamentoLazyListForObjeto<Evento> list = new CarregamentoLazyListForObjeto<Evento>(); | |
@Autowired | |
private EventoController eventoController; | |
@Autowired | |
private PacienteController pacienteController; | |
@Autowired | |
private MedicoController medicoController; | |
public ScheduleBean() { | |
event = new CustomScheduleEvent(); | |
model = new DefaultScheduleModel(); | |
evento = new Evento(); | |
} | |
@Override | |
public StreamedContent getArquivoReport() throws Exception { | |
super.setNomeRelatorioJasper("report_evento"); | |
super.setNomeRelatorioSaida("report_evento"); | |
super.setListDataBeanCollectionReport(eventoController.findList(getClassImp())); | |
return super.getArquivoReport(); | |
} | |
// LISTA DE PACIENTES | |
public List<SelectItem> getPacientes() throws Exception { | |
return pacienteController.getListPacientes(); | |
} | |
// LISTA DE MEDICOS | |
public List<SelectItem> getMedicos() throws Exception { | |
return medicoController.getListMedicos(); | |
} | |
@PostConstruct | |
public void init() throws Exception { | |
if (this.model != null) { | |
List<Evento> eventos = this.eventoController.listarEventos(); | |
// List<Evento> eventos = this.eventoDAO.listarTodos(); | |
if (this.scheduleEvents == null) { | |
this.scheduleEvents = new ArrayList<ScheduleEvent>(); | |
} | |
for (Evento eventoAtual : eventos) { // lista que popula os eventos e inseri | |
ScheduleEvent newEvent = new CustomScheduleEvent(eventoAtual.getTitulo(), eventoAtual.getDataInicio(), | |
eventoAtual.getDataFim(), eventoAtual.getTipoEvento().getCss(), eventoAtual.isDiaInteiro(), | |
eventoAtual.getDescricao(), eventoAtual.getMedico(), eventoAtual.getPaciente(), eventoAtual); | |
if (!this.scheduleEvents.contains(newEvent)) { | |
newEvent.setId(eventoAtual.getId().toString()); | |
this.scheduleEvents.add(newEvent); | |
this.model.addEvent(newEvent); | |
} | |
} | |
} | |
} | |
public boolean validarMedico() throws Exception { | |
String[] param = new String[] {"idMedico", "dataInicio", "dataFim","eventoId"}; | |
String hql ="FROM Evento e WHERE e.medico.idMedico = " | |
+ ":idMedico AND (e.dataInicio BETWEEN :dataInicio AND :dataFim " | |
+ "OR e.dataFim BETWEEN :dataInicio AND :dataFim) AND (e.id = :eventoId AND :eventoId IS NOT NULL)"; | |
List<Evento> lista = eventoController.findListByQueryDinamica( | |
hql, Arrays.asList(param) , evento.getMedico().getIdMedico(), evento.getDataInicio(), evento.getDataFim(),evento.getId()); | |
//novo | |
if (evento.getId() == null && !lista.isEmpty()) { | |
return false; | |
//se for igual as datas e lista vier completa | |
// vindo completa quer dizer que há um agendamento gravado | |
}else if((this.dataInicioSelecionada.compareTo(evento.getDataInicio()) != 0 || | |
this.dataFimSelecionada.compareTo(evento.getDataFim()) !=0) | |
&& !lista.isEmpty()){ | |
return false; | |
}else { | |
return true; | |
} | |
} | |
public void salvar() throws Exception { | |
// salva o construtor que implementa a interface do Schedule com os atributos. | |
ScheduleEvent newEvent = new CustomScheduleEvent(this.evento.getTitulo(), this.evento.getDataInicio(), | |
this.evento.getDataFim(), this.evento.getTipoEvento().getCss(), this.evento.isDiaInteiro(), | |
this.evento.getDescricao(), this.evento.getMedico(), this.evento.getPaciente(), this.evento); | |
if (evento.getDataInicio().before(evento.getDataFim()) && validarMedico()) { | |
if (evento.getId() == null) { | |
model.addEvent(newEvent); | |
} else { | |
newEvent.setId(event.getId()); | |
model.updateEvent(newEvent); | |
} | |
eventoController.merge(evento); | |
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Agendamento Salvo", | |
"Agendamento para: " + evento.getTitulo()); | |
addMessage(message); | |
}else { | |
FacesMessage message = new FacesMessage( | |
FacesMessage.SEVERITY_INFO, "Já existe um médico cadastrado CAIU NO ELSE", | |
"" ); | |
addMessage(message); | |
} | |
} | |
public void remover() throws Exception { | |
try{ | |
eventoController.delete(evento); | |
model.deleteEvent(event); | |
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Agendamento Removido", | |
"Agendamento Removido :" + evento.getTitulo()); | |
addMessage(message); | |
}catch (Exception e) { | |
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Impossivel remover", | |
"Há dependencias:" + evento.getTitulo()); | |
addMessage(message); | |
} | |
} | |
// AO SALVAR SELEÇÃO DE UMA AGENDAMENTO | |
public void onDateSelect(SelectEvent selectEvent) { | |
this.evento = new Evento(); | |
Date dataSelecionada = (Date) selectEvent.getObject(); | |
@SuppressWarnings("unused") | |
DateTime dataSelecionadaJoda = new DateTime(dataSelecionada.getTime()); | |
this.evento.setDataInicio(dataSelecionada); | |
// Adiciona 30min por consulta | |
//this.evento.setDataFim(dataSelecionadaJoda.plusMinutes(30).toDate()); | |
} | |
// EVENTO DE SELEÇÃO DOS HORARIOS AGENDADOS | |
public void onEventSelect(SelectEvent selectEvent) { | |
event = (CustomScheduleEvent) selectEvent.getObject(); | |
this.evento = (Evento) event.getData(); | |
this.dataInicioSelecionada = this.evento.getDataInicio(); | |
this.dataFimSelecionada = this.evento.getDataFim(); | |
} | |
// EVENTO QUE PERMITE REDIMENSIONAR HORARIOS | |
public void onEventResize(ScheduleEntryResizeEvent event) { | |
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Evento Redimensionado", | |
"Dia:" + event.getDayDelta() + ", Horário:" + event.getMinuteDelta()); | |
addMessage(message); | |
} | |
// EVENTO QUE PERMITE MOVER HORARIOS SELECIONADOS | |
public void onEventMove(ScheduleEntryMoveEvent event) { | |
/* | |
* if(evento.getDataInicio().getTime() <= evento.getDataFim().getTime()){ | |
* | |
* try{ eventoController.merge(evento); }catch(Exception ex){ | |
* FacesContext.getCurrentInstance().addMessage(null, new | |
* FacesMessage("Erro ao salvar trabalho", "Erro:" + ex.getMessage())); } evento | |
* = new Evento(); }else{ FacesContext.getCurrentInstance().addMessage(null, new | |
* FacesMessage("Data do começo do evento não pode ser maior que a do final", | |
* "")); } | |
*/ | |
// FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event | |
// moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + | |
// event.getMinuteDelta()); | |
// addMessage(message); | |
} | |
private void addMessage(FacesMessage message) { | |
FacesContext.getCurrentInstance().addMessage(null, message); | |
} | |
public TipoEvento[] getTiposEventos() { | |
return TipoEvento.values(); | |
} | |
// GETTERS E SETTERS | |
public List<ScheduleEvent> getScheduleEvents() { | |
return scheduleEvents; | |
} | |
public void setScheduleEvents(List<ScheduleEvent> scheduleEvents) { | |
this.scheduleEvents = scheduleEvents; | |
} | |
public Date getDataAtual() { | |
return dataAtual; | |
} | |
public void setDataAtual(Date dataAtual) { | |
// Pega somente a data para passar para data minima do calendario | |
LocalDate localDate = new LocalDate(); | |
dataAtual = localDate.toDate(); | |
this.dataAtual = dataAtual; | |
} | |
@Override | |
protected Class<Evento> getClassImp() { | |
return Evento.class; | |
} | |
@Override | |
protected InterfaceCrud<Evento> getController() { | |
return eventoController; | |
} | |
@Override | |
public void consultarEntidade() throws Exception { | |
evento = new Evento(); | |
list.clean(); | |
list.setTotalRegistroConsulta(super.totalRegistroConsulta(), super.getSqlLazyQuery()); | |
} | |
@Override | |
public String condicaoAndParaPesquisa() throws Exception { | |
return null; | |
} | |
public ScheduleModel getModel() { | |
return model; | |
} | |
public void setModel(ScheduleModel model) { | |
this.model = model; | |
} | |
public Evento getEvento() { | |
return evento; | |
} | |
public void setEvento(Evento evento) { | |
this.evento = evento; | |
} | |
public CarregamentoLazyListForObjeto<Evento> getList() { | |
return list; | |
} | |
public void setList(CarregamentoLazyListForObjeto<Evento> list) { | |
this.list = list; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment