package managedbean.visit; import java.io.IOException; import java.io.Serializable; import java.util.Date; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; import javax.inject.Named; import org.primefaces.model.LazyDataModel; import org.primefaces.model.SortOrder; import TO.PatientTO; import TO.VisitTO; import common.Constants; import common.UserType; import managedbean.common.ManagedBeanBase; import managedbean.common.SessionUtils; @Named("PatientVisitList") @ViewScoped public class PatientVisitListMBean extends ManagedBeanBase implements Serializable { private static final long serialVersionUID = 1L; private PatientTO patient; private Integer patientId; private LazyDataModel lazyDataModelVisitList; private Date selectedDate; private String lastUIQuery; private List patientList; public PatientVisitListMBean() { } @PostConstruct public void init() { this.lastUIQuery = ""; // Si el usuario es un paciente listamos las visitas de ese paciente, si es admnistrador listamos todas. switch (SessionUtils.getUserType()) { case ADMINISTRATOR: this.patientId = null; this.patient = null; this.selectedDate = new Date(); this.patientList = this.getRemoteManagerCommon().listPatientsPaged(0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH); break; case PATIENT: this.patientId = Integer.valueOf(SessionUtils.getUserId()); this.patient = this.getRemoteManagerCommon().findPatientById(patientId); this.selectedDate = null; break; case FAMILY_DOCTOR: case SPECIALIST_DOCTOR: this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Acesso denegado", "Su perfil de usuario no está autorizado acceder a esta página."); return; } this.lazyDataModelVisitList = new LazyDataModel() { private static final long serialVersionUID = 1L; @Override public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { if (patient != null) patientId = patient.getId(); else patientId = null; Long totalRowCount = getRemoteManagerVisit().getVisitsCount(patientId, selectedDate); this.setRowCount(totalRowCount.intValue()); return getRemoteManagerVisit().listVisitsPaged(patientId, selectedDate, (first / pageSize), pageSize); } }; } public List completePatient(String query) { if (query != null && query.equals(this.lastUIQuery) == false) { this.lastUIQuery = query; // Recuperamos las 200 primeras coincidencias this.patientList = this.getRemoteManagerCommon().listPatientsFiltered(query, 0, Constants.MAX_ITEMS_AUTOCOMPLETE_SEARCH); } return this.patientList; } public List getPatientList() { return patientList; } public void removeVisit(Integer visitId) throws IOException { this.getRemoteManagerVisit().removeVisit(visitId); this.addFacesMessage(FacesMessage.SEVERITY_ERROR, "Visita eliminada", String.format("La visita con Id: %d se ha eliminado correctamente", visitId)); } public void editVisit(Integer visitId) throws IOException { FacesContext ctx = FacesContext.getCurrentInstance(); ctx.getExternalContext().redirect(String.format("UpdateVisit.xhtml?id=%d&fromPage=%s", visitId, ctx.getViewRoot().getViewId())); } public LazyDataModel getLazyDataModelVisitList() { return lazyDataModelVisitList; } public void showData() { } public Date getSelectedDate() { return selectedDate; } public void setSelectedDate(Date selectedDate) { this.selectedDate = selectedDate; } public PatientTO getPatient() { return patient; } public void setPatient(PatientTO patient) { this.patient = patient; } public boolean isAdmin() { return SessionUtils.getUserType() == UserType.ADMINISTRATOR; } }