com.vphakala.SubscriberUI.java Source code

Java tutorial

Introduction

Here is the source code for com.vphakala.SubscriberUI.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vphakala;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.ui.*;
import com.vaadin.ui.themes.BaseTheme;
import java.math.BigDecimal;
import java.util.List;
import rtbvoucherws.types.CheckVoucherElement;
import rtbvoucherws.types.CheckVoucherResponseElement;
import rtbvoucherws.types.CheckVoucherResponseElement2;

/**
 *
 * @author FIVELHAKA
 */
public class SubscriberUI extends VerticalLayout {

    private SubscriberService service;
    private Subscriber subscriber;

    private TextField email = new TextField("Email");
    private TextField phoneNumber = new TextField("Phone Number");

    private TextField balance = new TextField("Balance");
    private Button recharge = new Button("Top up your balance", e -> rechargeAccount());
    private RtBVoucherWSImplService voucherService = new RtBVoucherWSImplService();
    private RtBVoucherWS voucher;
    private Label rechargeResponse = new Label();

    private Button create = new Button("", e -> createSubscriber());
    private Button update = new Button("", e -> updateSubscriber());
    private Button read = new Button("", e -> readSubscriber());
    private Button delete = new Button("", e -> deleteSubscriber());

    public SubscriberUI(SubscriberService service) {
        super();

        this.service = service;

        setMargin(true);
        setSpacing(true);
        setCaption("Subscriber");

        Toolbar toolbar = new Toolbar(this.getCaption(), read, create, update, delete);

        recharge.setStyleName(BaseTheme.BUTTON_LINK);
        HorizontalLayout account = new HorizontalLayout(balance, recharge, rechargeResponse);
        account.setSpacing(true);
        account.setComponentAlignment(recharge, Alignment.BOTTOM_LEFT);

        voucher = voucherService.getRtBVoucherWSImplPort();

        addComponents(toolbar, email, phoneNumber, account);
    }

    public void createSubscriber() {
        if (service.create(new Subscriber(0L, email.getValue(), phoneNumber.getValue(),
                new BigDecimal(balance.getValue()))) == true) {
            Notification.show("create OK");
        } else {
            Notification.show("Create failed");
        }
    }

    public void updateSubscriber() {
        BigDecimal bigDecimal = new BigDecimal(balance.getValue().replace(",", "."));
        subscriber.setPhoneNumber(phoneNumber.getValue());
        subscriber.setBalance(bigDecimal);

        service.update(subscriber);
    }

    public void readSubscriber() {
        List subscribers = service.read(email.getValue());
        if (subscribers.isEmpty() == true) {
            Notification.show("Read: not found");
            create.setEnabled(true);
        } else {
            create.setEnabled(false);
            subscriber = (Subscriber) subscribers.get(0);
            BeanFieldGroup.bindFieldsUnbuffered(subscriber, this);
            this.email.setReadOnly(true);
        }
    }

    public void deleteSubscriber() {
        service.delete(subscriber);
    }

    private void setEmailStr(String email) {
        this.email.setReadOnly(false);
        this.email.setValue(email);
        this.email.setReadOnly(true);
    }

    public void setUIFields(String email) {
        setEmailStr(email);

        phoneNumber.clear();
        balance.clear();

        readSubscriber();
    }

    public void rechargeAccount() {
        CheckVoucherElement parameters = new CheckVoucherElement();

        parameters.setSourceId("SUBS-UI");
        parameters.setUserId(email.getValue());
        parameters.setSecretNumber("12345678901234567890");

        CheckVoucherResponseElement response = voucher.checkVoucher(parameters);
        CheckVoucherResponseElement2 response2 = response.getResult();

        rechargeResponse.setValue(response2.getTransactionId() + "," + response2.getRetText());
    }
}