com.belladati.sdk.impl.TableViewImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-android Show documentation
Show all versions of sdk-android Show documentation
The BellaDati SDK allows accessing a BellaDati server from 3rd-party applications using Java. This project contains the implementation for Android.
package com.belladati.sdk.impl;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import com.belladati.httpclientandroidlib.client.utils.URIBuilder;
import com.belladati.sdk.exception.InternalConfigurationException;
import com.belladati.sdk.filter.Filter;
import com.belladati.sdk.view.TableView;
import com.fasterxml.jackson.databind.JsonNode;
class TableViewImpl extends ViewImpl implements TableView {
TableViewImpl(BellaDatiServiceImpl service, JsonNode node) throws UnknownViewTypeException {
super(service, node);
}
@Override
public Table loadContent(Filter>... filters) {
return (Table) super.loadContent(filters);
}
@Override
public Table loadContent(Collection> filters) {
return (Table) super.loadContent(filters);
}
static class TableImpl implements Table {
private final BellaDatiServiceImpl service;
private final String id;
private final int rowCount;
private final int columnCount;
private final int leftHeaderColumnCount;
private final int topHeaderRowCount;
private final Collection> filters;
TableImpl(BellaDatiServiceImpl service, String id, JsonNode node) {
this(service, id, node, Collections.> emptyList());
}
TableImpl(BellaDatiServiceImpl service, String id, JsonNode node, Collection> filters) {
this.service = service;
this.id = id;
this.filters = filters;
this.rowCount = node.hasNonNull("rowsCount") ? node.get("rowsCount").asInt() : 0;
this.columnCount = node.hasNonNull("columnsCount") ? node.get("columnsCount").asInt() : 0;
this.leftHeaderColumnCount = node.hasNonNull("leftHeaderColumnsCount") ? node.get("leftHeaderColumnsCount").asInt()
: 0;
this.topHeaderRowCount = node.hasNonNull("topHeaderRowsCount") ? node.get("topHeaderRowsCount").asInt() : 0;
}
@Override
public int getRowCount() {
return rowCount;
}
@Override
public int getColumnCount() {
return columnCount;
}
@Override
public boolean hasLeftHeader() {
return leftHeaderColumnCount > 0;
}
@Override
public boolean hasTopHeader() {
return topHeaderRowCount > 0;
}
@Override
public JsonNode loadLeftHeader(int startRow, int endRow) throws IllegalArgumentException {
if (startRow < 0) {
throw new IllegalArgumentException("First row must be > 0");
}
if (endRow > getRowCount()) {
throw new IllegalArgumentException("Last row must be <= row count");
}
if (startRow > endRow) {
throw new IllegalArgumentException("First row must be <= last row");
}
try {
URIBuilder builder = new URIBuilder("api/reports/views/" + id + "/table/leftHeader");
builder.addParameter("rowsFrom", "" + startRow);
builder.addParameter("rowsTo", "" + endRow);
service.appendFilter(builder, filters);
return service.loadJson(builder.build().toString());
} catch (URISyntaxException e) {
throw new InternalConfigurationException(e);
}
}
@Override
public JsonNode loadTopHeader(int startColumn, int endColumn) throws IllegalArgumentException {
if (startColumn < 0) {
throw new IllegalArgumentException("First column must be > 0");
}
if (endColumn > getColumnCount()) {
throw new IllegalArgumentException("Last column must be <= column count");
}
if (startColumn > endColumn) {
throw new IllegalArgumentException("First column must be <= last column");
}
try {
URIBuilder builder = new URIBuilder("api/reports/views/" + id + "/table/topHeader");
builder.addParameter("columnsFrom", "" + startColumn);
builder.addParameter("columnsTo", "" + endColumn);
service.appendFilter(builder, filters);
return service.loadJson(builder.build().toString());
} catch (URISyntaxException e) {
throw new InternalConfigurationException(e);
}
}
@Override
public JsonNode loadData(int startRow, int endRow, int startColumn, int endColumn) throws IllegalArgumentException {
if (startRow < 0) {
throw new IllegalArgumentException("First row must be > 0");
}
if (endRow > getRowCount()) {
throw new IllegalArgumentException("Last row must be <= row count");
}
if (startRow > endRow) {
throw new IllegalArgumentException("First row must be <= last row");
}
if (startColumn < 0) {
throw new IllegalArgumentException("First column must be > 0");
}
if (endColumn > getColumnCount()) {
throw new IllegalArgumentException("Last column must be <= column count");
}
if (startColumn > endColumn) {
throw new IllegalArgumentException("First column must be <= last column");
}
try {
URIBuilder builder = new URIBuilder("api/reports/views/" + id + "/table/data");
builder.addParameter("rowsFrom", "" + startRow);
builder.addParameter("rowsTo", "" + endRow);
builder.addParameter("columnsFrom", "" + startColumn);
builder.addParameter("columnsTo", "" + endColumn);
service.appendFilter(builder, filters);
return service.loadJson(builder.build().toString());
} catch (URISyntaxException e) {
throw new InternalConfigurationException(e);
}
}
@Override
public String toString() {
return "Table(id: " + id + ")";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TableImpl) {
return id.equals(((TableImpl) obj).id) && filters.equals(((TableImpl) obj).filters);
}
return false;
}
@Override
public int hashCode() {
return id.hashCode();
}
}
}