package com.contactlab.api.samples.findingsubscribers;

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

import com.contactlab.api.ws.ClabService;
import com.contactlab.api.ws.ClabService_Service;
import com.contactlab.api.ws.domain.AuthToken;
import com.contactlab.api.ws.domain.LookupMatchingMode;
import com.contactlab.api.ws.domain.LookupPreferences;
import com.contactlab.api.ws.domain.LookupSortingMode;
import com.contactlab.api.ws.domain.Subscriber;
import com.contactlab.api.ws.domain.SubscriberAttribute;
import com.contactlab.api.ws.domain.Subscribers;

public class FindSubscribers {

    // REQUIRED
	
    private String apiKey = << api key >>;
    private String userKey = << user key >>;
    
    private String attributeKey = << field_name >>; // userDB's field name to select
    private String attirbuteValue = << field_value >>; // userDB's field value to select, use "%%" for all users ( in LookupMatchingMode.LIKE )
    
    private int sourceIdentifier = << source_identifier >>; //userDB identifier
	
	//Optional
	
    private LookupMatchingMode matchingMode = LookupMatchingMode.LIKE;
    private LookupSortingMode sortingMode = LookupSortingMode.ASCENDING;
    private int pageNumber = 1;

	// Other variables

    private ClabService service;
    private AuthToken token;
    private Subscribers subscribers;
    private List<Subscriber> subscribersList;
    private SubscriberAttribute subscriberAttribute;
    private LookupPreferences lookupPrefs;
    
    
    public static void main(String[] args) {
        
        new FindSubscribers();

    }
    
    public FindSubscribers(){
        
		// init variables
		
        startVariables();

        // execute findSubscriber in loop
		
        findSubscribersExecution();
		
        for (Subscriber subscriber : this.subscribersList) {
            System.out.print(subscriber.getIdentifier());
        }
    }
    
    private void startVariables() {
        
        this.service = new ClabService_Service().getClabServicePort();
		
		// borrow token
        
        this.token = service.borrowToken(this.apiKey, this.userKey);

        this.lookupPrefs = new LookupPreferences();
        
        this.lookupPrefs.setMatchingMode(this.matchingMode);
        this.lookupPrefs.setSortingMode(this.sortingMode);
        this.lookupPrefs.setPageNumber(this.pageNumber);
        
        this.subscriberAttribute = new SubscriberAttribute();
        
        this.subscriberAttribute.setKey(attributeKey);
        this.subscriberAttribute.setValue(attirbuteValue);
        
        this.subscribersList = new ArrayList<Subscriber>();
    
    }
    
    private List<Subscriber> findSubscribersExecution() {
        
        this.subscribers = service.findSubscribers(
                this.token,
                this.sourceIdentifier,
                this.subscriberAttribute,
                this.lookupPrefs
            );
        
        if(!this.subscribers.getCurrentPageItems().isEmpty()){
            this.subscribersList.addAll(this.subscribers.getCurrentPageItems());            
            this.lookupPrefs.setPageNumber(++this.pageNumber);
			
			// every page has only 15 items. Use loop for all the subscribers in source
			
            this.subscribersList.addAll(findSubscribersExecution());
        }
        return this.subscribersList;
    }
}

@@ -55,63 +55,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 impo
@@ -185,343 +185,64 @@
 ain.
-AuthToken;%0D%0Aimport com.contactlab.api.ws.domain.LookupMatchingMode;%0D%0Aimport com.contactlab.api.ws.domain.LookupPreferences;%0D%0Aimport com.contactlab.api.ws.domain.LookupSortingMode;%0D%0Aimport com.contactlab.api.ws.domain.Subscriber;%0D%0Aimport com.contactlab.api.ws.domain.SubscriberAttribute;%0D%0Aimport com.contactlab.api.ws.domain.Subscribers
+*;%0D%0A%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List
 ;%0D%0A%0D
@@ -324,16 +324,17 @@
 piKey = 
+%22
 %3C%3C api k
@@ -334,24 +334,25 @@
 %3C api key %3E%3E
+%22
 ;%0D%0A    priva
@@ -371,16 +371,17 @@
 erKey = 
+%22
 %3C%3C user 
@@ -390,78 +390,38 @@
 y %3E%3E
+%22
 ;%0D%0A
-    
 %0D%0A    
-private String attributeKey = %3C%3C field_name %3E%3E; // userDB
+// subscriber source
 's f
@@ -467,22 +467,21 @@
  att
-i
 r
+i
 bute
-Value
+Key
  = 
+%22
 %3C%3C f
@@ -489,27 +489,43 @@
 eld_
-value %3E%3E; // userDB
+name %3E%3E%22;%0D%0A    // subscriber source
 's f
@@ -601,24 +601,78 @@
 LIKE )%0D%0A    
+private String attirbuteValue = %22%3C%3C field_value %3E%3E%22;%0D%0A
 %0D%0A    privat
@@ -700,41 +700,31 @@
 r = 
-%3C%3C source_identifier %3E%3E; //userDB
+0; // subscriber source
  ide
@@ -1208,22 +1208,16 @@
 %0D%0A    %0D%0A
-    %0D%0A
     publ
@@ -1250,34 +1250,24 @@
 %5B%5D args) %7B%0D%0A
-        %0D%0A
         new 
@@ -1282,26 +1282,24 @@
 cribers();%0D%0A
-%0D%0A
     %7D%0D%0A    %0D
@@ -1361,20 +1361,16 @@
 iables%0D%0A
-%09%09%0D%0A
         
@@ -1433,20 +1433,16 @@
 n loop%0D%0A
-%09%09%0D%0A
         

Update findSubscribers sample.