com.mp.gw2api.data.GW2APIAccount.java Source code

Java tutorial

Introduction

Here is the source code for com.mp.gw2api.data.GW2APIAccount.java

Source

/*
 * Copyright (C) 2016 Hero
 *
 * 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.mp.gw2api.data;

import com.mp.gw2api.base.GW2Access;
import com.mp.gw2api.base.GW2Access.AccessLevel;
import com.mp.gw2api.base.IGW2APIData;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Mitchell Pell
 */
public class GW2APIAccount implements IGW2APIData {

    public String id;
    public String name;
    public long world;
    public String[] guilds;
    public String created;
    public AccessLevel access;
    public boolean commander;
    public long fractalLevel;
    public long dailyAP;
    public long monthlyAP;
    public long wvwRank;
    public String errorText;

    @Override
    public void LoadFromJsonText(String text) {
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        JSONArray ja = null;
        JSONArray jb = null;
        try {
            //Parse json array
            json = (JSONObject) parser.parse(text);

            //Check for errors
            if (json.containsKey("text")) {
                errorText = (String) json.get("text");
                return;
            }

            //Gather Account information
            id = (String) json.get("id");
            name = (String) json.get("name");
            world = (long) json.get("world");

            ja = (JSONArray) json.get("guilds");
            if (ja != null)
                if (ja.size() > 0) {
                    guilds = new String[ja.size()];
                    for (int i = 0; i < ja.size(); i++)
                        guilds[i] = (String) ja.get(i);
                }

            created = (String) json.get("created");

            //access
            String accessStr = (String) json.get("access");
            //For each AccessLevel String known.
            for (int i = 0; i < GW2Access.AccessStrings.length; i++)
                //If the accessStr read matches one known
                if (GW2Access.AccessStrings[i].equals(accessStr)) {
                    //Fetch the enumeration related to that index
                    access = AccessLevel.values()[i];
                    break;//break loop
                    //If no values matched (error)
                } else if (i == GW2Access.AccessStrings.length - 1)
                    access = AccessLevel.NONE;

            commander = (boolean) json.get("commander");
            fractalLevel = (long) json.get("fractal_level");
            dailyAP = (long) json.get("daily_ap");
            monthlyAP = (long) json.get("monthly_ap");
            wvwRank = (long) json.get("wvw_rank");

        } catch (ParseException ex) {
            Logger.getLogger(com.mp.gw2api.lists.GW2APIMapList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}