Search in sources :

Example 1 with UnableToPrintException

use of org.openmrs.module.printer.UnableToPrintException in project openmrs-module-pihcore by PIH.

the class ZXPSeries3PrintHandler method print.

@Override
public void print(Printer printer, Map<String, Object> paramMap) throws UnableToPrintException {
    String name = (String) paramMap.get("name");
    String gender = (String) paramMap.get("gender");
    String birthdate = (String) paramMap.get("birthdate");
    Boolean birthdateEstimated = (Boolean) paramMap.get("birthdateEstimated");
    String patientIdentifier = (String) paramMap.get("patientIdentifier");
    List<String> addressLines = (paramMap.containsKey("addressLines") ? (List<String>) paramMap.get("addressLines") : null);
    String telephoneNumber = (paramMap.containsKey("telephoneNumber") ? (String) paramMap.get("telephoneNumber") : null);
    String issuingLocation = (paramMap.containsKey("issuingLocation") ? (String) paramMap.get("issuingLocation") : null);
    String issuedDate = (String) paramMap.get("issuedDate");
    String customCardLabel = (paramMap.containsKey("customCardLabel") ? (String) paramMap.get("customCardLabel") : null);
    ZebraCardPrinter zebraCardPrinter = null;
    ZebraGraphics graphics = null;
    Connection connection = null;
    List<GraphicsInfo> graphicsData;
    int nameFontSize = 16;
    // less-than-perfect attempt to lower font size for patients with large names
    if (name.length() > 28) {
        nameFontSize = 10;
    } else if (name.length() > 22) {
        nameFontSize = 12;
    }
    int retryCount = 0;
    boolean success = false;
    Integer jobId = null;
    while (!success && retryCount < MAX_RETRY) {
        log.info("Starting ID card print job for patient " + patientIdentifier + " on printer " + printer.getName());
        try {
            connection = new TcpConnection(printer.getIpAddress(), 9100);
            connection.open();
            zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
            graphicsData = new ArrayList<GraphicsInfo>();
            log.info("Connection opened for ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            GraphicsInfo grInfo = new GraphicsInfo();
            grInfo.side = CardSide.Front;
            grInfo.printType = PrintType.MonoK;
            grInfo.graphicType = GraphicType.BMP;
            graphics = new ZebraCardGraphics(zebraCardPrinter);
            graphics.setPrinterModel(PrinterModel.ZXPSeries3);
            graphics.initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.WHITE);
            // name
            graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, nameFontSize));
            graphics.drawText(name, 60, 20, Color.BLACK);
            // divider line
            graphics.drawLine(25, 100, 1000, 100, 3, Color.BLACK);
            // gender & birthdate
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Sèks", 60, 320, Color.BLACK);
            graphics.drawText("Dat ou fèt " + (birthdateEstimated != null && birthdateEstimated ? " (Estime)" : " "), 250, 320, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText((gender.equals("F") ? "Fi" : "Gason"), 60, 350, Color.BLACK);
            graphics.drawText(birthdate, 250, 350, Color.BLACK);
            // address
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            int verticalPosition = 110;
            if (addressLines != null && addressLines.size() > 0) {
                for (String addressLine : addressLines) {
                    graphics.drawText(addressLine, 60, verticalPosition, Color.BLACK);
                    verticalPosition = verticalPosition + 50;
                }
            }
            // telephone number
            if (StringUtils.isNotBlank(telephoneNumber)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
                graphics.drawText("Nimewo Telefòn", 600, 320, Color.BLACK);
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(telephoneNumber, 600, 350, Color.BLACK);
            }
            // divider line
            graphics.drawLine(25, 420, 1000, 420, 3, Color.BLACK);
            // bar code and identifier
            Code39Util barcode = ZebraBarcodeFactory.getCode39(graphics);
            barcode.setBarHeight(100);
            barcode.drawBarcode(patientIdentifier, 60, 450, Rotation.ROTATE_0);
            // custom card label, if specified
            if (StringUtils.isNotBlank(customCardLabel)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(customCardLabel, 420, 450, Color.BLACK);
            }
            // date and location issued
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Dat kat la fet la", 420, 520, Color.BLACK);
            graphics.drawText("Kote kat la fet la", 720, 520, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText(issuedDate, 420, 560, Color.BLACK);
            graphics.drawText((StringUtils.isNotBlank(issuingLocation) ? issuingLocation : ""), 720, 560, Color.BLACK);
            // do the actual printing
            grInfo.graphicData = graphics.createImage(null);
            graphics.clear();
            graphicsData.add(grInfo);
            log.info("Starting ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            jobId = zebraCardPrinter.print(1, graphicsData);
            log.info("Started ID card printing job " + jobId + " for patient " + patientIdentifier + " on printer " + printer.getName());
            success = pollJobStatus(zebraCardPrinter, jobId, printer);
        } catch (Exception e) {
            log.warn("Unable to print to printer " + printer.getName(), e);
        } finally {
            retryCount++;
            cleanUp(connection, zebraCardPrinter, jobId, graphics, printer, success);
        }
        // TODO remove
        if (success) {
            log.info("Success printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        } else {
            log.info("Failed printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        }
        if (!success && retryCount < MAX_RETRY) {
            try {
                Thread.sleep(TIME_BETWEEN_RETRIES_IN_MS);
            } catch (InterruptedException e) {
            // do nothing
            }
        }
    }
}
Also used : ZebraGraphics(com.zebra.sdk.common.card.graphics.ZebraGraphics) TcpConnection(com.zebra.sdk.comm.TcpConnection) Connection(com.zebra.sdk.comm.Connection) GraphicsInfo(com.zebra.sdk.common.card.containers.GraphicsInfo) TcpConnection(com.zebra.sdk.comm.TcpConnection) Font(java.awt.Font) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Code39Util(com.zebra.sdk.common.card.graphics.barcode.Code39Util) ZebraCardPrinter(com.zebra.sdk.common.card.printer.ZebraCardPrinter) ArrayList(java.util.ArrayList) List(java.util.List) ZebraCardGraphics(com.zebra.sdk.common.card.graphics.ZebraCardGraphics)

Example 2 with UnableToPrintException

use of org.openmrs.module.printer.UnableToPrintException in project openmrs-module-mirebalais by PIH.

the class ZlEmrIdCardPrinter method print.

/**
 * Prints a ZL EMR ID Card for the given patient at the given location
 */
public void print(Patient patient, Location location) throws UnableToPrintException {
    Location issuingLocation;
    try {
        issuingLocation = paperRecordService.getMedicalRecordLocationAssociatedWith(location);
    } catch (Exception e) {
        throw new UnableToPrintException(getMessage("zl.registration.patient.idcard.locationNotAssociatedWithMedicalRecordLocation", location.getName()));
    }
    Printer printer = printerService.getDefaultPrinter(location, PrinterType.ID_CARD);
    if (printer == null) {
        throw new UnableToPrintException(getMessage("zl.registration.patient.idcard.noPrinterConfiguredForLocation", location.getName()));
    }
    DateFormat df = new SimpleDateFormat("dd MMM yyyy", Context.getLocale());
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("name", getName(patient));
    paramMap.put("patientIdentifier", getIdentifier(patient));
    paramMap.put("gender", patient.getGender());
    paramMap.put("birthdate", df.format(patient.getBirthdate()));
    paramMap.put("birthdateEstimated", patient.getBirthdateEstimated());
    paramMap.put("issuingLocation", getIssuingLocationName(issuingLocation));
    paramMap.put("issuedDate", df.format(new Date()));
    paramMap.put("telephoneNumber", getTelephoneNumber(patient));
    paramMap.put("customCardLabel", config.getIdCardLabel());
    paramMap.put("addressLines", getAddressLines(patient));
    paramMap.put("locale", config.getIdCardLocale());
    // but if it is a GX430t, it's just a simple socket print with raw ZPL code, so we need to generate the data using the label template
    if (printer.getModel().getPrintHandler().equals("gx430tPrintHandler")) {
        ZplCardTemplate zlCardTemplate = Context.getRegisteredComponent("zplCardTemplate", ZplCardTemplate.class);
        paramMap.put("data", zlCardTemplate.generateLabel(paramMap));
        paramMap.put("encoding", zlCardTemplate.getEncoding());
        paramMap.put("wait", 500);
    }
    printerService.print(paramMap, printer, false);
}
Also used : HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Printer(org.openmrs.module.printer.Printer) SimpleDateFormat(java.text.SimpleDateFormat) ZplCardTemplate(org.openmrs.module.mirebalais.printer.template.ZplCardTemplate) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Date(java.util.Date) Location(org.openmrs.Location)

Example 3 with UnableToPrintException

use of org.openmrs.module.printer.UnableToPrintException in project openmrs-module-mirebalais by PIH.

the class ZXPSeries3PrintHandler method print.

@Override
public void print(Printer printer, Map<String, Object> paramMap) throws UnableToPrintException {
    String name = (String) paramMap.get("name");
    String gender = (String) paramMap.get("gender");
    String birthdate = (String) paramMap.get("birthdate");
    Boolean birthdateEstimated = (Boolean) paramMap.get("birthdateEstimated");
    String patientIdentifier = (String) paramMap.get("patientIdentifier");
    List<String> addressLines = (paramMap.containsKey("addressLines") ? (List<String>) paramMap.get("addressLines") : null);
    String telephoneNumber = (paramMap.containsKey("telephoneNumber") ? (String) paramMap.get("telephoneNumber") : null);
    String issuingLocation = (paramMap.containsKey("issuingLocation") ? (String) paramMap.get("issuingLocation") : null);
    String issuedDate = (String) paramMap.get("issuedDate");
    String customCardLabel = (paramMap.containsKey("customCardLabel") ? (String) paramMap.get("customCardLabel") : null);
    ZebraCardPrinter zebraCardPrinter = null;
    ZebraGraphics graphics = null;
    Connection connection = null;
    List<GraphicsInfo> graphicsData;
    int nameFontSize = 16;
    // less-than-perfect attempt to lower font size for patients with large names
    if (name.length() > 28) {
        nameFontSize = 10;
    } else if (name.length() > 22) {
        nameFontSize = 12;
    }
    int retryCount = 0;
    boolean success = false;
    Integer jobId = null;
    while (!success && retryCount < MAX_RETRY) {
        log.info("Starting ID card print job for patient " + patientIdentifier + " on printer " + printer.getName());
        try {
            connection = new TcpConnection(printer.getIpAddress(), 9100);
            connection.open();
            zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
            graphicsData = new ArrayList<GraphicsInfo>();
            log.info("Connection opened for ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            GraphicsInfo grInfo = new GraphicsInfo();
            grInfo.side = CardSide.Front;
            grInfo.printType = PrintType.MonoK;
            grInfo.graphicType = GraphicType.BMP;
            graphics = new ZebraCardGraphics(zebraCardPrinter);
            graphics.setPrinterModel(PrinterModel.ZXPSeries3);
            graphics.initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.WHITE);
            // name
            graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, nameFontSize));
            graphics.drawText(name, 60, 20, Color.BLACK);
            // divider line
            graphics.drawLine(25, 100, 1000, 100, 3, Color.BLACK);
            // gender & birthdate
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Sèks", 60, 320, Color.BLACK);
            graphics.drawText("Dat ou fèt " + (birthdateEstimated != null && birthdateEstimated ? " (Estime)" : " "), 250, 320, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText((gender.equals("F") ? "Fi" : "Gason"), 60, 350, Color.BLACK);
            graphics.drawText(birthdate, 250, 350, Color.BLACK);
            // address
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            int verticalPosition = 110;
            if (addressLines != null && addressLines.size() > 0) {
                for (String addressLine : addressLines) {
                    graphics.drawText(addressLine, 60, verticalPosition, Color.BLACK);
                    verticalPosition = verticalPosition + 50;
                }
            }
            // telephone number
            if (StringUtils.isNotBlank(telephoneNumber)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
                graphics.drawText("Nimewo Telefòn", 600, 320, Color.BLACK);
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(telephoneNumber, 600, 350, Color.BLACK);
            }
            // divider line
            graphics.drawLine(25, 420, 1000, 420, 3, Color.BLACK);
            // bar code and identifier
            Code39Util barcode = ZebraBarcodeFactory.getCode39(graphics);
            barcode.setBarHeight(100);
            barcode.drawBarcode(patientIdentifier, 60, 450, Rotation.ROTATE_0);
            // custom card label, if specified
            if (StringUtils.isNotBlank(customCardLabel)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(customCardLabel, 420, 450, Color.BLACK);
            }
            // date and location issued
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Dat kat la fet la", 420, 520, Color.BLACK);
            graphics.drawText("Kote kat la fet la", 720, 520, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText(issuedDate, 420, 560, Color.BLACK);
            graphics.drawText((StringUtils.isNotBlank(issuingLocation) ? issuingLocation : ""), 720, 560, Color.BLACK);
            // do the actual printing
            grInfo.graphicData = graphics.createImage(null);
            graphics.clear();
            graphicsData.add(grInfo);
            log.info("Starting ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            jobId = zebraCardPrinter.print(1, graphicsData);
            log.info("Started ID card printing job " + jobId + " for patient " + patientIdentifier + " on printer " + printer.getName());
            success = pollJobStatus(zebraCardPrinter, jobId, printer);
        } catch (Exception e) {
            log.warn("Unable to print to printer " + printer.getName(), e);
        } finally {
            retryCount++;
            cleanUp(connection, zebraCardPrinter, jobId, graphics, printer, success);
        }
        // TODO remove
        if (success) {
            log.info("Success printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        } else {
            log.info("Failed printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        }
        if (!success && retryCount < MAX_RETRY) {
            try {
                Thread.sleep(TIME_BETWEEN_RETRIES_IN_MS);
            } catch (InterruptedException e) {
            // do nothing
            }
        }
    }
}
Also used : ZebraGraphics(com.zebra.sdk.common.card.graphics.ZebraGraphics) TcpConnection(com.zebra.sdk.comm.TcpConnection) Connection(com.zebra.sdk.comm.Connection) GraphicsInfo(com.zebra.sdk.common.card.containers.GraphicsInfo) TcpConnection(com.zebra.sdk.comm.TcpConnection) Font(java.awt.Font) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Code39Util(com.zebra.sdk.common.card.graphics.barcode.Code39Util) ZebraCardPrinter(com.zebra.sdk.common.card.printer.ZebraCardPrinter) ArrayList(java.util.ArrayList) List(java.util.List) ZebraCardGraphics(com.zebra.sdk.common.card.graphics.ZebraCardGraphics)

Example 4 with UnableToPrintException

use of org.openmrs.module.printer.UnableToPrintException in project openmrs-module-pihcore by PIH.

the class ZlEmrIdCardPrinter method print.

/**
 * Prints a ZL EMR ID Card for the given patient at the given location
 */
public void print(Patient patient, Location location) throws UnableToPrintException {
    Location issuingLocation;
    try {
        issuingLocation = paperRecordService.getMedicalRecordLocationAssociatedWith(location);
    } catch (Exception e) {
        throw new UnableToPrintException(getMessage("zl.registration.patient.idcard.locationNotAssociatedWithMedicalRecordLocation", location.getName()));
    }
    Printer printer = printerService.getDefaultPrinter(location, PrinterType.ID_CARD);
    if (printer == null) {
        throw new UnableToPrintException(getMessage("zl.registration.patient.idcard.noPrinterConfiguredForLocation", location.getName()));
    }
    DateFormat df = new SimpleDateFormat("dd MMM yyyy", Context.getLocale());
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("name", getName(patient));
    paramMap.put("patientIdentifier", getIdentifier(patient));
    paramMap.put("gender", patient.getGender());
    paramMap.put("birthdate", df.format(patient.getBirthdate()));
    paramMap.put("birthdateEstimated", patient.getBirthdateEstimated());
    paramMap.put("issuingLocation", getIssuingLocationName(issuingLocation));
    paramMap.put("issuedDate", df.format(new Date()));
    paramMap.put("telephoneNumber", getTelephoneNumber(patient));
    paramMap.put("customCardLabel", config.getIdCardLabel());
    paramMap.put("addressLines", getAddressLines(patient));
    paramMap.put("locale", config.getIdCardLocale());
    // but if it is a GX430t, it's just a simple socket print with raw ZPL code, so we need to generate the data using the label template
    if (printer.getModel().getPrintHandler().equals("gx430tPrintHandler")) {
        ZplCardTemplate zlCardTemplate = Context.getRegisteredComponent("zplCardTemplate", ZplCardTemplate.class);
        paramMap.put("data", zlCardTemplate.generateLabel(paramMap));
        paramMap.put("encoding", zlCardTemplate.getEncoding());
        paramMap.put("wait", 500);
    }
    printerService.print(paramMap, printer, false);
}
Also used : HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) IdPrinter(org.openmrs.module.pihcore.printer.IdPrinter) Printer(org.openmrs.module.printer.Printer) SimpleDateFormat(java.text.SimpleDateFormat) ZplCardTemplate(org.openmrs.module.pihcore.printer.template.ZplCardTemplate) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Date(java.util.Date) Location(org.openmrs.Location)

Example 5 with UnableToPrintException

use of org.openmrs.module.printer.UnableToPrintException in project openmrs-module-pihcore by PIH.

the class LabLabelPrinterRestController method printLabel.

@RequestMapping(method = RequestMethod.GET, value = LAB_LABEL_PRINTER)
@ResponseBody
public Object printLabel(@RequestParam(value = "patient") String patientUuid, @RequestParam(value = "sessionLocation") String sessionLocationUuid) {
    SimpleObject response = new SimpleObject();
    if (StringUtils.isNotBlank(patientUuid)) {
        Patient patient = patientService.getPatientByUuid(patientUuid);
        Location location = locationService.getLocationByUuid(sessionLocationUuid);
        if (patient != null && location != null) {
            Printer printer = printerService.getDefaultPrinter(location, PrinterType.LABEL);
            if (printer != null) {
                Map<String, Object> paramMap = new HashMap<String, Object>();
                ZplLabLabelTemplate zplLabLabelTemplate = Context.getRegisteredComponent("zplLabLabelTemplate", ZplLabLabelTemplate.class);
                paramMap.put("data", zplLabLabelTemplate.generateLabel(patient));
                paramMap.put("encoding", zplLabLabelTemplate.getEncoding());
                paramMap.put("wait", 500);
                try {
                    printerService.print(paramMap, printer, false);
                    response = (SimpleObject) ConversionUtil.convertToRepresentation(patient, Representation.DEFAULT);
                } catch (UnableToPrintException e) {
                    response.put("error", true);
                    response.put("message", messageSourceService.getMessage("mirebalais.error.unableToContactPrinter"));
                }
            } else {
                response.put("error", true);
                response.put("message", messageSourceService.getMessage("mirebalais.error.noLabelPrinterConfiguredforLocation"));
            }
        }
    }
    return response;
}
Also used : SimpleObject(org.openmrs.module.webservices.rest.SimpleObject) HashMap(java.util.HashMap) Patient(org.openmrs.Patient) SimpleObject(org.openmrs.module.webservices.rest.SimpleObject) Printer(org.openmrs.module.printer.Printer) ZplLabLabelTemplate(org.openmrs.module.pihcore.printer.template.ZplLabLabelTemplate) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Location(org.openmrs.Location) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

UnableToPrintException (org.openmrs.module.printer.UnableToPrintException)5 HashMap (java.util.HashMap)3 Location (org.openmrs.Location)3 Printer (org.openmrs.module.printer.Printer)3 Connection (com.zebra.sdk.comm.Connection)2 ConnectionException (com.zebra.sdk.comm.ConnectionException)2 TcpConnection (com.zebra.sdk.comm.TcpConnection)2 GraphicsInfo (com.zebra.sdk.common.card.containers.GraphicsInfo)2 ZebraCardException (com.zebra.sdk.common.card.exceptions.ZebraCardException)2 ZebraCardGraphics (com.zebra.sdk.common.card.graphics.ZebraCardGraphics)2 ZebraGraphics (com.zebra.sdk.common.card.graphics.ZebraGraphics)2 Code39Util (com.zebra.sdk.common.card.graphics.barcode.Code39Util)2 ZebraCardPrinter (com.zebra.sdk.common.card.printer.ZebraCardPrinter)2 Font (java.awt.Font)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 Patient (org.openmrs.Patient)1