Java tutorial
/** * Copyright 2013 Universidad Icesi * * This file is part of SongStock. * * SongStock 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. * * SongStock 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 SongStock. If not, see <http://www.gnu.org/licenses/>. **/ package songstock.web.extensions.payments.creditcard; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import payments.dtos.ICreditCard; import payments.dtos.impl.CreditCard; import com.vaadin.annotations.AutoGenerated; import com.vaadin.data.Validator; import com.vaadin.data.Validator.InvalidValueException; import com.vaadin.server.AbstractErrorMessage.ContentMode; import com.vaadin.server.ErrorMessage.ErrorLevel; import com.vaadin.server.UserError; import com.vaadin.ui.AbsoluteLayout; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.ComboBox; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.Label; import com.vaadin.ui.Notification; import com.vaadin.ui.PasswordField; import com.vaadin.ui.Notification.Type; import com.vaadin.ui.TextField; /** * * @author Andrs Pz * */ public class CreditCardForm extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private AbsoluteLayout mainLayout; @AutoGenerated private ComboBox comboBoxCardBrand; @AutoGenerated private ComboBox comboBoxCountry; @AutoGenerated private Label labelCardBrand; @AutoGenerated private Label labelBillingAddressInformation; @AutoGenerated private ComboBox comboBoxExpirationDateYear; @AutoGenerated private ComboBox comboBoxExpirationDateMonth; @AutoGenerated private Label labelError; @AutoGenerated private Button buttonPay; @AutoGenerated private TextField textFieldPhoneNumber; @AutoGenerated private TextField textFieldState; @AutoGenerated private TextField textFieldCity; @AutoGenerated private TextField textFieldZipCode; @AutoGenerated private TextField textFieldBillingAddress; @AutoGenerated private PasswordField textFieldCardVerificationCode; @AutoGenerated private TextField textFieldCardholder; @AutoGenerated private TextField textFieldCardNumber; @AutoGenerated private Label labelPhoneNumber; @AutoGenerated private Label labelCountry; @AutoGenerated private Label labelState; @AutoGenerated private Label labelCity; @AutoGenerated private Label labelZipCode; @AutoGenerated private Label labelAddress; @AutoGenerated private Label labelVerificationCode; @AutoGenerated private Label labelExpirationDate; @AutoGenerated private Label labelCardholder; @AutoGenerated private Label labelNumber; @AutoGenerated private Label labelCreditCardInformation; /** * */ private static final long serialVersionUID = 1L; public static final String PAYMENT_METHOD = "creditCard"; /** * The constructor should first build the main layout, set the composition * root and then do any custom initialization. * * The constructor will not be automatically regenerated by the visual * editor. */ public CreditCardForm() { buildMainLayout(); setCompositionRoot(mainLayout); // User code this.labelError.setValue(""); this.labelError.setEnabled(false); comboBoxCardBrand.setRequiredError("Select the card's brand."); // Set card brands setCardBrands(); textFieldCardNumber.setRequiredError("Type the card's number."); textFieldCardNumber.addValidator(new Validator() { @Override public void validate(Object value) throws InvalidValueException { if (textFieldCardNumber.getValue().length() != 16) throw new InvalidValueException("The credit card number must have 16 characters"); } }); textFieldCardholder.setRequiredError("Type the cardholder's name."); comboBoxExpirationDateMonth.setRequiredError("Select the card's expiration month."); // Set months setExpirationMonths(); comboBoxExpirationDateMonth.addValidator(new Validator() { @Override public void validate(Object value) throws InvalidValueException { Calendar cal = GregorianCalendar.getInstance(); cal.setTime(new Date()); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; if (comboBoxExpirationDateMonth.getValue() != null && comboBoxExpirationDateYear.getValue() != null && Integer.parseInt(comboBoxExpirationDateYear.getValue().toString()) == currentYear && Integer.parseInt(comboBoxExpirationDateMonth.getValue().toString()) < currentMonth) throw new InvalidValueException("The credit card expired!"); } }); comboBoxExpirationDateYear.setRequiredError("Select the card's expiration year."); // Set years setExpirationYears(); comboBoxExpirationDateYear.addValidator(new Validator() { @Override public void validate(Object value) throws InvalidValueException { Calendar cal = GregorianCalendar.getInstance(); cal.setTime(new Date()); int currentYear = cal.get(Calendar.YEAR); int currentMonth = cal.get(Calendar.MONTH) + 1; if (comboBoxExpirationDateMonth.getValue() != null && comboBoxExpirationDateYear.getValue() != null && Integer.parseInt(comboBoxExpirationDateMonth.getValue().toString()) == currentMonth && Integer.parseInt(comboBoxExpirationDateYear.getValue().toString()) < currentYear) throw new InvalidValueException("The credit card expired!"); } }); textFieldCardVerificationCode.setRequiredError("Type the card's verification code."); textFieldCardVerificationCode.addValidator(new Validator() { @Override public void validate(Object value) throws InvalidValueException { if (!textFieldCardVerificationCode.getValue().matches("[0-9]+")) throw new InvalidValueException("The verification code must contain numbers only"); } }); textFieldBillingAddress.setRequiredError("Type the billing address."); textFieldZipCode.setRequiredError("Type the billing address's zip code."); textFieldCity.setRequiredError("Type the billing address's city."); textFieldState.setRequiredError("Type the billing address's state."); comboBoxCountry.setRequiredError("Select the billing address's country."); // Set countries setCountries(); textFieldPhoneNumber.setRequiredError("Type the billing address's phone number."); textFieldPhoneNumber.addValidator(new Validator() { @Override public void validate(Object value) throws InvalidValueException { if (!textFieldPhoneNumber.getValue().matches("[0-9]+")) throw new InvalidValueException("The phone number must contain numbers only"); } }); this.buttonPay.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { doPay(); } }); } private void setCountries() { comboBoxCountry.addItem("Colombia"); comboBoxCountry.addItem("United States"); } private void setExpirationYears() { for (int i = 2013; i <= 2020; i++) { comboBoxExpirationDateYear.addItem(new Integer(i)); } } private void setExpirationMonths() { for (int i = 1; i <= 12; i++) { comboBoxExpirationDateMonth.addItem(new Integer(i)); } } private void setCardBrands() { comboBoxCardBrand.addItem("American Express"); comboBoxCardBrand.addItem("Diners Club"); comboBoxCardBrand.addItem("MasterCard"); comboBoxCardBrand.addItem("Visa"); } public void loadCreditCard(ICreditCard creditCard) { comboBoxCardBrand.setValue(creditCard.getBrandName()); textFieldCardNumber.setValue(creditCard.getNumber()); textFieldCardholder.setValue(creditCard.getCardHolderName()); Calendar cal = Calendar.getInstance(); cal.setTime(creditCard.getExpirationDate()); comboBoxExpirationDateMonth.setValue(cal.get(Calendar.MONTH) + 1); comboBoxExpirationDateYear.setValue(cal.get(Calendar.YEAR)); textFieldBillingAddress.setValue(creditCard.getBillingAddress()); textFieldZipCode.setValue(creditCard.getZipCode()); textFieldCity.setValue(creditCard.getCity()); textFieldState.setValue(creditCard.getState()); comboBoxCountry.setValue(creditCard.getCountry()); textFieldPhoneNumber.setValue(creditCard.getPhoneNumber()); } private void validateFields() throws Exception { try { comboBoxCardBrand.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldCardNumber.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldCardholder.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { comboBoxExpirationDateMonth.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { comboBoxExpirationDateYear.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldCardVerificationCode.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldBillingAddress.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldZipCode.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldCity.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldState.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { comboBoxCountry.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } try { textFieldPhoneNumber.validate(); } catch (InvalidValueException e) { setErrorMessage(e.getMessage()); throw e; } } protected void doPay() { try { validateFields(); ICreditCard creditCard = new CreditCard(); creditCard.setBrandName(comboBoxCardBrand.getValue().toString()); creditCard.setNumber(textFieldCardNumber.getValue()); creditCard.setCardHolderName(textFieldCardholder.getValue()); Calendar cal = Calendar.getInstance(); cal.set(Integer.parseInt(comboBoxExpirationDateYear.getValue().toString()), Integer.parseInt(comboBoxExpirationDateMonth.getValue().toString()) - 1, 1); creditCard.setExpirationDate(cal.getTime()); creditCard.setCardVerificationCode(Integer.parseInt(textFieldCardVerificationCode.getValue())); creditCard.setBillingAddress(textFieldBillingAddress.getValue()); creditCard.setZipCode(textFieldZipCode.getValue()); creditCard.setCity(textFieldCity.getValue()); creditCard.setState(textFieldState.getValue()); creditCard.setCountry(comboBoxCountry.getValue().toString()); creditCard.setPhoneNumber(textFieldPhoneNumber.getValue()); CreditCardFormController.getInstance().doPay(creditCard); } catch (Exception e) { } } public void setErrorMessage(String message) { Notification.show("SongStock", message, Type.TRAY_NOTIFICATION); this.labelError.setValue("<i>" + message + "</i>"); this.buttonPay.setComponentError(new UserError(message, ContentMode.TEXT, ErrorLevel.WARNING)); } public void clear() { comboBoxCardBrand.setValue(""); textFieldCardNumber.setValue(""); textFieldCardholder.setValue(""); comboBoxExpirationDateMonth.setValue(""); comboBoxExpirationDateYear.setValue(""); textFieldCardVerificationCode.setValue(""); textFieldBillingAddress.setValue(""); textFieldZipCode.setValue(""); textFieldCity.setValue(""); textFieldState.setValue(""); comboBoxCountry.setValue(""); textFieldPhoneNumber.setValue(""); this.labelError.setValue(""); this.buttonPay.setComponentError(null); } @AutoGenerated private AbsoluteLayout buildMainLayout() { // common part: create layout mainLayout = new AbsoluteLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("100%"); mainLayout.setHeight("100%"); // top-level component properties setWidth("100.0%"); setHeight("100.0%"); // labelCreditCardInformation labelCreditCardInformation = new Label(); labelCreditCardInformation.setImmediate(false); labelCreditCardInformation.setWidth("-1px"); labelCreditCardInformation.setHeight("-1px"); labelCreditCardInformation.setValue("<b>Credit Card Information:</b>"); labelCreditCardInformation.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelCreditCardInformation, "top:62.0px;left:21.0px;"); // labelNumber labelNumber = new Label(); labelNumber.setImmediate(false); labelNumber.setWidth("-1px"); labelNumber.setHeight("-1px"); labelNumber.setValue("Card Number:"); mainLayout.addComponent(labelNumber, "top:142.0px;left:21.0px;"); // labelCardholder labelCardholder = new Label(); labelCardholder.setImmediate(false); labelCardholder.setWidth("-1px"); labelCardholder.setHeight("-1px"); labelCardholder.setValue("Cardholder Name:"); mainLayout.addComponent(labelCardholder, "top:180.0px;left:20.0px;"); // labelExpirationDate labelExpirationDate = new Label(); labelExpirationDate.setImmediate(false); labelExpirationDate.setWidth("-1px"); labelExpirationDate.setHeight("-1px"); labelExpirationDate.setValue("Expiration Date:"); mainLayout.addComponent(labelExpirationDate, "top:242.0px;left:21.0px;"); // labelVerificationCode labelVerificationCode = new Label(); labelVerificationCode.setImmediate(false); labelVerificationCode.setWidth("-1px"); labelVerificationCode.setHeight("-1px"); labelVerificationCode.setValue("Verification Code:"); mainLayout.addComponent(labelVerificationCode, "top:282.0px;left:20.0px;"); // labelAddress labelAddress = new Label(); labelAddress.setImmediate(false); labelAddress.setWidth("-1px"); labelAddress.setHeight("-1px"); labelAddress.setValue("Address:"); mainLayout.addComponent(labelAddress, "top:100.0px;left:440.0px;"); // labelZipCode labelZipCode = new Label(); labelZipCode.setImmediate(false); labelZipCode.setWidth("-1px"); labelZipCode.setHeight("-1px"); labelZipCode.setValue("Zip Code:"); mainLayout.addComponent(labelZipCode, "top:142.0px;left:440.0px;"); // labelCity labelCity = new Label(); labelCity.setImmediate(false); labelCity.setWidth("-1px"); labelCity.setHeight("-1px"); labelCity.setValue("City:"); mainLayout.addComponent(labelCity, "top:180.0px;left:440.0px;"); // labelState labelState = new Label(); labelState.setImmediate(false); labelState.setWidth("-1px"); labelState.setHeight("-1px"); labelState.setValue("State:"); mainLayout.addComponent(labelState, "top:220.0px;left:440.0px;"); // labelCountry labelCountry = new Label(); labelCountry.setImmediate(false); labelCountry.setWidth("-1px"); labelCountry.setHeight("-1px"); labelCountry.setValue("Country:"); mainLayout.addComponent(labelCountry, "top:260.0px;left:440.0px;"); // labelPhoneNumber labelPhoneNumber = new Label(); labelPhoneNumber.setImmediate(false); labelPhoneNumber.setWidth("-1px"); labelPhoneNumber.setHeight("-1px"); labelPhoneNumber.setValue("Phone Number:"); mainLayout.addComponent(labelPhoneNumber, "top:300.0px;left:440.0px;"); // textFieldCardNumber textFieldCardNumber = new TextField(); textFieldCardNumber.setImmediate(false); textFieldCardNumber.setWidth("260px"); textFieldCardNumber.setHeight("-1px"); mainLayout.addComponent(textFieldCardNumber, "top:140.0px;left:140.0px;"); // textFieldCardholder textFieldCardholder = new TextField(); textFieldCardholder.setImmediate(false); textFieldCardholder.setWidth("260px"); textFieldCardholder.setHeight("-1px"); mainLayout.addComponent(textFieldCardholder, "top:176.0px;left:140.0px;"); // textFieldCardVerificationCode textFieldCardVerificationCode = new PasswordField(); textFieldCardVerificationCode.setImmediate(false); textFieldCardVerificationCode.setWidth("100px"); textFieldCardVerificationCode.setHeight("-1px"); mainLayout.addComponent(textFieldCardVerificationCode, "top:280.0px;left:140.0px;"); // textFieldBillingAddress textFieldBillingAddress = new TextField(); textFieldBillingAddress.setImmediate(false); textFieldBillingAddress.setWidth("260px"); textFieldBillingAddress.setHeight("-1px"); mainLayout.addComponent(textFieldBillingAddress, "top:96.0px;left:540.0px;"); // textFieldZipCode textFieldZipCode = new TextField(); textFieldZipCode.setImmediate(false); textFieldZipCode.setWidth("260px"); textFieldZipCode.setHeight("-1px"); mainLayout.addComponent(textFieldZipCode, "top:140.0px;left:540.0px;"); // textFieldCity textFieldCity = new TextField(); textFieldCity.setImmediate(false); textFieldCity.setWidth("260px"); textFieldCity.setHeight("-1px"); mainLayout.addComponent(textFieldCity, "top:176.0px;left:540.0px;"); // textFieldState textFieldState = new TextField(); textFieldState.setImmediate(false); textFieldState.setWidth("260px"); textFieldState.setHeight("-1px"); mainLayout.addComponent(textFieldState, "top:218.0px;left:540.0px;"); // textFieldPhoneNumber textFieldPhoneNumber = new TextField(); textFieldPhoneNumber.setImmediate(false); textFieldPhoneNumber.setWidth("260px"); textFieldPhoneNumber.setHeight("-1px"); mainLayout.addComponent(textFieldPhoneNumber, "top:296.0px;left:540.0px;"); // buttonPay buttonPay = new Button(); buttonPay.setCaption("Continue with payment"); buttonPay.setImmediate(true); buttonPay.setWidth("-1px"); buttonPay.setHeight("-1px"); mainLayout.addComponent(buttonPay, "top:20.0px;left:21.0px;"); // labelError labelError = new Label(); labelError.setImmediate(false); labelError.setWidth("-1px"); labelError.setHeight("-1px"); labelError.setValue("<i>Label</i>"); labelError.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelError, "top:22.0px;left:200.0px;"); // comboBoxExpirationDateMonth comboBoxExpirationDateMonth = new ComboBox(); comboBoxExpirationDateMonth.setCaption("Month"); comboBoxExpirationDateMonth.setImmediate(false); comboBoxExpirationDateMonth.setWidth("120px"); comboBoxExpirationDateMonth.setHeight("-1px"); mainLayout.addComponent(comboBoxExpirationDateMonth, "top:240.0px;left:140.0px;"); // comboBoxExpirationDateYear comboBoxExpirationDateYear = new ComboBox(); comboBoxExpirationDateYear.setCaption("Year"); comboBoxExpirationDateYear.setImmediate(false); comboBoxExpirationDateYear.setWidth("120px"); comboBoxExpirationDateYear.setHeight("-1px"); mainLayout.addComponent(comboBoxExpirationDateYear, "top:240.0px;left:280.0px;"); // labelBillingAddressInformation labelBillingAddressInformation = new Label(); labelBillingAddressInformation.setImmediate(false); labelBillingAddressInformation.setWidth("-1px"); labelBillingAddressInformation.setHeight("-1px"); labelBillingAddressInformation.setValue("<b>Billing Address:</b>"); labelBillingAddressInformation.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelBillingAddressInformation, "top:62.0px;left:440.0px;"); // labelCardBrand labelCardBrand = new Label(); labelCardBrand.setImmediate(false); labelCardBrand.setWidth("-1px"); labelCardBrand.setHeight("-1px"); labelCardBrand.setValue("Card Brand:"); mainLayout.addComponent(labelCardBrand, "top:100.0px;left:20.0px;"); // comboBoxCountry comboBoxCountry = new ComboBox(); comboBoxCountry.setImmediate(false); comboBoxCountry.setWidth("260px"); comboBoxCountry.setHeight("-1px"); mainLayout.addComponent(comboBoxCountry, "top:256.0px;left:540.0px;"); // comboBoxCardBrand comboBoxCardBrand = new ComboBox(); comboBoxCardBrand.setImmediate(false); comboBoxCardBrand.setWidth("260px"); comboBoxCardBrand.setHeight("-1px"); mainLayout.addComponent(comboBoxCardBrand, "top:96.0px;left:140.0px;"); return mainLayout; } }