Skip to content

Commit

Permalink
Merge pull request #9827 from hmislk/#9825_Lab_Report_Export
Browse files Browse the repository at this point in the history
#9825 lab report export Closes #9825
  • Loading branch information
Irani96 authored Dec 24, 2024
2 parents aacb60c + d1891df commit 4f3bff6
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ public class ReportsController implements Serializable {
Map<Integer, Map<String, Map<Integer, Double>>> weeklyDailyBillItemMap7to1;
Map<Integer, Map<String, Map<Integer, Double>>> weeklyDailyBillItemMap1to7;


private boolean showChart;

public String getDischargedStatus() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/divudi/bean/lab/InvestigationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,23 @@ public String navigateToEditFormatSingle() {
return investigationItemController.toEditInvestigationFormat();
}

public String navigateExportReoirtFormat() {
if (current == null) {
JsfUtil.addErrorMessage("Please select investigation");
return "";
}
if (current.getId() == null) {
JsfUtil.addErrorMessage("Please save investigation first.");
return "";
}
if (current.getReportedAs() == null) {
current.setReportedAs(current);
}
investigationItemController.setCurrentInvestigation((Investigation) current.getReportedAs());
investigationItemController.listInvestigationItem();
return "/admin/lims/export?faces-redirect=true";
}

public String navigateToEditFormatSinglePastData() {
if (current == null) {
JsfUtil.addErrorMessage("Please select investigation");
Expand Down
216 changes: 216 additions & 0 deletions src/main/java/com/divudi/bean/lab/LabReportExportImportController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package com.divudi.bean.lab;

import javax.inject.Named;
import com.divudi.data.InvestigationItemType;
import com.divudi.entity.lab.Investigation;
import com.divudi.entity.lab.InvestigationItem;
import com.divudi.facade.InvestigationItemFacade;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.inject.Inject;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
*
* @author H.K. Damith Deshan | hkddrajapaksha@gmail.com
*/
@Named
@SessionScoped
public class LabReportExportImportController implements Serializable {

// <editor-fold defaultstate="collapsed" desc="Ejbs">
@EJB
InvestigationItemFacade investigationItemFacade;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Controllers">
@Inject
InvestigationItemController investigationItemController;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Navigation Method">

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Variables">
private Investigation current;
private List<InvestigationItem> successItems;

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Methods">
public LabReportExportImportController() {
}

public List<InvestigationItem> fillInvestigationComporentItems(Investigation investigation) {

List<InvestigationItem> investigationComporent = new ArrayList<>();

if (investigation == null || investigation.getId() == null) {
return investigationComporent;
}

List<InvestigationItemType> l = new ArrayList<>();
l.add(InvestigationItemType.Label);
l.add(InvestigationItemType.Value);
l.add(InvestigationItemType.Flag);
l.add(InvestigationItemType.Calculation);
l.add(InvestigationItemType.Css);
l.add(InvestigationItemType.Html);
l.add(InvestigationItemType.DynamicLabel);
l.add(InvestigationItemType.Investigation);
l.add(InvestigationItemType.AntibioticList);
l.add(InvestigationItemType.Template);
l.add(InvestigationItemType.Barcode);
l.add(InvestigationItemType.BarcodeVertical);
l.add(InvestigationItemType.Image);

investigationComporent = listInvestigationItemsFilteredByItemTypes(investigation, l);
return investigationComporent;
}

public List<InvestigationItem> listInvestigationItemsFilteredByItemTypes(Investigation ix, List<InvestigationItemType> types) {
List<InvestigationItem> tis = new ArrayList<>();
if (ix != null) {
String temSql;
temSql = "SELECT i FROM InvestigationItem i "
+ " where i.retired=false "
+ " and i.item=:item "
+ " and i.ixItemType in :types "
+ " order by i.riTop, i.riLeft";
Map m = new HashMap();
m.put("item", ix);
m.put("types", types);
tis = investigationItemFacade.findByJpql(temSql, m);
}
return tis;
}

public void export() throws IOException {
System.out.println("current = " + getCurrent().getName());
List<InvestigationItem> items = investigationItemController.getUserChangableItems();
System.out.println("Items = " + items.size());
exportInvestigationItemsToExcel(items);

System.out.println("Done");

}

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Export">
private String defaultIfNullOrEmpty(String value, String defaultValue) {
return (value == null || value.trim().isEmpty()) ? defaultValue : value;
}

public void exportInvestigationItemsToExcel(List<InvestigationItem> investigationItems) throws IOException {
// Define a predefined directory (e.g., user's desktop)
System.out.println("Investigation Items = " + investigationItems.size());
String userHome = System.getProperty("user.home");
String defaultDirectory = userHome + File.separator + "Desktop"; // Change to any other directory if needed
String fileName = getCurrent().getName().toString()+".xlsx";
String fullFilePath = defaultDirectory + File.separator + fileName;

// Ensure the directory exists
File file = new File(fullFilePath);
file.getParentFile().mkdirs(); // Create parent directories if they don't exist

// Create a new workbook and a sheet
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("InvestigationItems");

// Define Header Row
String[] headers = {
"Name", "IxItemType", "IxItemValueType", "HtmlText", "RiLeft", "RiTop", "RiWidth", "RiHeight",
"HtPix", "WtPix", "CssTextAlign", "CssVerticalAlign", "CssFontFamily", "RiFontSize",
"CssFontStyle", "CssFontWeight", "CssTextDecoration", "CustomCss", "CssBackColor", "CssColor",
"Automated"
};

// Create the header row
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}

successItems = new ArrayList();

// Populate data rows
int rowNum = 1;
for (InvestigationItem item : investigationItems) {
Row row = sheet.createRow(rowNum++);
System.out.println("Item = " + item.getId() + " - " + item);
row.createCell(0).setCellValue(defaultIfNullOrEmpty(item.getName(), ""));
row.createCell(1).setCellValue(defaultIfNullOrEmpty(item.getIxItemType() != null ? item.getIxItemType().toString() : null, ""));
row.createCell(2).setCellValue(defaultIfNullOrEmpty(item.getIxItemValueType() != null ? item.getIxItemValueType().toString() : null, ""));
row.createCell(3).setCellValue(defaultIfNullOrEmpty(item.getHtmltext(), ""));
row.createCell(4).setCellValue(item.getRiLeft());
row.createCell(5).setCellValue(item.getRiTop());
row.createCell(6).setCellValue(item.getRiWidth());
row.createCell(7).setCellValue(item.getRiHeight());
row.createCell(8).setCellValue(item.getHtPix());
row.createCell(9).setCellValue(item.getWtPix());
row.createCell(10).setCellValue(defaultIfNullOrEmpty(item.getCssTextAlign() != null ? item.getCssTextAlign().toString() : null, ""));
row.createCell(11).setCellValue(defaultIfNullOrEmpty(item.getCssVerticalAlign() != null ? item.getCssVerticalAlign().toString() : null, ""));
row.createCell(12).setCellValue(defaultIfNullOrEmpty(item.getCssFontFamily(), ""));
row.createCell(13).setCellValue(item.getRiFontSize());
row.createCell(14).setCellValue(defaultIfNullOrEmpty(item.getCssFontStyle() != null ? item.getCssFontStyle().toString() : null, ""));
row.createCell(15).setCellValue(defaultIfNullOrEmpty(item.getCssFontWeight(), ""));
row.createCell(16).setCellValue(defaultIfNullOrEmpty(item.getCssTextDecoration() != null ? item.getCssTextDecoration().toString() : null, ""));
row.createCell(17).setCellValue(defaultIfNullOrEmpty(item.getCustomCss(), ""));
row.createCell(18).setCellValue(defaultIfNullOrEmpty(item.getCssBackColor(), ""));
row.createCell(19).setCellValue(defaultIfNullOrEmpty(item.getCssColor(), ""));
row.createCell(20).setCellValue(item.isAutomated());

successItems.add(item);
System.out.println("success " + item.getName() + "Added");
}

// Resize columns to fit content
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}

// Write the workbook to the default file path
System.out.println("Attempting to write to: " + fullFilePath);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
System.out.println("Excel file created successfully at " + fullFilePath);
} catch (IOException e) {
System.out.println("Error writing Excel file: " + e.getMessage());
e.printStackTrace();
} finally {
workbook.close();
}
}

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Import">
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Getter & Setter">
public Investigation getCurrent() {
current = investigationItemController.getCurrentInvestigation();
return current;
}

public void setCurrent(Investigation current) {
this.current = current;
}

public List<InvestigationItem> getSuccessItems() {
return successItems;
}

public void setSuccessItems(List<InvestigationItem> successItems) {
this.successItems = successItems;
}

// </editor-fold>
}
76 changes: 76 additions & 0 deletions src/main/webapp/admin/lims/export.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<ui:composition template="/resources/template/template.xhtml">
<ui:define name="content">
<h:form id="form" >
<p:panel header="Format Expoet" styleClass="w-100 p-0 m-1" >
<f:facet name="header">
<div class="d-flex justify-content-between">
<h:outputText value="Format Expoet" class="mt-2" style="font-size: 22px; font-weight: 700"/>
<p:commandButton
value="Back"
icon="fa fa-arrow-left"
style="float: right;"
action="#{investigationController.navigateToManageInvestigation}"
ajax="false" >
</p:commandButton>
</div>

