|
| 1 | +/* |
| 2 | + * Open Hospital Management Information System |
| 3 | + * |
| 4 | + * Dr M H B Ariyaratne |
| 5 | + * Acting Consultant (Health Informatics) |
| 6 | + */ |
| 7 | +package com.divudi.bean.clinical; |
| 8 | + |
| 9 | +import com.divudi.bean.common.SessionController; |
| 10 | +import com.divudi.core.util.JsfUtil; |
| 11 | +import com.divudi.core.data.SymanticType; |
| 12 | +import com.divudi.core.entity.clinical.ClinicalEntity; |
| 13 | +import com.divudi.core.facade.ClinicalEntityFacade; |
| 14 | +import java.io.Serializable; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import javax.ejb.EJB; |
| 21 | +import javax.enterprise.context.SessionScoped; |
| 22 | +import javax.faces.component.UIComponent; |
| 23 | +import javax.faces.context.FacesContext; |
| 24 | +import javax.faces.convert.Converter; |
| 25 | +import javax.faces.convert.FacesConverter; |
| 26 | +import javax.inject.Inject; |
| 27 | +import javax.inject.Named; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | +import org.apache.poi.ss.usermodel.Row; |
| 30 | +import org.apache.poi.ss.usermodel.Sheet; |
| 31 | +import org.apache.poi.ss.usermodel.Workbook; |
| 32 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 33 | + |
| 34 | +/** |
| 35 | + * Manages configurable discharge condition values (e.g. Stable, DAMA, Referred) |
| 36 | + * used in the clinical discharge workflow. |
| 37 | + * |
| 38 | + * @author Dr. M. H. B. Ariyaratne |
| 39 | + */ |
| 40 | +@Named |
| 41 | +@SessionScoped |
| 42 | +public class DischargeConditionController implements Serializable { |
| 43 | + |
| 44 | + private static final long serialVersionUID = 1L; |
| 45 | + |
| 46 | + @Inject |
| 47 | + SessionController sessionController; |
| 48 | + |
| 49 | + @EJB |
| 50 | + private ClinicalEntityFacade ejbFacade; |
| 51 | + |
| 52 | + private ClinicalEntity current; |
| 53 | + private List<ClinicalEntity> items = null; |
| 54 | + private String selectText = ""; |
| 55 | + |
| 56 | + public DischargeConditionController() { |
| 57 | + } |
| 58 | + |
| 59 | + public void prepareAdd() { |
| 60 | + current = new ClinicalEntity(); |
| 61 | + current.setSymanticType(SymanticType.Discharge_Condition); |
| 62 | + } |
| 63 | + |
| 64 | + public void saveSelected() { |
| 65 | + current.setSymanticType(SymanticType.Discharge_Condition); |
| 66 | + if (getCurrent().getId() != null && getCurrent().getId() > 0) { |
| 67 | + getFacade().edit(current); |
| 68 | + JsfUtil.addSuccessMessage("Updated"); |
| 69 | + } else { |
| 70 | + current.setCreatedAt(new Date()); |
| 71 | + current.setCreater(getSessionController().getLoggedUser()); |
| 72 | + getFacade().create(current); |
| 73 | + JsfUtil.addSuccessMessage("Saved"); |
| 74 | + } |
| 75 | + recreateModel(); |
| 76 | + getItems(); |
| 77 | + } |
| 78 | + |
| 79 | + public void delete() { |
| 80 | + if (current != null) { |
| 81 | + current.setRetired(true); |
| 82 | + current.setRetiredAt(new Date()); |
| 83 | + current.setRetirer(getSessionController().getLoggedUser()); |
| 84 | + getFacade().edit(current); |
| 85 | + JsfUtil.addSuccessMessage("Deleted Successfully"); |
| 86 | + } else { |
| 87 | + JsfUtil.addErrorMessage("Nothing to Delete"); |
| 88 | + } |
| 89 | + recreateModel(); |
| 90 | + getItems(); |
| 91 | + current = null; |
| 92 | + getCurrent(); |
| 93 | + } |
| 94 | + |
| 95 | + public List<ClinicalEntity> getItems() { |
| 96 | + if (items == null) { |
| 97 | + Map m = new HashMap(); |
| 98 | + m.put("t", SymanticType.Discharge_Condition); |
| 99 | + String sql = "select c from ClinicalEntity c where c.retired=false and c.symanticType=:t order by c.name"; |
| 100 | + items = getFacade().findByJpql(sql, m); |
| 101 | + } |
| 102 | + return items; |
| 103 | + } |
| 104 | + |
| 105 | + public List<ClinicalEntity> completeDischargeConditions(String qry) { |
| 106 | + if (qry == null || qry.trim().isEmpty()) { |
| 107 | + return new ArrayList<>(); |
| 108 | + } |
| 109 | + List<ClinicalEntity> c; |
| 110 | + Map m = new HashMap(); |
| 111 | + m.put("t", SymanticType.Discharge_Condition); |
| 112 | + m.put("n", "%" + qry.toUpperCase() + "%"); |
| 113 | + String sql = "select c from ClinicalEntity c where c.retired=false and upper(c.name) like :n and c.symanticType=:t order by c.name"; |
| 114 | + c = getFacade().findByJpql(sql, m, 10); |
| 115 | + if (c == null) { |
| 116 | + c = new ArrayList<>(); |
| 117 | + } |
| 118 | + return c; |
| 119 | + } |
| 120 | + |
| 121 | + public void downloadAsExcel() { |
| 122 | + getItems(); |
| 123 | + FacesContext context = FacesContext.getCurrentInstance(); |
| 124 | + try (Workbook workbook = new XSSFWorkbook()) { |
| 125 | + Sheet sheet = workbook.createSheet("Discharge Conditions"); |
| 126 | + |
| 127 | + Row headerRow = sheet.createRow(0); |
| 128 | + headerRow.createCell(0).setCellValue("No"); |
| 129 | + headerRow.createCell(1).setCellValue("Name"); |
| 130 | + headerRow.createCell(2).setCellValue("Code"); |
| 131 | + headerRow.createCell(3).setCellValue("Description"); |
| 132 | + |
| 133 | + int rowNum = 1; |
| 134 | + for (ClinicalEntity item : items) { |
| 135 | + Row row = sheet.createRow(rowNum); |
| 136 | + row.createCell(0).setCellValue(rowNum); |
| 137 | + row.createCell(1).setCellValue(item.getName()); |
| 138 | + row.createCell(2).setCellValue(item.getCode()); |
| 139 | + row.createCell(3).setCellValue(item.getDescreption()); |
| 140 | + rowNum++; |
| 141 | + } |
| 142 | + |
| 143 | + HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); |
| 144 | + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| 145 | + response.setHeader("Content-Disposition", "attachment; filename=\"discharge_conditions.xlsx\""); |
| 146 | + |
| 147 | + workbook.write(response.getOutputStream()); |
| 148 | + context.responseComplete(); |
| 149 | + } catch (Exception e) { |
| 150 | + JsfUtil.addErrorMessage("Error generating Excel file: " + e.getMessage()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void recreateModel() { |
| 155 | + items = null; |
| 156 | + } |
| 157 | + |
| 158 | + private ClinicalEntityFacade getFacade() { |
| 159 | + return ejbFacade; |
| 160 | + } |
| 161 | + |
| 162 | + public ClinicalEntity getCurrent() { |
| 163 | + if (current == null) { |
| 164 | + current = new ClinicalEntity(); |
| 165 | + } |
| 166 | + return current; |
| 167 | + } |
| 168 | + |
| 169 | + public void setCurrent(ClinicalEntity current) { |
| 170 | + this.current = current; |
| 171 | + } |
| 172 | + |
| 173 | + public String getSelectText() { |
| 174 | + return selectText; |
| 175 | + } |
| 176 | + |
| 177 | + public void setSelectText(String selectText) { |
| 178 | + this.selectText = selectText; |
| 179 | + } |
| 180 | + |
| 181 | + public SessionController getSessionController() { |
| 182 | + return sessionController; |
| 183 | + } |
| 184 | + |
| 185 | + public void setSessionController(SessionController sessionController) { |
| 186 | + this.sessionController = sessionController; |
| 187 | + } |
| 188 | + |
| 189 | + public ClinicalEntityFacade getEjbFacade() { |
| 190 | + return ejbFacade; |
| 191 | + } |
| 192 | + |
| 193 | + public void setEjbFacade(ClinicalEntityFacade ejbFacade) { |
| 194 | + this.ejbFacade = ejbFacade; |
| 195 | + } |
| 196 | + |
| 197 | + @FacesConverter("dischargeConditionConverter") |
| 198 | + public static class DischargeConditionConverter implements Converter { |
| 199 | + |
| 200 | + @Override |
| 201 | + public Object getAsObject(FacesContext facesContext, UIComponent component, String value) { |
| 202 | + if (value == null || value.length() == 0) { |
| 203 | + return null; |
| 204 | + } |
| 205 | + try { |
| 206 | + DischargeConditionController controller = (DischargeConditionController) facesContext.getApplication() |
| 207 | + .getELResolver().getValue(facesContext.getELContext(), null, "dischargeConditionController"); |
| 208 | + return controller.getEjbFacade().find(Long.valueOf(value)); |
| 209 | + } catch (NumberFormatException e) { |
| 210 | + return null; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public String getAsString(FacesContext facesContext, UIComponent component, Object object) { |
| 216 | + if (object == null) { |
| 217 | + return null; |
| 218 | + } |
| 219 | + if (object instanceof ClinicalEntity) { |
| 220 | + ClinicalEntity o = (ClinicalEntity) object; |
| 221 | + return String.valueOf(o.getId()); |
| 222 | + } else { |
| 223 | + throw new IllegalArgumentException("object " + object + " is of type " |
| 224 | + + object.getClass().getName() + "; expected type: ClinicalEntity"); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments