Java tutorial
package com.tune.reporting.base.endpoints; /** * AdvertiserReportCohortBase.java * * <p> * Copyright (c) 2015 TUNE, Inc. * All rights reserved. * </p> * * <p> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * <p> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * </p> * * <p> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * </p> * * <p> * Java Version 1.6 * </p> * * <p> * @category tune-reporting * @package com.tune.reporting * @author Jeff Tanner jefft@tune.com * @copyright 2015 TUNE, Inc. (http://www.tune.com) * @license http://opensource.org/licenses/MIT The MIT License (MIT) * @version $Date: 2015-04-09 17:36:25 $ * @link https://developers.mobileapptracking.com @endlink * </p> */ import com.tune.reporting.base.service.TuneServiceResponse; import com.tune.reporting.helpers.TuneSdkException; import com.tune.reporting.helpers.TuneServiceException; import org.json.JSONException; import org.json.JSONObject; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * Base class for TUNE Service API reports insights endpoints. */ public abstract class AdvertiserReportCohortBase extends AdvertiserReportBase { /** * Allowed Cohort interval values. */ protected static final Set<String> COHORT_INTERVALS = new HashSet<String>( Arrays.asList("year_day", "year_week", "year_month", "year")); /** * Allowed Cohort types values. */ protected static final Set<String> COHORT_TYPES = new HashSet<String>(Arrays.asList("click", "install")); /** * Allowed aggregation types values. */ protected static final Set<String> AGGREGATION_TYPES = new HashSet<String>( Arrays.asList("incremental", "cumulative")); /** * Constructor. * * @param controller TUNE Service API endpoint name. * @param filterDebugMode Remove debug mode information from results. * @param filterTestProfileId Remove test profile information from results. */ public AdvertiserReportCohortBase(final String controller, final Boolean filterDebugMode, final Boolean filterTestProfileId) throws TuneSdkException { super(controller, filterDebugMode, filterTestProfileId); } /** * Counts all existing records that match filter criteria * and returns an array of found model data. * * @param mapParams Mapping of: <p><dl> * <dt>start_date</dt><dd>YYYY-MM-DD HH:MM:SS</dd> * <dt>end_date</dt><dd>YYYY-MM-DD HH:MM:SS</dd> * <dt>cohort_type</dt><dd>Cohort types: click, install</dd> * <dt>cohort_interval</dt><dd>Cohort intervals: * year_day, year_week, year_month, year</dd> * <dt>group</dt><dd>Group results using this endpoint's fields.</dd> * <dt>filter</dt><dd>Apply constraints based upon values associated with * this endpoint's fields.</dd> * <dt>response_timezone</dt><dd>Setting expected timezone for results, * default is set in account.</dd> * </dl><p> * * @return TuneServiceResponse * @throws IllegalArgumentException Invalid parameters. * @throws TuneSdkException If error within SDK. * @throws TuneServiceException If service fails to handle post request. */ public final TuneServiceResponse count(final Map<String, Object> mapParams) throws TuneSdkException, TuneServiceException, IllegalArgumentException { Map<String, String> mapQueryString = new HashMap<String, String>(); // Required parameters mapQueryString = super.validateDateTime(mapParams, "start_date", mapQueryString); mapQueryString = super.validateDateTime(mapParams, "end_date", mapQueryString); // Optional parameters if (mapParams.containsKey("filter") && (null != mapParams.get("filter"))) { mapQueryString = super.validateFilter(mapParams, mapQueryString); } if (mapParams.containsKey("group") && (null != mapParams.get("group"))) { mapQueryString = super.validateGroup(mapParams, mapQueryString); } if (mapParams.containsKey("response_timezone") && (null != mapParams.get("response_timezone"))) { mapQueryString = super.validateResponseTimezone(mapParams, mapQueryString); } if (mapParams.containsKey("cohort_type") && (null != mapParams.get("cohort_type"))) { mapQueryString = this.validateCohortType(mapParams, mapQueryString); } if (mapParams.containsKey("cohort_interval") && (null != mapParams.get("cohort_interval"))) { mapQueryString = this.validateCohortInterval(mapParams, mapQueryString); } return super.callService("count", mapQueryString); } /** * Query status of insight reports. Upon completion will * return url to download requested report. * * @param jobId Provided Job Identifier to reference * requested report on export queue. * * @return TuneServiceResponse * @throws IllegalArgumentException Invalid parameters. */ public final TuneServiceResponse status(final String jobId) throws IllegalArgumentException, TuneSdkException { if ((null == jobId) || jobId.isEmpty()) { throw new IllegalArgumentException("Parameter 'jobId' is not defined."); } Map<String, String> mapQueryString = new HashMap<String, String>(); mapQueryString.put("job_id", jobId); return super.call("status", mapQueryString); } /** * Validate value is expected cohort type. * * @param mapParams Action parameter mapping * @param mapQueryString Query String parameter mapping * * @return Bool Cohort type is valid. * @throws IllegalArgumentException Invalid parameters. */ public final Map<String, String> validateCohortType(final Map<String, Object> mapParams, Map<String, String> mapQueryString) throws IllegalArgumentException { if (!mapParams.containsKey("cohort_type")) { throw new IllegalArgumentException("Key 'cohort_type' is not defined."); } final String cohortType = (String) mapParams.get("cohort_type"); if ((null == cohortType) || cohortType.isEmpty()) { throw new IllegalArgumentException("Key 'cohort_type' is not defined."); } if (!AdvertiserReportCohortBase.COHORT_TYPES.contains(cohortType)) { throw new IllegalArgumentException( String.format("Parameter 'cohort_type' is invalid: '%s'.", cohortType)); } mapQueryString.put("cohort_type", cohortType); return mapQueryString; } /** * Validate value is expected cohort interval. * * @param mapParams Action parameter mapping * @param mapQueryString Query String parameter mapping * * @return dict * @throws IllegalArgumentException Invalid parameters. */ public final Map<String, String> validateCohortInterval(final Map<String, Object> mapParams, Map<String, String> mapQueryString) throws IllegalArgumentException { if (!mapParams.containsKey("cohort_interval")) { throw new IllegalArgumentException("Parameter 'cohort_interval' is not defined."); } final String cohortInterval = (String) mapParams.get("cohort_interval"); if ((null == cohortInterval) || cohortInterval.isEmpty()) { throw new IllegalArgumentException("Parameter 'cohort_interval' is not defined."); } if (!AdvertiserReportCohortBase.COHORT_INTERVALS.contains(cohortInterval)) { throw new IllegalArgumentException( String.format("Parameter 'cohort_interval' is invalid: '%s'.", cohortInterval)); } mapQueryString.put("interval", cohortInterval); return mapQueryString; } /** * Validate value is valid aggregation type. * * @param mapParams Action parameter mapping * @param mapQueryString Query String parameter mapping * * @return dict * @throws IllegalArgumentException Invalid parameter. */ public final Map<String, String> validateAggregationType(final Map<String, Object> mapParams, Map<String, String> mapQueryString) throws IllegalArgumentException { if (!mapParams.containsKey("aggregation_type")) { throw new IllegalArgumentException("Parameter 'aggregation_type' is not defined."); } final String strAggregationType = (String) mapParams.get("aggregation_type"); if ((null == strAggregationType) || strAggregationType.isEmpty()) { throw new IllegalArgumentException("Parameter 'aggregation_type' is not defined."); } if (!AdvertiserReportCohortBase.AGGREGATION_TYPES.contains(strAggregationType)) { throw new IllegalArgumentException( String.format("Parameter 'aggregation_type' is invalid: '%s'.", strAggregationType)); } mapQueryString.put("aggregation_type", strAggregationType); return mapQueryString; } /** * Parse response and gather job identifier. * * @param response @see TuneServiceResponse * * @return String Report Job Id on Export queue. * @throws TuneServiceException If service fails to handle post request. * @throws TuneSdkException If error within SDK. * @throws IllegalArgumentException Invalid parameter. */ public static String parseResponseReportJobId(final TuneServiceResponse response) throws TuneServiceException, TuneSdkException, IllegalArgumentException { if (null == response) { throw new IllegalArgumentException("Parameter 'response' is not defined."); } JSONObject jdata = (JSONObject) response.getData(); if (null == jdata) { throw new TuneServiceException("Report request failed to get export data."); } if (!jdata.has("job_id")) { throw new TuneSdkException(String.format("Export data does not contain report 'jobId', response: %s", response.toString())); } String reportJobId = null; try { reportJobId = jdata.getString("job_id"); } catch (JSONException ex) { throw new TuneSdkException(ex.getMessage(), ex); } catch (Exception ex) { throw new TuneSdkException(ex.getMessage(), ex); } if ((null == reportJobId) || reportJobId.isEmpty()) { throw new TuneSdkException( String.format("Export response 'jobId' is not defined, response: %s", response.toString())); } return reportJobId; } /** * Parse response and gather report url. * * @param response @see TuneServiceResponse * * @return String Report URL download from Export queue. * @throws TuneSdkException If error within SDK. * @throws TuneServiceException If service fails to handle post request. */ public static String parseResponseReportUrl(final TuneServiceResponse response) throws TuneSdkException, TuneServiceException { if (null == response) { throw new IllegalArgumentException("Parameter 'response' is not defined."); } JSONObject jdata = (JSONObject) response.getData(); if (null == jdata) { throw new TuneServiceException("Report export response failed to get data."); } if (!jdata.has("url")) { throw new TuneSdkException( String.format("Export data does not contain report 'url', response: %s", response.toString())); } String reportUrl = null; try { reportUrl = jdata.getString("url"); } catch (JSONException ex) { throw new TuneSdkException(ex.getMessage(), ex); } catch (Exception ex) { throw new TuneSdkException(ex.getMessage(), ex); } if ((null == reportUrl) || reportUrl.isEmpty()) { throw new TuneSdkException( String.format("Export response 'url' is not defined, response: %s", response.toString())); } return reportUrl; } }