/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.option;

import org.hisp.dhis.android.core.calls.factories.UidsCallFactory;
import org.hisp.dhis.android.core.calls.factories.UidsCallFactoryImpl;
import org.hisp.dhis.android.core.calls.fetchers.CallFetcher;
import org.hisp.dhis.android.core.calls.fetchers.UidsNoResourceCallFetcher;
import org.hisp.dhis.android.core.calls.processors.CallProcessor;
import org.hisp.dhis.android.core.calls.processors.TransactionalNoResourceCallProcessor;
import org.hisp.dhis.android.core.common.GenericCallData;
import org.hisp.dhis.android.core.common.Payload;
import org.hisp.dhis.android.core.common.UidsQuery;

import java.util.Set;

public final class OptionSetCall {

    private OptionSetCall() {}

    public static final UidsCallFactory<OptionSet> FACTORY = new UidsCallFactoryImpl<OptionSet>() {

        private static final int MAX_UID_LIST_SIZE = 130;

        @Override
        protected CallFetcher<OptionSet> fetcher(GenericCallData data, Set<String> uids) {
            final OptionSetService service = data.retrofit().create(OptionSetService.class);

            return new UidsNoResourceCallFetcher<OptionSet>(uids, MAX_UID_LIST_SIZE) {

                @Override
                protected retrofit2.Call<Payload<OptionSet>> getCall(UidsQuery query) {
                    return service.optionSets(OptionSetFields.allFields, OptionSet.uid.in(query.uids()),
                            null, query.paging());
                }
            };
        }

        @Override
        protected CallProcessor<OptionSet> processor(GenericCallData data) {
            return new TransactionalNoResourceCallProcessor<>(
                    data.databaseAdapter(),
                    OptionSetHandler.create(data.databaseAdapter()),
                    new OptionSetModelBuilder()
            );
        }
    };
}
@@ -1998,32 +1998,36 @@
 tionalNoResource
+Sync
 CallProcessor;%0Ai
@@ -2209,16 +2209,44 @@
 l.Set;%0A%0A
+import retrofit2.Retrofit;%0A%0A
 public f
@@ -2323,22 +2323,16 @@
  static 
-final 
 UidsCall
@@ -2354,17 +2354,135 @@
 et%3E 
-FACTORY =
+factory(Retrofit retrofit) %7B%0A%0A        final OptionSetService service = retrofit.create(OptionSetService.class);%0A%0A        return
  new
@@ -2522,24 +2522,28 @@
  %7B%0A%0A        
+ 
+   
 private stat
@@ -2581,32 +2581,36 @@
 = 130;%0A%0A        
+    
 @Override%0A      
@@ -2603,32 +2603,36 @@
 verride%0A        
+   
+ 
 protected CallFe
@@ -2702,102 +2702,13 @@
 ) %7B%0A
+%0A
     
-        final OptionSetService service = data.retrofit().create(OptionSetService.class);%0A%0A
     
@@ -2799,32 +2799,36 @@
                 
+  
+  
 @Override%0A      
@@ -2813,32 +2813,36 @@
       @Override%0A
+    
                 
@@ -2909,24 +2909,28 @@
 ry query) %7B%0A
+    
             
@@ -2999,16 +2999,22 @@
 ptionSet
+Fields
 .uid.in(
@@ -3024,24 +3024,28 @@
 ry.uids()),%0A
+    
             
@@ -3095,26 +3095,34 @@
             
-%7D%0A
+    %7D%0A    
             
@@ -3132,19 +3132,27 @@
         
+   
+ 
 %7D%0A%0A
+    
         
@@ -3153,32 +3153,36 @@
       @Override%0A
+    
         protecte
@@ -3246,32 +3246,36 @@
 ) %7B%0A            
+  
+  
 return new Trans
@@ -3288,24 +3288,28 @@
 alNoResource
+Sync
 CallProcesso
@@ -3313,16 +3313,20 @@
 ssor%3C%3E(%0A
+    
         
@@ -3381,16 +3381,20 @@
         
+    
 OptionSe
@@ -3432,17 +3432,16 @@
 apter())
-,
 %0A       
@@ -3453,39 +3453,10 @@
     
-    new OptionSetModelBuilder(
 )
+;
 %0A   
@@ -3456,34 +3456,33 @@
  );%0A            
-);
+%7D
 %0A        %7D%0A    %7D
@@ -3479,13 +3479,13 @@
    %7D
+;
 %0A    %7D
-;
 %0A%7D

Unify OptionSetCall