com.swisscom.safeconnect.model.Subscription.java Source code

Java tutorial

Introduction

Here is the source code for com.swisscom.safeconnect.model.Subscription.java

Source

/*
 Swisscom Safe Connect
 Copyright (C) 2014 Swisscom
    
 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 com.swisscom.safeconnect.model;

import com.swisscom.safeconnect.billing.SkuDetails;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.regex.Pattern;

/**
 * Created by vadim on 03.10.14.
 * combines data about subscription from plumber and google play
 */
public class Subscription implements Serializable {
    private static final Pattern pricePattern = Pattern.compile("(?:\\d*\\.?,?)?\\d+");
    private String sku;
    private String description;
    private String price;
    private String currency = "CHF";
    private String title;
    private int daysValid;
    private long validTill;
    private int id;

    public Subscription(String sku) {
        this.sku = sku;
    }

    public Subscription(String sku, String description, String price, String title, int daysValid, long validTill) {

        this.sku = sku;
        this.description = description;
        this.price = price;
        this.title = title;
        this.daysValid = daysValid;
        this.validTill = validTill;
    }

    public Subscription(PlumberSubscriptionResponse s) {
        this.sku = s.getName();
        this.description = s.getDescription();
        this.price = String.valueOf(s.getPrice());
        this.title = s.getDescription();
        this.daysValid = s.getDaysValid();
        this.validTill = s.getValidTill();
        this.id = s.getId();
    }

    public Subscription(SkuDetails sd, PlumberSubscriptionResponse s) {
        this.sku = sd.getSku();
        this.description = sd.getDescription();
        this.title = sd.getTitle();
        this.daysValid = s.getDaysValid();
        this.validTill = s.getValidTill();
        this.id = s.getId();

        try {
            JSONObject j = new JSONObject(sd.getJson());
            long priceMicros = j.optLong("price_amount_micros");
            float priceNormal = priceMicros / 1000000f;
            if (priceNormal == Math.ceil(priceNormal)) {
                this.price = String.format("%.0f", priceNormal);
            } else {
                this.price = String.format("%.2f", priceNormal);
            }
            this.currency = j.optString("price_currency_code");
        } catch (JSONException e) {
            this.price = String.format("%.2f", s.getPrice());
        }
    }

    public Subscription(Subscription s, long validTill) {
        this.sku = s.getSku();
        this.description = s.getDescription();
        this.price = s.price;
        this.currency = s.currency;
        this.title = s.getTitle();
        this.daysValid = s.getDaysValid();
        this.validTill = validTill;
        this.id = s.getId();
    }

    public String getSku() {
        return sku;
    }

    public String getDescription() {
        return description;
    }

    public String getPrice() {
        return price;
    }

    public String getTitle() {
        return title;
    }

    public int getDaysValid() {
        return daysValid;
    }

    public long getValidTill() {
        return validTill;
    }

    public int getId() {
        return id;
    }

    public String getCurrency() {
        return currency;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (!(o instanceof Subscription))
            return false;

        Subscription that = (Subscription) o;

        if (sku != null ? !sku.equals(that.sku) : that.sku != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return sku != null ? sku.hashCode() : 0;
    }

    @Override
    public String toString() {
        if (sku != null)
            return sku;
        return "nameless subscription";
    }

}