Bulk Update Opening Hours

This page describes the process of setting the opening hours for a large number of display units.

Use the following method: day_part_mgr_update.

 

The key to updating opening hours in bulk is the minute_mask parameter. In this section, we describe how to calculate the values for the minute_mask string.

You must encode the field in minutes per week. Each range represents a contiguous block of time, representing each opening hour period.

Count every minute in a week from 0 to 10079.

Keep in mind the following:

    • Minute 0 starts at 12:00 AM on Monday
    • There are 1440 minutes per day, 10080 minutes per week.
    • The last addressable minute in a week is 10079. It represents the minute from 23:59:00 to 24:00:00 on Sunday in a given week.
    • The end of a minute range includes the last minute it describes. For example, to end at exactly 5pm:
        • Entering 1019 is correct because it includes the minute from 4:59:00 to 5:00:00.
        • Entering 1020 is incorrect because it includes the minute from 5:00:00 to 5:01:00. Therefore, you would need to subtract 1 minute from the end of a range to be accurate.
    • You must separate each range with a semi-colon.

To represent opening hours that are 9AM to 5PM every day of the week, the minute_mask would be:

Day of the Week Hours Encoding
Monday 9 AM – 5 PM 540-1019
Tuesday 9 AM – 5 PM 1980-2459
Wednesday 9 AM – 5 PM 3420-3899
Thursday 9 AM – 5 PM 4860-5339
Friday 9 AM – 5 PM 6300-6779
Saturday 9 AM – 5 PM 7740-8219
Sunday 9 AM – 5 PM 9180-9659

 

We calculated the encoding in this way:

9 AM on Monday = 9 * 60 minutes = 540
5 PM on Monday = (9 * 60 minutes) + [(8 * 60 mins) - 1] = 1019
9 AM on Tuesday = 33 * 60 mins = 1980
5 PM on Tuesday = (33 * 60 mins) + [(8 * 60 mins) - 1] = 2459
9 AM on Wednesday = (57 * 60 mins) = 3420
5 PM on Wednesday = (57 * 60 mins) + [(8 * 60 mins) - 1] = 3899

You would enter the string in the minute_mask parameter using the following format:

540-1019;1980-2459;3420-3899;4860-5339;6300-6779;7740-8219;9180-9659
 
These code samples illustrate how to bulk update opening hours.

#!/usr/bin/php
<?php
	## Bulk update opening hours via day parts
	## On DU 123789456
	## 
	## Usage:  php day_part_mgr_update_v5 --domain=domainId --day_part="daypartId_1,daypartId_2"
	## Usage:  php day_part_mgr_update_v5 --domain=123698744 --ids="987654321,123456789" 

	include "soaplib.php";
	include "cmdutil.php";

	$options = get_arguments("domain!" , "ids!");

	foreach (explode(",", $options["ids"]) as $day_part) 
	{
		$body = new stdClass();
		$body -> id = $day_part ; # ID of your day part
		$body -> day_mask = 127;
		$body -> domain_id = $options["domain"]; # ID of your domain
		$body -> start_date = "2016-01-01";
		$body -> end_date = "2016-01-01";
		$body -> end_time = "23:59:59";
		$body -> impressions_per_hour = -1;
		$body -> minute_mask = "0-59;1440-1499;2880-2939;4320-4379;5760-5819;7200-7259;8640-8699" ;
		$body -> start_time = "00:00:00";
		$body -> name = "day_part_modified_using_sdk" ;
		$body -> virtual_start_date="1756-01-01" ;
		$body -> virtual_end_date="8000-12-31";
		$body -> weight = 2 ;
		$body -> active = true ;
				$response = performSimpleOperation("day_part", "update", 5, $body);
				
				if (!$response || !isset($response -> day_part[0] -> id)) 
	{
		echo "\nCould not edit the day part.\n";
		return;
	}
	else 
		echo ("\nDay part(s) updated : {$response -> day_part[0] -> id}\n");
	}
	#echo json_encode($response);
?>

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

import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;

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;
import com.broadsign.www.wsdl_ops.DayPartMgrListManyResponseBody;

class Main {

	public static void main(String[] args) throws Exception
	{
		setupKeystore();
		BsapiStub javaStub = createStub();
		
		List<String> dayPartList = new ArrayList<String>();
		dayPartList.add("12345678");
		dayPartList.add("23456789");
		dayPartList.add("34567890");
		dayPartList.add("45678901");
		dayPartList.add("56789012");
		
        /*
         * The opening hours are encoded into the minute mask in the following fashion:
         * It is a text field that encodes the opening hours in a semi-colon delimited list of minute ranges.  
         * Minute zero is 00:00 AM on Monday and minute 10079 is 23:59 PM on Sunday.
         * 
         * For example, opening hours of 09:00 AM to 17:00 PM on Mon, Tue, Wed, Thu, Fri 
         * would be represented in the minute_mask field as:
         * "540-1019;1980-2459;3420-3899;4860-5339;6300-6779"
         */
		String minuteMask = "540-1019;1980-2459;3420-3899;4860-5339;6300-6779";

		DayPartFactory dayPartFactory = new DayPartFactory(javaStub);
		
		for(String dayPartId : dayPartList) {
			updateBusinessHours(dayPartFactory, dayPartId, minuteMask);
		}

	}
	
	private static void updateBusinessHours(DayPartFactory dayPartFactory, String dayPartId, String minuteMask) throws Exception {
		DayPartMgrListManyResponseBody rBody = dayPartFactory.listMany(dayPartId);
		// Keep everything the same but change the minute mask
		dayPartFactory.updateDayPart(
				rBody.getName(), 
				dayPartId, 
				rBody.getDayMask(), 
				rBody.getStartDate(), 
				rBody.getEndDate(), 
				rBody.getStartTime(), 
				rBody.getEndTime(), 
				rBody.getVirtualStartDate(), 
				rBody.getVirtualEndDate(),
				rBody.getImpressionsPerHour(),
				minuteMask,
				rBody.getWeight(),
				((BigInteger) rBody.getDomainId()).toString(),
				rBody.getActive());
	}


	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
 */

/*
 * DayPartFactory Class
 *
 * This class allows the creation of day parts
 */

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

import org.apache.xmlbeans.*;

import com.broadsign.www.wsdl_ops.*;

class DayPartFactory {

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

	public String createDefaultDayPart(String displayUnitId, String domainId)
	{
		try {
			return createDayPart("24/7",
					displayUnitId,
					127,
					null,
					null,
					"00:00:00",
					"00:00:00",
					"1756-01-01",
					"8000-12-31",
					-1,
					0,
					domainId);
		} catch (java.lang.Exception e) {
			System.out.println("Error:" + e.toString());
		}

		return "";
	}

	public String createDayPart(String name,
			String displayUnitId,
			int dayMask,
			String startDate,
			String endDate,
			String startTime,
			String endTime,
			String virtualStartDate,
			String virtualEndDate,
			int impressionsPerHour,
			int weight,
			String domainId)
	{
		String dpId = null;

		try {
			DayPartMgrAddRequestBody dpMgrAddRequestBody = DayPartMgrAddRequestBody.Factory.newInstance();
			WsdlRequest dpWsdlRequest = WsdlRequest.Factory.newInstance();
			RequestDocument dpRequestDocument = RequestDocument.Factory.newInstance();
			XmlObject dpBodies[];
			ResponseDocument dpResponseDocument;

			dpMgrAddRequestBody.setName(name);
			dpMgrAddRequestBody.setDayMask(dayMask);
			dpMgrAddRequestBody.setParentId(displayUnitId);
			dpMgrAddRequestBody.setEndDate(endDate);
			dpMgrAddRequestBody.setEndTime(endTime);
			dpMgrAddRequestBody.setStartDate(startDate);
			dpMgrAddRequestBody.setStartTime(startTime);
			dpMgrAddRequestBody.setImpressionsPerHour(impressionsPerHour);
			dpMgrAddRequestBody.setVirtualEndDate(virtualEndDate);
			dpMgrAddRequestBody.setVirtualStartDate(virtualStartDate);
			dpMgrAddRequestBody.setWeight(weight);
			dpMgrAddRequestBody.setDomainId(domainId);
			dpWsdlRequest.setName("day_part_mgr_add");
			dpWsdlRequest.setVersion(5);
			dpWsdlRequest.setDayPart(dpMgrAddRequestBody);
			dpRequestDocument.setRequest(dpWsdlRequest);
			dpRequestDocument.documentProperties().setEncoding("utf-16");
			System.out.println(dpRequestDocument.xmlText());
			dpResponseDocument = m_stub.day_part_mgr_add_v5(dpRequestDocument);
			System.out.println(dpResponseDocument.xmlText());
			dpBodies = dpResponseDocument.getResponse().getDayPartArray();

			if (dpBodies.length == 1) {
				DayPartMgrAddResponseBody dayPart = DayPartMgrAddResponseBody.Factory.parse(dpBodies[0].xmlText());

				dpId = dayPart.getId().toString();
			} else {
				System.out.println("Error: Failed to create day part.");
			}
		} catch (java.lang.Exception e) {
			System.out.println("Error:" + e.toString());
		}

		return dpId;
	}

	public List<String> list(String domainId, String parentContainerIds) throws Exception
	{
		List<String> results = new ArrayList<String>();
		WsdlRequest wsdlRequest = WsdlRequest.Factory.newInstance();
		XmlObject xmlBodies[];
		RequestDocument requestDocument = RequestDocument.Factory.newInstance();
		ResponseDocument responseDocument;
		WsdlResponse response;

		if (!parentContainerIds.isEmpty()) {
			DayPartMgrListScopedRequestBody requestBody = DayPartMgrListScopedRequestBody.Factory.newInstance();

			requestBody.setDomainId(domainId);
			requestBody.setParentContainerIds(parentContainerIds);
			wsdlRequest.setName("day_part_mgr_list_scoped");
			wsdlRequest.setVersion(5);
			wsdlRequest.setDayPart(requestBody);
		} else {
			DayPartMgrListRequestBody requestBody = DayPartMgrListRequestBody.Factory.newInstance();

			requestBody.setDomainId(domainId);
			wsdlRequest.setName("day_part_mgr_list");
			wsdlRequest.setVersion(5);
			wsdlRequest.setDayPart(requestBody);
		}

		requestDocument.setRequest(wsdlRequest);
		requestDocument.documentProperties().setEncoding("utf-16");
		System.out.println(requestDocument.xmlText());

		if (!parentContainerIds.isEmpty())
			responseDocument = m_stub.day_part_mgr_list_scoped_v5(requestDocument);
		else
			responseDocument = m_stub.day_part_mgr_list_v5(requestDocument);

		System.out.println(responseDocument.xmlText());
		response = responseDocument.getResponse();
		xmlBodies = response.getDayPartArray();

		if (xmlBodies.length > 0) {
			for (XmlObject xmlBody : xmlBodies) {
				if (!parentContainerIds.isEmpty()) {
					DayPartMgrListScopedResponseBody dayPart = DayPartMgrListScopedResponseBody.Factory.parse(xmlBody.xmlText());

					results.add(dayPart.getId().toString());
				} else {
					DayPartMgrListResponseBody dayPart = DayPartMgrListResponseBody.Factory.parse(xmlBody.xmlText());

					results.add(dayPart.getId().toString());
				}
			}
		}

		return results;
	}

	public DayPartMgrListManyResponseBody listMany(String ids) throws Exception
	{
		WsdlRequest wsdlRequest = WsdlRequest.Factory.newInstance();
		XmlObject xmlBodies[];
		RequestDocument requestDocument = RequestDocument.Factory.newInstance();
		ResponseDocument responseDocument;
		WsdlResponse response;

		DayPartMgrListManyRequestBody requestBody = DayPartMgrListManyRequestBody.Factory.newInstance();

		requestBody.setIds(ids);
		wsdlRequest.setName("day_part_mgr_list_many");
		wsdlRequest.setVersion(5);
		wsdlRequest.setDayPart(requestBody);


		requestDocument.setRequest(wsdlRequest);
		requestDocument.documentProperties().setEncoding("utf-16");
		System.out.println(requestDocument.xmlText());

		responseDocument = m_stub.day_part_mgr_list_many_v5(requestDocument);

		System.out.println(responseDocument.xmlText());
		response = responseDocument.getResponse();
		xmlBodies = response.getDayPartArray();

		if (xmlBodies.length > 0) {
			for (XmlObject xmlBody : xmlBodies) {
					DayPartMgrListManyResponseBody dayPart = DayPartMgrListManyResponseBody.Factory.parse(xmlBody.xmlText());
					return dayPart;
			}
		}

		return null;
	}

	public String updateDayPart(String name,
			String id,
			int dayMask,
			String startDate,
			String endDate,
			String startTime,
			String endTime,
			String virtualStartDate,
			String virtualEndDate,
			int impressionsPerHour,
			String minuteMask,
			int weight,
			String domainId,
			boolean active)
	{
		String dpId = null;

		try {
			DayPartMgrUpdateRequestBody dpMgUpdateRequestBody = DayPartMgrUpdateRequestBody.Factory.newInstance();
			WsdlRequest dpWsdlRequest = WsdlRequest.Factory.newInstance();
			RequestDocument dpRequestDocument = RequestDocument.Factory.newInstance();
			XmlObject dpBodies[];
			ResponseDocument dpResponseDocument;

			dpMgUpdateRequestBody.setName(name);
			dpMgUpdateRequestBody.setDayMask(dayMask);
			dpMgUpdateRequestBody.setId(id);
			dpMgUpdateRequestBody.setEndDate(endDate);
			dpMgUpdateRequestBody.setEndTime(endTime);
			dpMgUpdateRequestBody.setStartDate(startDate);
			dpMgUpdateRequestBody.setStartTime(startTime);
			dpMgUpdateRequestBody.setImpressionsPerHour(impressionsPerHour);
			dpMgUpdateRequestBody.setMinuteMask(minuteMask);
			dpMgUpdateRequestBody.setVirtualEndDate(virtualEndDate);
			dpMgUpdateRequestBody.setVirtualStartDate(virtualStartDate);
			dpMgUpdateRequestBody.setWeight(weight);
			dpMgUpdateRequestBody.setDomainId(domainId);
			dpMgUpdateRequestBody.setActive(active);
			dpWsdlRequest.setName("day_part_mgr_update");
			dpWsdlRequest.setVersion(5);
			dpWsdlRequest.setDayPart(dpMgUpdateRequestBody);
			dpRequestDocument.setRequest(dpWsdlRequest);
			dpRequestDocument.documentProperties().setEncoding("utf-16");
			System.out.println(dpRequestDocument.xmlText());
			dpResponseDocument = m_stub.day_part_mgr_update_v5(dpRequestDocument);
			System.out.println(dpResponseDocument.xmlText());
			dpBodies = dpResponseDocument.getResponse().getDayPartArray();

			if (dpBodies.length == 1) {
				DayPartMgrUpdateResponseBody dayPart = DayPartMgrUpdateResponseBody.Factory.parse(dpBodies[0].xmlText());

				dpId = dayPart.getId().toString();
			} else {
				System.out.println("Error: Failed to create day part.");
			}
		} catch (java.lang.Exception e) {
			System.out.println("Error:" + e.toString());
		}

		return dpId;
	}

	private BsapiStub m_stub = null;
}