--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.beans.checkout;
+
+import java.util.List;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.enterprise.context.SessionScoped;
+import javax.faces.FacesException;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcore.model.contact.gender.Gender;
+import org.mxchange.jcoreee.beans.BaseFrameworkBean;
+import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.customer.Customer;
+import org.mxchange.jshopcore.model.customer.ShopCustomer;
+import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
+import org.mxchange.jshopcore.wrapper.WrapableCheckout;
+import org.mxchange.pizzaapplication.beans.basket.BasketWebController;
+
+/**
+ * Checkout controller
+ *
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@Named ("checkoutController")
+@SessionScoped
+public class CheckoutWebBean extends BaseFrameworkBean implements CheckoutWebController {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 51_987_348_347_183L;
+
+ /**
+ * Connection
+ */
+ private Connection connection;
+
+ /**
+ * Message producer
+ */
+ private MessageProducer messageProducer;
+
+ /**
+ * Queue instance
+ */
+ private Queue queue;
+
+ /**
+ * Session instance
+ */
+ private Session session;
+
+ /**
+ * Object message
+ */
+ private ObjectMessage message;
+
+ ////////////////////// Bean injections ///////////////////////
+ /**
+ * Basket bean
+ */
+ @Inject
+ private BasketWebController basketController;
+
+ /////////////////////// Properties /////////////////////
+ /**
+ * Gender
+ */
+ private Gender gender;
+
+ /**
+ * First name
+ */
+ private String firstName;
+
+ /**
+ * Family name
+ */
+ private String familyName;
+
+ /**
+ * Company name
+ */
+ private String companyName;
+
+ /**
+ * Street
+ */
+ private String street;
+
+ /**
+ * House number
+ */
+ private Long houseNumber;
+
+ /**
+ * ZIP code
+ */
+ private Long zipCode;
+
+ /**
+ * City
+ */
+ private String city;
+
+ /**
+ * Phone number
+ */
+ private String phoneNumber;
+
+ /**
+ * Fax number
+ */
+ private String faxNumber;
+
+ /**
+ * Cellphone number
+ */
+ private String cellphoneNumber;
+
+ @Override
+ public String doCheckout () {
+ // Is the bean set?
+ if (this.basketController != null) {
+ // Abort here
+ throw new NullPointerException("basketController is null");
+ }
+
+ // Are at least the required fields set?
+ if (!this.isRequiredPersonalDataSet()) {
+ // Not set, should not happen
+ return "checkout2";
+ } else if (this.basketController.isEmpty()) {
+ // Nothing to order
+ return "empty_basket";
+ }
+
+ // Create customer instance
+ Customer customer = this.createCustomer();
+
+ // Get ordered list
+ List<AddableBasketItem> list = this.basketController.allItems();
+
+ // Construct container
+ WrapableCheckout wrapper = new CheckoutWrapper();
+ wrapper.setCustomer(customer);
+ wrapper.setList(list);
+
+ try {
+ // Construct object message
+ this.message.setObject(wrapper);
+
+ // Send message
+ this.messageProducer.send(this.message);
+ } catch (final JMSException ex) {
+ // TODO: Log exception?
+ // Not working
+ return "jms_failed";
+ }
+
+ // Clear basket
+ this.basketController.clear();
+
+ // All fine
+ return "checkout_done";
+ }
+
+ @Override
+ public String getCellphoneNumber () {
+ return this.cellphoneNumber;
+ }
+
+ @Override
+ public void setCellphoneNumber (final String cellphoneNumber) {
+ this.cellphoneNumber = cellphoneNumber;
+ }
+
+ @Override
+ public String getCity () {
+ return this.city;
+ }
+
+ @Override
+ public void setCity (final String city) {
+ this.city = city;
+ }
+
+ @Override
+ public String getCompanyName () {
+ return this.companyName;
+ }
+
+ @Override
+ public void setCompanyName (final String companyName) {
+ this.companyName = companyName;
+ }
+
+ @Override
+ public String getFamilyName () {
+ return this.familyName;
+ }
+
+ @Override
+ public void setFamilyName (final String familyName) {
+ this.familyName = familyName;
+ }
+
+ @Override
+ public String getFaxNumber () {
+ return this.faxNumber;
+ }
+
+ @Override
+ public void setFaxNumber (final String faxNumber) {
+ this.faxNumber = faxNumber;
+ }
+
+ @Override
+ public String getFirstName () {
+ return this.firstName;
+ }
+
+ @Override
+ public void setFirstName (final String firstName) {
+ this.firstName = firstName;
+ }
+
+ @Override
+ public Gender getGender () {
+ return this.gender;
+ }
+
+ @Override
+ public void setGender (final Gender gender) {
+ this.gender = gender;
+ }
+
+ @Override
+ public Long getHouseNumber () {
+ return this.houseNumber;
+ }
+
+ @Override
+ public void setHouseNumber (final Long houseNumber) {
+ this.houseNumber = houseNumber;
+ }
+
+ @Override
+ public String getPhoneNumber () {
+ return this.phoneNumber;
+ }
+
+ @Override
+ public void setPhoneNumber (final String phoneNumber) {
+ this.phoneNumber = phoneNumber;
+ }
+
+ @Override
+ public String getStreet () {
+ return this.street;
+ }
+
+ @Override
+ public void setStreet (final String street) {
+ this.street = street;
+ }
+
+ @Override
+ public Long getZipCode () {
+ return this.zipCode;
+ }
+
+ @Override
+ public void setZipCode (final Long zipCode) {
+ this.zipCode = zipCode;
+ }
+
+ @PostConstruct
+ public void init () {
+ // Call super init for getting resource bundle
+ super.genericInit();
+
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Get factory from JMS resource
+ QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
+
+ // Lookup queue
+ this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
+
+ // Create connection
+ this.connection = connectionFactory.createConnection();
+
+ // Init session instance
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // And message producer
+ this.messageProducer = session.createProducer(queue);
+ } catch (final NamingException | JMSException e) {
+ // Continued to throw
+ throw new FacesException(e);
+ }
+ }
+
+ /**
+ * Destructor
+ */
+ @PreDestroy
+ public void destroy () {
+ try {
+ // Try to close all
+ this.messageProducer.close();
+ this.session.close();
+ this.connection.close();
+ } catch (final JMSException ex) {
+ // TODO: Continue to throw is fine?
+ throw new FacesException(ex);
+ }
+ }
+
+ /**
+ * Creates a full customer instance
+ *
+ * @return Customer instance
+ */
+ private Customer createCustomer () {
+ // Required personal data must be set
+ assert (this.isRequiredPersonalDataSet()) : "not all personal data is set";
+
+ // Create new customer instance
+ Customer customer = new ShopCustomer();
+ customer.setGender(this.getGender());
+ customer.setFirstName(this.getFirstName());
+ customer.setFamilyName(this.getFamilyName());
+ customer.setCompanyName(this.getCompanyName());
+ customer.setStreet(this.getStreet());
+ customer.setHouseNumber(this.getHouseNumber());
+ customer.setZipCode(this.getZipCode());
+ customer.setCity(this.getCity());
+ customer.setPhoneNumber(this.getPhoneNumber());
+ customer.setFaxNumber(this.getFaxNumber());
+ customer.setCellphoneNumber(this.getCellphoneNumber());
+
+ // Return it
+ return customer;
+ }
+
+ /**
+ * Checks if the required personal data is set
+ *
+ * @return Whether the required personal data is set
+ */
+ private boolean isRequiredPersonalDataSet () {
+ return ((this.getGender() != null) && (this.getFirstName() != null) && (this.getFamilyName() != null) && (this.getStreet() != null) && (this.getHouseNumber() != null) && (this.getZipCode() != null) && (this.getCity() != null));
+ }
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.beans.checkout;
+
+import java.io.Serializable;
+import org.mxchange.jcore.model.contact.gender.Gender;
+
+/**
+ * An interface for the shop
+ *
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+public interface CheckoutWebController extends Serializable {
+ /**
+ * Runs the actual checkout and returns a proper page redirection target.
+ *
+ * @return Page redirection target
+ */
+ public String doCheckout ();
+
+ /**
+ * Getter for cellphone number
+ *
+ * @return Cellphone number
+ */
+ public String getCellphoneNumber ();
+
+ /**
+ * Setter for cellphone number
+ *
+ * @param cellphoneNumber Cellphone number
+ */
+ public void setCellphoneNumber (final String cellphoneNumber);
+
+ /**
+ * Getter for city
+ *
+ * @return Cellphone number
+ */
+ public String getCity ();
+
+ /**
+ * Setter for city
+ *
+ * @param city City
+ */
+ public void setCity (final String city);
+
+ /**
+ * Getter for company name
+ *
+ * @return City
+ */
+ public String getCompanyName ();
+
+ /**
+ * Setter for company name
+ *
+ * @param companyName Company name
+ */
+ public void setCompanyName (final String companyName);
+
+ /**
+ * Getter for family name
+ *
+ * @return Company name
+ */
+ public String getFamilyName ();
+
+ /**
+ * Setter for family name
+ *
+ * @param familyName Family name
+ */
+ public void setFamilyName (final String familyName);
+
+ /**
+ * Getter for fax number
+ *
+ * @return Family name
+ */
+ public String getFaxNumber ();
+
+ /**
+ * Setter for fax number
+ *
+ * @param faxNumber First name
+ */
+ public void setFaxNumber (final String faxNumber);
+
+ /**
+ * Getter for first name
+ *
+ * @return First name
+ */
+ public String getFirstName ();
+
+ /**
+ * Setter for first name
+ *
+ * @param firstName Gender
+ */
+ public void setFirstName (final String firstName);
+
+ /**
+ * Getter for gender
+ *
+ * @return Gender
+ */
+ public Gender getGender ();
+
+ /**
+ * Setter for gender
+ *
+ * @param gender Gender
+ */
+ public void setGender (final Gender gender);
+
+ /**
+ * Getter for house number
+ *
+ * @return House number
+ */
+ public Long getHouseNumber ();
+
+ /**
+ * Setter for house number
+ *
+ * @param houseNumber House number
+ */
+ public void setHouseNumber (final Long houseNumber);
+
+ /**
+ * Getter for phone number
+ *
+ * @return Phone number
+ */
+ public String getPhoneNumber ();
+
+ /**
+ * Setter for phone number
+ *
+ * @param phoneNumber Phone number
+ */
+ public void setPhoneNumber (final String phoneNumber);
+
+ /**
+ * Getter for street
+ *
+ * @return Street
+ */
+ public String getStreet ();
+
+ /**
+ * Setter for street
+ *
+ * @param street Street
+ */
+ public void setStreet (final String street);
+
+ /**
+ * Getter for ZIP code
+ *
+ * @return ZIP code
+ */
+ public Long getZipCode ();
+
+ /**
+ * Setter for ZIP code
+ *
+ * @param zipCode ZIP code
+ */
+ public void setZipCode (final Long zipCode);
+}