@@ -0,0 +1,3956 @@
+/*%0A * Copyright (c) 2017, University of Oslo%0A *%0A * All rights reserved.%0A * Redistribution and use in source and binary forms, with or without%0A * modification, are permitted provided that the following conditions are met:%0A * Redistributions of source code must retain the above copyright notice, this%0A * list of conditions and the following disclaimer.%0A *%0A * Redistributions in binary form must reproduce the above copyright notice,%0A * this list of conditions and the following disclaimer in the documentation%0A * and/or other materials provided with the distribution.%0A * Neither the name of the HISP project nor the names of its contributors may%0A * be used to endorse or promote products derived from this software without%0A * specific prior written permission.%0A *%0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22 AND%0A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED%0A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE%0A * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR%0A * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES%0A * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;%0A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON%0A * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT%0A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS%0A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A */%0A%0Apackage org.hisp.dhis.android.core.common;%0A%0Aimport org.hisp.dhis.android.core.data.database.Transaction;%0Aimport org.hisp.dhis.android.core.resource.ResourceModel;%0A%0Aimport java.io.IOException;%0Aimport java.util.List;%0A%0Aimport retrofit2.Response;%0A%0Apublic abstract class AbstractEndpointCall%3CP, M extends Model, Q extends BaseQuery, C%3E extends SyncCall%3CC%3E %7B%0A    private final GenericCallData data;%0A    private final GenericHandler%3CP, M%3E handler;%0A%0A    private final ResourceModel.Type resourceType;%0A    private final ModelBuilder%3CP, M%3E modelBuilder;%0A    public final Q query;%0A%0A    AbstractEndpointCall(GenericCallData data, GenericHandler%3CP, M%3E handler,%0A                         ResourceModel.Type resourceType,%0A                         ModelBuilder%3CP, M%3E modelBuilder, Q query) %7B%0A        this.data = data;%0A        this.handler = handler;%0A        this.resourceType = resourceType;%0A        this.modelBuilder = modelBuilder;%0A        this.query = query;%0A    %7D%0A%0A    protected abstract retrofit2.Call%3CC%3E getCall(Q query, String lastUpdated) throws IOException;%0A    protected abstract List%3CP%3E getPojoList(Response%3CC%3E response);%0A    protected abstract boolean isValidResponse(Response%3CC%3E response);%0A%0A    @Override%0A    public final Response%3CC%3E call() throws Exception %7B%0A        super.setExecuted();%0A%0A        if (!query.isValid()) %7B%0A            throw new IllegalArgumentException(%22Invalid query%22);%0A        %7D%0A%0A        String lastUpdated = data.resourceHandler().getLastUpdated(resourceType);%0A        Response%3CC%3E response = getCall(query, lastUpdated).execute();%0A%0A        if (isValidResponse(response)) %7B%0A            persist(response);%0A            return response;%0A        %7D else %7B%0A            throw CallException.create(response);%0A        %7D%0A    %7D%0A%0A    private void persist(Response%3CC%3E response) %7B%0A        if (response == null) %7B%0A            throw new RuntimeException(%22Trying to process call without download data%22);%0A        %7D%0A        List%3CP%3E pojoList = getPojoList(response);%0A        if (pojoList != null && !pojoList.isEmpty()) %7B%0A            Transaction transaction = data.databaseAdapter().beginNewTransaction();%0A%0A            try %7B%0A                handler.handleMany(pojoList, modelBuilder);%0A                data.resourceHandler().handleResource(resourceType, data.serverDate());%0A%0A                transaction.setSuccessful();%0A            %7D finally %7B%0A                transaction.end();%0A            %7D%0A        %7D%0A    %7D%0A%7D

add AbstractEndpointCall