Mass Apply Criteria to Resources

This use case describes how to mass apply a criterion to several resources: in this case, an ad copy, a display unit and a frame.

SOAP API Method Reference

To mass apply a criterion to several resources, use the following method: resource_criteria_mgr_add.

Code Samples
#!/usr/bin/php
<?php
	## Mass assign criteria to resources
	## Assign a criterion to an ad copy, a display unit and a frame
	## The association type will be left to default = 0
	##
	## Usage : php mass_assign_criteria --domain=domainId [--ids="adcopyId,displayunitId,frameId"] criteria_id=criteriaId
	## Usage example : php mass_assign_criteria --domain=68110253 --ids="123782926,100263097,100263099" --criteria_id=130688021

	include "soaplib.php";
	include "cmdutil.php";
	
				$options = get_arguments("domain!" , "criteria_id!" , "parent_id" , "ids");
		
	if (isset($options["criteria_id"]))
		{
			
			if (!isset($options["ids"]))
				{
						$body = new stdClass();
						$body -> domain_id = $options["domain"]; # ID of your domain
						$body -> parent_id = $options["parent_id"] ; # ID of your resource
						$body -> criteria_id = $options["criteria_id"] ; #ID of your criterion
						$response = performSimpleOperation("resource_criteria", "add", 7, $body);
				}
			else
				{
				foreach (explode(",", $options["ids"]) as $rd_id) 
						{
								$body = new stdClass();
								$body -> parent_id = $rd_id; # The ID of the resource 
								$body -> domain_id = $options["domain"]; # The ID of your domain
								$body -> criteria_id = $options["criteria_id"] ; #ID of your criterion
								$response = performSimpleOperation("resource_criteria", "add", 7, $body);
						}
				}
		}
		else
		{
				print("PLease provide a valid criterion");
				return ;
		}

	#echo json_encode($response);
?>

/*
 * (c) 2002 - 2015 BroadSign International, LLC
 */

import java.util.ArrayList;
import java.util.List;

import org.apache.axis2.Constants.Configuration;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
import org.apache.axis2.transport.http.HTTPConstants;

import com.broadsign.www.wsdl_ops.BsapiStub;

class Main {

	public static void main(String[] args) throws Exception
	{
		setupKeystore();
		BsapiStub javaStub = createStub();
		
		String basicDomainId = "12345678";
		String basicCriteriaId = "23456789";
		
		List<String> targetResourceList = new ArrayList<String>();
		targetResourceList.add("34567890");
		targetResourceList.add("45678901");
		targetResourceList.add("56789012");

		CriteriaFactory criteriaFactory = new CriteriaFactory(javaStub);
		
		for(String targetResourceId : targetResourceList) {
			criteriaFactory.assignCriteriaToResource(targetResourceId, basicCriteriaId, basicDomainId);
		}

	}


	private static BsapiStub createStub()
	{
		try {
			ConfigurationContext config = ConfigurationContextFactory.createDefaultConfigurationContext();
			TransportOutDescription transport = new TransportOutDescription("https_mutual_auth");
			CommonsHTTPTransportSender sender = new CommonsHTTPTransportSender();

			sender.init(config, transport);
			BsapiStub javaStub = new BsapiStub();
			transport.setSender(sender);
			javaStub._getServiceClient().getOptions().setProperty(Configuration.CHARACTER_SET_ENCODING, "utf-16");
			javaStub._getServiceClient().getOptions().setProperty(HTTPConstants.CHUNKED, "false");
			javaStub._getServiceClient().getOptions().setTransportOut(transport);
			return javaStub;
		} catch (java.lang.Exception e) {
			System.out.println("Error:" + e.toString());
		}
		return null;
	}
	private static void setupKeystore()
	{
		// NOTE: Make sure to update the folders where your keystore files are located
        //       as well as the actual passwords for your keystores
		System.setProperty("javax.net.ssl.keyStoreType", "jks");
		System.setProperty("javax.net.ssl.keyStore", "YOUR_SOAP_KEYSTORE_HERE.keystore");
		System.setProperty("javax.net.ssl.keyStorePassword", "YOUR_SOAP_KEYSTORE_PASSWORD_HERE");
		System.setProperty("javax.net.ssl.trustStoreType", "jks");
		System.setProperty("javax.net.ssl.trustStore", "YOUR_TRUSTED_KEYSTORE_HERE.keystore");
		System.setProperty("javax.net.ssl.trustStorePassword", "YOUR_TRUSTED_KEYSTORE_PASSWORD_HERE");
	}
}

