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+ }
0 commit comments