From: Roland Häder Date: Thu, 10 Aug 2017 21:44:08 +0000 (+0200) Subject: Maybe not cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bf432fd6b3575cbeee863b62d6a846076d6b14fe;p=pizzaservice-ejb.git Maybe not cherry-pick: - removed left-over from some renaming-party? ;-) - moved PDF/receipt stuff into generic package Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jshopreceipt/receipt/PdfReceiptBean.java b/src/java/org/mxchange/jshopreceipt/receipt/PdfReceiptBean.java new file mode 100644 index 0000000..2aa4e20 --- /dev/null +++ b/src/java/org/mxchange/jshopreceipt/receipt/PdfReceiptBean.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jshopreceipt.receipt; + +import gnu.jpdf.PDFJob; +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.inject.Inject; + +/** + * A "centralized" logger bean + *

+ * @author Roland Häder + */ +@Startup +@Singleton (name = "pdfReceipt", description = "An EJB for producing PDF receipts") +public class PdfReceiptBean implements PdfReceiptBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 19_365_786_772_164L; + + /** + * PDF job instance + */ + @Inject + @Receipt + private PDFJob job; + + /** + * Default constructor + */ + public PdfReceiptBean () { + // Call super constructor + super(); + } + + /** + * Getter for PDF job + *

+ * @return PDF job + */ + private PDFJob getJob () { + return this.job; + } + +} diff --git a/src/java/org/mxchange/jshopreceipt/receipt/ReceiptFactory.java b/src/java/org/mxchange/jshopreceipt/receipt/ReceiptFactory.java new file mode 100644 index 0000000..0910f98 --- /dev/null +++ b/src/java/org/mxchange/jshopreceipt/receipt/ReceiptFactory.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jshopreceipt.receipt; + +import gnu.jpdf.PDFJob; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import org.mxchange.jshopreceipt.receipt.Receipt; + +/** + * A receipt factory factory + *

+ * @author Roland Häder + */ +public class ReceiptFactory { + + /** + * Own job + */ + private final PDFJob job; + + /** + * Factory method + */ + public ReceiptFactory () { + this.job = new PDFJob(); + } + + /** + * Produces a PDF job instance + *

+ * @param caller Injection point + *

+ * @return PDF job + */ + @Produces + @Receipt + public PDFJob getJob (final InjectionPoint caller) { + return this.job; + } + +} diff --git a/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptBean.java b/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptBean.java deleted file mode 100644 index cac8d6f..0000000 --- a/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptBean.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.pizzaapplication.model.receipt; - -import gnu.jpdf.PDFJob; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.inject.Inject; -import org.mxchange.jshopreceipt.receipt.PdfReceiptBeanLocal; -import org.mxchange.jshopreceipt.receipt.Receipt; - -/** - * A "centralized" logger bean - *

- * @author Roland Häder - */ -@Startup -@Singleton (name = "pdf-receipt", description = "An EJB for producing PDF receipts") -public class PdfReceiptBean implements PdfReceiptBeanLocal { - - /** - * PDF job instance - */ - @Inject - @Receipt - private PDFJob job; - - /** - * Default constructor - */ - public PdfReceiptBean () { - // Call super constructor - super(); - } - - /** - * Getter for PDF job - *

- * @return PDF job - */ - private PDFJob getJob () { - return this.job; - } -} diff --git a/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java deleted file mode 100644 index e8ee770..0000000 --- a/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.pizzaapplication.model.receipt; - -import java.text.MessageFormat; -import java.util.Locale; -import java.util.MissingResourceException; -import java.util.ResourceBundle; -import javax.ejb.Stateless; -import javax.faces.application.FacesMessage; -import javax.faces.context.FacesContext; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jcustomercore.model.customer.Customer; -import org.mxchange.jshopcore.model.receipt.ReceiptBeanRemote; -import org.mxchange.jshopcore.model.receipt.WrapableReceipt; -import org.mxchange.jshopreceipt.receipt.PdfReceiptBeanLocal; -import org.mxchange.jshopreceipt.receipt.Receipt; - -/** - * A stateless session-scoped bean for producing official receipts in Abobe's whacky - * binary format. This class uses GNUjpdf which allows using ordinary graphic - * libraries. - *

- * @author Roland Häder - */ -@Stateless (name = "pdf", description = "A bean creating PDF receipts") -public abstract class PdfReceiptSessionBean extends BaseDatabaseBean implements ReceiptBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 384_578_171_659_628L; - - /** - * Access key - */ - private String accessKey; - - /** - * Customer instance - */ - private Customer customer; - - /** - * PDF receipt injection - */ - @Receipt - private PdfReceiptBeanLocal pdfReceiptBeanLocal; - - /** - * Default constructor - */ - public PdfReceiptSessionBean () { - // Call super constructor - super(); - - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup pdf receipt - this.pdfReceiptBeanLocal = (PdfReceiptBeanLocal) context.lookup("java:global/jshop-receipt-ejb/pdf-receipt!org.mxchange.jshopreceipt.receipt.PdfReceiptBeanLocal"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } - } - - @Override - public WrapableReceipt createReceiptFromAccessKey (final String accessKey) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("createReceiptFromAccessKey: accessKey={0} - CALLED!", accessKey)); //NOI18N - - return null; - } - - @Override - public String fetchAccessKey (final Customer customer) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("fetchAccessKey: customer={0} - EXIT!", customer)); //NOI18N - - // customer should not be null - if (null == customer) { - // Abort here - throw new NullPointerException("customer is null"); //NOI18N - } else if (this.getCustomer() == null) { - // Don't continue, too. - throw new NullPointerException("this.getCustomer() returns null"); //NOI18N - } - - // Default is null - String key = null; - - // Is customer the same? - if (customer.equals(this.getCustomer())) { - // Set access key - key = this.getAccessKey(); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("fetchAccessKey: key={0} - EXIT!", key)); //NOI18N - - // Return it - return key; - } - - @Override - public String getAccessKey () { - return this.accessKey; - } - - @Override - public void setAccessKey (final String accessKey) { - this.accessKey = accessKey; - } - - @Override - public Customer getCustomer () { - return this.customer; - } - - @Override - public void setCustomer (final Customer customer) { - this.customer = customer; - } - - /** - * Returns given property key or throws an exception if not found. - *

- * @param parameterKey Property key - *

- * @return Property value - *

- * @throws NullPointerException If given key is not found - * @throws NumberFormatException If no number is given in context parameter - */ - protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { - // Get context parameter - Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); - // Return it - return contextValue; - } - - /** - * Returns given property key or throws an exception if not found. - *

- * @param parameterKey Property key - *

- * @return Property value - *

- * @throws NullPointerException If given key is not found - */ - protected String getStringContextParameter (final String parameterKey) throws NullPointerException { - // Get context parameter - String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); - // Is it null? - if (null == contextValue) { - // Throw NPE - throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N - } - // Return it - return contextValue; - } - - /** - * Checks whether debug mode is enabled for given controller - *

- * @param controllerName Name of controller - *

- * @return Whether debug mode is enabled - */ - protected boolean isDebugModeEnabled (final String controllerName) { - // Parameters should be valid - if (null == controllerName) { - // Throw NPE - throw new NullPointerException("controllerName is null"); //NOI18N - } else if (controllerName.isEmpty()) { - // Is empty - throw new IllegalArgumentException("controllerName is empty"); //NOI18N - } - // Try to get context parameter - String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N - // Is it set and true? - boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; - // Return it - return isEnabled; - } - - /** - * Loads resource bundle for given locale. This must be implemented per - * project so all projects can still customize their methods. Calling - * ResourceBundleloadBundle() in this class means that also the bundle files - * must be present here. - *

- * @param locale Locale from e.g. FacesContext - *

- * @return Initialized and loaded resource bundle - */ - protected abstract ResourceBundle loadResourceBundle (final Locale locale); - - /** - * Shows a faces message for given causing exception. The message from the - * exception is being inserted into the message. - *

- * @param clientId Client id to send message to - * @param cause Causing exception - */ - protected void showFacesMessage (final String clientId, final Throwable cause) { - // Get context and add message - this.showFacesMessage(clientId, cause.getMessage()); - } - - /** - * Shows a faces message with given message (i18n) key. - *

- * @param clientId Client id to send message to - * @param i18nKey Message key - *

- * @throws NullPointerException If clientId or i18nKey is null - * @throws IllegalArgumentException If clientId or i18nKey is empty - */ - protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { - // Both parameter must be valid - if (null == clientId) { - // Throw NPE - throw new NullPointerException("clientId is null"); //NOI18N - } else if (clientId.isEmpty()) { - // Is empty - throw new IllegalArgumentException("clientId is null"); //NOI18N - } else if (null == i18nKey) { - // Throw NPE - throw new NullPointerException("i18nKey is null"); //NOI18N - } else if (i18nKey.isEmpty()) { - // Is empty - throw new IllegalArgumentException("i18nKey is null"); //NOI18N - } - // Get current locale - Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); - // Get bundle bundle - ResourceBundle bundle = this.loadResourceBundle(locale); - // Default is i18nKey - String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N - // Try it - try { - // Get message - message = bundle.getString(i18nKey); - } catch (final MissingResourceException ex) { - // Did not find it, ignored - } - // Get context and add message - FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); - } - -} diff --git a/src/java/org/mxchange/pizzaapplication/model/receipt/ReceiptFactory.java b/src/java/org/mxchange/pizzaapplication/model/receipt/ReceiptFactory.java deleted file mode 100644 index c05d8e1..0000000 --- a/src/java/org/mxchange/pizzaapplication/model/receipt/ReceiptFactory.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package org.mxchange.pizzaapplication.model.receipt; - -import gnu.jpdf.PDFJob; -import javax.enterprise.inject.Produces; -import javax.enterprise.inject.spi.InjectionPoint; -import org.mxchange.jshopreceipt.receipt.Receipt; - -/** - * A logger factory - *

- * @author Roland Häder - */ -public class ReceiptFactory { - - /** - * Own job - */ - private final PDFJob job; - - /** - * Factory method - */ - public ReceiptFactory () { - this.job = new PDFJob(); - } - - /** - * Produces a PDF job instance - *

- * @param caller Injection point - *

- * @return PDF job - */ - @Produces - @Receipt - public PDFJob getJob (final InjectionPoint caller) { - return this.job; - } - -}