/backend/src/main/java/com/gooddata/integration/rest/GdcRESTApiWrapper.java
Java | 3612 lines | 2682 code | 258 blank | 672 comment | 638 complexity | bb3f18c8650bfde5a04dfb923f1c73d6 MD5 | raw file
Possible License(s): BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /*
- * Copyright (c) 2009, GoodData Corporation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification, are permitted provided
- * that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice, this list of conditions and
- * the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
- * and the following disclaimer in the documentation and/or other materials provided with the distribution.
- * * Neither the name of the GoodData Corporation nor the names of its contributors may be used to endorse
- * or promote products derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package com.gooddata.integration.rest;
- import com.gooddata.Constants;
- import com.gooddata.exception.*;
- import com.gooddata.integration.model.Column;
- import com.gooddata.integration.model.Project;
- import com.gooddata.integration.model.SLI;
- import com.gooddata.integration.rest.configuration.NamePasswordConfiguration;
- import com.gooddata.util.FileUtil;
- import com.gooddata.util.NetUtil;
- import net.sf.json.JSON;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpMethod;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.cookie.CookiePolicy;
- import org.apache.commons.httpclient.methods.DeleteMethod;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.PutMethod;
- import org.apache.commons.lang.StringUtils;
- import org.apache.log4j.Logger;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * The GoodData REST API Java wrapper.
- *
- * @author Zdenek Svoboda <zd@gooddata.org>
- * @version 1.0
- */
- public class GdcRESTApiWrapper {
- private static Logger l = Logger.getLogger(GdcRESTApiWrapper.class);
- /**
- * GDC URIs
- */
- private static final String PLATFORM_URI = "/gdc/";
- private static final String MD_URI = "/gdc/md/";
- private static final String LOGIN_URI = "/gdc/account/login";
- private static final String DOMAIN_URI = "/gdc/account/domains";
- private static final String DOMAIN_USERS_SUFFIX = "/users";
- private static final String PROJECT_USERS_SUFFIX = "/users";
- private static final String PROJECT_ROLES_SUFFIX = "/roles";
- private static final String TOKEN_URI = "/gdc/account/token";
- private static final String DATA_INTERFACES_URI = "/ldm/singleloadinterface";
- private static final String PROJECTS_URI = "/gdc/projects";
- private static final String PULL_URI = "/etl/pull";
- private static final String IDENTIFIER_URI = "/identifiers";
- private static final String SLI_DESCRIPTOR_URI = "/descriptor";
- public static final String MAQL_EXEC_URI = "/ldm/manage";
- public static final String MAQL_ASYNC_EXEC_URI = "/ldm/manage2";
- public static final String DML_EXEC_URI = "/dml/manage";
- public static final String PROJECT_EXPORT_URI = "/maintenance/export";
- public static final String PROJECT_IMPORT_URI = "/maintenance/import";
- public static final String PROJECT_PARTIAL_EXPORT_URI = "/maintenance/partialmdexport";
- public static final String PROJECT_PARTIAL_IMPORT_URI = "/maintenance/partialmdimport";
- public static final String REPORT_QUERY = "/query/reports";
- public static final String ATTR_QUERY = "/query/attributes";
- public static final String EXECUTOR = "/gdc/xtab2/executor3";
- public static final String EXPORT_EXECUTOR = "/gdc/exporter/executor";
- public static final String INVITATION_URI = "/invitations";
- public static final String ETL_MODE_URI = "/etl/mode";
- public static final String OBJ_URI = "/obj";
- public static final String ROLES_URI = "/roles";
- public static final String USERS_URI = "/users";
- public static final String ETL_MODE_DLI = "DLI";
- public static final String ETL_MODE_VOID = "VOID";
- public static final String LINKS_UPLOADS_KEY = "uploads";
- public static final String DLI_MANIFEST_FILENAME = "upload_info.json";
-
- public static final String QUERY_PROJECTDASHBOARDS = "projectdashboards";
- public static final String QUERY_FOLDERS = "folders";
- public static final String QUERY_DATASETS = "datasets";
- public static final String QUERY_DIMENSIONS = "dimensions";
- public static final String QUERY_PREFIX = "/query/";
- protected HttpClient client;
- protected NamePasswordConfiguration config;
- private JSONObject userLogin = null;
- private JSONObject profile;
- private static HashMap<String, String> ROLES = new HashMap<String, String>();
-
- private static final Pattern NEWLINE_SEPARATOR_PARSER = Pattern.compile( "\n" );
- /* TODO This is fragile and may not work for all projects and/or future versions.
- * Use /gdc/projects/{projectId}/roles to retrieve roles for a particular project.
- */
- static {
- ROLES.put("ADMIN", "adminRole");
- ROLES.put("EDITOR", "editorRole");
- ROLES.put("DASHBOARD ONLY", "dashboardOnlyRole");
- ROLES.put("UNVERIFIED ADMIN", "unverifiedAdminRole");
- ROLES.put("READONLY", "readOnlyUserRole");
- }
- /**
- * Constructs the GoodData REST API Java wrapper
- *
- * @param config NamePasswordConfiguration object with the GDC name and password configuration
- */
- public GdcRESTApiWrapper(NamePasswordConfiguration config) {
- this.config = config;
- client = new HttpClient();
- NetUtil.configureHttpProxy(client);
- }
- /**
- * GDC login - obtain GDC SSToken
- *
- * @throws HttpMethodException
- */
- public void login() throws HttpMethodException {
- //logout();
- l.debug("Logging into GoodData.");
- JSONObject loginStructure = getLoginStructure();
- PostMethod loginPost = createPostMethod(getServerUrl() + LOGIN_URI);
- InputStreamRequestEntity request = new InputStreamRequestEntity(new ByteArrayInputStream(loginStructure.toString().getBytes()));
- loginPost.setRequestEntity(request);
- try {
- String resp = executeMethodOk(loginPost, false); // do not re-login on SC_UNAUTHORIZED
- // enabling this prevents the following message:
- // WARN org.apache.commons.httpclient.HttpMethodDirector -
- // Unable to respond to any of these challenges:
- // {gooddata=GoodData realm="GoodData API" cookie=GDCAuthTT}
- // appearing always after those:
- // DEBUG com.gooddata.integration.rest.GdcRESTApiWrapper -
- // Logging into GoodData.
- // DEBUG com.gooddata.integration.rest.GdcRESTApiWrapper -
- // Successfully logged into GoodData.
- setTokenCookie();
- l.debug("Successfully logged into GoodData.");
- JSONObject rsp = JSONObject.fromObject(resp);
- userLogin = rsp.getJSONObject("userLogin");
- String profileUri = userLogin.getString("profile");
- if (profileUri != null && profileUri.length() > 0) {
- GetMethod gm = createGetMethod(getServerUrl() + profileUri);
- try {
- resp = executeMethodOk(gm);
- this.profile = JSONObject.fromObject(resp);
- }
- finally {
- gm.releaseConnection();
- }
- } else {
- l.debug("Empty account profile.");
- throw new GdcRestApiException("Empty account profile.");
- }
- } finally {
- loginPost.releaseConnection();
- }
- }
- /**
- * Creates a new login JSON structure
- *
- * @return the login JSON structure
- */
- private JSONObject getLoginStructure() {
- JSONObject credentialsStructure = new JSONObject();
- credentialsStructure.put("login", config.getUsername());
- credentialsStructure.put("password", config.getPassword());
- credentialsStructure.put("remember", 1);
- JSONObject loginStructure = new JSONObject();
- loginStructure.put("postUserLogin", credentialsStructure);
- return loginStructure;
- }
- /**
- * Sets the SS token
- *
- * @throws HttpMethodException
- */
- private void setTokenCookie() throws HttpMethodException {
- HttpMethod secutityTokenGet = createGetMethod(getServerUrl() + TOKEN_URI);
- try {
- executeMethodOk(secutityTokenGet);
- } finally {
- secutityTokenGet.releaseConnection();
- }
- }
- /**
- * GDC logout - remove active session, if any exists
- *
- * @throws HttpMethodException
- */
- public void logout() throws HttpMethodException {
- if (userLogin == null)
- return;
- l.debug("Logging out.");
- DeleteMethod logoutDelete = createDeleteMethod(getServerUrl() + userLogin.getString("state"));
- try {
- String resp = executeMethodOk(logoutDelete, false); // do not re-login on SC_UNAUTHORIZED
- userLogin = null;
- profile = null;
- l.debug("Successfully logged out.");
- } finally {
- logoutDelete.releaseConnection();
- }
- this.client = new HttpClient();
- NetUtil.configureHttpProxy( client );
- }
- /**
- * Retrieves the project info by the project's ID
- *
- * @param id the project id
- * @return the GoodDataProjectInfo populated with the project's information
- * @throws HttpMethodException
- * @throws GdcProjectAccessException
- */
- public Project getProjectById(String id) throws HttpMethodException, GdcProjectAccessException {
- l.debug("Getting project by id=" + id);
- HttpMethod req = createGetMethod(getServerUrl() + PROJECTS_URI + "/" + id);
- try {
- String resp = executeMethodOk(req);
- JSONObject parsedResp = JSONObject.fromObject(resp);
- if(parsedResp != null && !parsedResp.isEmpty() && !parsedResp.isNullObject()) {
- JSONObject project = parsedResp.getJSONObject("project");
- if(project != null && !project.isEmpty() && !project.isNullObject()) {
- JSONObject meta = project.getJSONObject("meta");
- String title = meta.getString("title");
- if(title != null && title.length() > 0)
- return new Project(MD_URI + "/" + id, id, title);
- else
- throw new InvalidArgumentException("getProjectById: The project structure doesn't contain the title key.");
- }
- else {
- throw new InvalidArgumentException("getProjectById: The project structure doesn't contain the project key.");
- }
- } else {
- throw new InvalidArgumentException("getProjectById: Invalid response.");
- }
- } catch (HttpMethodException e) {
- l.debug("The project id=" + id + " doesn't exists.");
- throw new GdcProjectAccessException("The project id=" + id + " doesn't exists.");
- } finally {
- req.releaseConnection();
- }
- }
- /**
- * Returns the global platform links
- *
- * @return accessible platform links
- * @throws com.gooddata.exception.HttpMethodException
- *
- */
- @SuppressWarnings("unchecked")
- private Iterator<JSONObject> getPlatformLinks() throws HttpMethodException {
- l.debug("Getting project links.");
- HttpMethod req = createGetMethod(getServerUrl() + PLATFORM_URI);
- try {
- String resp = executeMethodOk(req);
- JSONObject parsedResp = JSONObject.fromObject(resp);
- JSONObject about = parsedResp.getJSONObject("about");
- JSONArray links = about.getJSONArray("links");
- l.debug("Got platform links " + links);
- return links.iterator();
- } finally {
- req.releaseConnection();
- }
- }
- /**
- *
- *
- * @return the WebDav URL from the platform configuration
- */
- public URL getWebDavURL() {
- Iterator<JSONObject> links = getPlatformLinks();
- while(links.hasNext()) {
- JSONObject link = links.next();
- if(link != null && !link.isEmpty() && !link.isNullObject()) {
- String category = link.getString("category");
- if(category != null && category.length() > 0 && category.equalsIgnoreCase(LINKS_UPLOADS_KEY)) {
- try {
- String uri = link.getString("link");
- if(uri != null && uri.length()>0) {
- if(uri.startsWith("/")) {
- uri = getServerUrl() + uri;
- }
- return new URL(uri);
- }
- else {
- throw new InvalidArgumentException("No uploads URL configured for the server: "+category);
- }
- }
- catch (MalformedURLException e) {
- throw new InvalidArgumentException("Invalid uploads URL configured for the server: "+category);
- }
- }
- }
- }
- throw new InvalidArgumentException("No uploads platform link configured for the GoodData cluster.");
- }
- /**
- * Returns a list of project's SLIs
- *
- * @param projectId project's ID
- * @return a list of project's SLIs
- * @throws HttpMethodException if there is a communication error
- * @throws GdcProjectAccessException if the SLI doesn't exist
- */
- public List<SLI> getSLIs(String projectId) throws HttpMethodException, GdcProjectAccessException {
- l.debug("Getting SLIs from project id=" + projectId);
- List<SLI> list = new ArrayList<SLI>();
- String ifcUri = getSLIsUri(projectId);
- HttpMethod interfacesGet = createGetMethod(ifcUri);
- try {
- String response = executeMethodOk(interfacesGet);
- JSONObject responseObject = JSONObject.fromObject(response);
- if (responseObject.isNullObject()) {
- l.debug("The project id=" + projectId + " doesn't exist!");
- throw new GdcProjectAccessException("The project id=" + projectId + " doesn't exist!");
- }
- JSONObject interfaceQuery = responseObject.getJSONObject("about");
- if (interfaceQuery.isNullObject()) {
- l.debug("The project id=" + projectId + " doesn't exist!");
- throw new GdcProjectAccessException("The project id=" + projectId + " doesn't exist!");
- }
- JSONArray links = interfaceQuery.getJSONArray("links");
- if (links == null) {
- l.debug("The project id=" + projectId + " doesn't exist!");
- throw new GdcProjectAccessException("The project id=" + projectId + " doesn't exist!");
- }
- for (Object ol : links) {
- JSONObject link = (JSONObject) ol;
- SLI ii = new SLI(link);
- list.add(ii);
- }
- l.debug("Got SLIs " + list + " from project id=" + projectId);
- } finally {
- interfacesGet.releaseConnection();
- }
- return list;
- }
- /**
- * Retrieves the SLI columns
- *
- * @param uri the SLI uri
- * @return list of SLI columns
- * @throws GdcProjectAccessException if the SLI doesn't exist
- * @throws HttpMethodException if there is a communication issue with the GDC platform
- */
- public List<Column> getSLIColumns(String uri) throws GdcProjectAccessException, HttpMethodException {
- l.debug("Retrieveing SLI columns for SLI uri=" + uri);
- List<Column> list = new ArrayList<Column>();
- HttpMethod sliGet = createGetMethod(getServerUrl() + uri + "/manifest");
- try {
- String response = executeMethodOk(sliGet);
- JSONObject responseObject = JSONObject.fromObject(response);
- if (responseObject.isNullObject()) {
- l.debug("The SLI uri=" + uri + " doesn't exist!");
- throw new GdcProjectAccessException("The SLI uri=" + uri + " doesn't exist!");
- }
- JSONObject dataSetSLIManifest = responseObject.getJSONObject("dataSetSLIManifest");
- if (dataSetSLIManifest.isNullObject()) {
- l.debug("The SLI uri=" + uri + " doesn't exist!");
- throw new GdcProjectAccessException("The SLI uri=" + uri + " doesn't exist!");
- }
- JSONArray parts = dataSetSLIManifest.getJSONArray("parts");
- for (Object oPart : parts) {
- list.add(new Column((JSONObject) oPart));
- }
- } finally {
- sliGet.releaseConnection();
- }
- return list;
- }
- /**
- * Retrieves the SLI column data type
- *
- * @param projectId projectId
- * @param sliColumnIdentifier SLI column identifier (name in the SLI manifest)
- * @return the SLI column datatype
- */
- public String getSLIColumnDataType(String projectId, String sliColumnIdentifier) {
- l.debug("Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier);
- MetadataObject o = getMetadataObject(projectId, sliColumnIdentifier);
- if (o != null) {
- JSONObject c = o.getContent();
- if (c != null) {
- String type = c.getString("columnType");
- if (type != null && type.length() > 0) {
- return type;
- } else {
- l.debug("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " No columnType key in the content.");
- throw new GdcRestApiException("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " No columnType key in the content.");
- }
- } else {
- l.debug("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " No content structure.");
- throw new GdcRestApiException("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " No content structure.");
- }
- } else {
- l.debug("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " MD object doesn't exist.");
- throw new GdcRestApiException("Error Retrieveing SLI column datatype projectId=" + projectId + " SLI column name=" + sliColumnIdentifier + " MD object doesn't exist.");
- }
- }
- /**
- * Retrieves the SLI columns
- *
- * @param uri the SLI uri
- * @return JSON manifest
- * @throws GdcProjectAccessException if the SLI doesn't exist
- * @throws HttpMethodException if there is a communication issue with the GDC platform
- */
- public JSONObject getSLIManifest(String uri) throws GdcProjectAccessException, HttpMethodException {
- l.debug("Retrieveing SLI columns for SLI uri=" + uri);
- List<Column> list = new ArrayList<Column>();
- HttpMethod sliGet = createGetMethod(getServerUrl() + uri + "/manifest");
- try {
- String response = executeMethodOk(sliGet);
- JSONObject responseObject = JSONObject.fromObject(response);
- if (responseObject.isNullObject()) {
- l.debug("The SLI uri=" + uri + " doesn't exist!");
- throw new GdcProjectAccessException("The SLI uri=" + uri + " doesn't exist!");
- }
- return responseObject;
- } finally {
- sliGet.releaseConnection();
- }
- }
- /**
- * Finds a project SLI by it's id
- *
- * @param id the SLI id
- * @param projectId the project id
- * @return the SLI
- * @throws GdcProjectAccessException if the SLI doesn't exist
- * @throws HttpMethodException if there is a communication issue with the GDC platform
- */
- public SLI getSLIById(String id, String projectId) throws GdcProjectAccessException, HttpMethodException {
- l.debug("Get SLI by id=" + id + " project id=" + projectId);
- List<SLI> slis = getSLIs(projectId);
- return getSLIById(id, slis, projectId);
- }
- /**
- * Finds a project SLI in list of SLI
- *
- * @param id the SLI id
- * @param slis of SLI (related to one project)
- * @param projectId the project id
- * @return the SLI
- * @throws GdcProjectAccessException if the SLI doesn't exist
- */
- public static SLI getSLIById(String id, List<SLI> slis, String projectId) throws GdcProjectAccessException {
- l.debug("Get SLI by id=" + id + " project id=" + projectId);
- for (SLI sli : slis) {
- if (id.equals(sli.getId())) {
- l.debug("Got SLI by id=" + id + " project id=" + projectId);
- return sli;
- }
- }
- l.debug("The SLI id=" + id + " doesn't exist in the project id=" + projectId);
- throw new GdcProjectAccessException("The SLI id=" + id + " doesn't exist in the project id=" + projectId);
- }
- /**
- * Enumerates all attributes in the project
- *
- * @param projectId project Id
- * @return LIst of attr uris
- */
- public List<String> enumerateAttributes(String projectId) {
- l.debug("Enumerating attributes for project id=" + projectId);
- List<String> list = new ArrayList<String>();
- String qUri = getProjectMdUrl(projectId) + ATTR_QUERY;
- HttpMethod qGet = createGetMethod(qUri);
- try {
- String qr = executeMethodOk(qGet);
- JSONObject q = JSONObject.fromObject(qr);
- if (q.isNullObject()) {
- l.debug("Enumerating attributes for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating attributes for project id=" + projectId + " failed.");
- }
- JSONObject qry = q.getJSONObject("query");
- if (qry.isNullObject()) {
- l.debug("Enumerating attributes for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating reports for project id=" + projectId + " failed.");
- }
- JSONArray entries = qry.getJSONArray("entries");
- if (entries == null) {
- l.debug("Enumerating attributes for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating reports for project id=" + projectId + " failed.");
- }
- for (Object oentry : entries) {
- JSONObject entry = (JSONObject) oentry;
- list.add(entry.getString("link"));
- }
- } finally {
- qGet.releaseConnection();
- }
- return list;
- }
- /**
- * Gets attribute PK
- *
- * @param attrUri attribute URI
- * @return list of attribute PKs (columns)
- */
- public List<JSONObject> getAttributePk(String attrUri) {
- List<JSONObject> ret = new ArrayList<JSONObject>();
- JSONObject attr = getObjectByUri(attrUri);
- JSONObject a = attr.getJSONObject("attribute");
- if (a != null && !a.isEmpty() && !a.isEmpty()) {
- JSONObject c = a.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- JSONArray pks = c.getJSONArray("pk");
- if (pks != null && !pks.isEmpty()) {
- Object[] p = pks.toArray();
- for (Object pko : p) {
- JSONObject pk = (JSONObject) pko;
- String columnUri = pk.getString("data");
- if (columnUri != null) {
- ret.add(getObjectByUri(columnUri));
- } else {
- l.debug("Error getting attribute PK. No PK data.");
- throw new GdcProjectAccessException("Error getting attribute PK. No PK data.");
- }
- }
- }
- } else {
- l.debug("Error getting attribute PK. No content.");
- throw new GdcProjectAccessException("Error getting attribute PK. No content.");
- }
- } else {
- l.debug("Error getting attribute PK. No attribute.");
- throw new GdcProjectAccessException("Error getting attribute PK. No attribute.");
- }
- return ret;
- }
- /**
- * Gets attribute FK
- *
- * @param attrUri attribute URI
- * @return list of attribute FKs (columns)
- */
- public List<JSONObject> getAttributeFk(String attrUri) {
- List<JSONObject> ret = new ArrayList<JSONObject>();
- JSONObject attr = getObjectByUri(attrUri);
- JSONObject a = attr.getJSONObject("attribute");
- if (a != null && !a.isEmpty() && !a.isEmpty()) {
- JSONObject c = a.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- if (c.containsKey("fk")) {
- JSONArray pks = c.getJSONArray("fk");
- if (pks != null && !pks.isEmpty()) {
- Object[] p = pks.toArray();
- for (Object pko : p) {
- JSONObject pk = (JSONObject) pko;
- String columnUri = pk.getString("data");
- if (columnUri != null && columnUri.trim().length() > 0) {
- ret.add(getObjectByUri(columnUri));
- } else {
- l.debug("Error getting attribute FK. No FK data.");
- throw new GdcProjectAccessException("Error getting attribute FK. No FK data.");
- }
- }
- }
- }
- } else {
- l.debug("Error getting attribute FK. No content.");
- throw new GdcProjectAccessException("Error getting attribute FK. No content.");
- }
- } else {
- l.debug("Error getting attribute FK. No attribute.");
- throw new GdcProjectAccessException("Error getting attribute FK. No attribute.");
- }
- return ret;
- }
- /**
- * Gets column DB name
- *
- * @param column column object
- * @return column DB name
- */
- public String getColumnDbName(JSONObject column) {
- JSONObject cl = column.getJSONObject("column");
- if (cl != null && !cl.isEmpty() && !cl.isEmpty()) {
- JSONObject c = cl.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- String cn = c.getString("columnDBName");
- if (cn != null && cn.trim().length() > 0) {
- return cn;
- } else {
- l.debug("Error getting column name. No columnDBName.");
- throw new GdcProjectAccessException("Error getting column name. No columnDBName.");
- }
- } else {
- l.debug("Error getting column name. No content.");
- throw new GdcProjectAccessException("Error getting column name. No content.");
- }
- } else {
- l.debug("Error getting column name. No column.");
- throw new GdcProjectAccessException("Error getting column name. No column.");
- }
- }
- /**
- * Gets column table name
- *
- * @param column column object
- * @return column table name
- */
- public String getColumnTableName(JSONObject column) {
- JSONObject cl = column.getJSONObject("column");
- if (cl != null && !cl.isEmpty() && !cl.isEmpty()) {
- JSONObject c = cl.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- String t = c.getString("table");
- if (t != null && t.trim().length() > 0) {
- JSONObject tbl = getObjectByUri(t);
- JSONObject root = tbl.getJSONObject("table");
- if (root != null && !root.isEmpty() && !root.isEmpty()) {
- c = root.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- String dl = c.getString("activeDataLoad");
- if (dl != null && dl.trim().length() > 0) {
- JSONObject tdl = getObjectByUri(dl);
- root = tdl.getJSONObject("tableDataLoad");
- if (root != null && !root.isEmpty() && !root.isEmpty()) {
- c = root.getJSONObject("content");
- if (c != null && !c.isEmpty() && !c.isEmpty()) {
- String tn = c.getString("dataSourceLocation");
- if (tn != null && tn.trim().length() > 0) {
- return tn;
- } else {
- l.debug("Error getting column name. No dataSourceLocation.");
- throw new GdcProjectAccessException("Error getting column name. No dataSourceLocation.");
- }
- } else {
- l.debug("Error getting column name. No active table data load content.");
- throw new GdcProjectAccessException("Error getting column name. No active table data load content.");
- }
- } else {
- l.debug("Error getting column name. No table data load root.");
- throw new GdcProjectAccessException("Error getting column name. No table data load root.");
- }
- } else {
- l.debug("Error getting column name. No active data load.");
- throw new GdcProjectAccessException("Error getting column name. No active data load.");
- }
- } else {
- l.debug("Error getting column name. No table content.");
- throw new GdcProjectAccessException("Error getting column name. No table content.");
- }
- } else {
- l.debug("Error getting column table. No table root.");
- throw new GdcProjectAccessException("Error getting column table. No table root.");
- }
- } else {
- l.debug("Error getting column name. No table.");
- throw new GdcProjectAccessException("Error getting column name. No table.");
- }
- } else {
- l.debug("Error getting column name. No content.");
- throw new GdcProjectAccessException("Error getting column name. No content.");
- }
- } else {
- l.debug("Error getting column name. No column.");
- throw new GdcProjectAccessException("Error getting column name. No column.");
- }
- }
- /**
- * Enumerates all attributes in the project
- *
- * @param attrUri attribute URI
- * @return attribute object
- */
- public JSONObject getAttribute(String attrUri) {
- l.debug("Getting attribute uri=" + attrUri);
- String qUri = getServerUrl() + attrUri;
- HttpMethod qGet = createGetMethod(qUri);
- try {
- String qr = executeMethodOk(qGet);
- return JSONObject.fromObject(qr);
- } finally {
- qGet.releaseConnection();
- }
- }
- /**
- * Enumerates all reports on in a project
- *
- * @param projectId project Id
- * @return LIst of report uris
- */
- public List<String> enumerateReports(String projectId) {
- l.debug("Enumerating reports for project id=" + projectId);
- List<String> list = new ArrayList<String>();
- String qUri = getProjectMdUrl(projectId) + REPORT_QUERY;
- HttpMethod qGet = createGetMethod(qUri);
- try {
- String qr = executeMethodOk(qGet);
- JSONObject q = JSONObject.fromObject(qr);
- if (q.isNullObject()) {
- l.debug("Enumerating reports for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating reports for project id=" + projectId + " failed.");
- }
- JSONObject qry = q.getJSONObject("query");
- if (qry.isNullObject()) {
- l.debug("Enumerating reports for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating reports for project id=" + projectId + " failed.");
- }
- JSONArray entries = qry.getJSONArray("entries");
- if (entries == null) {
- l.debug("Enumerating reports for project id=" + projectId + " failed.");
- throw new GdcProjectAccessException("Enumerating reports for project id=" + projectId + " failed.");
- }
- for (Object oentry : entries) {
- JSONObject entry = (JSONObject) oentry;
- int deprecated = entry.getInt("deprecated");
- if (deprecated == 0)
- list.add(entry.getString("link"));
- }
- } finally {
- qGet.releaseConnection();
- }
- return list;
- }
- private String getProjectIdFromObjectUri(String uri) {
- Pattern regexp = Pattern.compile("gdc/md/.*?/");
- Matcher m = regexp.matcher(uri);
- if (m.find()) {
- return m.group().split("/")[2];
- } else {
- l.debug("The passed string '" + uri + "' doesn't have the GoodData URI structure!");
- throw new InvalidParameterException("The passed string '" + uri + "' doesn't have the GoodData URI structure!");
- }
- }
- /**
- * Computes the metric value
- *
- * @param metricUri metric URI
- * @return the metric value
- */
- public double computeMetric(String metricUri) {
- l.debug("Computing metric uri=" + metricUri);
- double retVal = 0;
- String projectId = getProjectIdFromObjectUri(metricUri);
- JSONObject reportDefinition = new JSONObject();
- JSONObject metric = new JSONObject();
- metric.put("alias", "");
- metric.put("uri", metricUri);
- JSONArray metrics = new JSONArray();
- metrics.add(metric);
- JSONArray columns = new JSONArray();
- columns.add("metricGroup");
- JSONObject grid = new JSONObject();
- grid.put("metrics", metrics);
- grid.put("columns", columns);
- grid.put("rows", new JSONArray());
- grid.put("columnWidths", new JSONArray());
- JSONObject sort = new JSONObject();
- sort.put("columns", new JSONArray());
- sort.put("rows", new JSONArray());
- grid.put("sort", sort);
- JSONObject content = new JSONObject();
- content.put("grid", grid);
- content.put("filters", new JSONArray());
- content.put("format", "grid");
- reportDefinition.put("content", content);
- JSONObject meta = new JSONObject();
- meta.put("category", "reportDefinition");
- meta.put("title", "N/A");
- reportDefinition.put("meta", meta);
- MetadataObject obj = new MetadataObject();
- obj.put("reportDefinition", reportDefinition);
- MetadataObject resp = new MetadataObject(createMetadataObject(projectId, obj));
- int retryCnt = Constants.MAX_RETRY;
- boolean hasFinished = false;
- while (retryCnt-- > 0 && !hasFinished) {
- try {
- String dataResultUri = executeReportDefinition(resp.getUri());
- JSONObject result = getObjectByUri(dataResultUri);
- hasFinished = true;
- if (result != null && !result.isEmpty() && !result.isNullObject()) {
- JSONObject xtabData = result.getJSONObject("xtab_data");
- if (xtabData != null && !xtabData.isEmpty() && !xtabData.isNullObject()) {
- JSONArray data = xtabData.getJSONArray("data");
- if (data != null && !data.isEmpty()) {
- retVal = data.getJSONArray(0).getDouble(0);
- } else {
- l.debug("Can't compute the metric. No data structure in result.");
- throw new InvalidParameterException("Can't compute the metric. No data structure in result.");
- }
- } else {
- l.debug("Can't compute the metric. No xtab_data structure in result.");
- throw new InvalidParameterException("Can't compute the metric. No xtab_data structure in result.");
- }
- } else {
- l.debug("Can't compute the metric. No result from XTAB.");
- throw new InvalidParameterException("Can't compute the metric. No result from XTAB.");
- }
- } catch (HttpMethodNotFinishedYetException e) {
- l.debug("computeMetric: Waiting for DataResult");
- try {
- Thread.sleep(Constants.POLL_INTERVAL);
- } catch (InterruptedException ex) {
- // do nothing
- }
- }
- }
- l.debug("Metric uri=" + metricUri + " computed. Result is " + retVal);
- return retVal;
- }
- /**
- * Computes a simple report and returns the report text
- *
- * @param reportUri report URI
- * @return the report rendered in text
- */
- public String computeReport(String reportUri) {
- l.debug("Computing report uri=" + reportUri);
- String retVal = "";
- int retryCnt = Constants.MAX_RETRY;
- boolean hasFinished = false;
- while (retryCnt-- > 0 && !hasFinished) {
- try {
- String dataResultUri = executeReport(reportUri);
- JSONObject result = getObjectByUri(dataResultUri);
- hasFinished = true;
- if (result != null && !result.isEmpty() && !result.isNullObject()) {
- JSONObject xtabData = result.getJSONObject("xtab_data");
- if (xtabData != null && !xtabData.isEmpty() && !xtabData.isNullObject()) {
- JSONArray data = xtabData.getJSONArray("data");
- if (data != null && !data.isEmpty()) {
- double[] values = new double[data.size()];
- for (int i = 0; i < data.size(); i++) {
- JSONArray vals = data.getJSONArray(i);
- values[i] = vals.getDouble(0);
- }
- JSONObject rows = xtabData.getJSONObject("rows");
- if (rows != null && !rows.isEmpty() && !rows.isNullObject()) {
- JSONArray lookups = rows.getJSONArray("lookups");
- if (lookups != null && !lookups.isEmpty()) {
- Map<String, String> attributes = new HashMap<String, String>();
- JSONObject lkpData = lookups.getJSONObject(0);
- for (Object key : lkpData.keySet()) {
- Object value = lkpData.get(key);
- if (key != null && value != null)
- attributes.put(key.toString(), value.toString());
- }
- JSONObject tree = rows.getJSONObject("tree");
- if (tree != null && !tree.isEmpty() && !tree.isNullObject()) {
- Map<String, Integer> indexes = new HashMap<String, Integer>();
- JSONObject index = tree.getJSONObject("index");
- if (index != null && !index.isEmpty()) {
- for (Object key : index.keySet()) {
- if (key != null) {
- JSONArray valIdxs = index.getJSONArray(key.toString());
- if (valIdxs != null && !valIdxs.isEmpty()) {
- indexes.put(key.toString(), valIdxs.getInt(0));
- }
- }
- }
- JSONArray children = tree.getJSONArray("children");
- if (children != null && !children.isEmpty()) {
- for (int i = 0; i < children.size(); i++) {
- JSONObject c = children.getJSONObject(i);
- String id = c.getString("id");
- if (id != null && id.length() > 0) {
- String attribute = attributes.get(id);
- int v = indexes.get(id);
- double vl = values[v];
- if (retVal.length() > 0) {
- retVal += ", " + attribute + " : " + vl;
- } else {
- retVal += attribute + " : " + vl;
- }
- } else {
- l.debug("Can't compute the report. No id in children.");
- throw new InvalidParameterException("Can't compute the report. No id in children.");
- }
- }
- } else {
- l.debug("Can't compute the report. No tree structure in result.");
- throw new InvalidParameterException("Can't compute the report. No tree structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No index structure in result.");
- throw new InvalidParameterException("Can't compute the report. No index structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No tree structure in result.");
- throw new InvalidParameterException("Can't compute the report. No tree structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No lookups structure in result.");
- throw new InvalidParameterException("Can't compute the report. No lookups structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No rows structure in result.");
- throw new InvalidParameterException("Can't compute the report. No rows structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No data structure in result.");
- throw new InvalidParameterException("Can't compute the report. No data structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No xtab_data structure in result.");
- throw new InvalidParameterException("Can't compute the report. No xtab_data structure in result.");
- }
- } else {
- l.debug("Can't compute the report. No result from XTAB.");
- throw new InvalidParameterException("Can't compute the metric. No result from XTAB.");
- }
- } catch (HttpMethodNotFinishedYetException e) {
- l.debug("computeReport: Waiting for DataResult");
- try {
- Thread.sleep(Constants.POLL_INTERVAL);
- } catch (InterruptedException ex) {
- // do nothing
- }
- }
- }
- l.debug("Report uri=" + reportUri + " computed.");
- return retVal;
- }
- /**
- * Report definition to execute
- *
- * @param reportDefUri report definition to execute
- */
- public String executeReportDefinition(String reportDefUri) {
- l.debug("Executing report definition uri=" + reportDefUri);
- PostMethod execPost = createPostMethod(getServerUrl() + EXECUTOR);
- JSONObject execDef = new JSONObject();
- execDef.put("reportDefinition", reportDefUri);
- JSONObject exec = new JSONObject();
- exec.put("report_req", execDef);
- InputStreamRequestEntity request = new InputStreamRequestEntity(new ByteArrayInputStream(exec.toString().getBytes()));
- execPost.setRequestEntity(request);
- try {
- String task = executeMethodOk(execPost);
- if (task != null && task.length() > 0) {
- JSONObject tr = JSONObject.fromObject(task);
- if (tr.isNullObject()) {
- l.debug("Executing report definition uri=" + reportDefUri + " failed. Returned invalid result result=" + tr);
- throw new GdcRestApiException("Executing report definition uri=" + reportDefUri + " failed. " +
- "Returned invalid result result=" + tr);
- }
- JSONObject reportResult = tr.getJSONObject("execResult");
- if (reportResult.isNullObject()) {
- l.debug("Executing report definition uri=" + reportDefUri + " failed. Returned invalid result result=" + tr);
- throw new GdcRestApiException("Executing report definition uri=" + reportDefUri + " failed. " +
- "Returned invalid result result=" + tr);
- }
- String dataResult = reportResult.getString("dataResult");
- if (dataResult == null || dataResult.length()<=0) {
- l.debug("Executing report definition uri=" + reportDefUri + " failed. Returned invalid result result=" + tr);
- throw new GdcRestApiException("Executing report definition uri=" + reportDefUri + " failed. " +
- "Returned invalid result result=" + tr);
- }
- return dataResult;
- } else {
- l.debug("Executing report definition uri=" + reportDefUri + " failed. Returned invalid task link uri=" + task);
- throw new GdcRestApiException("Executing report definition uri=" + reportDefUri +
- " failed. Returned invalid task link uri=" + task);
- }
- } catch (HttpMethodException ex) {
- l.debug("Executing report definition uri=" + reportDefUri + " failed.", ex);
- throw new GdcRestApiException("Executing report definition uri=" + reportDefUri + " failed.");
- } finally {
- execPost.releaseConnection();
- }
- …
Large files files are truncated, but you can click here to view the full file