Skip to content

Commit ef0ac6e

Browse files
committed
Add support for v201408
1 parent 5e11aa4 commit ef0ac6e

File tree

10 files changed

+691
-0
lines changed

10 files changed

+691
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package eu.adlogix.com.google.api.ads.dfp.v201408;
2+
3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import java.rmi.RemoteException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.joda.time.DateTime;
10+
11+
import com.google.api.ads.dfp.axis.factory.DfpServices;
12+
import com.google.api.ads.dfp.axis.utils.v201408.StatementBuilder;
13+
import com.google.api.ads.dfp.axis.v201408.AdUnit;
14+
import com.google.api.ads.dfp.axis.v201408.AdUnitPage;
15+
import com.google.api.ads.dfp.axis.v201408.ApiException;
16+
import com.google.api.ads.dfp.axis.v201408.InventoryServiceInterface;
17+
import com.google.api.ads.dfp.axis.v201408.NetworkServiceInterface;
18+
import com.google.api.ads.dfp.lib.client.DfpSession;
19+
import com.google.common.collect.Iterables;
20+
import com.google.common.collect.Lists;
21+
22+
import eu.adlogix.com.google.api.ads.dfp.StatementCondition;
23+
24+
public class AdUnitFinder {
25+
26+
private final InventoryServiceInterface inventoryService;
27+
28+
private final NetworkServiceInterface networkServiceInterface;
29+
30+
/**
31+
* Allows to construct the {@link AdUnitFinder} with {@link DfpServices} and
32+
* {@link DfpSession} which will extract the necessary services to use in
33+
* this class.
34+
*
35+
* @param dfpServices
36+
* @param session
37+
*/
38+
public AdUnitFinder(final DfpServices dfpServices, final DfpSession session) {
39+
this(dfpServices.get(session, InventoryServiceInterface.class), dfpServices.get(session, NetworkServiceInterface.class));
40+
}
41+
42+
/**
43+
* Constructs the {@link AdUnitFinder} with an instance of
44+
* {@link InventoryServiceInterface} and {@link NetworkServiceInterface}.
45+
*
46+
* The {@link NetworkServiceInterface} is necessary for example when we need
47+
* to find the effective root AdUnit.
48+
*
49+
* @param inventoryService
50+
* @param networkServiceInterface
51+
*/
52+
public AdUnitFinder(final InventoryServiceInterface inventoryService,
53+
final NetworkServiceInterface networkServiceInterface) {
54+
checkNotNull(inventoryService, "inventoryService should not be null");
55+
this.inventoryService = inventoryService;
56+
this.networkServiceInterface = networkServiceInterface;
57+
}
58+
59+
/**
60+
* @return The effective root {@link AdUnit} of the network
61+
*/
62+
public AdUnit findRoot() {
63+
try {
64+
return findById(networkServiceInterface.getCurrentNetwork().getEffectiveRootAdUnitId());
65+
} catch (Exception exception) {
66+
throw new RuntimeException(exception);
67+
}
68+
}
69+
70+
public AdUnit findById(String id) {
71+
return Iterables.getOnlyElement(findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator().withId(id, StatementCondition.EQUAL)));
72+
}
73+
74+
public List<AdUnit> findByIds(List<String> ids) {
75+
return findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator().withIds(ids));
76+
}
77+
78+
public List<AdUnit> findAllByParentId(String parentId) {
79+
return findAllByParentId(parentId, null);
80+
}
81+
82+
public List<AdUnit> findAllByParentId(String parentId, DateTime lastModifiedDateTime) {
83+
return findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator().withParentId(parentId, StatementCondition.EQUAL), lastModifiedDateTime);
84+
}
85+
86+
public List<AdUnit> findAllActiveByParentId(String parentId) {
87+
return findAllActiveByParentId(parentId, null);
88+
}
89+
90+
public List<AdUnit> findAllActiveByParentId(String parentId, DateTime lastModifiedDateTime) {
91+
return findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator().withParentId(parentId, StatementCondition.EQUAL)
92+
.withStatusActive(), lastModifiedDateTime);
93+
}
94+
95+
public List<AdUnit> findAll() {
96+
return findAll(null);
97+
}
98+
99+
public List<AdUnit> findAll(DateTime lastModifiedDateTime) {
100+
return findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator(), lastModifiedDateTime);
101+
}
102+
103+
public List<AdUnit> findAllActive() {
104+
return findAllActive(null);
105+
}
106+
107+
public List<AdUnit> findAllActive(DateTime lastModifiedDateTime) {
108+
return findByAdUnitStatementBuilder(new AdUnitStatementBuilderCreator().withStatusActive(), lastModifiedDateTime);
109+
}
110+
111+
public List<AdUnit> findByStatementBuilder(StatementBuilder statementBuilder) {
112+
113+
statementBuilder.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
114+
115+
int totalResultSetSize = 0;
116+
117+
List<AdUnit> adUnits = new ArrayList<AdUnit>();
118+
119+
try {
120+
do {
121+
AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.toStatement());
122+
123+
if (page.getResults() != null) {
124+
totalResultSetSize = page.getTotalResultSetSize();
125+
adUnits.addAll(Lists.newArrayList(page.getResults()));
126+
}
127+
128+
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
129+
} while (statementBuilder.getOffset() < totalResultSetSize);
130+
131+
} catch (ApiException e) {
132+
throw new RuntimeException(e);
133+
} catch (RemoteException e) {
134+
throw new RuntimeException(e);
135+
}
136+
137+
return adUnits;
138+
}
139+
140+
private List<AdUnit> findByAdUnitStatementBuilder(AdUnitStatementBuilderCreator adUnitStatementBuilder) {
141+
return findByAdUnitStatementBuilder(adUnitStatementBuilder, null);
142+
}
143+
144+
private List<AdUnit> findByAdUnitStatementBuilder(AdUnitStatementBuilderCreator adUnitStatementBuilder,
145+
DateTime lastModifiedDateTime) {
146+
147+
if (null != lastModifiedDateTime) {
148+
adUnitStatementBuilder.withLastModifiedDateTime(lastModifiedDateTime, StatementCondition.GREATER_OR_EQUAL);
149+
}
150+
151+
return findByStatementBuilder(adUnitStatementBuilder.toStatementBuilder());
152+
}
153+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package eu.adlogix.com.google.api.ads.dfp.v201408;
2+
3+
import java.util.List;
4+
5+
import org.joda.time.DateTime;
6+
7+
import com.google.api.ads.dfp.axis.utils.v201408.DateTimes;
8+
9+
import eu.adlogix.com.google.api.ads.dfp.AdUnitStatementQueryFilter;
10+
import eu.adlogix.com.google.api.ads.dfp.StatementCondition;
11+
import eu.adlogix.com.google.api.ads.dfp.StatementQueryValue;
12+
13+
public class AdUnitStatementBuilderCreator extends BaseStatementBuilderCreator {
14+
15+
public AdUnitStatementBuilderCreator withId(String id, StatementCondition condition) {
16+
where(AdUnitStatementQueryFilter.ID, new StatementQueryValue(id, condition));
17+
return this;
18+
}
19+
20+
public AdUnitStatementBuilderCreator withIds(List<String> ids) {
21+
where(AdUnitStatementQueryFilter.ID, new StatementQueryValue(ids, StatementCondition.IN));
22+
return this;
23+
}
24+
25+
public AdUnitStatementBuilderCreator withParentId(String parentId, StatementCondition condition) {
26+
where(AdUnitStatementQueryFilter.PARENT_ID, new StatementQueryValue(parentId, condition));
27+
return this;
28+
}
29+
30+
public AdUnitStatementBuilderCreator withStatusActive() {
31+
return withStatus("ACTIVE", StatementCondition.EQUAL);
32+
}
33+
34+
public AdUnitStatementBuilderCreator withStatus(String status, StatementCondition condition) {
35+
where(AdUnitStatementQueryFilter.STATUS, new StatementQueryValue(status, condition));
36+
return this;
37+
}
38+
39+
public AdUnitStatementBuilderCreator withLastModifiedDateTime(DateTime lastModifiedDateTime, StatementCondition condition) {
40+
where(AdUnitStatementQueryFilter.LAST_MODIFIED_DATE_TIME, new StatementQueryValue(DateTimes.toDateTime(lastModifiedDateTime), condition));
41+
return this;
42+
}
43+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package eu.adlogix.com.google.api.ads.dfp.v201408;
2+
3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import java.util.Collection;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.commons.lang.StringUtils;
11+
12+
import com.google.api.ads.dfp.axis.utils.v201408.StatementBuilder;
13+
import com.google.common.collect.Lists;
14+
import com.google.common.collect.Maps;
15+
16+
import eu.adlogix.com.google.api.ads.dfp.StatementQueryFilter;
17+
import eu.adlogix.com.google.api.ads.dfp.StatementQueryValue;
18+
19+
public abstract class BaseStatementBuilderCreator {
20+
21+
private Map<StatementQueryFilter, StatementQueryValue> where = Maps.newHashMap();
22+
23+
protected void where(final StatementQueryFilter statementQueryFilter, StatementQueryValue statementQueryValue) {
24+
where.put(statementQueryFilter, statementQueryValue);
25+
}
26+
27+
private void buildWhereCondition(final StatementBuilder statementBuilder,
28+
final Map<StatementQueryFilter, StatementQueryValue> where) {
29+
30+
List<String> queryConditions = Lists.newArrayList();
31+
32+
for (Map.Entry<StatementQueryFilter, StatementQueryValue> entry : where.entrySet()) {
33+
if (entry.getValue().getObject() != null && !(entry.getValue().getObject() instanceof Collection<?>)) {
34+
statementBuilder.withBindVariableValue(entry.getKey().getId(), entry.getValue().getObject());
35+
}
36+
queryConditions.add(buildWhereConditionEntry(entry));
37+
}
38+
39+
statementBuilder.where(StringUtils.join(queryConditions, " AND "));
40+
}
41+
42+
private String buildWhereConditionEntry(Map.Entry<StatementQueryFilter, StatementQueryValue> entry) {
43+
44+
String filterId = entry.getKey().getId();
45+
Object filterObject = entry.getValue().getObject();
46+
String filterConditionOperator = entry.getValue().getCondition().getOperator();
47+
48+
if (filterObject == null) {
49+
return filterId + " IS NULL";
50+
}
51+
52+
if (filterObject instanceof List<?>) {
53+
return filterId + " IN (" + listToQuery((List<?>) filterObject) + ")";
54+
}
55+
56+
return filterId + " " + filterConditionOperator + " :" + filterId;
57+
}
58+
59+
private String listToQuery(List<?> filterObjects) {
60+
61+
StringBuilder queryBuilder = new StringBuilder();
62+
63+
Iterator<?> iterator = filterObjects.iterator();
64+
65+
while (iterator.hasNext()) {
66+
67+
Object filterObject = iterator.next();
68+
69+
queryBuilder.append(filterObject);
70+
71+
if (iterator.hasNext()) {
72+
queryBuilder.append(",");
73+
}
74+
75+
}
76+
77+
return queryBuilder.toString();
78+
}
79+
80+
public final StatementBuilder toStatementBuilder() {
81+
return toStatementBuilder(new StatementBuilder());
82+
}
83+
84+
public final StatementBuilder toStatementBuilder(StatementBuilder statementBuilder) {
85+
checkNotNull(statementBuilder);
86+
buildWhereCondition(statementBuilder, where);
87+
return statementBuilder;
88+
}
89+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package eu.adlogix.com.google.api.ads.dfp.v201408;
2+
3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import java.rmi.RemoteException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import org.joda.time.DateTime;
10+
11+
import com.google.api.ads.dfp.axis.factory.DfpServices;
12+
import com.google.api.ads.dfp.axis.utils.v201408.StatementBuilder;
13+
import com.google.api.ads.dfp.axis.v201408.ApiException;
14+
import com.google.api.ads.dfp.axis.v201408.LineItem;
15+
import com.google.api.ads.dfp.axis.v201408.LineItemPage;
16+
import com.google.api.ads.dfp.axis.v201408.LineItemServiceInterface;
17+
import com.google.api.ads.dfp.lib.client.DfpSession;
18+
import com.google.common.collect.Iterables;
19+
import com.google.common.collect.Lists;
20+
21+
import eu.adlogix.com.google.api.ads.dfp.StatementCondition;
22+
23+
public class LineItemFinder {
24+
25+
private final LineItemServiceInterface lineItemService;
26+
27+
/**
28+
* Allows to construct the {@link LineItemFinder} with {@link DfpServices}
29+
* and {@link DfpSession} which will extract the necessary services to use
30+
* in this class.
31+
*
32+
* @param dfpServices
33+
* @param session
34+
*/
35+
public LineItemFinder(final DfpServices dfpServices, final DfpSession session) {
36+
this(dfpServices.get(session, LineItemServiceInterface.class));
37+
}
38+
39+
/**
40+
* Constructs the {@link LineItemFinder}
41+
*
42+
* @param lineItemService
43+
*/
44+
public LineItemFinder(final LineItemServiceInterface lineItemService) {
45+
checkNotNull(lineItemService);
46+
this.lineItemService = lineItemService;
47+
}
48+
49+
public LineItem findById(Long id) {
50+
return Iterables.getOnlyElement(findByLineItemStatementBuilder(new LineItemStatementBuilderCreator().withId(id, StatementCondition.EQUAL)));
51+
}
52+
53+
public List<LineItem> findByStatementBuilder(StatementBuilder statementBuilder) {
54+
55+
statementBuilder.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
56+
57+
int totalResultSetSize = 0;
58+
59+
List<LineItem> lineItems = new ArrayList<LineItem>();
60+
61+
try {
62+
do {
63+
LineItemPage page = lineItemService.getLineItemsByStatement(statementBuilder.toStatement());
64+
65+
if (page.getResults() != null) {
66+
totalResultSetSize = page.getTotalResultSetSize();
67+
lineItems.addAll(Lists.newArrayList(page.getResults()));
68+
}
69+
70+
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
71+
} while (statementBuilder.getOffset() < totalResultSetSize);
72+
73+
} catch (ApiException e) {
74+
throw new RuntimeException(e);
75+
} catch (RemoteException e) {
76+
throw new RuntimeException(e);
77+
}
78+
79+
return lineItems;
80+
}
81+
82+
private List<LineItem> findByLineItemStatementBuilder(LineItemStatementBuilderCreator lineItemStatementBuilder) {
83+
return findByLineItemStatementBuilder(lineItemStatementBuilder, null);
84+
85+
}
86+
87+
private List<LineItem> findByLineItemStatementBuilder(LineItemStatementBuilderCreator lineItemStatementBuilder,
88+
DateTime lastModifiedDateTime) {
89+
if (null != lastModifiedDateTime) {
90+
lineItemStatementBuilder.withLastModifiedDateTime(lastModifiedDateTime, StatementCondition.GREATER_OR_EQUAL);
91+
}
92+
93+
return findByStatementBuilder(lineItemStatementBuilder.toStatementBuilder());
94+
}
95+
}

0 commit comments

Comments
 (0)