</f:facet>

<div class="d-flex justify-content-between">
<div class="d-flex col-5 gap-4" style="font-size: 18px; font-weight: 700">
<h:outputText value="Current Investigation" class="mt-2"/>
<h:outputText value="-" class="mt-2"/>
<h:outputText value="#{investigationItemController.currentInvestigation.name}" class="mt-2 text-dark"/>
</div>
<p:commandButton
ajax="false"
style="float: right; width: 200px;"
icon="fas fa-file-export export-icon"
class="ui-button-warning"
action="#{labReportExportImportController.export()}"
value="Export" >
</p:commandButton>
</div>

<p:panel id="pIx" class=" w-100 mt-3">

<f:facet name="header">
<h:outputText value="Investigation Items List" class="mt-2"/>
</f:facet>

<div class="row mb-1" style="font-weight: 700; font-size: 16px;">
<div class="col-2"><p:outputLabel value="Item Name" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="Item Type" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="Item Value Type" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="Automated" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="Retired" ></p:outputLabel></div>
</div>
<ui:repeat value="#{investigationItemController.userChangableItems}" var="ii">
<div class="row mb-1">
<div class="col-2"><p:outputLabel value="#{ii.name}" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="#{ii.ixItemType}" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="#{ii.ixItemValueType}" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="#{ii.automated}" ></p:outputLabel></div>
<div class="col-1"><p:outputLabel value="#{ii.retired}" ></p:outputLabel></div>
</div>

</ui:repeat>

</p:panel>
</p:panel>

</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
8 changes: 8 additions & 0 deletions src/main/webapp/admin/lims/investigation.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@
action="#{investigationController.navigateToEditFormatMultiple()}"
value="Manage Format - All Items" >
</p:commandButton>

<p:commandButton
class="w-100"
ajax="false"
icon="fas fa-file-export export-icon"
action="#{investigationController.navigateExportReoirtFormat()}"
value="Export Format" >
</p:commandButton>

<p:commandButton
class="w-100"
Expand Down

0 comments on commit 4f3bff6

Please sign in to comment.