All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kaltura.client.KalturaParams Maven / Gradle / Ivy

Go to download

KalturaClient is a library of Java classes that can be used to interact with the Kaltura REST API. More information about the REST API can be found at http://corp.kaltura.com/Products/Kaltura-API Many of the Java classes in this library are auto-generated from a schema that defines the objects that are used to interect with the API. The current schema can be found at http://www.kaltura.com/api_v3/api_schema.php

The newest version!
// ===================================================================================================
//                           _  __     _ _
//                          | |/ /__ _| | |_ _  _ _ _ __ _
//                          | ' .
//
// @ignore
// ===================================================================================================
package com.kaltura.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

import com.kaltura.client.enums.KalturaEnumAsInt;
import com.kaltura.client.enums.KalturaEnumAsString;

/**
 * Helper class that provides a collection of Kaltura parameters (key-value
 * pairs).
 * 
 * @author jpotts
 * 
 */
public class KalturaParams extends JSONObject {

	public String toQueryString() throws KalturaApiException {
		return toQueryString(null);
	}

	public String toQueryString(String prefix) throws KalturaApiException {

		StringBuffer str = new StringBuffer();
		Object value;
		String key;
		for (Object keyObject : keySet()) {
			key = (String) keyObject;
			if (str.length() > 0) {
				str.append("&");
			}

			try {
				value = get(key);
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}

			if (prefix != null) {
				key = prefix + "[" + key + "]";
			}
			if (value instanceof KalturaParams) {
				str.append(((KalturaParams) value).toQueryString(key));
			} else {
				str.append(key);
				str.append("=");
				str.append(value);
			}
		}

		return str.toString();
	}

	public void add(String key, int value) throws KalturaApiException {
		if (value == KalturaParamsValueDefaults.KALTURA_UNDEF_INT) {
			return;
		}

		if (value == KalturaParamsValueDefaults.KALTURA_NULL_INT) {
			putNull(key);
			return;
		}

		try {
			put(key, value);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public void add(String key, long value) throws KalturaApiException {
		if (value == KalturaParamsValueDefaults.KALTURA_UNDEF_LONG) {
			return;
		}
		if (value == KalturaParamsValueDefaults.KALTURA_NULL_LONG) {
			putNull(key);
			return;
		}

		try {
			put(key, value);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public void add(String key, double value) throws KalturaApiException {
		if (value == KalturaParamsValueDefaults.KALTURA_UNDEF_DOUBLE) {
			return;
		}
		if (value == KalturaParamsValueDefaults.KALTURA_NULL_DOUBLE) {
			putNull(key);
			return;
		}

		try {
			put(key, value);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public void add(String key, String value) throws KalturaApiException {
		if (value == null) {
			return;
		}

		if (value.equals(KalturaParamsValueDefaults.KALTURA_NULL_STRING)) {
			putNull(key);
			return;
		}

		try {
			put(key, value);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public void add(String key, KalturaObjectBase object)
			throws KalturaApiException {
		if (object == null)
			return;

		try {
			put(key, object.toParams());
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public  void add(String key, ArrayList array)
			throws KalturaApiException {
		if (array == null)
			return;

		if (array.isEmpty()) {
			KalturaParams emptyParams = new KalturaParams();
			try {
				emptyParams.put("-", "");
				put(key, emptyParams);
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}
		} else {
			JSONArray arrayParams = new JSONArray();
			for (KalturaObjectBase baseObj : array) {
				arrayParams.put(baseObj.toParams());
			}
			try {
				put(key, arrayParams);
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}
		}
	}

	public  void add(String key,
			HashMap map) throws KalturaApiException {
		if (map == null)
			return;

		if (map.isEmpty()) {
			KalturaParams emptyParams = new KalturaParams();
			try {
				emptyParams.put("-", "");
				put(key, emptyParams);
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}
		} else {
			KalturaParams mapParams = new KalturaParams();
			for (String itemKey : map.keySet()) {
				KalturaObjectBase baseObj = map.get(itemKey);
				mapParams.add(itemKey, baseObj);
			}
			try {
				put(key, mapParams);
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}
		}
	}

	public  void add(String key,
			KalturaParams params) throws KalturaApiException {
		try {
			if (params instanceof KalturaParams && has(key)
					&& get(key) instanceof KalturaParams) {
				KalturaParams existingParams = (KalturaParams) get(key);
				existingParams.putAll((KalturaParams) params);
			} else {
				put(key, params);
			}
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	public Iterable keySet() {
		return new Iterable() {
			@SuppressWarnings("unchecked")
			public Iterator iterator() {
				return keys();
			}
		};
	}

	private void putAll(KalturaParams params) throws KalturaApiException {
		for (Object key : params.keySet()) {
			String keyString = (String) key;
			try {
				put(keyString, params.get(keyString));
			} catch (JSONException e) {
				throw new KalturaApiException(e.getMessage());
			}
		}
	}

	public void add(KalturaParams objectProperties) throws KalturaApiException {
		putAll(objectProperties);
	}

	protected void putNull(String key) throws KalturaApiException {
		try {
			put(key + "__null", "");
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	/**
	 * Pay attention - this function does not check if the value is null.
	 * neither it supports setting value to null.
	 */
	public void add(String key, boolean value) throws KalturaApiException {
		try {
			put(key, value);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
	}

	/**
	 * Pay attention - this function does not support setting value to null.
	 */
	public void add(String key, KalturaEnumAsString value)
			throws KalturaApiException {
		if (value == null)
			return;

		add(key, value.getHashCode());
	}

	/**
	 * Pay attention - this function does not support setting value to null.
	 */
	public void add(String key, KalturaEnumAsInt value)
			throws KalturaApiException {
		if (value == null)
			return;

		add(key, value.getHashCode());
	}

	public boolean containsKey(String key) {
		return has(key);
	}

	public void clear() {
		for (Object key : keySet()) {
			remove((String) key);
		}
	}

	public KalturaParams getParams(String key) throws KalturaApiException {
		if (!has(key))
			return null;

		Object value;
		try {
			value = get(key);
		} catch (JSONException e) {
			throw new KalturaApiException(e.getMessage());
		}
		if (value instanceof KalturaParams)
			return (KalturaParams) value;

		throw new KalturaApiException("Key value [" + key
				+ "] is not instance of KalturaParams");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy