*/
public class GetComedyContent {
public static void runExample(DfpServices dfpServices, DfpSession session) throws Exception {
// Get the ContentService.
ContentServiceInterface contentService =
dfpServices.get(session, ContentServiceInterface.class);
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService =
dfpServices.get(session, CustomTargetingServiceInterface.class);
// Get the NetworkService.
NetworkServiceInterface networkService =
dfpServices.get(session, NetworkServiceInterface.class);
// Get content browse custom targeting key ID.
long contentBrowseCustomTargetingKeyId =
networkService.getCurrentNetwork().getContentBrowseCustomTargetingKeyId();
if (contentBrowseCustomTargetingKeyId == 0) {
throw new RuntimeException("Your network does not have content categories enabled.");
}
// Create a statement to select the categories matching the name comedy.
Statement categoryFilterStatement = new StatementBuilder()
.where("customTargetingKeyId = :contentBrowseCustomTargetingKeyId and name = :category")
.limit(1)
.withBindVariableValue(
"contentBrowseCustomTargetingKeyId", contentBrowseCustomTargetingKeyId)
.withBindVariableValue("category", "comedy").toStatement();
// Get categories matching the filter statement.
CustomTargetingValuePage customTargetingValuePage =
customTargetingService.getCustomTargetingValuesByStatement(categoryFilterStatement);
// Get the custom targeting value ID for the comedy category.
long categoryCustomTargetingValueId = customTargetingValuePage.getResults()[0].getId();
// Create a statement to select all content.
StatementBuilder statementBuilder = new StatementBuilder()
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get content by statement.
ContentPage page = contentService.getContentByStatementAndCustomTargetingValue(
statementBuilder.toStatement(), categoryCustomTargetingValueId);
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();