Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package com.github.dannil.scbjavaclient.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.dannil.scbjavaclient.exception.SCBClientUrlNotFoundException;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.Localization;
Expand Down Expand Up @@ -163,33 +161,54 @@ protected String post(String url, String query) {
* @param url
* the URL to retrieve the regions from
* @return a list of the available regions for the given URL
* @throws UnsupportedOperationException
* @throws IllegalArgumentException
* if the specified URL doesn't supply a regions table
*/
public List<String> getRegions(String url) {
String content = get(url);

JsonNode contentAsJsonNode = JsonUtility.toNode(content);

List<String> codes = contentAsJsonNode.findValuesAsText("code");
List<JsonNode> values = contentAsJsonNode.findValues("values");

int position = codes.indexOf("Region");
if (position < 0) {
Object[] variables = new Object[] { url };

throw new UnsupportedOperationException(this.localization.getString("regions_is_not_supported_for_url",
variables));
}

JsonNode jsonRegions = values.get(position);

List<String> regions = new ArrayList<String>(jsonRegions.size());
for (int j = 0; j < jsonRegions.size(); j++) {
regions.add(jsonRegions.get(j).asText());
String json = get(url);
String code = "Region";
try {
List<String> values = JsonUtility.getValues(json, code);
return values;
} catch (IllegalArgumentException e) {
Object[] variables = new Object[] { code, url };
throw new IllegalArgumentException(this.localization.getString("code_is_not_supported_for_url", variables),
e);
}

return regions;
// if (!inputs.containsKey("Region")) {
// Object[] variables = new Object[] { url };
//
// throw new
// UnsupportedOperationException(this.localization.getString("regions_is_not_supported_for_url",
// variables));
// }
// return inputs.get("Region");

// String content = get(url);
//
// JsonNode contentAsJsonNode = JsonUtility.toNode(content);
//
// List<String> codes = contentAsJsonNode.findValuesAsText("code");
// List<JsonNode> values = contentAsJsonNode.findValues("values");
//
// int position = codes.indexOf("Region");
// if (position < 0) {
// Object[] variables = new Object[] { url };
//
// throw new
// UnsupportedOperationException(this.localization.getString("regions_is_not_supported_for_url",
// variables));
// }
//
// JsonNode jsonRegions = values.get(position);
//
// List<String> regions = new ArrayList<String>(jsonRegions.size());
// for (int j = 0; j < jsonRegions.size(); j++) {
// regions.add(jsonRegions.get(j).asText());
// }
//
// return regions;
}

/**
Expand All @@ -202,29 +221,41 @@ public List<String> getRegions(String url) {
* if the specified URL doesn't supply a years table
*/
public List<String> getYears(String url) {
String content = get(url);

JsonNode contentAsJsonNode = JsonUtility.toNode(content);

List<String> codes = contentAsJsonNode.findValuesAsText("code");
List<JsonNode> values = contentAsJsonNode.findValues("values");

int position = codes.indexOf("Tid");
if (position < 0) {
Object[] variables = new Object[] { url };

throw new UnsupportedOperationException(this.localization.getString("years_is_not_supported_for_url",
variables));
}

JsonNode jsonYears = values.get(position);

List<String> years = new ArrayList<String>(jsonYears.size());
for (int j = 0; j < jsonYears.size(); j++) {
years.add(jsonYears.get(j).asText());
String json = get(url);
String code = "Tid";
try {
List<String> values = JsonUtility.getValues(json, code);
return values;
} catch (IllegalArgumentException e) {
Object[] variables = new Object[] { code, url };
throw new IllegalArgumentException(this.localization.getString("code_is_not_supported_for_url", variables),
e);
}

return years;
//
// String content = get(url);
//
// JsonNode contentAsJsonNode = JsonUtility.toNode(content);
//
// List<String> codes = contentAsJsonNode.findValuesAsText("code");
// List<JsonNode> values = contentAsJsonNode.findValues("values");
//
// int position = codes.indexOf("Tid");
// if (position < 0) {
// Object[] variables = new Object[] { url };
//
// throw new
// UnsupportedOperationException(this.localization.getString("years_is_not_supported_for_url",
// variables));
// }
//
// JsonNode jsonYears = values.get(position);
//
// List<String> years = new ArrayList<String>(jsonYears.size());
// for (int j = 0; j < jsonYears.size(); j++) {
// years.add(jsonYears.get(j).asText());
// }
//
// return years;
}

// private void validateLocale() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public PopulationClient population() {
* the table to fetch data from
* @return a JSON string containing all available data in the specified table
*
* @see com.github.dannil.scbjavaclient.utility.JsonUtility#getContentCodes(String)
* JsonUtility#getContentsCodes(String)
* @see com.github.dannil.scbjavaclient.utility.JsonUtility#getValues(String, String)
* JsonUtility#getValues(String, String)
*/
public String getRawData(String table) {
String json = super.get(table);

Map<String, Collection<?>> inputs = new HashMap<String, Collection<?>>();
inputs.put("ContentsCode", JsonUtility.getContentCodes(json));
inputs.put("ContentsCode", JsonUtility.getValues(json, "ContentsCode"));

return getRawData(table, inputs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.environment.landandwaterarea;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractRegionYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for area data.
Expand Down Expand Up @@ -118,12 +121,13 @@ public String toString() {
}

/**
* Get the codes for the area model from the API.
* Get the available codes and their respective values for the area data from the API.
*
* @return a list of codes that is used by the API to index the values
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("MI/MI0802/Areal2012"));
public static Map<String, List<String>> getInputs() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("MI/MI0802/Areal2012"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.population.demography;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractRegionYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for fertility rate.
Expand Down Expand Up @@ -118,12 +121,13 @@ public String toString() {
}

/**
* Get the codes for the fertility rate from the API.
*
* @return a list of codes that is used by the API to index the values
* Get the available codes and their respective values for the fertility rate data from the API.
*
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("BE/BE0701/FruktsamhetSumNy"));
public static Map<String, List<String>> getInputs() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("BE/BE0701/FruktsamhetSumNy"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.population.demography;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractRegionYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for mean age first child data.
Expand Down Expand Up @@ -118,12 +121,14 @@ public String toString() {
}

/**
* Get the codes for the mean age first child model from the API.
* Get the available codes and their respective values for the mean age first child data from
* the API.
*
* @return a list of codes that is used by the API to index the values
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("BE/BE0701/MedelAlderNY"));
public static Map<String, List<String>> getCodes() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("BE/BE0701/MedelAlderNY"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.population.name;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for number of children born with first name.
Expand Down Expand Up @@ -114,12 +117,14 @@ public String toString() {
}

/**
* Get the codes for the number of children born with first name from the API.
*
* @return a list of codes that is used by the API to index the values
* Get the available codes and their respective values for the number of children born with
* first name data from the API.
*
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("BE/BE0001/BE0001T04Ar"));
public static Map<String, List<String>> getCodes() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("BE/BE0001/BE0001T04Ar"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.population.statistic;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractRegionYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for average age.
Expand Down Expand Up @@ -118,12 +121,13 @@ public String toString() {
}

/**
* Get the codes for the average age from the API.
* Get the available codes and their respective values for the average age data from the API.
*
* @return a list of codes that is used by the API to index the values
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("BE/BE0101/BE0101B/BefolkningMedelAlder"));
public static Map<String, List<String>> getInputs() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("BE/BE0101/BE0101B/BefolkningMedelAlder"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.dannil.scbjavaclient.model.population.statistic;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.dannil.scbjavaclient.model.AbstractRegionYearAndValueModel;
import com.github.dannil.scbjavaclient.model.ValueNode;
import com.github.dannil.scbjavaclient.utility.JsonUtility;
import com.github.dannil.scbjavaclient.utility.requester.AbstractRequester;
import com.github.dannil.scbjavaclient.utility.requester.RequestMethod;
import com.github.dannil.scbjavaclient.utility.requester.RequesterFactory;

/**
* Model for live births data.
Expand Down Expand Up @@ -146,12 +149,13 @@ public String toString() {
}

/**
* Get the codes for the live births from the API.
*
* @return a list of codes that is used by the API to index the values
* Get the available codes and their respective values for the live birth data from the API.
*
* @return a list of the available codes and their values
*/
public static List<String> getCodes() {
return JsonUtility.getCodes(AbstractRequester.getCodes("BE/BE0101/BE0101H/FoddaK"));
public static Map<String, List<String>> getCodes() {
AbstractRequester get = RequesterFactory.getRequester(RequestMethod.GET);
return JsonUtility.getInputs(get.getBodyAsStringFromTable("BE/BE0101/BE0101H/FoddaK"));
}

}
Loading