/*
 * (c) 2002 - 2015 BroadSign International, LLC
 */

import java.util.ArrayList;
import java.util.List;

import org.apache.xmlbeans.XmlObject;

import com.broadsign.www.wsdl_ops.BsapiStub;
import com.broadsign.www.wsdl_ops.CriteriaMgrListRequestBody;
import com.broadsign.www.wsdl_ops.CriteriaMgrListResponseBody;
import com.broadsign.www.wsdl_ops.RequestDocument;
import com.broadsign.www.wsdl_ops.ResourceCriteriaMgrAddRequestBody;
import com.broadsign.www.wsdl_ops.ResourceCriteriaMgrAddResponseBody;
import com.broadsign.www.wsdl_ops.ResponseDocument;
import com.broadsign.www.wsdl_ops.WsdlRequest;
import com.broadsign.www.wsdl_ops.WsdlResponse;

public class CriteriaFactory {

	public CriteriaFactory(BsapiStub stub)
	{
		assert(stub != null);
		m_stub = stub;
	}

	public List<String> list(String domainId) throws Exception
	{
		List<String> criterias = new ArrayList<String>();
		CriteriaMgrListRequestBody requestBody = CriteriaMgrListRequestBody.Factory.newInstance();
		WsdlRequest wsdlRequest = WsdlRequest.Factory.newInstance();
		XmlObject bodies[];
		RequestDocument requestDocument = RequestDocument.Factory.newInstance();
		ResponseDocument responseDocument;

		requestBody.setDomainId(domainId);
		wsdlRequest.setName("criteria_mgr_list");
		wsdlRequest.setVersion(7);
		wsdlRequest.setCriteria(requestBody);
		requestDocument.setRequest(wsdlRequest);
		requestDocument.documentProperties().setEncoding("utf-16");
		System.out.println(requestDocument.xmlText());
		responseDocument = m_stub.criteria_mgr_list_v7(requestDocument);
		System.out.println(responseDocument.xmlText());
		WsdlResponse response = responseDocument.getResponse();
		bodies = response.getCriteriaArray();

		if (bodies.length > 0) {
			for (XmlObject body : bodies) {
				CriteriaMgrListResponseBody criteria = CriteriaMgrListResponseBody.Factory.parse(body.xmlText());

				criterias.add(criteria.getName());
			}
		}

		return criterias;
	}

	public String assignCriteriaToResource(String targetResourceId, String criteriaId, String domainId)
	{
		String id = null;

		try {
			ResourceCriteriaMgrAddRequestBody requestBody = ResourceCriteriaMgrAddRequestBody.Factory.newInstance();
			WsdlRequest wsdlRequest = WsdlRequest.Factory.newInstance();
			RequestDocument requestDocument = RequestDocument.Factory.newInstance();
			XmlObject bodies[];
			ResponseDocument responseDocument;

			requestBody.setCriteriaId(criteriaId);
			requestBody.setParentId(targetResourceId);
			requestBody.setDomainId(domainId);
			wsdlRequest.setName("resource_criteria_mgr_add");
			wsdlRequest.setVersion(7);
			wsdlRequest.setResourceCriteria(requestBody);
			requestDocument.setRequest(wsdlRequest);
			requestDocument.documentProperties().setEncoding("utf-16");
			System.out.println(requestDocument.xmlText());
			responseDocument = m_stub.resource_criteria_mgr_add_v7(requestDocument);
			System.out.println(responseDocument.xmlText());
			bodies = responseDocument.getResponse().getResourceCriteriaArray();

			if (bodies.length == 1) {
				ResourceCriteriaMgrAddResponseBody body = ResourceCriteriaMgrAddResponseBody.Factory.parse(bodies[0].xmlText());

				id = body.getId().toString();
			} else {
				System.out.println("Error: Failed to create frame.");
			}
		} catch (java.lang.Exception e) {
			System.out.println("Error:" + e.toString());
		}

		return id;
	}

	private BsapiStub m_stub;
}